| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_apimanagementservice_info |
| 14 |
version_added: '1.5.0' |
| 15 |
short_description: Get ApiManagementService info |
| 16 |
description: |
| 17 |
- Get info of ApiManagementService. |
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- The name of the resource group. |
| 22 |
type: str |
| 23 |
name: |
| 24 |
description: |
| 25 |
- Resource name. |
| 26 |
type: str |
| 27 |
|
| 28 |
extends_documentation_fragment: |
| 29 |
- azure.azcollection.azure |
| 30 |
author: |
| 31 |
- Nikhil Patne (@nikhilpatne) |
| 32 |
- Sakar Mehra (@Sakar97) |
| 33 |
|
| 34 |
''' |
| 35 |
|
| 36 |
EXAMPLES = ''' |
| 37 |
- name: Get Api Management Service By Name and Resource Group |
| 38 |
azure_rm_apimanagementservice_info: |
| 39 |
resource_group: myResourceGroup |
| 40 |
name: myApiName |
| 41 |
|
| 42 |
- name: Get Api Management Service By Resource Group |
| 43 |
azure_rm_apimanagementservice_info: |
| 44 |
resource_group: myResourceGroup |
| 45 |
|
| 46 |
- name: Get Api Management Service By Subscription |
| 47 |
azure_rm_apimanagementservice_info: |
| 48 |
''' |
| 49 |
|
| 50 |
RETURN = ''' |
| 51 |
api_management_service: |
| 52 |
description: |
| 53 |
- A list of dict results where the key is the name of the ApiManagementService. |
| 54 |
- The values are the facts for that ApiManagementService. |
| 55 |
returned: always |
| 56 |
type: complex |
| 57 |
contains: |
| 58 |
id: |
| 59 |
description: |
| 60 |
- Resource ID. |
| 61 |
returned: always |
| 62 |
type: str |
| 63 |
sample: /subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.ApiManagement/service/myPolicy |
| 64 |
name: |
| 65 |
description: |
| 66 |
- Resource name. |
| 67 |
returned: always |
| 68 |
type: str |
| 69 |
sample: myPolicy |
| 70 |
type: |
| 71 |
description: |
| 72 |
- Resource type for API Management resource is set to Microsoft.ApiManagement. |
| 73 |
returned: always |
| 74 |
type: str |
| 75 |
sample: Microsoft.ApiManagement/service |
| 76 |
tags: |
| 77 |
description: |
| 78 |
- Resource tags. |
| 79 |
returned: always |
| 80 |
type: dict |
| 81 |
sample: {'key1':'value1'} |
| 82 |
properties: |
| 83 |
description: |
| 84 |
- Properties of the API Management service. |
| 85 |
returned: always |
| 86 |
type: dict |
| 87 |
sample: null |
| 88 |
location: |
| 89 |
description: |
| 90 |
- Resource location. |
| 91 |
type: str |
| 92 |
returned: always |
| 93 |
sample: 'East US' |
| 94 |
sku: |
| 95 |
description: |
| 96 |
- SKU properties of the API Management service. |
| 97 |
returned: always |
| 98 |
type: str |
| 99 |
sample: Developer |
| 100 |
etag: |
| 101 |
description: |
| 102 |
- ETag of the resource. |
| 103 |
returned: always |
| 104 |
type: str |
| 105 |
sample: AAAAAAAsQK8= |
| 106 |
zones: |
| 107 |
description: |
| 108 |
- Zone of the resource. |
| 109 |
type: str |
| 110 |
returned: always |
| 111 |
sample: null |
| 112 |
identity: |
| 113 |
description: |
| 114 |
- Identity of the resource. |
| 115 |
type: str |
| 116 |
returned: always |
| 117 |
sample: null |
| 118 |
''' |
| 119 |
|
| 120 |
import json |
| 121 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient |
| 122 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 123 |
|
| 124 |
|
| 125 |
class AzureRMApiManagementServiceInfo(AzureRMModuleBaseExt): |
| 126 |
def __init__(self): |
| 127 |
self.module_arg_spec = dict( |
| 128 |
resource_group=dict( |
| 129 |
type='str' |
| 130 |
), |
| 131 |
name=dict( |
| 132 |
type='str' |
| 133 |
) |
| 134 |
) |
| 135 |
|
| 136 |
self.resource_group = None |
| 137 |
self.name = None |
| 138 |
|
| 139 |
self.results = dict(changed=False) |
| 140 |
self.mgmt_client = None |
| 141 |
self.state = None |
| 142 |
self.url = None |
| 143 |
self.status_code = [200] |
| 144 |
|
| 145 |
self.query_parameters = {} |
| 146 |
self.query_parameters['api-version'] = '2022-08-01' |
| 147 |
self.header_parameters = {} |
| 148 |
self.header_parameters['Content-Type'] = 'application/json; charset=utf-8' |
| 149 |
|
| 150 |
self.mgmt_client = None |
| 151 |
super(AzureRMApiManagementServiceInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False) |
| 152 |
|
| 153 |
def exec_module(self, **kwargs): |
| 154 |
|
| 155 |
for key in self.module_arg_spec: |
| 156 |
setattr(self, key, kwargs[key]) |
| 157 |
|
| 158 |
self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient, |
| 159 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 160 |
|
| 161 |
if (self.resource_group is not None and self.name is not None): |
| 162 |
self.results['api_management_service'] = self.get() |
| 163 |
elif (self.resource_group is not None): |
| 164 |
self.results['api_management_service'] = self.listbyresourcegroup() |
| 165 |
else: |
| 166 |
self.results['api_management_service'] = self.list() |
| 167 |
return self.results |
| 168 |
|
| 169 |
def get(self): |
| 170 |
response = None |
| 171 |
results = {} |
| 172 |
self.url = ('/subscriptions' + |
| 173 |
'/{{ subscription_id }}' + |
| 174 |
'/resourceGroups' + |
| 175 |
'/{{ resource_group }}' + |
| 176 |
'/providers' + |
| 177 |
'/Microsoft.ApiManagement' + |
| 178 |
'/service' + |
| 179 |
'/{{ service_name }}') |
| 180 |
self.url = self.url.replace('{{ subscription_id }}', self.subscription_id) |
| 181 |
self.url = self.url.replace('{{ resource_group }}', self.resource_group) |
| 182 |
self.url = self.url.replace('{{ service_name }}', self.name) |
| 183 |
|
| 184 |
try: |
| 185 |
response = self.mgmt_client.query(self.url, |
| 186 |
'GET', |
| 187 |
self.query_parameters, |
| 188 |
self.header_parameters, |
| 189 |
None, |
| 190 |
self.status_code, |
| 191 |
600, |
| 192 |
30) |
| 193 |
results = json.loads(response.body()) |
| 194 |
except Exception as e: |
| 195 |
self.log('Could not get info for @(Model.ModuleOperationNameUpper).') |
| 196 |
|
| 197 |
return self.format_item(results) |
| 198 |
|
| 199 |
def listbyresourcegroup(self): |
| 200 |
response = None |
| 201 |
results = {} |
| 202 |
|
| 203 |
self.url = ('/subscriptions' + |
| 204 |
'/{{ subscription_id }}' + |
| 205 |
'/resourceGroups' + |
| 206 |
'/{{ resource_group }}' + |
| 207 |
'/providers' + |
| 208 |
'/Microsoft.ApiManagement' + |
| 209 |
'/service') |
| 210 |
self.url = self.url.replace('{{ subscription_id }}', self.subscription_id) |
| 211 |
self.url = self.url.replace('{{ resource_group }}', self.resource_group) |
| 212 |
|
| 213 |
try: |
| 214 |
response = self.mgmt_client.query(self.url, |
| 215 |
'GET', |
| 216 |
self.query_parameters, |
| 217 |
self.header_parameters, |
| 218 |
None, |
| 219 |
self.status_code, |
| 220 |
600, |
| 221 |
30) |
| 222 |
results = json.loads(response.body()) |
| 223 |
except Exception as e: |
| 224 |
self.log('Could not get info for @(Model.ModuleOperationNameUpper).') |
| 225 |
|
| 226 |
return [self.format_item(x) for x in results['value']] if results['value'] else [] |
| 227 |
|
| 228 |
def list(self): |
| 229 |
response = None |
| 230 |
results = {} |
| 231 |
|
| 232 |
self.url = ('/subscriptions' + |
| 233 |
'/{{ subscription_id }}' + |
| 234 |
'/providers' + |
| 235 |
'/Microsoft.ApiManagement' + |
| 236 |
'/service') |
| 237 |
self.url = self.url.replace('{{ subscription_id }}', self.subscription_id) |
| 238 |
|
| 239 |
try: |
| 240 |
response = self.mgmt_client.query(self.url, |
| 241 |
'GET', |
| 242 |
self.query_parameters, |
| 243 |
self.header_parameters, |
| 244 |
None, |
| 245 |
self.status_code, |
| 246 |
600, |
| 247 |
30) |
| 248 |
results = json.loads(response.body()) |
| 249 |
except Exception as e: |
| 250 |
self.log('Could not get info for @(Model.ModuleOperationNameUpper).') |
| 251 |
|
| 252 |
return [self.format_item(x) for x in results['value']] if results['value'] else [] |
| 253 |
|
| 254 |
def format_item(self, item): |
| 255 |
if item: |
| 256 |
d = { |
| 257 |
'id': item['id'], |
| 258 |
'name': item['name'], |
| 259 |
'type': item['type'], |
| 260 |
'sku': item['sku']['name'], |
| 261 |
'identity': item['identity'], |
| 262 |
'zones': item['zones'], |
| 263 |
'location': item['location'], |
| 264 |
'etag': item['etag'], |
| 265 |
'properties': item['properties'] |
| 266 |
} |
| 267 |
else: |
| 268 |
return dict() |
| 269 |
return d |
| 270 |
|
| 271 |
|
| 272 |
def main(): |
| 273 |
AzureRMApiManagementServiceInfo() |
| 274 |
|
| 275 |
|
| 276 |
if __name__ == '__main__': |
| 277 |
main() |