-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added "az acr" commands for Azure container registries
1. Added commands to manage Azure container registries (create/delete/show/list/update). 2. Integrated repository list and show-tags commands. 3. Added mgmt_acr SDK for Python.
- Loading branch information
Showing
30 changed files
with
1,677 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
Microsoft Azure CLI 'acr' Command Module | ||
================================== | ||
|
||
Commands to manage Azure container registries | ||
------------- | ||
:: | ||
|
||
Group | ||
az acr: Commands to manage Azure container registries. | ||
|
||
Subgroups: | ||
repository | ||
|
||
Commands: | ||
create : Create a container registry. | ||
delete : Delete a container registry. | ||
list : List container registries. | ||
show : Get a container registry. | ||
update : Update a container registry. | ||
|
||
Create a container registry | ||
------------- | ||
:: | ||
|
||
Command | ||
az acr create: Create a container registry. | ||
|
||
Arguments | ||
--location -l [Required]: Location. | ||
--name -n [Required]: Name of container registry. | ||
--resource-group -g [Required]: Name of resource group. | ||
--storage-account-name -s : Name of storage account. | ||
|
||
Examples | ||
Create a container registry with a new storage account | ||
az acr create -n <registry-name> -g <resource-group> -l <location> | ||
Create a container registry with a new/existing storage account | ||
az acr create -n <registry-name> -g <resource-group> -l <location> -s <storage-account-name> | ||
|
||
Delete a container registry | ||
------------- | ||
:: | ||
|
||
Command | ||
az acr delete: Delete a container registry. | ||
|
||
Arguments | ||
--name -n [Required]: Name of container registry. | ||
|
||
List container registries | ||
------------- | ||
:: | ||
|
||
Command | ||
az acr list: List container registries. | ||
|
||
Arguments | ||
--resource-group -g: Name of resource group. | ||
|
||
Examples | ||
List container registries and show result in a table | ||
az acr list -o table | ||
List container registries in a resource group and show result in a table | ||
az acr list -g <resource-group> -o table | ||
|
||
Get a container registry | ||
------------- | ||
:: | ||
|
||
Command | ||
az acr show: Get a container registry. | ||
|
||
Arguments | ||
--name -n [Required]: Name of container registry. | ||
|
||
Update a container registry | ||
------------- | ||
:: | ||
|
||
Command | ||
az acr update: Update a container registry. | ||
|
||
Arguments | ||
--name -n [Required]: Name of container registry. | ||
--tags : Multiple semicolon separated tags in 'key[=value]' format. Use "" to | ||
clear existing tags. | ||
Examples | ||
Update tags of a container registry and show result in a table | ||
az acr update -n <registry-name> --tags key1=value1;key2=value2 -o table | ||
|
||
List repositories in a given container registry | ||
------------- | ||
:: | ||
|
||
Command | ||
az acr repository list: List repositories in a given container registry. | ||
|
||
Arguments | ||
--login-server [Required]: The URL of a container registry login server. | ||
--password : The password used to log into the container registry. | ||
--username : The username used to log into the container registry. | ||
|
||
Examples | ||
List repositories in a given container registry under the current subscription | ||
az acr repository list --login-server <login-server> | ||
List repositories in a given container registry with credentials | ||
az acr repository list --login-server <login-server> --username <username> --password | ||
<password> | ||
|
||
Show tags of a given repository in a given container registry | ||
------------- | ||
:: | ||
|
||
Command | ||
az acr repository show-tags: Show tags of a given repository in a given container registry. | ||
|
||
Arguments | ||
--login-server [Required]: The URL of a container registry login server. | ||
--repository [Required]: The repository to obtain tags from. | ||
--password : The password used to log into the container registry. | ||
--username : The username used to log into the container registry. | ||
|
||
Examples | ||
Show tags of a given repository in a given container registry under the current subscription | ||
az acr repository show-tags --login-server <login-server> --repository <repository> | ||
Show tags of a given repository in a given container registry with credentials | ||
az acr repository show-tags --login-server <login-server> --repository <repository> | ||
--username <username> --password <password> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#--------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
#--------------------------------------------------------------------------------------------- | ||
import pkg_resources | ||
pkg_resources.declare_namespace(__name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#--------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
#--------------------------------------------------------------------------------------------- | ||
import pkg_resources | ||
pkg_resources.declare_namespace(__name__) |
6 changes: 6 additions & 0 deletions
6
src/command_modules/azure-cli-acr/azure/cli/command_modules/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#--------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
#--------------------------------------------------------------------------------------------- | ||
import pkg_resources | ||
pkg_resources.declare_namespace(__name__) |
11 changes: 11 additions & 0 deletions
11
src/command_modules/azure-cli-acr/azure/cli/command_modules/acr/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#--------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
#--------------------------------------------------------------------------------------------- | ||
|
||
#pylint: disable=unused-import | ||
|
||
import azure.cli.command_modules.acr._help | ||
import azure.cli.command_modules.acr._params | ||
import azure.cli.command_modules.acr.custom | ||
import azure.cli.command_modules.acr.repository |
99 changes: 99 additions & 0 deletions
99
src/command_modules/azure-cli-acr/azure/cli/command_modules/acr/_arm_utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#--------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
#--------------------------------------------------------------------------------------------- | ||
|
||
from azure.cli.command_modules.acr.mgmt_acr.models import RegistryParameters | ||
|
||
from ._constants import ( | ||
RESOURCE_PROVIDER, | ||
RESOURCE_TYPE | ||
) | ||
from ._factory import get_arm_service_client | ||
|
||
from azure.cli.command_modules.acr.mgmt_acr import VERSION | ||
|
||
def arm_get_registries_in_subscription(): | ||
'''Returns the list of container registries in the current subscription. | ||
''' | ||
client = get_arm_service_client() | ||
filter_str = "resourceType eq '{}'".format(RESOURCE_TYPE) | ||
result = list(client.resources.list(filter=filter_str)) | ||
|
||
return [RegistryParameters(item.id, item.name, item.location, item.tags) for item in result] | ||
|
||
def arm_get_registries_in_resource_group(resource_group_name): | ||
'''Returns the list of container registries in the resource group. | ||
:param str resource_group_name: The name of resource group | ||
''' | ||
client = get_arm_service_client() | ||
filter_str = "resourceType eq '{}'".format(RESOURCE_TYPE) | ||
result = list(client.resource_groups.list_resources(resource_group_name, filter=filter_str)) | ||
|
||
return [RegistryParameters(item.id, item.name, item.location, item.tags) for item in result] | ||
|
||
def arm_get_registry_by_name(registry_name): | ||
'''Returns the container registry that matches the registry name. | ||
:param str registry_name: The name of container registry | ||
''' | ||
registries = arm_get_registries_in_subscription() | ||
elements = [item for item in registries if item.name.lower() == registry_name.lower()] | ||
|
||
if len(elements) == 0: | ||
return None | ||
elif len(elements) == 1: | ||
return elements[0] | ||
else: | ||
raise ValueError('More than one container registries are found with name: ' + registry_name) | ||
|
||
def arm_deploy_template(resource_group_name, registry_name, location, storage_account_name): | ||
'''Deploys ARM template to create a container registry. | ||
:param str resource_group_name: The name of resource group | ||
:param str registry_name: The name of container registry | ||
:param str location: The name of location | ||
:param str storage_account_name: The name of storage account | ||
''' | ||
from azure.mgmt.resource.resources.models import DeploymentProperties | ||
from azure.cli.core._util import get_file_json | ||
import os | ||
|
||
file_path = os.path.join(os.path.dirname(__file__), 'template.json') | ||
template = get_file_json(file_path) | ||
parameters = _parameters(registry_name, location, storage_account_name) | ||
properties = DeploymentProperties(template=template, parameters=parameters, mode='incremental') | ||
|
||
return _arm_deploy_template(resource_group_name, properties) | ||
|
||
def _arm_deploy_template(resource_group_name, properties, index=0): | ||
'''Deploys ARM template to create a container registry. | ||
:param str resource_group_name: The name of resource group | ||
:param DeploymentProperties properties: The properties of a deployment | ||
:param int index: The index added to deployment name to avoid conflict | ||
''' | ||
if index == 0: | ||
deployment_name = RESOURCE_PROVIDER | ||
else: | ||
deployment_name = RESOURCE_PROVIDER + '_' + str(index) | ||
|
||
client = get_arm_service_client() | ||
|
||
try: | ||
client.deployments.validate(resource_group_name, deployment_name, properties) | ||
return client.deployments.create_or_update(resource_group_name, deployment_name, properties) | ||
except: #pylint: disable=W0702 | ||
return _arm_deploy_template(resource_group_name, properties, index + 1) | ||
|
||
def _parameters(registry_name, location, storage_account_name): | ||
'''Returns a dict of deployment parameters. | ||
:param str registry_name: The name of container registry | ||
:param str location: The name of location | ||
:param str storage_account_name: The name of storage account | ||
''' | ||
parameters = { | ||
'registryName': {'value': registry_name}, | ||
'registryLocation': {'value': location}, | ||
'registryApiVersion': {'value': VERSION}, | ||
'storageAccountName': {'value': storage_account_name}, | ||
'storageAccountApiVersion': {'value': '2015-05-01-preview'} | ||
} | ||
return parameters |
7 changes: 7 additions & 0 deletions
7
src/command_modules/azure-cli-acr/azure/cli/command_modules/acr/_constants.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#--------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
#--------------------------------------------------------------------------------------------- | ||
|
||
RESOURCE_PROVIDER = 'Microsoft.ContainerRegistry' | ||
RESOURCE_TYPE = RESOURCE_PROVIDER + '/registries' |
46 changes: 46 additions & 0 deletions
46
src/command_modules/azure-cli-acr/azure/cli/command_modules/acr/_factory.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#--------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
#--------------------------------------------------------------------------------------------- | ||
|
||
from azure.cli.core._profile import Profile | ||
from azure.cli.core._config import az_config | ||
from azure.mgmt.resource.resources import ResourceManagementClient | ||
|
||
from azure.cli.core.commands.client_factory import ( | ||
configure_common_settings, | ||
get_mgmt_service_client | ||
) | ||
|
||
from azure.cli.command_modules.acr.mgmt_acr import ( | ||
ContainerRegistry, | ||
ContainerRegistryConfiguration, | ||
VERSION | ||
) | ||
|
||
import azure.cli.core._logging as _logging | ||
logger = _logging.get_az_logger(__name__) | ||
|
||
def get_arm_service_client(): | ||
'''Returns the client for managing ARM resources. | ||
''' | ||
return get_mgmt_service_client(ResourceManagementClient) | ||
|
||
def get_registry_service_client(): | ||
'''Returns the client for managing container registries. | ||
''' | ||
profile = Profile() | ||
credentials, subscription_id, _ = profile.get_login_credentials() | ||
|
||
customized_api_version = az_config.get('acr', 'apiversion', None) | ||
if customized_api_version: | ||
logger.warning('Customized api-version is used: ' + customized_api_version) | ||
|
||
api_version = customized_api_version or VERSION | ||
|
||
config = ContainerRegistryConfiguration(subscription_id, api_version, credentials) | ||
client = ContainerRegistry(config) | ||
|
||
configure_common_settings(client) | ||
|
||
return client.registries |
Oops, something went wrong.