Skip to content
Merged
4 changes: 4 additions & 0 deletions src/connectedvmware/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
Release History
===============

0.1.4
++++++
* Added vm extension support.
Comment thread
iamsantoshmishra-zz marked this conversation as resolved.
Outdated

0.1.3
++++++
* Fixed inventory item issue.
Expand Down
46 changes: 45 additions & 1 deletion src/connectedvmware/azext_connectedvmware/_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
# pylint: disable= protected-access, too-few-public-methods
# pylint: disable= protected-access, too-few-public-methods, raise-missing-from, no-self-use

"""
This file contains actions for parsing complex arguments.
"""

import argparse
from collections import defaultdict
from azext_connectedvmware.vmware_utils import create_dictionary_from_arg_string
from azure.cli.core.azclierror import InvalidArgumentValueError


class VmNicAddAction(argparse._AppendAction):
Expand All @@ -36,3 +38,45 @@ def __call__(self, parser, namespace, values, option_string=None):
namespace.disks.append(disk_params_dict)
else:
namespace.disks = [disk_params_dict]


class AddStatus(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
action = self.get_action(values, option_string)
namespace.status = action

def get_action(self, values, option_string):
try:
properties = defaultdict(list)
for (k, v) in (x.split('=', 1) for x in values):
properties[k].append(v)
properties = dict(properties)
except ValueError:
raise InvalidArgumentValueError('usage error: {} [KEY=VALUE ...]'.format(option_string))
d = {}
for k in properties:
kl = k.lower()
v = properties[k]

if kl == 'code':
d['code'] = v[0]

elif kl == 'level':
d['level'] = v[0]

elif kl == 'display-status':
d['display_status'] = v[0]

elif kl == 'message':
d['message'] = v[0]

elif kl == 'time':
d['time'] = v[0]

else:
raise InvalidArgumentValueError(
'Unsupported Key {} is provided for parameter status. All possible keys are: code, level,'
' display-status, message, time'.format(k)
)

return d
6 changes: 5 additions & 1 deletion src/connectedvmware/azext_connectedvmware/_client_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,8 @@ def cf_guest_agent(cli_ctx, *_):
"""
Client factory for guest agent.
"""
return cf_connectedvmware(cli_ctx).guest_agent
return cf_connectedvmware(cli_ctx).guest_agents


def cf_machine_extension(cli_ctx, *_):
return cf_connectedvmware(cli_ctx).machine_extensions
58 changes: 57 additions & 1 deletion src/connectedvmware/azext_connectedvmware/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,6 @@
--vm-name "vm name"
"""


helps[
'connectedvmware vm guest-agent show'
] = """
Expand All @@ -638,6 +637,63 @@
--vm-name "name of the vm"
"""

helps['connectedvmware vm extension'] = """
type: group
short-summary: Manage vm extension with connectedvmware
"""

helps['connectedvmware vm extension list'] = """
type: command
short-summary: "The operation to get all extensions of a non-Azure vm."
examples:
- name: Get all VM Extensions
text: |-
az connectedvmware vm extension list --vm-name "vm name" --resource-group "myResourceGroup"
"""

helps['connectedvmware vm extension show'] = """
type: command
short-summary: "The operation to get the extension."
examples:
- name: Get VM Extension
text: |-
az connectedvmware vm extension show --name "CustomScriptExtension" --vm-name "vm name" \
--resource-group "myResourceGroup"
"""

helps['connectedvmware vm extension create'] = """
type: command
short-summary: "The operation to Create the extension."
examples:
- name: Create or Update a VM Extension
Comment thread
iamsantoshmishra-zz marked this conversation as resolved.
Outdated
text: |-
az connectedvmware vm extension create --name "CustomScriptExtension" --location "eastus2euap" --type \
"CustomScriptExtension" --publisher "Microsoft.Compute" --settings "{\\"commandToExecute\\":\\"powershell.exe -c \
\\\\\\"Get-Process | Where-Object { $_.CPU -gt 10000 }\\\\\\"\\"}" --type-handler-version "1.10" --vm-name \
"vm name" --resource-group "myResourceGroup"
"""

helps['connectedvmware vm extension update'] = """
type: command
short-summary: "The operation to create or update the extension."
Comment thread
zhoxing-ms marked this conversation as resolved.
Outdated
examples:
- name: Create or Update a VM Extension
text: |-
az connectedvmware vm extension update --name "CustomScriptExtension" --type "CustomScriptExtension" \
--publisher "Microsoft.Compute" --settings "{\\"commandToExecute\\":\\"powershell.exe -c \\\\\\"Get-Process | \
Where-Object { $_.CPU -lt 100 }\\\\\\"\\"}" --type-handler-version "1.10" --vm-name "vm name" --resource-group \
"myResourceGroup"
"""

helps['connectedvmware vm extension delete'] = """
type: command
short-summary: "The operation to delete the extension."
examples:
- name: Delete a VM Extension
text: |-
az connectedvmware vm extension delete --name "vm extension name" --vm-name "vm name" --resource-group \
"myResourceGroup"
"""

helps[
'connectedvmware vm-template'
Expand Down
79 changes: 75 additions & 4 deletions src/connectedvmware/azext_connectedvmware/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
# pylint: disable= too-many-statements

from knack.arguments import CLIArgumentType
from azure.cli.core.commands.parameters import tags_type
from azure.cli.core.commands.validators import get_default_location_from_resource_group
from azure.cli.core.commands.parameters import (
tags_type,
get_three_state_flag,
)
from azure.cli.core.commands.validators import (
get_default_location_from_resource_group,
validate_file_or_dict
)
from ._actions import VmNicAddAction, VmDiskAddAction


Expand Down Expand Up @@ -346,12 +352,77 @@ def load_arguments(self, _):
help="Username to use for connecting to the vm.",
)
c.argument(
'password',
options_list=['--password'],
'password', options_list=['--password'],
help="Username password credentials to use for connecting to the VM.",
)

with self.argument_context('connectedvmware vm guest-agent show') as c:
c.argument(
'password', options_list=['--password'],
help="Username password credentials to use for connecting to the VM.",
)

with self.argument_context('connectedvmware vm guest-agent show') as c:
c.argument(
'vm_name', options_list=['--vm-name'], help="Name of the VM.",
)

with self.argument_context('connectedvmware vm extension list') as c:
c.argument('vm_name', options_list=['--vm-name'], type=str, help='The name of the vm containing the extension.')
c.argument('expand', type=str, help='The expand expression to apply on the operation.')
Comment thread
iamsantoshmishra-zz marked this conversation as resolved.
Outdated

with self.argument_context('connectedvmware vm extension show') as c:
c.argument(
'vm_name', options_list=['--vm-name'], type=str, help='The name of the vm containing the extension.',
Comment thread
iamsantoshmishra-zz marked this conversation as resolved.
Outdated
id_part='name')
c.argument('name', type=str, help='The name of the vm extension.', id_part='child_name_1')

with self.argument_context('connectedvmware vm extension create') as c:
c.argument(
'vm_name', options_list=['--vm-name'], type=str, help='The name of the vm where the extension '
Comment thread
iamsantoshmishra-zz marked this conversation as resolved.
Outdated
'should be created or updated.')
c.argument('name', type=str, help='The name of the vm extension.')
c.argument('tags', tags_type)
c.argument('force_update_tag', type=str, help='How the extension handler should be forced to update even if '
'the extension configuration has not changed.')
c.argument('publisher', type=str, help='The name of the extension handler publisher.')
c.argument('type_', options_list=['--type'], type=str, help='Specifies the type of the extension; an example '
'is "CustomScriptExtension".')
c.argument('type_handler_version', type=str, help='Specifies the version of the script handler.')
Comment thread
iamsantoshmishra-zz marked this conversation as resolved.
Outdated
c.argument('auto_upgrade_minor', arg_type=get_three_state_flag(), help='Indicate whether the extension should '
'use a newer minor version if one is available at deployment time. Once deployed, however, the '
'extension will not upgrade minor versions unless redeployed, even with this property set to true.')
c.argument('settings', type=validate_file_or_dict, help='Json formatted public settings for the extension. '
'Expected value: json-string/json-file/@json-file.')
c.argument('protected_settings', type=validate_file_or_dict, help='The extension can contain either '
'protectedSettings or protectedSettingsFromKeyVault or no protected settings at all. Expected '
'value: json-string/json-file/@json-file.')
c.argument('instance_view_type', type=str, help='Specify the type of the extension; an example is '
'"CustomScriptExtension".', arg_group='Instance View')
c.argument('inst_handler_version', type=str, help='Specify the version of the script handler.',
arg_group='Instance View')

with self.argument_context('connectedvmware vm extension update') as c:
Comment thread
iamsantoshmishra-zz marked this conversation as resolved.
Outdated
c.argument('vm_name', options_list=['--vm-name'], type=str, help='The name of the vm where the extension '
Comment thread
iamsantoshmishra-zz marked this conversation as resolved.
Outdated
'should be created or updated.', id_part='name')
c.argument('name', type=str, help='The name of the vm extension.', id_part='child_name_1')
c.argument('tags', tags_type)
c.argument('force_update_tag', type=str, help='How the extension handler should be forced to update even if '
'the extension configuration has not changed.')
c.argument('publisher', type=str, help='The name of the extension handler publisher.')
c.argument('type_', options_list=['--type'], type=str, help='Specifies the type of the extension; an example '
'is "CustomScriptExtension".')
c.argument('type_handler_version', type=str, help='Specifies the version of the script handler.')
Comment thread
iamsantoshmishra-zz marked this conversation as resolved.
Outdated
c.argument('auto_upgrade_minor', arg_type=get_three_state_flag(), help='Indicate whether the extension should '
'use a newer minor version if one is available at deployment time. Once deployed, however, the '
'extension will not upgrade minor versions unless redeployed, even with this property set to true.')
c.argument('settings', type=validate_file_or_dict, help='Json formatted public settings for the extension. '
'Expected value: json-string/json-file/@json-file.')
c.argument('protected_settings', type=validate_file_or_dict, help='The extension can contain either '
'protectedSettings or protectedSettingsFromKeyVault or no protected settings at all. Expected '
'value: json-string/json-file/@json-file.')

with self.argument_context('connectedvmware vm extension delete') as c:
c.argument('vm_name', options_list=['--vm-name'], type=str, help='The name of the vm where the extension '
Comment thread
iamsantoshmishra-zz marked this conversation as resolved.
Outdated
'should be deleted.', id_part='name')
c.argument('name', type=str, help='The name of the vm extension.', id_part='child_name_1')
10 changes: 10 additions & 0 deletions src/connectedvmware/azext_connectedvmware/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
cf_cluster,
cf_datastore,
cf_host,
cf_machine_extension,
)


Expand Down Expand Up @@ -116,5 +117,14 @@ def load_command_table(self, _):
g.custom_command('enable', 'enable_guest_agent', supports_no_wait=True)
g.custom_show_command('show', 'show_guest_agent')

with self.command_group(
'connectedvmware vm extension', client_factory=cf_machine_extension
) as g:
g.custom_command('list', 'connectedvmware_extension_list')
g.custom_show_command('show', 'connectedvmware_extension_show')
g.custom_command('create', 'connectedvmware_extension_create', supports_no_wait=True)
g.custom_command('update', 'connectedvmware_extension_update', supports_no_wait=True)
g.custom_command('delete', 'connectedvmware_extension_delete', supports_no_wait=True, confirmation=True)

with self.command_group('connectedvmware', is_preview=False):
pass
Loading