Skip to content

Commit 2e8b347

Browse files
[API Management] az apim: Add new subcommand backend (#32569)
1 parent 7fbddc0 commit 2e8b347

File tree

9 files changed

+6969
-6729
lines changed

9 files changed

+6969
-6729
lines changed

src/azure-cli/azure/cli/command_modules/apim/_client_factory.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@ def cf_graphqlapiresolver(cli_ctx, *_):
5656

5757
def cf_graphqlapiresolverpolicy(cli_ctx, *_):
5858
return cf_apim(cli_ctx).graph_ql_api_resolver_policy
59+
60+
61+
def cf_backend(cli_ctx, *_):
62+
return cf_apim(cli_ctx).backend

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@
8888
short-summary: Manage soft-deleted Azure API Management services.
8989
"""
9090

91+
helps['apim backend'] = """
92+
type: group
93+
short-summary: Manage Azure API Management Backends.
94+
"""
95+
9196
helps['apim backup'] = """
9297
type: command
9398
short-summary: Creates a backup of the API Management service to the given Azure Storage Account. This is long running operation and could take several minutes to complete.
@@ -749,3 +754,66 @@
749754
text: |
750755
az apim graphql resolver policy list --service-name MyApim -g MyResourceGroup --api-id MyApi --resolver-id MyResolverId
751756
"""
757+
758+
helps['apim backend list'] = """
759+
type: command
760+
short-summary: List API Management Backends.
761+
examples:
762+
- name: List all Backends in an APIM instance.
763+
text: |-
764+
az apim backend list --resource-group MyResourceGroup --service-name MyServiceName
765+
"""
766+
767+
helps['apim backend show'] = """
768+
type: command
769+
short-summary: Show details of an API Management Backend.
770+
examples:
771+
- name: Show details of a Backend in an APIM instance.
772+
text: |-
773+
az apim backend show --resource-group MyResourceGroup --service-name MyServiceName --backend-id MyBackendId
774+
"""
775+
776+
helps['apim backend delete'] = """
777+
type: command
778+
short-summary: Delete an API Management Backend.
779+
examples:
780+
- name: Delete a Backend in an APIM instance.
781+
text: |-
782+
az apim backend delete --resource-group MyResourceGroup --service-name MyServiceName --backend-id MyBackendId
783+
"""
784+
785+
helps['apim backend create'] = """
786+
type: command
787+
short-summary: Create or Update an API Management Backend.
788+
parameters:
789+
- name: --backend-id
790+
type: string
791+
short-summary: unique name for the Backend to be created or updated
792+
long-summary: |
793+
Must be unique in the current API Management service instance.
794+
- name: --url
795+
type: string
796+
short-summary: The URL of the backend service.
797+
- name: --protocol
798+
type: string
799+
short-summary: The protocol used to communicate with the backend service.
800+
examples:
801+
- name: Create a Backend.
802+
text: |-
803+
az apim backend create --service-name MyApim -g MyResourceGroup --backend-id MyBackendId --url https://mybackend.com --protocol http
804+
"""
805+
806+
helps['apim backend update'] = """
807+
type: command
808+
short-summary: Update an API Management Backend.
809+
parameters:
810+
- name: --backend-id
811+
type: string
812+
short-summary: unique name of the Backend to be updated
813+
long-summary: |
814+
Must be unique in the current API Management service instance.
815+
examples:
816+
- name: Update a Backend.
817+
text: |-
818+
az apim backend update --service-name MyApim -g MyResourceGroup --backend-id MyBackendId --url https://mynewbackend.com
819+
"""

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
resource_group_name_type,
1313
get_three_state_flag)
1414

15-
from azure.mgmt.apimanagement.models import (SkuType, VirtualNetworkType, Protocol, ApiType, ProductState)
15+
from azure.mgmt.apimanagement.models import (SkuType, VirtualNetworkType, Protocol, ApiType, ProductState, BackendProtocol)
1616
from azure.cli.command_modules.apim.actions import (TemplateParameter)
1717

1818

1919
SKU_TYPES = SkuType
2020
VNET_TYPES = VirtualNetworkType
2121
API_PROTOCOLS = Protocol
2222
API_TYPES = ApiType
23+
BACKEND_PROTOCOLS = BackendProtocol
2324

2425

2526
class ImportFormat(Enum):
@@ -51,6 +52,8 @@ def load_arguments(self, _):
5152
help='API identifier. Must be unique in the current API Management service instance. Non-current revision has ;rev=n as a suffix where n is the revision number.')
5253
schema_id = CLIArgumentType(arg_group='Schema',
5354
help='Schema identifier. Must be unique in the current API Management service instance. Non-current revision has ;rev=n as a suffix where n is the revision number.')
55+
backend_id = CLIArgumentType(arg_group='Backend',
56+
help='Backend identifier. Must be unique in the current API Management service instance.')
5457

5558
from azure.cli.core.commands.parameters import tags_type
5659
from azure.cli.core.commands.validators import get_default_location_from_resource_group
@@ -502,3 +505,37 @@ def load_arguments(self, _):
502505
c.argument('api_id', arg_type=api_id)
503506
c.argument('resolver_id', help='Resolver identifier within a GraphQL API. Must be unique in the current API Management service instance.')
504507
c.argument('if_match', help='ETag of the Entity.')
508+
509+
with self.argument_context('apim backend create') as c:
510+
c.argument('service_name', options_list=['--service-name', '-n'],
511+
help='The name of the API Management service instance.')
512+
c.argument('backend_id', arg_type=backend_id, help='Identifier of the Backend.')
513+
c.argument('url', help='Backend service URL.', required=True)
514+
c.argument('protocol', arg_type=get_enum_type(BACKEND_PROTOCOLS),
515+
help='Protocol used to communicate with the backend service. Possible values include: `http`, `soap`.')
516+
c.argument('description', help='Description of the Backend. May include HTML formatting tags.')
517+
c.argument('if_match', help='ETag of the Entity.')
518+
519+
with self.argument_context('apim backend update') as c:
520+
c.argument('service_name', options_list=['--service-name', '-n'],
521+
help='The name of the API Management service instance.')
522+
c.argument('backend_id', arg_type=backend_id, help='Identifier of the Backend.')
523+
c.argument('url', help='Backend service URL.')
524+
c.argument('protocol', arg_type=get_enum_type(BACKEND_PROTOCOLS),
525+
help='Protocol used to communicate with the backend service. Possible values include: `http`, `soap`.')
526+
c.argument('description', help='Description of the Backend. May include HTML formatting tags.')
527+
528+
with self.argument_context('apim backend delete') as c:
529+
c.argument('service_name', options_list=['--service-name', '-n'],
530+
help='The name of the API Management service instance.')
531+
c.argument('backend_id', arg_type=backend_id, help='Identifier of the Backend.')
532+
c.argument('if_match', help='ETag of the Entity.')
533+
534+
with self.argument_context('apim backend show') as c:
535+
c.argument('service_name', options_list=['--service-name', '-n'],
536+
help='The name of the API Management service instance.')
537+
c.argument('backend_id', arg_type=backend_id, help='Identifier of the Backend.')
538+
539+
with self.argument_context('apim backend list') as c:
540+
c.argument('service_name', options_list=['--service-name', '-n'],
541+
help='The name of the API Management service instance.')

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from azure.cli.command_modules.apim._client_factory import (cf_service, cf_api, cf_product, cf_nv, cf_apiops,
1212
cf_apirelease, cf_apirevision, cf_apiversionset,
1313
cf_apischema, cf_ds, cf_graphqlapiresolver,
14-
cf_graphqlapiresolverpolicy)
14+
cf_graphqlapiresolverpolicy, cf_backend)
1515

1616

1717
def load_command_table(self, _):
@@ -75,6 +75,11 @@ def load_command_table(self, _):
7575
client_factory=cf_graphqlapiresolverpolicy
7676
)
7777

78+
backend_sdk = CliCommandType(
79+
operations_tmpl='azure.mgmt.apimanagement.operations#BackendOperations.{}',
80+
client_factory=cf_backend
81+
)
82+
7883
# pylint: disable=line-too-long
7984
with self.command_group('apim', service_sdk) as g:
8085
g.custom_command('create', 'apim_create', supports_no_wait=True,
@@ -178,3 +183,10 @@ def load_command_table(self, _):
178183
g.custom_command('delete', 'apim_graphql_resolver_policy_delete', confirmation=True)
179184
g.custom_show_command('show', 'apim_graphql_resolver_policy_show')
180185
g.custom_command('list', 'apim_graphql_resolver_policy_list')
186+
187+
with self.command_group('apim backend', backend_sdk) as g:
188+
g.custom_command('create', 'apim_backend_create')
189+
g.custom_show_command('show', 'apim_backend_show')
190+
g.custom_command('list', 'apim_backend_list')
191+
g.custom_command('delete', 'apim_backend_delete', confirmation=True)
192+
g.generic_update_command('update', custom_func_name='apim_backend_update')

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

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
OpenIdAuthenticationSettingsContract, ProductContract, ProductState,
3939
NamedValueCreateContract, VersioningScheme, ApiVersionSetContract,
4040
OperationContract, ApiManagementServiceCheckNameAvailabilityParameters,
41-
ApiReleaseContract, SchemaContract, ResolverContract, PolicyContract)
41+
ApiReleaseContract, SchemaContract, ResolverContract, PolicyContract,
42+
BackendContract)
4243

4344
logger = get_logger(__name__)
4445

@@ -1165,3 +1166,62 @@ def apim_graphql_resolver_policy_delete(
11651166
resolver_id=resolver_id,
11661167
policy_id="policy",
11671168
if_match="*" if if_match is None else if_match)
1169+
1170+
1171+
def apim_backend_create(
1172+
client, resource_group_name, service_name, backend_id, url, protocol, description=None,
1173+
no_wait=False, if_match=None):
1174+
1175+
parameters = BackendContract(
1176+
url=url,
1177+
protocol=protocol,
1178+
description=description
1179+
)
1180+
1181+
return sdk_no_wait(no_wait, client.backend.create_or_update,
1182+
resource_group_name=resource_group_name,
1183+
service_name=service_name,
1184+
backend_id=backend_id,
1185+
parameters=parameters,
1186+
if_match="*" if if_match is None else if_match)
1187+
1188+
1189+
def apim_backend_delete(
1190+
client, resource_group_name, service_name, backend_id, if_match=None, no_wait=False):
1191+
1192+
return sdk_no_wait(no_wait,
1193+
client.backend.delete,
1194+
resource_group_name=resource_group_name,
1195+
service_name=service_name,
1196+
backend_id=backend_id,
1197+
if_match="*" if if_match is None else if_match)
1198+
1199+
1200+
def apim_backend_show(client, resource_group_name, service_name, backend_id):
1201+
1202+
return client.backend.get(
1203+
resource_group_name=resource_group_name,
1204+
service_name=service_name,
1205+
backend_id=backend_id)
1206+
1207+
1208+
def apim_backend_list(client, resource_group_name, service_name):
1209+
1210+
return client.backend.list_by_service(
1211+
resource_group_name=resource_group_name,
1212+
service_name=service_name)
1213+
1214+
1215+
def apim_backend_update(
1216+
instance, url=None, protocol=None, description=None):
1217+
1218+
if url is not None:
1219+
instance.url = url
1220+
1221+
if protocol is not None:
1222+
instance.protocol = protocol
1223+
1224+
if description is not None:
1225+
instance.description = description
1226+
1227+
return instance

0 commit comments

Comments
 (0)