Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_apimanagement_info.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2020 Sakar Mehra (@sakar97), Nikhil Patne (@nikhilpatne)
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10
11 DOCUMENTATION = '''
12 ---
13 module: azure_rm_apimanagement_info
14 version_added: "1.6.0"
15 short_description: Get the infomation of the API Instance
16 description:
17 - Get the information of api instance.
18 options:
19 resource_group:
20 description:
21 - The name of the resource group.
22 required: true
23 type: str
24 service_name:
25 description:
26 - The name of the API Management service.
27 required: true
28 type: str
29 expand_api_version_set:
30 description:
31 - Include full ApiVersionSet resource in response
32 type: bool
33 include_not_tagged_apis:
34 description:
35 - Included not tagged APIs in the response.
36 type: bool
37 api_id:
38 description:
39 - API revision identifier. It must be unique in the current API Management service instance.
40 type: str
41 extends_documentation_fragment:
42 - azure.azcollection.azure
43 - azure.azcollection.azure_tags
44 author:
45 - Sakar Mehra (@sakar97)
46 - Nikhil Patne (@nikhilpatne)
47
48 '''
49
50 EXAMPLES = '''
51 - name: Get the information of api
52 azure_rm_apimanagement_info:
53 resource_group: myResourceGroup
54 service_name: myService
55 - name: Get the information of api
56 azure_rm_apimanagement_info:
57 resource_group: myResourceGroup
58 service_name: myService
59 api_id: testApi
60 '''
61
62 RETURN = '''
63 api:
64 description:
65 - A list of dict results where the key is the name of the Api and the values are the facts for that Api.
66 returned: always
67 type: complex
68 contains:
69 api_name:
70 description:
71 - The api name provided by the user.
72 type: complex
73 contains:
74 id:
75 description:
76 - Resource ID.
77 returned: always
78 type: str
79 sample: null
80 name:
81 description:
82 - Resource name.
83 returned: always
84 type: str
85 sample: null
86 type:
87 description:
88 - Resource type for API Management resource.
89 returned: always
90 type: str
91 sample: null
92 properties:
93 description:
94 - Api entity contract properties.
95 returned: always
96 type: dict
97 sample: null
98
99 '''
100
101 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient
102 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
103 import json
104
105
106 class AzureApiManagementInfo(AzureRMModuleBaseExt):
107 def __init__(self):
108 self.module_arg_spec = dict(
109 resource_group=dict(
110 type='str',
111 required=True
112 ),
113 service_name=dict(
114 type='str',
115 required=True
116 ),
117 expand_api_version_set=dict(
118 type='bool'
119 ),
120 include_not_tagged_apis=dict(
121 type='bool'
122 ),
123 api_id=dict(
124 type='str'
125 )
126 )
127
128 self.resource_group = None
129 self.service_name = None
130 self.tags = None
131 self.expand_api_version_set = None
132 self.include_not_tagged_apis = None
133 self.api_id = None
134
135 self.results = dict(changed=False)
136 self.mgmt_client = None
137 self.state = None
138 self.url = None
139 self.status_code = [200]
140
141 self.query_parameters = {}
142 self.query_parameters['api-version'] = '2022-08-01'
143 self.header_parameters = {}
144 self.header_parameters['Content-Type'] = 'application/json; charset=utf-8'
145
146 super(AzureApiManagementInfo, self).__init__(derived_arg_spec=self.module_arg_spec,
147 supports_check_mode=True,
148 supports_tags=True)
149
150 def exec_module(self, **kwargs):
151 for key in list(self.module_arg_spec.keys()):
152 if hasattr(self, key):
153 setattr(self, key, kwargs[key])
154 elif kwargs[key] is not None:
155 self.body[key] = kwargs[key]
156
157 self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient,
158 base_url=self._cloud_environment.endpoints.resource_manager)
159
160 if (self.resource_group is not None and
161 self.service_name is not None and
162 self.api_id is not None):
163 self.results['api'] = self.get_api_info()
164 elif (self.resource_group is not None and
165 self.service_name is not None):
166 self.results['api'] = self.listbytags()
167 elif (self.resource_group is not None and
168 self.service_name is not None):
169 self.results['api'] = self.listbyservice()
170 return self.results
171
172 def get_url(self):
173 return '/subscriptions' + '/' + self.subscription_id \
174 + '/resourceGroups' + '/' + self.resource_group \
175 + '/providers' + '/Microsoft.ApiManagement' + '/service' \
176 + '/' + self.service_name + '/apis' + '/' + self.api_id
177
178 def get_url_bytags(self):
179 return '/subscriptions' + '/' + self.subscription_id \
180 + '/resourceGroups' + '/' + self.resource_group \
181 + '/providers' + '/Microsoft.ApiManagement' + '/service' \
182 + '/' + self.service_name + '/apisByTags'
183
184 def get_url_byservice(self):
185 return '/subscriptions' + '/' + self.subscription_id \
186 + '/resourceGroups' + '/' + self.resource_group \
187 + '/providers' + '/Microsoft.ApiManagement' + '/service' \
188 + '/' + self.service_name + '/apis'
189
190 def get_api_info(self):
191 self.url = self.get_url()
192 response = None
193
194 try:
195 response = self.mgmt_client.query(
196 self.url,
197 'GET',
198 self.query_parameters,
199 self.header_parameters,
200 None,
201 self.status_code,
202 600,
203 30,
204 )
205 except Exception as e:
206 self.log('Could not get the information.{0}'.format(e))
207 try:
208 response = json.loads(response.body())
209 except Exception:
210 return None
211
212 return response
213
214 def listbytags(self):
215 self.url = self.get_url_bytags()
216 response = None
217 try:
218 response = self.mgmt_client.query(
219 self.url,
220 'GET',
221 self.query_parameters,
222 self.header_parameters,
223 None,
224 self.status_code,
225 600,
226 30,
227 )
228 except Exception as e:
229 self.log('Could not get info for the given api tags {0}'.format(e))
230 try:
231 response = json.loads(response.body())
232 except Exception:
233 return None
234
235 return response
236
237 def listbyservice(self):
238 self.url = self.get_url_byservice()
239 response = None
240 try:
241 response = self.mgmt_client.query(
242 self.url,
243 'GET',
244 self.query_parameters,
245 self.header_parameters,
246 None,
247 self.status_code,
248 600,
249 30,
250 )
251 response = json.loads(response.body())
252 except Exception as e:
253 self.log('Could not get info for a given services.{0}'.format(e))
254 try:
255 response = json.loads(response.text)
256 except Exception:
257 return None
258
259 return response
260
261
262 def main():
263 AzureApiManagementInfo()
264
265
266 if __name__ == '__main__':
267 main()