Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Module [API Management] #322

Merged
merged 21 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
670 changes: 670 additions & 0 deletions plugins/modules/azure_rm_apimanagement.py

Large diffs are not rendered by default.

275 changes: 275 additions & 0 deletions plugins/modules/azure_rm_apimanagement_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
#!/usr/bin/python
#
# Copyright (c) 2020 Sakar Mehra (@sakar97), Nikhil Patne (@nikhilpatne)
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type


DOCUMENTATION = '''
---
module: azure_rm_apimanagement_info
version_added: "1.6.0"
short_description: Get the infomation of the API Instance
description:
- Get the information of api instance.
options:
resource_group:
description:
- The name of the resource group.
required: true
type: str
service_name:
description:
- The name of the API Management service.
required: true
type: str
expand_api_version_set:
description:
- Include full ApiVersionSet resource in response
type: bool
include_not_tagged_apis:
description:
- Included not tagged APIs in the response.
type: bool
api_id:
description:
- API revision identifier. It must be unique in the current API Management service instance.
type: str
extends_documentation_fragment:
- azure.azcollection.azure
- azure.azcollection.azure_tags
author:
- Sakar Mehra (@sakar97)
- Nikhil Patne (@nikhilpatne)

'''

EXAMPLES = '''
- name: Get the information of api
azure_rm_apimanagement_info:
resource_group: myResourceGroup
service_name: myService
- name: Get the information of api
azure_rm_apimanagement_info:
resource_group: myResourceGroup
service_name: myService
api_id: testApi
'''

RETURN = '''
api:
description:
- A list of dict results where the key is the name of the Api and the values are the facts for that Api.
returned: always
type: complex
contains:
api_name:
description:
- The api name provided by the user.
type: complex
contains:
id:
description:
- Resource ID.
returned: always
type: str
sample: null
name:
description:
- Resource name.
returned: always
type: str
sample: null
type:
description:
- Resource type for API Management resource.
returned: always
type: str
sample: null
properties:
description:
- Api entity contract properties.
returned: always
type: dict
sample: null

'''

from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
from copy import deepcopy
import time
import json
import re
try:
from msrestazure.azure_exceptions import CloudError
except ImportError:
# This is handled in azure_rm_common
pass

sakar97 marked this conversation as resolved.
Show resolved Hide resolved

class AzureApiManagementInfo(AzureRMModuleBaseExt):
def __init__(self):
self.module_arg_spec = dict(
resource_group=dict(
type='str',
required=True
),
service_name=dict(
type='str',
required=True
),
expand_api_version_set=dict(
type='bool'
),
include_not_tagged_apis=dict(
type='bool'
),
api_id=dict(
type='str'
)
)

self.resource_group = None
self.service_name = None
self.tags = None
self.expand_api_version_set = None
self.include_not_tagged_apis = None
self.api_id = None

self.results = dict(changed=False)
self.mgmt_client = None
self.state = None
self.url = None
self.status_code = [200]

self.query_parameters = {}
self.query_parameters['api-version'] = '2020-06-01-preview'
self.header_parameters = {}
self.header_parameters['Content-Type'] = 'application/json; charset=utf-8'

super(AzureApiManagementInfo, self).__init__(derived_arg_spec=self.module_arg_spec,
supports_check_mode=True,
supports_tags=True)

def exec_module(self, **kwargs):
for key in list(self.module_arg_spec.keys()):
if hasattr(self, key):
setattr(self, key, kwargs[key])
elif kwargs[key] is not None:
self.body[key] = kwargs[key]

self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient,
base_url=self._cloud_environment.endpoints.resource_manager)

if (self.resource_group is not None and
self.service_name is not None and
self.api_id is not None):
self.results['api'] = self.get_api_info()
elif (self.resource_group is not None and
self.service_name is not None):
self.results['api'] = self.listbytags()
elif (self.resource_group is not None and
self.service_name is not None):
self.results['api'] = self.listbyservice()
return self.results

def get_url(self):
return '/subscriptions' + '/' + self.subscription_id \
+ '/resourceGroups' + '/' + self.resource_group \
+ '/providers' + '/Microsoft.ApiManagement' + '/service' \
+ '/' + self.service_name + '/apis' + '/' + self.api_id

def get_url_bytags(self):
return '/subscriptions' + '/' + self.subscription_id \
+ '/resourceGroups' + '/' + self.resource_group \
+ '/providers' + '/Microsoft.ApiManagement' + '/service' \
+ '/' + self.service_name + '/apisByTags'

def get_url_byservice(self):
return '/subscriptions' + '/' + self.subscription_id \
+ '/resourceGroups' + '/' + self.resource_group \
+ '/providers' + '/Microsoft.ApiManagement' + '/service' \
+ '/' + self.service_name + '/apis'

def get_api_info(self):
self.url = self.get_url()
response = None

try:
response = self.mgmt_client.query(
self.url,
'GET',
self.query_parameters,
self.header_parameters,
None,
self.status_code,
600,
30,
)
except CloudError as e:
self.log('Could not get the information.{0}'.format(e))
try:
response = json.loads(response.text)
except Exception:
return None

return response

def listbytags(self):
self.url = self.get_url_bytags()
response = None
try:
response = self.mgmt_client.query(
self.url,
'GET',
self.query_parameters,
self.header_parameters,
None,
self.status_code,
600,
30,
)
except CloudError as e:
self.log('Could not get info for the given api tags {0}'.format(e))
try:
response = json.loads(response.text)
except Exception:
return None

return response

def listbyservice(self):
self.url = self.get_url_byservice()
response = None
try:
response = self.mgmt_client.query(
self.url,
'GET',
self.query_parameters,
self.header_parameters,
None,
self.status_code,
600,
30,
)
response = json.loads(response.text)
except CloudError as e:
self.log('Could not get info for a given services.{0}'.format(e))
try:
response = json.loads(response.text)
except Exception:
return None

return response

sakar97 marked this conversation as resolved.
Show resolved Hide resolved

def main():
AzureApiManagementInfo()


if __name__ == '__main__':
main()
4 changes: 4 additions & 0 deletions pr-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ parameters:
- "azure_rm_adgroup"
- "azure_rm_aduser"
- "azure_rm_aks"
- "azure_rm_apimanagement"
- "azure_rm_appgateway"
- "azure_rm_appserviceplan"
- "azure_rm_automationaccount"
Expand Down Expand Up @@ -179,6 +180,9 @@ jobs:
"Python${{ parameters.PYTHON_VER }}_14":
python.version: '${{ parameters.PYTHON_VER }}'
test.key: '14'
"Python${{ parameters.PYTHON_VER }}_15":
python.version: '${{ parameters.PYTHON_VER }}'
test.key: '15'

steps:
- task: UsePythonVersion@0
Expand Down
1 change: 1 addition & 0 deletions requirements-azure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ xmltodict
azure-cli-core==2.11.1
azure-cli-nspkg==3.0.2
azure-common==1.1.11
azure-mgmt-apimanagement==0.2.0
azure-mgmt-authorization==0.51.1
azure-mgmt-batch==5.0.1
azure-mgmt-cdn==3.0.0
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/targets/azure_rm_apimanagement/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cloud/azure
shippable/azure/group15
destructive
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dependencies:
- setup_azure
Loading