Skip to content

Commit

Permalink
custom commands
Browse files Browse the repository at this point in the history
  • Loading branch information
cxznmhdcxz committed Jan 3, 2025
1 parent 1841ca4 commit 0ddfb39
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 86 deletions.
4 changes: 0 additions & 4 deletions src/azure-cli/azure/cli/command_modules/vm/_client_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ def cf_shared_galleries(cli_ctx, *_):
return cf_vm_cl(cli_ctx).shared_galleries


def cf_gallery_sharing_profile(cli_ctx, *_):
return cf_vm_cl(cli_ctx).gallery_sharing_profile


def cf_shared_gallery_image(cli_ctx, *_):
return cf_vm_cl(cli_ctx).shared_gallery_images

Expand Down
47 changes: 0 additions & 47 deletions src/azure-cli/azure/cli/command_modules/vm/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,53 +1110,6 @@
short-summary: Manage gallery sharing profile
"""

helps['sig share add'] = """
type: command
short-summary: Share gallery with subscriptions and tenants
examples:
- name: Share entire gallery with all members of a subscription and/or tenant.
text: |
az sig share add --resource-group MyResourceGroup --gallery-name MyGallery \\
--subscription-ids subId1 subId2 --tenant-ids tenantId1 tenantId2
"""

helps['sig share remove'] = """
type: command
short-summary: stop sharing gallery with a subscription or tenant
examples:
- name: Stop sharing with a subscription or tenant ID
text: |
az sig share remove --resource-group MyResourceGroup --gallery-name MyGallery \\
--subscription-ids subId1 subId2 --tenant-ids tenantId1 tenantId2
"""

helps['sig share reset'] = """
type: command
short-summary: disable gallery from being shared with subscription or tenant
examples:
- name: Reset sharing profile of a gallery.
text: |
az sig share reset --resource-group MyResourceGroup --gallery-name MyGallery
"""

helps['sig share enable-community'] = """
type: command
short-summary: Allow to share gallery to the community
examples:
- name: Allow to share gallery to the community
text: |
az sig share enable-community --resource-group MyResourceGroup --gallery-name MyGallery
"""

helps['sig share wait'] = """
type: command
short-summary: Place the CLI in a waiting state until a condition of a shared gallery is met.
examples:
- name: Place the CLI in a waiting state until the gallery sharing object is updated.
text: |
az sig share wait --updated --resource-group MyResourceGroup --gallery-name Gallery
"""

helps['sig gallery-application'] = """
type: group
short-summary: Manage gallery application
Expand Down
24 changes: 0 additions & 24 deletions src/azure-cli/azure/cli/command_modules/vm/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,23 +1253,6 @@ def load_arguments(self, _):
c.argument('gallery_image_name', options_list=['--gallery-image-definition', '-i'], help='gallery image definition')
c.argument('gallery_image_version', options_list=['--gallery-image-version', '-e'], help='gallery image version')

for scope in ['sig share add', 'sig share remove']:
with self.argument_context(scope) as c:
c.argument('gallery_name', type=str, help='The name of the Shared Image Gallery.', id_part='name')
c.argument('subscription_ids', nargs='+', help='A list of subscription ids to share the gallery.')
c.argument('tenant_ids', nargs='+', help='A list of tenant ids to share the gallery.')

with self.argument_context('sig share add') as c:
c.argument('op_type', default='Add', deprecate_info=c.deprecate(hide=True),
help='distinguish add operation and remove operation')

with self.argument_context('sig share remove') as c:
c.argument('op_type', default='Remove', deprecate_info=c.deprecate(hide=True),
help='distinguish add operation and remove operation')

with self.argument_context('sig share reset') as c:
c.argument('gallery_name', type=str, help='The name of the Shared Image Gallery.', id_part='name')

with self.argument_context('sig image-definition create') as c:
c.argument('offer', options_list=['--offer', '-f'], help='image offer')
c.argument('sku', options_list=['--sku', '-s'], help='image sku')
Expand Down Expand Up @@ -1414,13 +1397,6 @@ def load_arguments(self, _):
c.argument('marker', arg_type=marker_type)
c.argument('show_next_marker', action='store_true', help='Show nextMarker in result when specified.')

with self.argument_context('sig share enable-community') as c:
c.argument('gallery_name', type=str, help='The name of the Shared Image Gallery.', id_part='name')
c.argument('subscription_ids', nargs='+', help='A list of subscription ids to share the gallery.')
c.argument('tenant_ids', nargs='+', help='A list of tenant ids to share the gallery.')
c.argument('op_type', default='EnableCommunity', deprecate_info=c.deprecate(hide=True),
help='distinguish add operation and remove operation')

# endregion

# region Gallery applications
Expand Down
19 changes: 8 additions & 11 deletions src/azure-cli/azure/cli/command_modules/vm/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
cf_dedicated_hosts, cf_dedicated_host_groups,
cf_log_analytics_data_plane,
cf_disk_encryption_set,
cf_gallery_sharing_profile, cf_shared_gallery_image,
cf_shared_gallery_image,
cf_shared_gallery_image_version,
cf_capacity_reservation_groups, cf_capacity_reservations,
cf_vmss_run_commands, cf_gallery_application,
Expand Down Expand Up @@ -527,18 +527,15 @@ def load_command_table(self, _):
operations_tmpl=(
'azure.mgmt.compute.operations._gallery_sharing_profile_operations#GallerySharingProfileOperations.{}'
),
client_factory=cf_gallery_sharing_profile,
operation_group='shared_galleries'
)
with self.command_group('sig share', vm_gallery_sharing_profile,
client_factory=cf_gallery_sharing_profile,
operation_group='shared_galleries',
min_api='2020-09-30') as g:
g.custom_command('add', 'sig_share_update', supports_no_wait=True)
g.custom_command('remove', 'sig_share_update', supports_no_wait=True)
g.custom_command('reset', 'sig_share_reset', supports_no_wait=True)
g.custom_command('enable-community', 'sig_share_update', supports_no_wait=True)
g.wait_command('wait', getter_name='get_gallery_instance', getter_type=compute_custom)
with self.command_group('sig share', vm_gallery_sharing_profile, operation_group='shared_galleries'):
from .operations.sig_share import SigShareAdd, SigShareRemove, SigShareReset, SigShareEnableCommunity, SigShareWait
self.command_table['sig share add'] = SigShareAdd(loader=self)
self.command_table['sig share remove'] = SigShareRemove(loader=self)
self.command_table['sig share reset'] = SigShareReset(loader=self)
self.command_table['sig share enable-community'] = SigShareEnableCommunity(loader=self)
self.command_table['sig share wait'] = SigShareWait(loader=self)

vm_shared_gallery_image = CliCommandType(
operations_tmpl='azure.mgmt.compute.operations._shared_gallery_images_operations#SharedGalleryImagesOperations.'
Expand Down
185 changes: 185 additions & 0 deletions src/azure-cli/azure/cli/command_modules/vm/operations/sig_share.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
# pylint: disable=no-self-use, line-too-long, protected-access, too-few-public-methods, unused-argument
from knack.log import get_logger

from azure.cli.core.aaz import register_command, AAZListArg, AAZStrArg
from azure.cli.core.azclierror import CLIError
from ..aaz.latest.sig.share import Update as _SigShareUpdate
from ..aaz.latest.sig import Wait as _SigWait

logger = get_logger(__name__)


@register_command(
"sig share add",
)
class SigShareAdd(_SigShareUpdate):
"""Share gallery with subscriptions and tenants.
:example: Share entire gallery with all members of a subscription and/or tenant.
az sig share add --resource-group MyResourceGroup --gallery-name MyGallery \\
--subscription-ids subId1 subId2 --tenant-ids tenantId1 tenantId2
"""

@classmethod
def _build_arguments_schema(cls, *args, **kwargs):
args_schema = super()._build_arguments_schema(*args, **kwargs)
args_schema.subscription_ids = AAZListArg(
options=["--subscription-ids"],
help="A list of subscription ids to share the gallery with.",
)
subscription_ids = args_schema.subscription_ids
subscription_ids.Element = AAZStrArg()

args_schema.tenant_ids = AAZListArg(
options=["--tenant-ids"],
help="A list of tenant ids to share the gallery with.",
)
tenant_ids = args_schema.tenant_ids
tenant_ids.Element = AAZStrArg()

args_schema.operation_type._required = False
args_schema.operation_type._registered = False
args_schema.groups._required = False
args_schema.groups._registered = False

return args_schema

def pre_operations(self):
args = self.ctx.args
if not args.subscription_ids and not args.tenant_ids:
raise CLIError('At least one of subscription ids or tenant ids must be provided.')

args.operation_type = 'Add'
args.groups = []
if args.subscription_ids:
args.groups.append({
'type': 'Subscriptions',
'ids': args.subscription_ids
})
if args.tenant_ids:
args.groups.append({
'type': 'AADTenants',
'ids': args.tenant_ids
})


@register_command(
"sig share remove",
)
class SigShareRemove(_SigShareUpdate):
"""Stop sharing gallery with a subscription or tenant.
:example: Stop sharing with a subscription or tenant ID
az sig share remove --resource-group MyResourceGroup --gallery-name MyGallery \\
--subscription-ids subId1 subId2 --tenant-ids tenantId1 tenantId2
"""

@classmethod
def _build_arguments_schema(cls, *args, **kwargs):
args_schema = super()._build_arguments_schema(*args, **kwargs)
args_schema.subscription_ids = AAZListArg(
options=["--subscription-ids"],
help="A list of subscription ids to share the gallery with.",
)
subscription_ids = args_schema.subscription_ids
subscription_ids.Element = AAZStrArg()

args_schema.tenant_ids = AAZListArg(
options=["--tenant-ids"],
help="A list of tenant ids to share the gallery with.",
)
tenant_ids = args_schema.tenant_ids
tenant_ids.Element = AAZStrArg()

args_schema.operation_type._required = False
args_schema.operation_type._registered = False
args_schema.groups._required = False
args_schema.groups._registered = False

return args_schema

def pre_operations(self):
args = self.ctx.args

if not args.subscription_ids and not args.tenant_ids:
raise CLIError('At least one of subscription ids or tenant ids must be provided.')
args.operation_type = 'Remove'
args.groups = []
if args.subscription_ids:
args.groups.append({
'type': 'Subscriptions',
'ids': args.subscription_ids
})
if args.tenant_ids:
args.groups.append({
'type': 'AADTenants',
'ids': args.tenant_ids
})


@register_command(
"sig share reset",
)
class SigShareReset(_SigShareUpdate):
"""Disable gallery from being shared with subscription or tenant.
:example: Reset sharing profile of a gallery.
az sig share reset --resource-group MyResourceGroup --gallery-name MyGallery
"""

@classmethod
def _build_arguments_schema(cls, *args, **kwargs):
args_schema = super()._build_arguments_schema(*args, **kwargs)
args_schema.operation_type._required = False
args_schema.operation_type._registered = False
args_schema.operation_type._default = 'Reset'
args_schema.groups._required = False
args_schema.groups._registered = False

return args_schema


@register_command(
"sig share enable-community"
)
class SigShareEnableCommunity(_SigShareUpdate):
"""Allow to share gallery to the community.
:example: Allow to share gallery to the community
az sig share enable-community --resource-group MyResourceGroup --gallery-name MyGallery
"""

@classmethod
def _build_arguments_schema(cls, *args, **kwargs):
args_schema = super()._build_arguments_schema(*args, **kwargs)
args_schema.operation_type._required = False
args_schema.operation_type._registered = False
args_schema.operation_type._default = 'EnableCommunity'
args_schema.groups._required = False
args_schema.groups._registered = False

return args_schema


@register_command(
"sig share wait"
)
class SigShareWait(_SigWait):
"""Place the CLI in a waiting state until a condition of a shared gallery is met.
:example: Place the CLI in a waiting state until the gallery sharing object is updated.
az sig share wait --updated --resource-group MyResourceGroup --gallery-name Gallery
"""

@classmethod
def _build_arguments_schema(cls, *args, **kwargs):
args_schema = super()._build_arguments_schema(*args, **kwargs)
args_schema.gallery_name._help['short-summary'] = 'Gallery name.'
args_schema.expand._registered = False
args_schema.select._registered = False

return args_schema

0 comments on commit 0ddfb39

Please sign in to comment.