Skip to content

Commit e24d94a

Browse files
authored
feat!: redesign parameters to specify APIM instance for import-from-apim command (#45)
1 parent 6539ccd commit e24d94a

File tree

6 files changed

+1282
-294
lines changed

6 files changed

+1282
-294
lines changed

src/apic-extension/azext_apic_extension/aaz/latest/apic/service/_import_from_apim.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@
1717
class ImportFromApim(AAZCommand):
1818
"""Imports APIs from an Azure API Management service instance.
1919
20-
:example: Import From APIM
21-
az apic service import-from-apim -g api-center-test --service-name contosoeuap --source-resource-ids '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/servicegroup/providers/Microsoft.ApiManagement/service/contoso/apis/contosoapi'
20+
:example: Import all APIs from APIM in same resource group
21+
az apic service import-from-apim -g api-center-test --service-name contoso-apic --apim-name contoso-apim --apim-apis *
22+
23+
:example: Import selected APIs from APIM in same resource group
24+
az apic service import-from-apim -g api-center-test --service-name contoso-apic --apim-name contoso-apim --apim-apis [echo,foo]
25+
26+
:example: Import all APIs from APIM in another subscription and resource group
27+
az apic service import-from-apim -g api-center-test --service-name contoso-apic --apim-subscription 00000000-0000-0000-0000-000000000000 --apim-resource-group apim-rg --apim-name contoso-apim --apim-apis *
2228
"""
2329

2430
_aaz_info = {

src/apic-extension/azext_apic_extension/command_patches.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
)
4949
from .aaz.latest.apic.service import ImportFromApim
5050

51+
from azure.cli.core.aaz._arg import AAZStrArg, AAZListArg
52+
5153

5254
class DefaultWorkspaceParameter:
5355
# pylint: disable=too-few-public-methods
@@ -205,5 +207,58 @@ class ImportFromApimExtension(ImportFromApim):
205207
def _build_arguments_schema(cls, *args, **kwargs):
206208
# pylint: disable=protected-access
207209
args_schema = super()._build_arguments_schema(*args, **kwargs)
208-
args_schema.source_resource_ids._required = True
210+
args_schema.source_resource_ids._required = False
211+
args_schema.source_resource_ids._registered = False
212+
213+
args_schema.apim_subscription_id = AAZStrArg(
214+
options=["--apim-subscription"],
215+
help="The subscription id of the source APIM instance.",
216+
required=False
217+
)
218+
219+
args_schema.apim_resource_group = AAZStrArg(
220+
options=["--apim-resource-group"],
221+
help="The resource group of the source APIM instance.",
222+
required=False
223+
)
224+
225+
args_schema.apim_name = AAZStrArg(
226+
options=["--apim-name"],
227+
help="The name of the source APIM instance.",
228+
required=True
229+
)
230+
231+
args_schema.apim_apis = AAZListArg(
232+
options=["--apim-apis"],
233+
help="The APIs to be imported.",
234+
required=True
235+
)
236+
args_schema.apim_apis.Element = AAZStrArg()
237+
209238
return args_schema
239+
240+
def pre_operations(self):
241+
super().pre_operations()
242+
args = self.ctx.args
243+
244+
# compose sourceResourceIds property in the request body
245+
# Use same subscription id and resource group as API Center by default
246+
resource_group = args.resource_group
247+
subscription_id = self.ctx.subscription_id
248+
249+
# Use user provided subscription id
250+
if args.apim_subscription_id:
251+
subscription_id = args.apim_subscription_id
252+
253+
# Use user provided resource group
254+
if args.apim_resource_group:
255+
resource_group = args.apim_resource_group
256+
257+
source_resource_ids = []
258+
for item in args.apim_apis:
259+
source_resource_ids.append(
260+
f"/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/"
261+
f"Microsoft.ApiManagement/service/{args.apim_name}/apis/{item}"
262+
)
263+
264+
args.source_resource_ids = source_resource_ids

0 commit comments

Comments
 (0)