1# --------------------------------------------------------------------------------------------
2# Copyright (c) Microsoft Corporation. All rights reserved.
3# Licensed under the MIT License. See License.txt in the project root for license information.
4# --------------------------------------------------------------------------------------------
5# pylint: disable=line-too-long
6# pylint: disable=too-many-statements
7
8from enum import Enum
9from azure.cli.core.commands.parameters import (get_enum_type,
10                                                get_location_type,
11                                                resource_group_name_type,
12                                                get_three_state_flag)
13
14from azure.mgmt.apimanagement.models import (SkuType, VirtualNetworkType, Protocol, ApiType, ProductState)
15from azure.cli.command_modules.apim.actions import (TemplateParameter)
16
17
18SKU_TYPES = SkuType
19VNET_TYPES = VirtualNetworkType
20API_PROTOCOLS = Protocol
21API_TYPES = ApiType
22
23
24class ImportFormat(Enum):
25    Wadl = "Wadl"
26    Swagger = "Swagger"
27    OpenApi = "OpenApi"
28    OpenApiJson = "OpenApiJson"
29    Wsdl = "Wsdl"
30
31
32def load_arguments(self, _):
33
34    from azure.cli.core.commands.parameters import tags_type
35    from azure.cli.core.commands.validators import get_default_location_from_resource_group
36
37    with self.argument_context('apim') as c:
38        c.argument('resource_group_name', arg_type=resource_group_name_type)
39        c.argument('tags', tags_type)
40        c.argument('service_name', options_list=['--name', '-n'], help="The name of the api management service instance", id_part=None)
41        c.argument('name', options_list=['--name', '-n'], help="The name of the api management service instance", id_part=None)
42        c.argument('location', validator=get_default_location_from_resource_group)
43
44    with self.argument_context('apim create') as c:
45        c.argument('location', arg_type=get_location_type(self.cli_ctx), validator=get_default_location_from_resource_group)
46        c.argument('publisher_name', help='The name of your organization for use in the developer portal and e-mail notifications.', required=True)
47        c.argument('publisher_email', help='The e-mail address to receive all system notifications.')
48        c.argument('enable_client_certificate', arg_type=get_three_state_flag(), help='Enforces a client certificate to be presented on each request to the gateway and also enables the ability to authenticate the certificate in the policy on the gateway.')
49        c.argument('virtual_network_type', get_enum_type(VNET_TYPES), options_list=['--virtual-network', '-v'], help='The virtual network type.')
50        c.argument('sku_name', arg_type=get_enum_type(SKU_TYPES), help='The sku of the api management instance')
51        c.argument('sku_capacity', type=int, help='The number of deployed units of the SKU.')
52        c.argument('enable_managed_identity', arg_type=get_three_state_flag(), help='Create a managed identity for the API Management service to access other Azure resources.')
53
54    with self.argument_context('apim update') as c:
55        c.argument('publisher_name', help='The name of your organization for use in the developer portal and e-mail notifications.')
56        c.argument('publisher_email', help='The e-mail address to receive all system notifications.')
57        c.argument('enable_client_certificate', arg_type=get_three_state_flag(), help='Enforces a client certificate to be presented on each request to the gateway and also enables the ability to authenticate the certificate in the policy on the gateway.')
58        c.argument('virtual_network_type', get_enum_type(VNET_TYPES), options_list=['--virtual-network', '-v'], help='The virtual network type.')
59        c.argument('sku_name', arg_type=get_enum_type(SKU_TYPES), help='The sku of the api management instance')
60        c.argument('sku_capacity', type=int, help='The number of deployed units of the SKU.')
61        c.argument('enable_managed_identity', arg_type=get_three_state_flag(), help='Create a managed identity for the API Management service to access other Azure resources.')
62
63    with self.argument_context('apim backup') as c:
64        c.argument('backup_name', help='The name of the backup file to create.')
65        c.argument('storage_account_name', arg_group='Storage', help='The name of the storage account used to place the backup.')
66        c.argument('storage_account_key', arg_group='Storage', help='The access key of the storage account used to place the backup.')
67        c.argument('storage_account_container', options_list=['--storage-account-container', '--container-name'], help='The name of the storage account container used to place the backup.')
68
69    with self.argument_context('apim restore') as c:
70        c.argument('backup_name', help='The name of the backup file to restore.')
71        c.argument('storage_account_name', arg_group='Storage', help='The name of the storage account used to retrieve the backup from.')
72        c.argument('storage_account_key', arg_group='Storage', help='The access key of the storage account used to retrieve the backup from.')
73        c.argument('storage_account_container', options_list=['--storage-account-container', '--container-name'], help='The name of the storage account container used to retrieve the backup from.')
74
75    with self.argument_context('apim api show') as c:
76        c.argument('service_name', options_list=['--service-name'], help='The name of the API Management service instance.')
77        c.argument('api_id', arg_group='API', help='API revision 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.')
78
79    with self.argument_context('apim api list') as c:
80        c.argument('service_name', options_list=['--service-name', '-n'], help='The name of the API Management service instance.')
81        c.argument('filter_display_name', arg_group='API', help='Filter of APIs by displayName.')
82        c.argument('skip', type=int, help='Number of records to skip.')
83        c.argument('top', type=int, help='Number of records to return.')
84
85    with self.argument_context('apim api create') as c:
86        c.argument('service_name', options_list=['--service-name', '-n'], help='The name of the API Management service instance.')
87        c.argument('api_id', arg_group='API', help='API revision 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.', required=True)
88        c.argument('path', arg_group='API', help='Required. Relative URL uniquely identifying this API and all of its resource paths within the API Management service instance.', required=True)
89        c.argument('display_name', arg_group='API', help='API name. Must be 1 to 300 characters long.', required=True)
90        c.argument('description', arg_group='API', help='Description of the API. May include HTML formatting tags.')
91        c.argument('subscription_key_header_name', arg_group='API', help='Specifies the subscription key header name.')
92        c.argument('subscription_key_query_param_name', arg_group='API', help='Specifies the subscription key query string parameter name.')
93        c.argument('open_id_provider_id', help='Specifies the openid in the authentication setting.')
94        c.argument('bearer_token_sending_methods', nargs='+', help='Specifies the sending methods for bearer token.')
95        c.argument('authorization_server_id', help='Specifies the OAuth authorization server ID.')
96        c.argument('authorization_scope', help='Specifies the OAuth operations scope.')
97        c.argument('service_url', arg_group='API', help='Absolute URL of the backend service implementing this API. Cannot be more than 2000 characters long.')
98        c.argument('protocols', arg_group='API', arg_type=get_enum_type(API_PROTOCOLS), nargs='+', help='Describes on which protocols the operations in this API can be invoked.')
99        c.argument('api_type', arg_group='API', arg_type=get_enum_type(API_TYPES), help='The type of the API.')
100        c.argument('subscription_required', arg_group='API', arg_type=get_three_state_flag(), help='If true, the API requires a subscription key on requests.')
101        c.argument('subscription_key_required', help='Specifies whether subscription key is required during call to this API, true - API is included into closed products only, false - API is included into open products alone, null - there is a mix of products.')
102        c.argument('tags', tags_type)
103
104    with self.argument_context('apim api delete') as c:
105        c.argument('service_name', options_list=['--service-name', '-n'], help='The name of the API Management service instance.')
106        c.argument('api_id', arg_group='API', help='API revision 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.')
107        c.argument('delete_revisions', help='Delete all revisions of the Api.')
108        c.argument('if_match', help='ETag of the Entity.')
109
110    with self.argument_context('apim api update') as c:
111        c.argument('service_name', options_list=['--service-name', '-n'], help='The name of the API Management service instance.')
112        c.argument('api_id', arg_group='API', help='API revision 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.', required=True)
113        c.argument('display_name', arg_group='API', help='API name. Must be 1 to 300 characters long.')
114        c.argument('path', arg_group='API', help='Required. Relative URL uniquely identifying this API and all of its resource paths within the API Management service instance.')
115        c.argument('description', arg_group='API', help='Description of the API. May include HTML formatting tags.')
116        c.argument('subscription_key_header_name', arg_group='API', help='Specifies the subscription key header name.')
117        c.argument('subscription_key_query_param_name', arg_group='API', help='Specifies the subscription key query string parameter name.')
118        c.argument('service_url', arg_group='API', help='Absolute URL of the backend service implementing this API. Cannot be more than 2000 characters long.')
119        c.argument('protocols', arg_group='API', arg_type=get_enum_type(API_PROTOCOLS), nargs='+', help='Describes on which protocols the operations in this API can be invoked.')
120        c.argument('api_type', arg_group='API', arg_type=get_enum_type(API_TYPES), help='The type of the API.')
121        c.argument('subscription_required', arg_group='API', arg_type=get_three_state_flag(), help='If true, the API requires a subscription key on requests.')
122        c.argument('tags', tags_type)
123
124    with self.argument_context('apim api import') as c:
125        c.argument('service_name', options_list=['--service-name', '-n'], help="The name of the api management service instance", id_part=None)
126        c.argument('path', arg_group='API', help='Required. Relative URL uniquely identifying this API and all of its resource paths within the API Management service instance.', required=True)
127        c.argument('specification_format', arg_type=get_enum_type(ImportFormat), help='Specify the format of the imported API.')
128        c.argument('description', arg_group='API', help='Description of the API. May include HTML formatting tags.')
129        c.argument('subscription_key_header_name', arg_group='API', help='Specifies the subscription key header name.')
130        c.argument('subscription_key_query_param_name', arg_group='API', help='Specifies the subscription key query string parameter name.')
131        c.argument('api_id', arg_group='API', help='API revision 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.')
132        c.argument('api_revision', arg_group='API', help='Describes the Revision of the Api. If no value is provided, default revision 1 is created.')
133        c.argument('api_version', arg_group='API', help='Describes the Version of the Api. If you add a version to a non-versioned API, an Original version will be automatically created and will respond on the default URL')
134        c.argument('api_version_set_id', arg_group='API', help='Describes the Version Set to be used with the API')
135        c.argument('service_url', arg_group='API', help='Absolute URL of the backend service implementing this API. Cannot be more than 2000 characters long.')
136        c.argument('protocols', arg_group='API', arg_type=get_enum_type(API_PROTOCOLS), nargs='+', help='Describes on which protocols(one or more) the operations in this API can be invoked.')
137        c.argument('api_type', arg_group='API', arg_type=get_enum_type(API_TYPES), help='The type of the API.')
138        c.argument('subscription_required', arg_group='API', arg_type=get_three_state_flag(), help='If true, the API requires a subscription key on requests.')
139        c.argument('display_name', arg_group='API', help='Display name of this API.')
140        c.argument('specification_path', help='File path specified to import the API.')
141        c.argument('specification_url', help='Url specified to import the API.')
142        c.argument('soap_api_type', help='The type of API when file format is WSDL.')
143        c.argument('wsdl_service_name', help='Local name of WSDL Service to be imported.')
144        c.argument('wsdl_endpoint_name', help='Local name of WSDL Endpoint (port) to be imported.')
145
146    with self.argument_context('apim product api list') as c:
147        c.argument('service_name', options_list=['--service-name', '-n'], help="The name of the api management service instance", id_part=None)
148        c.argument('product_id', help="Product identifier. Must be unique in the current API Management service instance.")
149
150    with self.argument_context('apim product api check') as c:
151        c.argument('service_name', options_list=['--service-name', '-n'], help="The name of the api management service instance", id_part=None)
152        c.argument('product_id', help="Product identifier. Must be unique in the current API Management service instance.")
153        c.argument('api_id', arg_group='API', help='API revision 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.')
154
155    with self.argument_context('apim product api add') as c:
156        c.argument('service_name', options_list=['--service-name', '-n'], help="The name of the api management service instance", id_part=None)
157        c.argument('product_id', help="Product identifier. Must be unique in the current API Management service instance.")
158        c.argument('api_id', arg_group='API', help='API revision 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.')
159
160    with self.argument_context('apim product api delete') as c:
161        c.argument('service_name', options_list=['--service-name', '-n'], help="The name of the api management service instance", id_part=None)
162        c.argument('product_id', help="Product identifier. Must be unique in the current API Management service instance.")
163        c.argument('api_id', arg_group='API', help='API revision 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.')
164
165    with self.argument_context('apim product') as c:
166        c.argument('service_name', options_list=['--service-name', '-n'], help="The name of the api management service instance", id_part=None)
167        c.argument('product_id', help="Product identifier. Must be unique in the current API Management service instance.")
168        c.argument('product_name', help="Product name.")
169        c.argument('description', help="Product description. May include HTML formatting tags.")
170        c.argument('legal_terms', help="Product terms of use. Developers trying to subscribe to the product will be presented and required to accept these terms before they can complete the subscription process.")
171        c.argument('subscription_required', options_list=['--subscription-required', '-s'], help="Whether a product subscription is required for accessing APIs included in this product.")
172        c.argument('approval_required', help="whether subscription approval is required. If false, new subscriptions will be approved automatically enabling developers to call the product’s APIs immediately after subscribing. If true, administrators must manually approve the subscription before the developer can any of the product’s APIs. Can be present only if subscriptionRequired property is present and has a value of false.")
173        c.argument('subscriptions_limit', help="Whether the number of subscriptions a user can have to this product at the same time. Set to null or omit to allow unlimited per user subscriptions. Can be present only if subscriptionRequired property is present and has a value of false.")
174        c.argument('state', arg_type=get_enum_type(ProductState), help="whether product is published or not. Published products are discoverable by users of developer portal. Non published products are visible only to administrators. Default state of Product is notPublished. Possible values include: 'notPublished', 'published'")
175        c.argument('delete_subscriptions', help="Delete existing subscriptions associated with the product or not.")
176        c.argument('if_match', help='ETag of the Entity.')
177
178    with self.argument_context('apim nv') as c:
179        c.argument('service_name', options_list=['--service-name', '-n'], help='The name of the API Management service instance.')
180        c.argument('named_value_id', help='Identifier of the NamedValue.')
181        c.argument('display_name', help='Required. Unique name of NamedValue. It may contain only letters, digits, period, dash, and underscore characters.')
182        c.argument('value', help='Required. Value of the NamedValue. Can contain policy expressions. It may not be empty or consist only of whitespace. This property will not be filled on GET operations! Use /listSecrets POST request to get the value.')
183        c.argument('secret', arg_type=get_three_state_flag(), help='Determines whether the value is a secret and should be encrypted or not. Default value is false.')
184        c.argument('tags', tags_type)
185
186    with self.argument_context('apim api operation') as c:
187        c.argument('service_name', options_list=['--service-name', '-n'], help="The name of the API Management service instance.")
188        c.argument('api_id', 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.')
189        c.argument('operation_id', help='Operation identifier within an API. Must be unique in the current API Management service instance.')
190        c.argument('description', help="Description of the operation. May include HTML formatting tags.")
191        c.argument('template_parameters', options_list=['--template-parameters', '--params', '-p'], action=TemplateParameter, nargs='+', help="Collection of URL template parameters.")
192        c.argument('method', help="Required. A Valid HTTP Operation Method. Typical Http Methods like GET, PUT, POST but not limited by only them.")
193        c.argument('display_name', help="Required. Operation Name.")
194        c.argument('if_match', help='ETag of the Entity.')
195        c.argument('url_template', help='Relative URL template identifying the target resource for this operation. May include parameters.')
196
197    with self.argument_context('apim api release') as c:
198        c.argument('service_name', options_list=['--service-name', '-n'], help="The name of the API Management service instance.")
199        c.argument('api_id', 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.')
200        c.argument('release_id', help="Release identifier within an API. Must be unique in the current API Management service instance.")
201        c.argument('api_revision', help="API revision number.")
202        c.argument('notes', help="Release Notes.")
203        c.argument('if_match', help='ETag of the Entity.')
204
205    with self.argument_context('apim api revision') as c:
206        c.argument('service_name', options_list=['--service-name', '-n'], help="The name of the API Management service instance.")
207        c.argument('api_id', 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.')
208        c.argument('api_revision', help='Describes the Revision of the Api.')
209        c.argument('api_revision_description', options_list=['--api-revision-description', '--rev-description'], help='Description of the Api Revision.')
210
211    with self.argument_context('apim api versionset') as c:
212        c.argument('version_set_id', help="A resource identifier for the related ApiVersionSet.")
213        c.argument('versioning_scheme', help="Required. An value that determines where the API Version identifer will be located in a HTTP request. Possible values include: 'Segment', 'Query', 'Header'")
214        c.argument('display_name', help="Required. Name of API Version Set")
215        c.argument('service_name', options_list=['--service-name', '-n'], help="The name of the API Management service instance.")
216        c.argument('description', help="Description of API Version Set.")
217        c.argument('version_query_name', help="Name of query parameter that indicates the API Version if versioningScheme is set to `query`.")
218        c.argument('version_header_name', help="Name of HTTP header parameter that indicates the API Version if versioningScheme is set to `header`.")
219        c.argument('if_match', help='ETag of the Entity.')
220