Skip to content

Commit

Permalink
Initial commit for Appliance commands (#2985)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivsriaus authored and troydai committed May 5, 2017
1 parent 5721f0c commit 38e2936
Show file tree
Hide file tree
Showing 13 changed files with 1,070 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/command_modules/azure-cli-acr/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

DEPENDENCIES = [
'azure-cli-core',
'azure-mgmt-resource==1.0.0rc1',
'azure-mgmt-resource==1.0.0rc3',
'azure-mgmt-storage==1.0.0rc1',
'azure-mgmt-containerregistry==0.2.1',
]
Expand Down
2 changes: 1 addition & 1 deletion src/command_modules/azure-cli-network/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
'azure-mgmt-network==1.0.0rc3',
'azure-mgmt-trafficmanager==0.30.0',
'azure-mgmt-dns==1.0.1',
'azure-mgmt-resource==1.0.0rc1',
'azure-mgmt-resource==1.0.0rc3',
'azure-cli-core'
]

Expand Down
4 changes: 4 additions & 0 deletions src/command_modules/azure-cli-resource/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
Release History
===============

unreleased
++++++++++++++++++
* Add managedapp and managedapp definition commands (#2985)

2.0.4 (2017-04-28)
++++++++++++++++++
* Support 'provider operation' commands (#2908)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def _authorization_management_client(**_):
return get_mgmt_service_client(AuthorizationManagementClient)


def _resource_managedapps_client_factory(**_):
from azure.cli.core.commands.client_factory import get_mgmt_service_client
from azure.mgmt.resource.managedapplications import ManagedApplicationClient
return get_mgmt_service_client(ManagedApplicationClient)


def cf_resource_groups(_):
return _resource_client_factory().resource_groups

Expand Down Expand Up @@ -78,3 +84,11 @@ def cf_management_locks(_):

def cf_resource_links():
return _resource_links_client_factory().resource_links


def cf_resource_managedapplications(_):
return _resource_managedapps_client_factory().appliances


def cf_resource_managedappdefinitions(_):
return _resource_managedapps_client_factory().appliance_definitions
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,49 @@
from azure.cli.core.help_files import helps # pylint: disable=unused-import

# pylint: disable=line-too-long, too-many-lines
helps['managedapp'] = """
type: group
short-summary: Manage managed applications and managed application definitions.
"""
helps['managedapp definition'] = """
type: group
short-summary: Manage managed application definitions.
"""
helps['managedapp create'] = """
type: command
short-summary: Creates a managed application.
examples:
- name: Create a managed application of kind 'ServiceCatalog'. This requires a valid managed application definition id.
text: >
az managedapp create -g MyResourceGroup -n MyManagedApp -l westcentralus --kind ServiceCatalog -m "/subscriptions/0b1f6471-1bf0-4dda-aec3-111111111111/resourceGroups/myManagedRG" -d "/subscriptions/0b1f6471-1bf0-4dda-aec3-111111111111/resourceGroups/MyResourceGroup/providers/Microsoft.Solutions/applianceDefinitions/myManagedAppDef"
- name: Create a managed application of kind 'MarketPlace'. This requires a valid plan, containing details about existing marketplace package like plan name, version, publisher and product
text: >
az managedapp create -g MyResourceGroup -n MyManagedApp -l westcentralus --kind MarketPlace -m "/subscriptions/0b1f6471-1bf0-4dda-aec3-111111111111/resourceGroups/myManagedRG" -plan-name ContosoAppliance --plan-version "1.0" --plan-product "contoso-appliance" --plan-publisher Contoso
"""
helps['managedapp definition create'] = """
type: command
short-summary: Creates a managed application definition.
examples:
- name: Create a managed application defintion.
text: >
az managedapp definition create -g MyResourceGroup -n MyManagedAppDef -l eastus --display-name "MyManagedAppDef" --description "My Managed App Def description" -a "myPrincipalId:myRoleId" --lock-level None --package-file-uri "https://path/to/myPackage.zip"
"""
helps['managedapp definition delete'] = """
type: command
short-summary: Delete a managed application definition.
"""
helps['managedapp definition list'] = """
type: command
short-summary: Lists managed application definitions.
"""
helps['managedapp delete'] = """
type: command
short-summary: Delete a managed application.
"""
helps['managedapp list'] = """
type: command
short-summary: Lists managed applications by resource group, or by subscription.
"""
helps['lock'] = """
type: group
short-summary: Manage Azure locks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from azure.mgmt.resource.resources.models import DeploymentMode
from azure.mgmt.resource.locks.models import LockLevel
from azure.mgmt.resource.managedapplications.models import ApplianceLockLevel
from azure.cli.core.commands import register_cli_argument, CliArgumentType
from azure.cli.core.commands.parameters import (ignore_type, resource_group_name_type, tag_type,
tags_type, get_resource_group_completion_list,
Expand Down Expand Up @@ -94,3 +95,17 @@
register_cli_argument('lock', 'resource_type', arg_type=resource_type_type,
completer=get_resource_types_completion_list,)
register_cli_argument('lock', 'resource_name', options_list=('--resource-name'), help='The name of the resource this lock applies to.')

register_cli_argument('managedapp', 'resource_group_name', arg_type=resource_group_name_type, help='the resource group of the managed application')
register_cli_argument('managedapp', 'appliance_name', options_list=('--name', '-n'))
register_cli_argument('managedapp', 'managedapp_id', options_list=('--id',), help='The managed application resource ID')
register_cli_argument('managedapp definition', 'resource_group_name', arg_type=resource_group_name_type, help='the resource group of the managed application definition')
register_cli_argument('managedapp definition', 'appliance_definition_name', options_list=('--name', '-n'))
register_cli_argument('managedapp definition', 'managedapp_definition_id', options_list=('--id',), help='The managed application definition resource ID')
register_cli_argument('managedapp create', 'name', options_list=('--name', '-n'), help='name of the new managed application', completer=None)
register_cli_argument('managedapp create', 'location', help='the managed application location')
register_cli_argument('managedapp create', 'managedapp_definition_id', options_list=('--managedapp-definition-id', '-d'), help='the full qualified managed application definition id')
register_cli_argument('managedapp create', 'managedby_resource_group_id', options_list=('--managed-rg-id', '-m'), help='the resource group managed by the managed application')
register_cli_argument('managedapp create', 'parameters', help='JSON formatted string or a path to a file with such content', type=file_type)
register_cli_argument('managedapp definition create', 'lock_level', **enum_choice_list(ApplianceLockLevel))
register_cli_argument('managedapp definition create', 'authorizations', options_list=('--authorizations', '-a'), nargs='+', help="space separated authorization pairs in a format of <principalId>:<roleDefinitionId>")
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
cf_deployments,
cf_deployment_operations,
cf_policy_definitions,
cf_resource_links)
cf_resource_links,
cf_resource_managedapplications,
cf_resource_managedappdefinitions)


# Resource group commands
Expand Down Expand Up @@ -125,3 +127,13 @@ def transform_deployments_list(result):
cli_command(__name__, 'resource link show', 'azure.mgmt.resource.links.operations#ResourceLinksOperations.get', cf_resource_links, exception_handler=empty_on_404)
cli_command(__name__, 'resource link list', 'azure.cli.command_modules.resource.custom#list_resource_links')
cli_command(__name__, 'resource link update', 'azure.cli.command_modules.resource.custom#update_resource_link')

cli_command(__name__, 'managedapp create', 'azure.cli.command_modules.resource.custom#create_appliance')
cli_command(__name__, 'managedapp delete', 'azure.mgmt.resource.managedapplications.operations#AppliancesOperations.delete', cf_resource_managedapplications)
cli_command(__name__, 'managedapp show', 'azure.cli.command_modules.resource.custom#show_appliance', exception_handler=empty_on_404)
cli_command(__name__, 'managedapp list', 'azure.cli.command_modules.resource.custom#list_appliances')

cli_command(__name__, 'managedapp definition create', 'azure.cli.command_modules.resource.custom#create_appliancedefinition')
cli_command(__name__, 'managedapp definition delete', 'azure.mgmt.resource.managedapplications.operations#ApplianceDefinitionsOperations.delete', cf_resource_managedappdefinitions)
cli_command(__name__, 'managedapp definition show', 'azure.cli.command_modules.resource.custom#show_appliancedefinition')
cli_command(__name__, 'managedapp definition list', 'azure.mgmt.resource.managedapplications.operations#ApplianceDefinitionsOperations.list_by_resource_group', cf_resource_managedappdefinitions, exception_handler=empty_on_404)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
from azure.mgmt.resource.locks.models import ManagementLockObject
from azure.mgmt.resource.links.models import ResourceLinkProperties

from azure.mgmt.resource.managedapplications.models import Appliance
from azure.mgmt.resource.managedapplications.models import Plan
from azure.mgmt.resource.managedapplications.models import ApplianceDefinition
from azure.mgmt.resource.managedapplications.models import ApplianceProviderAuthorization

from azure.cli.core.parser import IncorrectUsageError
from azure.cli.core.prompting import prompt, prompt_pass, prompt_t_f, prompt_choice_list, prompt_int
from azure.cli.core.util import CLIError, get_file_json, shell_safe_json_parse
Expand All @@ -28,7 +33,8 @@
_resource_policy_client_factory,
_resource_lock_client_factory,
_resource_links_client_factory,
_authorization_management_client)
_authorization_management_client,
_resource_managedapps_client_factory)

logger = azlogging.get_az_logger(__name__)

Expand Down Expand Up @@ -67,6 +73,125 @@ def create_resource_group(rg_name, location, tags=None):
return rcf.resource_groups.create_or_update(rg_name, parameters)


def create_appliance(resource_group_name, # pylint: disable=too-many-arguments
appliance_name, managedby_resource_group_id,
location, kind, managedapp_definition_id=None,
plan_name=None, plan_publisher=None, plan_product=None,
plan_version=None, tags=None, parameters=None):
""" Create a new managed application.
:param str resource_group_name:the desired resource group name
:param str appliance_name:the managed application name
:param str kind:the managed application kind. can be marketplace or servicecatalog
:param str plan_name:the managed application package plan name
:param str plan_publisher:the managed application package plan publisher
:param str plan_product:the managed application package plan product
:param str plan_version:the managed application package plan version
:param str tags:tags in 'a=b c' format
"""
racf = _resource_managedapps_client_factory()
appliance = Appliance(
location=location,
managed_resource_group_id=managedby_resource_group_id,
kind=kind,
tags=tags
)

if kind.lower() == 'servicecatalog':
if managedapp_definition_id:
appliance.appliance_definition_id = managedapp_definition_id
else:
raise CLIError('--managedapp-definition-id is required if kind is ServiceCatalog')
elif kind.lower() == 'marketplace':
if (plan_name is None and plan_product is None and
plan_publisher is None and plan_version is None):
raise CLIError('--plan-name, --plan-product, --plan-publisher and \
--plan-version are all required if kind is MarketPlace')
else:
appliance.plan = Plan(plan_name, plan_publisher, plan_product, plan_version)

applianceParameters = None

if parameters:
if os.path.exists(parameters):
applianceParameters = get_file_json(parameters)
else:
applianceParameters = shell_safe_json_parse(parameters)

appliance.parameters = applianceParameters

return racf.appliances.create_or_update(resource_group_name, appliance_name, appliance)


def show_appliance(resource_group_name=None, appliance_name=None, managedapp_id=None):
""" Gets a managed application.
:param str resource_group_name:the resource group name
:param str appliance_name:the managed application name
"""
racf = _resource_managedapps_client_factory()
if managedapp_id:
appliance = racf.appliances.get_by_id(managedapp_id)
else:
appliance = racf.appliances.get(resource_group_name, appliance_name)
return appliance


def show_appliancedefinition(resource_group_name=None, appliance_definition_name=None,
managedapp_definition_id=None):
""" Gets a managed application definition.
:param str resource_group_name:the resource group name
:param str appliance_definition_name:the managed application definition name
"""
racf = _resource_managedapps_client_factory()
if managedapp_definition_id:
appliancedef = racf.appliance_definitions.get_by_id(managedapp_definition_id)
else:
appliancedef = racf.appliance_definitions.get(resource_group_name,
appliance_definition_name)
return appliancedef


def create_appliancedefinition(resource_group_name, # pylint: disable=too-many-arguments
appliance_definition_name, location,
lock_level, package_file_uri, authorizations,
description, display_name, tags=None):
""" Create a new managed application definition.
:param str resource_group_name:the desired resource group name
:param str appliance_definition_name:the managed application definition name
:param str description:the managed application definition description
:param str display_name:the managed application definition display name
:param str package_file_uri:the managed application definition package file uri
:param str tags:tags in 'a=b c' format
"""
racf = _resource_managedapps_client_factory()
authorizations = authorizations or []
applianceAuthList = []

for name_value in authorizations:
# split at the first ':', neither principalId nor roldeDefinitionId should have a ':'
principalId, roleDefinitionId = name_value.split(':', 1)
applianceAuth = ApplianceProviderAuthorization(principalId, roleDefinitionId)
applianceAuthList.append(applianceAuth)

applianceDef = ApplianceDefinition(lock_level, applianceAuthList, package_file_uri)
applianceDef.display_name = display_name
applianceDef.description = description
applianceDef.location = location
applianceDef.tags = tags

return racf.appliance_definitions.create_or_update(resource_group_name,
appliance_definition_name, applianceDef)


def list_appliances(resource_group_name=None):
racf = _resource_managedapps_client_factory()

if resource_group_name:
appliances = racf.appliances.list_by_resource_group(resource_group_name)
else:
appliances = racf.appliances.list_by_subscription()
return list(appliances)


def export_group_as_template(
resource_group_name, include_comments=False, include_parameter_default_value=False):
"""Captures a resource group as a template.
Expand Down
2 changes: 1 addition & 1 deletion src/command_modules/azure-cli-resource/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
]

DEPENDENCIES = [
'azure-mgmt-resource==1.0.0rc1',
'azure-mgmt-resource==1.0.0rc3',
'azure-cli-core',
'azure-mgmt-authorization==0.30.0rc6'
]
Expand Down
Loading

0 comments on commit 38e2936

Please sign in to comment.