Skip to content

Commit 273e7cc

Browse files
authored
resource: support generic resource create (#2849)
* resource: add generic create * address review feedback
1 parent 9256f11 commit 273e7cc

File tree

7 files changed

+1049
-0
lines changed

7 files changed

+1049
-0
lines changed

src/command_modules/azure-cli-resource/HISTORY.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
33
Release History
44
===============
5+
2.0.3 (unreleased)
6+
7+
* Support generic resource create (#2606)
58

69
2.0.3 (2017-04-17)
710
++++++++++++++++++

src/command_modules/azure-cli-resource/azure/cli/command_modules/resource/_help.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@
160160
az resource tag --tags vmlist=vm1 --id /subscriptions/0b1f6471-1bf0-4dda-aec3-111111111111/resourceGroups/MyResourceGroup/providers/Microsoft.Web/sites/MyWebapp
161161
"""
162162

163+
helps['resource create'] = """
164+
type: command
165+
short-summary: create a resource.
166+
examples:
167+
- name: Create a resource by providing a full resource object json. Note, you can also use "@<file>" to load from a json file.
168+
text: >
169+
az resource create -g myRG -n myPlan --resource-type Microsoft.web/serverFarms --is-full-object --properties "{ \\"location\\":\\"westus\\",\\"sku\\":{\\"name\\":\\"B1\\",\\"tier\\":\\"BASIC\\"}}"
170+
- name: Create a resource by only providing resource properties
171+
text: >
172+
az resource create -g myRG -n myWeb --resource-type Microsoft.web/sites --properties "{\\"serverFarmId\\":\\"myPlan\\"}"
173+
"""
174+
163175
helps['resource update'] = """
164176
type: command
165177
short-summary: Update a resource.

src/command_modules/azure-cli-resource/azure/cli/command_modules/resource/_params.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
register_cli_argument('resource', 'tags', tags_type)
3939
register_cli_argument('resource list', 'name', resource_name_type)
4040
register_cli_argument('resource move', 'ids', nargs='+')
41+
register_cli_argument('resource create', 'properties', options_list=('--properties', '-p'),
42+
help='a JSON-formatted string containing resource properties')
43+
register_cli_argument('resource create', 'is_full_object', action='store_true',
44+
help='Indicates that the properties object includes other options such as location, tags, sku, and/or plan.')
4145

4246
register_cli_argument('provider', 'top', ignore_type)
4347
register_cli_argument('provider', 'resource_provider_namespace', options_list=('--namespace', '-n'), completer=get_providers_completion_list,

src/command_modules/azure-cli-resource/azure/cli/command_modules/resource/commands.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def transform_resource_list(result):
4747
transformed.append(res)
4848
return transformed
4949

50+
cli_command(__name__, 'resource create', 'azure.cli.command_modules.resource.custom#create_resource')
5051
cli_command(__name__, 'resource delete', 'azure.cli.command_modules.resource.custom#delete_resource')
5152
cli_command(__name__, 'resource show', 'azure.cli.command_modules.resource.custom#show_resource', exception_handler=empty_on_404)
5253
cli_command(__name__, 'resource list', 'azure.cli.command_modules.resource.custom#list_resources', table_transformer=transform_resource_list)

src/command_modules/azure-cli-resource/azure/cli/command_modules/resource/custom.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ def export_deployment_as_template(resource_group_name, deployment_name):
208208
result = smc.deployments.export_template(resource_group_name, deployment_name)
209209
print(json.dumps(result.template, indent=2))#pylint: disable=no-member
210210

211+
def create_resource(properties, resource_group_name=None, resource_provider_namespace=None,
212+
parent_resource_path=None, resource_type=None, resource_name=None,
213+
resource_id=None, api_version=None, location=None, is_full_object=False):
214+
res = _ResourceUtils(resource_group_name, resource_provider_namespace,
215+
parent_resource_path, resource_type, resource_name,
216+
resource_id, api_version)
217+
return res.create_resource(properties, location, is_full_object)
218+
211219
def show_resource(resource_group_name=None, resource_provider_namespace=None,
212220
parent_resource_path=None, resource_type=None, resource_name=None,
213221
resource_id=None, api_version=None):
@@ -747,6 +755,34 @@ def __init__(self, resource_group_name=None, resource_provider_namespace=None,
747755
self.resource_id = resource_id
748756
self.api_version = api_version
749757

758+
def create_resource(self, properties, location, is_full_object):
759+
res = json.loads(properties)
760+
if not is_full_object:
761+
if not location:
762+
if self.resource_id:
763+
rg_name = parse_resource_id(self.resource_id)['resource_group']
764+
else:
765+
rg_name = self.resource_group_name
766+
location = self.rcf.resource_groups.get(rg_name).location
767+
768+
res = GenericResource(location=location, properties=res)
769+
elif res.get('location', None) is None:
770+
raise IncorrectUsageError("location of the resource is required")
771+
772+
if self.resource_id:
773+
resource = self.rcf.resources.create_or_update_by_id(self.resource_id,
774+
self.api_version,
775+
res)
776+
else:
777+
resource = self.rcf.resources.create_or_update(self.resource_group_name,
778+
self.resource_provider_namespace,
779+
self.parent_resource_path or '',
780+
self.resource_type,
781+
self.resource_name,
782+
self.api_version,
783+
res)
784+
return resource
785+
750786
def get_resource(self):
751787
if self.resource_id:
752788
resource = self.rcf.resources.get_by_id(self.resource_id, self.api_version)

0 commit comments

Comments
 (0)