Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/azure-cli/azure/cli/command_modules/vm/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ def load_arguments(self, _):
c.argument('enable_vtpm', enable_vtpm_type)
c.argument('user_data', help='UserData for the VM. It can be passed in as file or string.', completer=FilesCompleter(), type=file_type, min_api='2021-03-01')
c.argument('enable_hibernation', arg_type=get_three_state_flag(), min_api='2021-03-01', help='The flag that enable or disable hibernation capability on the VM.')
c.argument('enable_azure_monitor_metrics', options_list=['--enable-azure-monitor-metrics', '--enable-monitor-agent'], action='store_true', min_api='2021-03-01', help='The flag that enable azure monitor metrics on the VM.')

for scope in ['vm create', 'vm update']:
with self.argument_context(scope) as c:
Expand Down
28 changes: 27 additions & 1 deletion src/azure-cli/azure/cli/command_modules/vm/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ def create_vm(cmd, vm_name, resource_group_name, image=None, size='Standard_DS1_
proxy_agent_mode=None, source_snapshots_or_disks=None, source_snapshots_or_disks_size_gb=None,
source_disk_restore_point=None, source_disk_restore_point_size_gb=None, ssh_key_type=None,
additional_scheduled_events=None, enable_user_reboot_scheduled_events=None,
enable_user_redeploy_scheduled_events=None):
enable_user_redeploy_scheduled_events=None, enable_azure_monitor_metrics=None):

from azure.cli.core.commands.client_factory import get_subscription_id
from azure.cli.core.util import random_string, hash_string
Expand Down Expand Up @@ -1167,6 +1167,32 @@ def create_vm(cmd, vm_name, resource_group_name, image=None, size='Standard_DS1_
except Exception as e:
error_type = "Trusted Launch" if is_trusted_launch else "Confidential VM"
logger.error('Failed to install Guest Attestation Extension for %s. %s', error_type, e)

if enable_azure_monitor_metrics:
vm = get_vm(cmd, resource_group_name, vm_name, 'instanceView')
client = _compute_client_factory(cmd.cli_ctx)
if vm.storage_profile.os_disk.os_type == 'Linux':
ext_name = 'AzureMonitorLinuxAgent'
if vm.storage_profile.os_disk.os_type == 'Windows':
ext_name = 'AzureMonitorWindowsAgent'
version = _normalize_extension_version(cmd.cli_ctx, 'Microsoft.Azure.Monitor',
ext_name, None, vm.location)
VirtualMachineExtension = cmd.get_models('VirtualMachineExtension')
ext = VirtualMachineExtension(location=vm.location,
publisher='Microsoft.Azure.Monitor',
type_properties_type=ext_name,
protected_settings=None,
type_handler_version=version,
settings=None,
auto_upgrade_minor_version=True,
enable_automatic_upgrade=True)
try:
LongRunningOperation(cmd.cli_ctx)(client.virtual_machine_extensions.begin_create_or_update(
resource_group_name, vm_name, ext_name, ext))
logger.info('Azure Monitor Agent Extension has been successfully installed')
except Exception as e:
logger.error('Failed to install Azure Monitor Agent Extension: %s', e)

if count:
vm_names = [vm_name + str(i) for i in range(count)]
else:
Expand Down
Loading
Loading