Skip to content

Commit

Permalink
[Cognitive Services] Support 'list-usages', 'list-kinds' (Azure#7042)
Browse files Browse the repository at this point in the history
* [Cognitive Services] Support 'list-usages', 'list-kinds'

* Add populator for --kind --sku
  • Loading branch information
yangyuan authored and tjprescott committed Aug 28, 2018
1 parent c85e81d commit a08e675
Show file tree
Hide file tree
Showing 11 changed files with 444 additions and 19 deletions.
7 changes: 7 additions & 0 deletions src/command_modules/azure-cli-cognitiveservices/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
Release History
===============

0.2.2
+++++
* Add new command `az cognitiveservices account list-usage`
* Add new command `az cognitiveservices account list-kinds`
* Add new command `az cognitiveservices account list`
* Deprecate `az cognitiveservices list`

0.2.1
+++++
* Add new parameter --api-properties, which is required for creating some of the services.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import azure.cli.command_modules.cognitiveservices._help # pylint: disable=unused-import

from azure.cli.command_modules.cognitiveservices._client_factory import cf_cognitive_service_accounts
from azure.cli.command_modules.cognitiveservices._client_factory import cf_accounts


class CognitiveServicesCommandsLoader(AzCommandsLoader):
Expand All @@ -16,7 +16,7 @@ def __init__(self, cli_ctx=None):
from azure.cli.core.commands import CliCommandType
custom_type = CliCommandType(
operations_tmpl='azure.cli.command_modules.cognitiveservices.custom#{}',
client_factory=cf_cognitive_service_accounts)
client_factory=cf_accounts)
super(CognitiveServicesCommandsLoader, self).__init__(cli_ctx=cli_ctx,
custom_command_type=custom_type,
min_profile='2017-03-10-profile')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
def get_cognitiveservices_management_client(cli_ctx, *_):
from azure.cli.core.commands.client_factory import get_mgmt_service_client
from azure.mgmt.cognitiveservices import CognitiveServicesManagementClient
return get_mgmt_service_client(cli_ctx, CognitiveServicesManagementClient, location='notused')


def cf_cognitive_service_accounts(cli_ctx, *_):
return get_cognitiveservices_management_client(cli_ctx).cognitive_services_accounts
return get_mgmt_service_client(cli_ctx, CognitiveServicesManagementClient)


def cf_accounts(cli_ctx, *_):
return get_cognitiveservices_management_client(cli_ctx).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 @@ -20,6 +20,14 @@
text: az cognitiveservices list -g MyResourceGroup
"""

helps['cognitiveservices account list'] = """
type: command
short-summary: List available Azure Cognitive Services accounts.
examples:
- name: List all the Cognitive Services accounts in a resource group.
text: az cognitiveservices account list -g MyResourceGroup
"""

helps['cognitiveservices account'] = """
type: group
short-summary: Manage Azure Cognitive Services accounts.
Expand All @@ -36,6 +44,10 @@
helps['cognitiveservices account create'] = """
type: command
short-summary: Create an Azure Cognitive Services account.
parameters:
- name: --kind
populator-commands:
- az cognitiveservices account list-kinds
examples:
- name: Create an S0 face API Cognitive Services account in West Europe without confirmation required.
text: az cognitiveservices account create -n myresource -g myResourceGroup --kind Face --sku S0 -l WestEurope --yes
Expand All @@ -52,6 +64,10 @@
helps['cognitiveservices account update'] = """
type: command
short-summary: Update the properties of an Azure Cognitive Services account.
parameters:
- name: --sku
populator-commands:
- az cognitiveservices account list-skus
examples:
- name: Update sku and tags.
text: az cognitiveservices account update --name myresource -g cognitive-services-resource-group --sku S0 --tags external-app=chatbot-HR azure-web-app-bot=HR-external azure-app-service=HR-external-app-service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,30 @@
# --------------------------------------------------------------------------------------------

from azure.cli.core.commands import CliCommandType
from azure.cli.command_modules.cognitiveservices._client_factory import (
cf_cognitive_service_accounts,
cf_accounts)
from azure.cli.command_modules.cognitiveservices._client_factory import cf_accounts, cf_resource_skus


def load_command_table(self, _):
mgmt_type = CliCommandType(
operations_tmpl='azure.mgmt.cognitiveservices.operations.cognitive_services_accounts_operations#CognitiveServicesAccountsOperations.{}', # pylint: disable=line-too-long
client_factory=cf_cognitive_service_accounts
operations_tmpl='azure.mgmt.cognitiveservices.operations.accounts_operations#AccountsOperations.{}',
client_factory=cf_accounts
)

with self.command_group('cognitiveservices account', mgmt_type) as g:
g.custom_command('create', 'create')
g.command('delete', 'delete')
g.show_command('show', 'get_properties')
g.custom_command('update', 'update')
g.custom_command('list', 'list_resources')
g.command('list-skus', 'list_skus')
g.custom_command('list-usage', 'list_usages')
g.custom_command('list-kinds', 'list_kinds', client_factory=cf_resource_skus)

with self.command_group('cognitiveservices account keys', mgmt_type) as g:
g.command('regenerate', 'regenerate_key')
g.command('list', 'list_keys')

# deprecating this
with self.command_group('cognitiveservices') as g:
g.custom_command('list', 'list_resources', client_factory=cf_accounts)
g.custom_command('list', 'list_resources',
deprecate_info=g.deprecate(redirect='az cognitiveservices account list', hide=True))
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ def list_resources(client, resource_group_name=None):
return client.list()


def list_usages(client, resource_group_name, account_name):
"""
List usages for Azure Cognitive Services account.
"""
return client.get_usages(resource_group_name, account_name).value


def list_kinds(client):
"""
List all valid kinds for Azure Cognitive Services account.
:param client: the ResourceSkusOperations
:return: a list
"""
# The client should be ResourceSkusOperations, and list() should return a list of SKUs for all regions.
# The sku will have "kind" and we use that to extract full list of kinds.
kinds = set([x.kind for x in client.list()])
return sorted(list(kinds))


def create(
client, resource_group_name, account_name, sku_name, kind, location, tags=None, api_properties=None, yes=None):

Expand All @@ -42,16 +62,17 @@ def create(
option = prompt_y_n(hint)
if not option:
raise CLIError('Operation cancelled.')
sku = Sku(sku_name)
sku = Sku(name=sku_name)

if api_properties is None:
properties = {}
else:
properties = {"apiProperties": api_properties}
params = CognitiveServicesAccountCreateParameters(sku, kind, location, properties, tags)
params = CognitiveServicesAccountCreateParameters(sku=sku, kind=kind, location=location,
properties=properties, tags=tags)
return client.create(resource_group_name, account_name, params)


def update(client, resource_group_name, account_name, sku_name=None, tags=None):
sku = Sku(sku_name)
sku = Sku(name=sku_name)
return client.update(resource_group_name, account_name, sku, tags)

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
interactions:
- request:
body: '{"location": "westus", "tags": {"product": "azurecli", "cause": "automation",
"date": "2018-08-23T22:39:40Z"}}'
headers:
Accept: [application/json]
Accept-Encoding: ['gzip, deflate']
CommandName: [group create]
Connection: [keep-alive]
Content-Length: ['110']
Content-Type: [application/json; charset=utf-8]
User-Agent: [python/3.6.5 (Windows-10-10.0.17134-SP0) requests/2.19.1 msrest/0.5.1
msrest_azure/0.4.34 resourcemanagementclient/2.0.0 Azure-SDK-For-Python
AZURECLI/2.0.45]
accept-language: [en-US]
method: PUT
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/clitest.rg000001?api-version=2018-05-01
response:
body: {string: '{"id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001","name":"clitest.rg000001","location":"westus","tags":{"product":"azurecli","cause":"automation","date":"2018-08-23T22:39:40Z"},"properties":{"provisioningState":"Succeeded"}}'}
headers:
cache-control: [no-cache]
content-length: ['384']
content-type: [application/json; charset=utf-8]
date: ['Thu, 23 Aug 2018 22:39:43 GMT']
expires: ['-1']
pragma: [no-cache]
strict-transport-security: [max-age=31536000; includeSubDomains]
x-content-type-options: [nosniff]
x-ms-ratelimit-remaining-subscription-writes: ['1199']
status: {code: 201, message: Created}
- request:
body: '{"sku": {"name": "S0"}, "kind": "Face", "location": "westeurope", "properties":
{}}'
headers:
Accept: [application/json]
Accept-Encoding: ['gzip, deflate']
CommandName: [cognitiveservices account create]
Connection: [keep-alive]
Content-Length: ['83']
Content-Type: [application/json; charset=utf-8]
User-Agent: [python/3.6.5 (Windows-10-10.0.17134-SP0) requests/2.19.1 msrest/0.5.1
msrest_azure/0.4.34 azure-mgmt-cognitiveservices/3.0.0 Azure-SDK-For-Python
AZURECLI/2.0.45]
accept-language: [en-US]
method: PUT
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.CognitiveServices/accounts/cs_cli_test_000002?api-version=2017-04-18
response:
body: {string: '{"id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.CognitiveServices/accounts/cs_cli_test_000002","name":"cs_cli_test_000002","type":"Microsoft.CognitiveServices/accounts","etag":"\"62006d1f-0000-0000-0000-5b7f37b10000\"","location":"westeurope","sku":{"name":"S0"},"kind":"Face","properties":{"endpoint":"https://westeurope.api.cognitive.microsoft.com/face/v1.0","internalId":"ed611adbee814c7b80a849be7cfa8c17","dateCreated":"2018-08-23T22:39:45.5518443Z","provisioningState":"Succeeded"}}'}
headers:
cache-control: [no-cache]
content-length: ['605']
content-type: [application/json; charset=utf-8]
date: ['Thu, 23 Aug 2018 22:39:46 GMT']
etag: ['"62006d1f-0000-0000-0000-5b7f37b10000"']
expires: ['-1']
pragma: [no-cache]
server: [Microsoft-IIS/10.0]
strict-transport-security: [max-age=31536000; includeSubDomains]
x-aspnet-version: [4.0.30319]
x-content-type-options: [nosniff]
x-ms-ratelimit-remaining-subscription-writes: ['1199']
x-powered-by: [ASP.NET]
status: {code: 201, message: Created}
- request:
body: null
headers:
Accept: [application/json]
Accept-Encoding: ['gzip, deflate']
CommandName: [cognitiveservices account list-skus]
Connection: [keep-alive]
Content-Type: [application/json; charset=utf-8]
User-Agent: [python/3.6.5 (Windows-10-10.0.17134-SP0) requests/2.19.1 msrest/0.5.1
msrest_azure/0.4.34 azure-mgmt-cognitiveservices/3.0.0 Azure-SDK-For-Python
AZURECLI/2.0.45]
accept-language: [en-US]
method: GET
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.CognitiveServices/accounts/cs_cli_test_000002/skus?api-version=2017-04-18
response:
body: {string: '{"value":[{"resourceType":"Microsoft.CognitiveServices/accounts","sku":{"name":"F0","tier":"Free"}},{"resourceType":"Microsoft.CognitiveServices/accounts","sku":{"name":"S0","tier":"Standard"}}]}'}
headers:
cache-control: [no-cache]
content-length: ['195']
content-type: [application/json; charset=utf-8]
date: ['Thu, 23 Aug 2018 22:39:47 GMT']
expires: ['-1']
pragma: [no-cache]
server: [Microsoft-IIS/10.0]
strict-transport-security: [max-age=31536000; includeSubDomains]
transfer-encoding: [chunked]
vary: [Accept-Encoding]
x-aspnet-version: [4.0.30319]
x-content-type-options: [nosniff]
x-powered-by: [ASP.NET]
status: {code: 200, message: OK}
- request:
body: null
headers:
Accept: [application/json]
Accept-Encoding: ['gzip, deflate']
CommandName: [group delete]
Connection: [keep-alive]
Content-Length: ['0']
Content-Type: [application/json; charset=utf-8]
User-Agent: [python/3.6.5 (Windows-10-10.0.17134-SP0) requests/2.19.1 msrest/0.5.1
msrest_azure/0.4.34 resourcemanagementclient/2.0.0 Azure-SDK-For-Python
AZURECLI/2.0.45]
accept-language: [en-US]
method: DELETE
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/clitest.rg000001?api-version=2018-05-01
response:
body: {string: ''}
headers:
cache-control: [no-cache]
content-length: ['0']
date: ['Thu, 23 Aug 2018 22:39:48 GMT']
expires: ['-1']
location: ['https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/operationresults/eyJqb2JJZCI6IlJFU09VUkNFR1JPVVBERUxFVElPTkpPQi1DTElURVNUOjJFUkczUUZFR0ZaT1dSQUNMUkhDVE0yQzJUSzI2WE9NRDVYUjZYVHw1RjlGQzc0QUY1RDE5QjQzLVdFU1RVUyIsImpvYkxvY2F0aW9uIjoid2VzdHVzIn0?api-version=2018-05-01']
pragma: [no-cache]
strict-transport-security: [max-age=31536000; includeSubDomains]
x-content-type-options: [nosniff]
x-ms-ratelimit-remaining-subscription-deletes: ['14999']
status: {code: 202, message: Accepted}
version: 1
Loading

0 comments on commit a08e675

Please sign in to comment.