Skip to content

Commit

Permalink
scheduledevent
Browse files Browse the repository at this point in the history
  • Loading branch information
Jing-song committed Dec 31, 2024
1 parent a8d8f05 commit ee9a512
Show file tree
Hide file tree
Showing 5 changed files with 3,541 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/azure-cli/azure/cli/command_modules/vm/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,12 @@ def load_arguments(self, _):
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.')

for scope in ['vm create', 'vm update']:
with self.argument_context(scope) as c:
c.argument('additional_scheduled_events', options_list=['--additional-scheduled-events', '--scheduled-event-additional-publishing-target-event-grid-and-resource-graph', '--additional-events'], arg_type=get_three_state_flag(), min_api='2024-07-01', help='The configuration parameter used while creating event grid and resource graph scheduled event setting.')
c.argument('enable_user_reboot_scheduled_events', options_list=['--enable-user-reboot-scheduled-events', '--enable-reboot'], arg_type=get_three_state_flag(), min_api='2024-07-01', help='The configuration parameter used while publishing scheduled events additional publishing targets.')
c.argument('enable_user_redeploy_scheduled_events', options_list=['--enable-user-redeploy-scheduled-events', '--enable-redeploy'], arg_type=get_three_state_flag(), min_api='2024-07-01', help='The configuration parameter used while creating user initiated redeploy scheduled event setting creation.')

with self.argument_context('vm create', arg_group='Storage') as c:
c.argument('attach_os_disk', help='Attach an existing OS disk to the VM. Can use the name or ID of a managed disk or the URI to an unmanaged disk VHD.')
c.argument('attach_data_disks', nargs='+', help='Attach existing data disks to the VM. Can use the name or ID of a managed disk or the URI to an unmanaged disk VHD.')
Expand Down Expand Up @@ -870,6 +876,7 @@ def load_arguments(self, _):
c.argument('enable_user_redeploy_scheduled_events', options_list=['--enable-user-redeploy-scheduled-events', '--enable-redeploy'], arg_type=get_three_state_flag(), min_api='2024-03-01', help='The configuration parameter used while creating user initiated redeploy scheduled event setting creation.')
c.argument('enable_auto_os_upgrade', enable_auto_os_upgrade_type)
c.argument('upgrade_policy_mode', help='Specify the mode of an upgrade to virtual machines in the scale set.', arg_type=get_enum_type(UpgradeMode))
c.argument('enable_terminate_notification', options_list=['--enable-terminate-notification'], arg_type=get_three_state_flag(), min_api='2024-07-01', help='Specify whether the Terminate Scheduled event is enabled or disabled.')

with self.argument_context('vmss update') as c:
c.argument('instance_id', id_part='child_name_1', help="Update the VM instance with this ID. If missing, update the VMSS.")
Expand Down
27 changes: 26 additions & 1 deletion src/azure-cli/azure/cli/command_modules/vm/_template_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ def build_vm_resource( # pylint: disable=too-many-locals, too-many-statements,
enable_vtpm=None, count=None, edge_zone=None, os_disk_delete_option=None, user_data=None,
capacity_reservation_group=None, enable_hibernation=None, v_cpus_available=None, v_cpus_per_core=None,
os_disk_security_encryption_type=None, os_disk_secure_vm_disk_encryption_set=None, disk_controller_type=None,
enable_proxy_agent=None, proxy_agent_mode=None):
enable_proxy_agent=None, proxy_agent_mode=None, additional_scheduled_events=None,
enable_user_reboot_scheduled_events=None, enable_user_redeploy_scheduled_events=None):

os_caching = disk_info['os'].get('caching')

Expand Down Expand Up @@ -576,6 +577,30 @@ def _build_storage_profile():
vm_properties = {'hardwareProfile': {'vmSize': size}, 'networkProfile': {'networkInterfaces': nics},
'storageProfile': _build_storage_profile()}

scheduled_events_policy = {}
if additional_scheduled_events is not None:
scheduled_events_policy.update({
"scheduledEventsAdditionalPublishingTargets": {
"eventGridAndResourceGraph": {
"enable": additional_scheduled_events
}
}
})
if enable_user_redeploy_scheduled_events is not None:
scheduled_events_policy.update({
"userInitiatedRedeploy": {
"automaticallyApprove": enable_user_redeploy_scheduled_events
}
})
if enable_user_reboot_scheduled_events is not None:
scheduled_events_policy.update({
"userInitiatedReboot": {
"automaticallyApprove": enable_user_reboot_scheduled_events
}
})
if scheduled_events_policy:
vm_properties['scheduledEventsPolicy'] = scheduled_events_policy

vm_size_properties = {}
if v_cpus_available is not None:
vm_size_properties['vCPUsAvailable'] = v_cpus_available
Expand Down
43 changes: 40 additions & 3 deletions src/azure-cli/azure/cli/command_modules/vm/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,9 @@ def create_vm(cmd, vm_name, resource_group_name, image=None, size='Standard_DS1_
os_disk_security_encryption_type=None, os_disk_secure_vm_disk_encryption_set=None,
disk_controller_type=None, disable_integrity_monitoring_autoupgrade=False, enable_proxy_agent=None,
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):
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):

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 @@ -1044,7 +1046,9 @@ def create_vm(cmd, vm_name, resource_group_name, image=None, size='Standard_DS1_
os_disk_security_encryption_type=os_disk_security_encryption_type,
os_disk_secure_vm_disk_encryption_set=os_disk_secure_vm_disk_encryption_set,
disk_controller_type=disk_controller_type, enable_proxy_agent=enable_proxy_agent,
proxy_agent_mode=proxy_agent_mode)
proxy_agent_mode=proxy_agent_mode, additional_scheduled_events=additional_scheduled_events,
enable_user_reboot_scheduled_events=enable_user_reboot_scheduled_events,
enable_user_redeploy_scheduled_events=enable_user_redeploy_scheduled_events)

vm_resource['dependsOn'] = vm_dependencies

Expand Down Expand Up @@ -1539,7 +1543,8 @@ def update_vm(cmd, resource_group_name, vm_name, os_disk=None, disk_caching=None
enable_vtpm=None, user_data=None, capacity_reservation_group=None,
dedicated_host=None, dedicated_host_group=None, size=None, ephemeral_os_disk_placement=None,
enable_hibernation=None, v_cpus_available=None, v_cpus_per_core=None, disk_controller_type=None,
security_type=None, enable_proxy_agent=None, proxy_agent_mode=None, **kwargs):
security_type=None, enable_proxy_agent=None, proxy_agent_mode=None, additional_scheduled_events=None,
enable_user_reboot_scheduled_events=None, enable_user_redeploy_scheduled_events=None, **kwargs):
from azure.mgmt.core.tools import parse_resource_id, resource_id, is_valid_resource_id
from ._vm_utils import update_write_accelerator_settings, update_disk_caching
SecurityProfile, UefiSettings = cmd.get_models('SecurityProfile', 'UefiSettings')
Expand Down Expand Up @@ -1702,6 +1707,38 @@ def update_vm(cmd, resource_group_name, vm_name, os_disk=None, disk_caching=None
if disk_controller_type is not None:
vm.storage_profile.disk_controller_type = disk_controller_type

if additional_scheduled_events is not None or \
enable_user_reboot_scheduled_events is not None or enable_user_redeploy_scheduled_events is not None:
if vm.scheduled_events_policy is None:
ScheduledEventsPolicy = cmd.get_models('ScheduledEventsPolicy')
UserInitiatedRedeploy = cmd.get_models('UserInitiatedRedeploy')
UserInitiatedReboot = cmd.get_models('UserInitiatedReboot')
EventGridAndResourceGraph = cmd.get_models('EventGridAndResourceGraph')
ScheduledEventsAdditionalPublishingTargets = cmd.get_models('ScheduledEventsAdditionalPublishingTargets')
vm.scheduled_events_policy = ScheduledEventsPolicy()
vm.scheduled_events_policy.scheduled_events_additional_publishing_targets = \
ScheduledEventsAdditionalPublishingTargets()
vm.scheduled_events_policy.scheduled_events_additional_publishing_targets.\
event_grid_and_resource_graph = EventGridAndResourceGraph()
vm.scheduled_events_policy.user_initiated_reboot = UserInitiatedReboot()
vm.scheduled_events_policy.user_initiated_redeploy = UserInitiatedRedeploy()
vm.scheduled_events_policy.scheduled_events_additional_publishing_targets.event_grid_and_resource_graph.\
enable = additional_scheduled_events if additional_scheduled_events is not None else False
vm.scheduled_events_policy.user_initiated_redeploy.automatically_approve = \
enable_user_redeploy_scheduled_events if enable_user_redeploy_scheduled_events is not None else False
vm.scheduled_events_policy.user_initiated_reboot.automatically_approve = \
enable_user_reboot_scheduled_events if enable_user_reboot_scheduled_events is not None else False
else:
if additional_scheduled_events is not None:
vm.scheduled_events_policy.scheduled_events_additional_publishing_targets.\
event_grid_and_resource_graph.enable = additional_scheduled_events
if enable_user_redeploy_scheduled_events is not None:
vm.scheduled_events_policy.user_initiated_redeploy.automatically_approve = \
enable_user_redeploy_scheduled_events
if enable_user_reboot_scheduled_events is not None:
vm.scheduled_events_policy.user_initiated_reboot.automatically_approve = \
enable_user_reboot_scheduled_events

client = _compute_client_factory(cmd.cli_ctx, aux_subscriptions=aux_subscriptions)
return sdk_no_wait(no_wait, client.virtual_machines.begin_create_or_update, resource_group_name, vm_name, **kwargs)
# endregion
Expand Down
Loading

0 comments on commit ee9a512

Please sign in to comment.