Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{Cognitive Services} Update CLI to latest SDK, add soft-delete support. #18464

Merged
merged 7 commits into from Jun 21, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ def cf_accounts(cli_ctx, *_):
return get_cognitiveservices_management_client(cli_ctx).accounts


def cf_deleted_accounts(cli_ctx, *_):
return get_cognitiveservices_management_client(cli_ctx).deleted_accounts


def cf_resource_skus(cli_ctx, *_):
return get_cognitiveservices_management_client(cli_ctx).resource_skus
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,40 @@
- name: Remove the identity from a Cognitive Services account.
text: az cognitiveservices account identity remove --name myresource --resource-group cognitive-services-resource-group
"""

helps['cognitiveservices account list-deleted'] = """
type: command
short-summary: List soft-deleted Azure Cognitive Services accounts.
long-summary: This article lists the Azure CLI commands for Azure Cognitive Services account and subscription management only. Refer to the documentation at https://docs.microsoft.com/azure/cognitive-services/ for individual services to learn how to use the APIs and supported SDKs.
examples:
- name: List all the Cognitive Services accounts in a subscription.
text: az cognitiveservices account list-deleted
"""


helps['cognitiveservices account show-deleted'] = """
type: command
short-summary: Show a soft-deleted Azure Cognitive Services account.
long-summary: This article lists the Azure CLI commands for Azure Cognitive Services account and subscription management only. Refer to the documentation at https://docs.microsoft.com/azure/cognitive-services/ for individual services to learn how to use the APIs and supported SDKs.
examples:
- name: Show a soft-deleted Azure Cognitive Services account.
text: az cognitiveservices account show-deleted --location eastus --resource-group cognitive-services-resource-group --name cognitive-services-account-name
"""

helps['cognitiveservices account recover'] = """
type: command
short-summary: Recover a soft-deleted Azure Cognitive Services account.
long-summary: This article lists the Azure CLI commands for Azure Cognitive Services account and subscription management only. Refer to the documentation at https://docs.microsoft.com/azure/cognitive-services/ for individual services to learn how to use the APIs and supported SDKs.
examples:
- name: Recover a soft-deleted Azure Cognitive Services account.
text: az cognitiveservices account recover --location eastus --resource-group cognitive-services-resource-group --name cognitive-services-account-name
"""

helps['cognitiveservices account purge'] = """
type: command
short-summary: Purge a soft-deleted Azure Cognitive Services account.
long-summary: This article lists the Azure CLI commands for Azure Cognitive Services account and subscription management only. Refer to the documentation at https://docs.microsoft.com/azure/cognitive-services/ for individual services to learn how to use the APIs and supported SDKs.
examples:
- name: Purge a soft-deleted Azure Cognitive Services account.
text: az cognitiveservices account purge --location eastus --resource-group cognitive-services-resource-group --name cognitive-services-account-name
"""
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from azure.cli.core.commands.parameters import (
tags_type,
get_enum_type,
resource_group_name_type,
get_resource_name_completion_list,
get_location_type)
Expand All @@ -19,6 +20,8 @@

from azure.cli.command_modules.cognitiveservices._client_factory import cf_resource_skus

from azure.mgmt.cognitiveservices.models import KeyName

logger = get_logger(__name__)
name_arg_type = CLIArgumentType(options_list=['--name', '-n'], metavar='NAME')

Expand Down Expand Up @@ -147,7 +150,7 @@ def load_arguments(self, _):
c.argument('kind', help='the API name of cognitive services account',
completer=kind_completer)
c.argument('tags', tags_type)
c.argument('key_name', required=True, help='Key name to generate', choices=['key1', 'key2'])
c.argument('key_name', required=True, help='Key name to generate', arg_type=get_enum_type(KeyName))
c.argument('api_properties', api_properties_type)
c.argument('custom_domain', help='User domain assigned to the account. Name is the CNAME source.')
c.argument('storage', help='The storage accounts for this resource, in JSON array format.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# --------------------------------------------------------------------------------------------

from azure.cli.core.commands import CliCommandType
from azure.cli.command_modules.cognitiveservices._client_factory import cf_accounts, cf_resource_skus
from azure.cli.command_modules.cognitiveservices._client_factory import cf_accounts, cf_resource_skus,\
cf_deleted_accounts


def load_command_table(self, _):
Expand All @@ -19,6 +20,16 @@ def load_command_table(self, _):
g.show_command('show', 'get')
g.custom_command('update', 'update')
g.custom_command('list', 'list_resources')
g.show_command('show-deleted', 'get',
operations_tmpl='azure.mgmt.cognitiveservices.operations#DeletedAccountsOperations.{}',
client_factory=cf_deleted_accounts)
g.command('list-deleted', 'list',
operations_tmpl='azure.mgmt.cognitiveservices.operations#DeletedAccountsOperations.{}',
client_factory=cf_deleted_accounts)
g.command('purge', 'begin_purge',
operations_tmpl='azure.mgmt.cognitiveservices.operations#DeletedAccountsOperations.{}',
client_factory=cf_deleted_accounts)
g.custom_command('recover', 'recover')
g.custom_command('list-skus', 'list_skus')
g.custom_command('list-usage', 'list_usages')
g.custom_command('list-kinds', 'list_kinds', client_factory=cf_resource_skus)
Expand Down
48 changes: 28 additions & 20 deletions src/azure-cli/azure/cli/command_modules/cognitiveservices/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
from knack.util import CLIError
from knack.log import get_logger

from azure.mgmt.cognitiveservices.models import Account, Sku,\
from azure.mgmt.cognitiveservices.models import Account as CognitiveServicesAccount, Sku,\
VirtualNetworkRule, IpRule, NetworkRuleSet, NetworkRuleAction,\
AccountProperties, ApiProperties,\
Identity, ResourceIdentityType
AccountProperties as CognitiveServicesAccountProperties, ApiProperties as CognitiveServicesAccountApiProperties,\
Identity, ResourceIdentityType as IdentityType
from azure.cli.command_modules.cognitiveservices._client_factory import cf_accounts, cf_resource_skus


logger = get_logger(__name__)


Expand All @@ -25,6 +24,15 @@ def list_resources(client, resource_group_name=None):
return client.list()


def recover(client, location, resource_group_name, account_name):
properties = CognitiveServicesAccountProperties()
properties.restore = True
params = CognitiveServicesAccount(properties=properties)
params.location = location

return client.begin_create(resource_group_name, account_name, params)


def list_usages(client, resource_group_name, account_name):
"""
List usages for Azure Cognitive Services account.
Expand Down Expand Up @@ -109,16 +117,16 @@ def create(

sku = Sku(name=sku_name)

properties = AccountProperties()
properties = CognitiveServicesAccountProperties()
if api_properties is not None:
api_properties = ApiProperties.deserialize(api_properties)
api_properties = CognitiveServicesAccountApiProperties.deserialize(api_properties)
properties.api_properties = api_properties
if custom_domain:
properties.custom_sub_domain_name = custom_domain
params = Account(sku=sku, kind=kind, location=location,
properties=properties, tags=tags)
params = CognitiveServicesAccount(sku=sku, kind=kind, location=location,
properties=properties, tags=tags)
if assign_identity:
params.identity = Identity(type=ResourceIdentityType.system_assigned)
params.identity = Identity(type=IdentityType.system_assigned)

if storage is not None:
params.properties.user_owned_storage = json.loads(storage)
Expand All @@ -138,13 +146,13 @@ def update(client, resource_group_name, account_name, sku_name=None, custom_doma

sku = Sku(name=sku_name)

properties = AccountProperties()
properties = CognitiveServicesAccountProperties()
if api_properties is not None:
api_properties = ApiProperties.deserialize(api_properties)
api_properties = CognitiveServicesAccountApiProperties.deserialize(api_properties)
properties.api_properties = api_properties
if custom_domain:
properties.custom_sub_domain_name = custom_domain
params = Account(sku=sku, properties=properties, tags=tags)
params = CognitiveServicesAccount(sku=sku, properties=properties, tags=tags)

if storage is not None:
params.properties.user_owned_storage = json.loads(storage)
Expand Down Expand Up @@ -191,9 +199,9 @@ def add_network_rule(client, resource_group_name, account_name, subnet=None,
rules.ip_rules = []
rules.ip_rules.append(IpRule(value=ip_address))

properties = AccountProperties()
properties = CognitiveServicesAccountProperties()
properties.network_acls = rules
params = Account(properties=properties)
params = CognitiveServicesAccount(properties=properties)

return client.begin_update(resource_group_name, account_name, params)

Expand All @@ -212,23 +220,23 @@ def remove_network_rule(client, resource_group_name, account_name, ip_address=No
if ip_address:
rules.ip_rules = [x for x in rules.ip_rules if x.value != ip_address]

properties = AccountProperties()
properties = CognitiveServicesAccountProperties()
properties.network_acls = rules
params = Account(properties=properties)
params = CognitiveServicesAccount(properties=properties)

return client.begin_update(resource_group_name, account_name, params)


def identity_assign(client, resource_group_name, account_name):
params = Account()
params.identity = Identity(type=ResourceIdentityType.system_assigned)
params = CognitiveServicesAccount()
params.identity = Identity(type=IdentityType.system_assigned)
sa = client.begin_update(resource_group_name, account_name, params).result()
return sa.identity if sa.identity else {}


def identity_remove(client, resource_group_name, account_name):
params = Account()
params.identity = Identity(type=ResourceIdentityType.none)
params = CognitiveServicesAccount()
params.identity = Identity(type=IdentityType.none)
client.begin_update(resource_group_name, account_name, params)


Expand Down
Loading