1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
#!/usr/bin/python
#
# Copyright (c) 2018 Hai Cao, <t-haicao@microsoft.com>, Yunge Zhu <yungez@microsoft.com>
#
# 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_cdnprofile
version_added: "0.1.2"
short_description: Manage a Azure CDN profile
description:
- Create, update and delete a Azure CDN profile.
options:
resource_group:
description:
- Name of a resource group where the CDN profile exists or will be created.
required: true
type: str
name:
description:
- Name of the CDN profile.
required: true
type: str
location:
description:
- Valid Azure location. Defaults to location of the resource group.
type: str
sku:
description:
- The pricing tier, defines a CDN provider, feature list and rate of the CDN profile.
- Detailed pricing can be find at U(https://azure.microsoft.com/en-us/pricing/details/cdn/).
type: str
choices:
- standard_verizon
- premium_verizon
- custom_verizon
- standard_akamai
- standard_chinacdn
- standard_microsoft
state:
description:
- Assert the state of the CDN profile. Use C(present) to create or update a CDN profile and C(absent) to delete it.
default: present
type: str
choices:
- absent
- present
extends_documentation_fragment:
- azure.azcollection.azure
- azure.azcollection.azure_tags
author:
- Hai Cao (@caohai)
- Yunge Zhu (@yungezz)
'''
EXAMPLES = '''
- name: Create a CDN profile
azure_rm_cdnprofile:
resource_group: myResourceGroup
name: myCDN
sku: standard_akamai
tags:
testing: testing
- name: Delete the CDN profile
azure_rm_cdnprofile:
resource_group: myResourceGroup
name: myCDN
state: absent
'''
RETURN = '''
id:
description: Current state of the CDN profile.
returned: always
type: dict
example:
id: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.Cdn/profiles/myCDN
'''
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
import uuid
try:
from azure.mgmt.cdn.models import Profile, Sku
from azure.mgmt.cdn import CdnManagementClient
except ImportError as ec:
# This is handled in azure_rm_common
pass
def cdnprofile_to_dict(cdnprofile):
return dict(
id=cdnprofile.id,
name=cdnprofile.name,
type=cdnprofile.type,
location=cdnprofile.location,
sku=cdnprofile.sku.name,
resource_state=cdnprofile.resource_state,
provisioning_state=cdnprofile.provisioning_state,
tags=cdnprofile.tags
)
class AzureRMCdnprofile(AzureRMModuleBase):
def __init__(self):
self.module_arg_spec = dict(
resource_group=dict(
type='str',
required=True
),
name=dict(
type='str',
required=True
),
location=dict(
type='str'
),
state=dict(
type='str',
default='present',
choices=['present', 'absent']
),
sku=dict(
type='str',
choices=['standard_verizon', 'premium_verizon', 'custom_verizon', 'standard_akamai', 'standard_chinacdn', 'standard_microsoft']
)
)
self.resource_group = None
self.name = None
self.location = None
self.state = None
self.tags = None
self.sku = None
self.cdn_client = None
required_if = [
('state', 'present', ['sku'])
]
self.results = dict(changed=False)
super(AzureRMCdnprofile, self).__init__(derived_arg_spec=self.module_arg_spec,
supports_check_mode=True,
supports_tags=True,
required_if=required_if)
def exec_module(self, **kwargs):
"""Main module execution method"""
for key in list(self.module_arg_spec.keys()) + ['tags']:
setattr(self, key, kwargs[key])
self.cdn_client = self.get_cdn_client()
to_be_updated = False
resource_group = self.get_resource_group(self.resource_group)
if not self.location:
self.location = resource_group.location
response = self.get_cdnprofile()
if self.state == 'present':
if not response:
self.log("Need to create the CDN profile")
if not self.check_mode:
new_response = self.create_cdnprofile()
self.results['id'] = new_response['id']
self.results['changed'] = True
else:
self.log('Results : {0}'.format(response))
update_tags, response['tags'] = self.update_tags(response['tags'])
if response['provisioning_state'] == "Succeeded":
if update_tags:
to_be_updated = True
if to_be_updated:
self.log("Need to update the CDN profile")
if not self.check_mode:
new_response = self.update_cdnprofile()
self.results['id'] = new_response['id']
self.results['changed'] = True
elif self.state == 'absent':
if not response:
self.fail("CDN profile {0} not exists.".format(self.name))
else:
self.log("Need to delete the CDN profile")
self.results['changed'] = True
if not self.check_mode:
self.delete_cdnprofile()
self.results['id'] = response['id']
return self.results
def create_cdnprofile(self):
'''
Creates a Azure CDN profile.
:return: deserialized Azure CDN profile instance state dictionary
'''
self.log("Creating the Azure CDN profile instance {0}".format(self.name))
parameters = Profile(
location=self.location,
sku=Sku(name=self.sku),
tags=self.tags
)
xid = str(uuid.uuid1())
try:
poller = self.cdn_client.profiles.begin_create(self.resource_group,
self.name,
parameters)
response = self.get_poller_result(poller)
return cdnprofile_to_dict(response)
except Exception as exc:
self.log('Error attempting to create Azure CDN profile instance.')
self.fail("Error Creating Azure CDN profile instance: {0}".format(exc.message))
def update_cdnprofile(self):
'''
Updates a Azure CDN profile.
:return: deserialized Azure CDN profile instance state dictionary
'''
self.log("Updating the Azure CDN profile instance {0}".format(self.name))
try:
poller = self.cdn_client.profiles.begin_update(self.resource_group, self.name, {'tags': self.tags})
response = self.get_poller_result(poller)
return cdnprofile_to_dict(response)
except Exception as exc:
self.log('Error attempting to update Azure CDN profile instance.')
self.fail("Error updating Azure CDN profile instance: {0}".format(exc.message))
def delete_cdnprofile(self):
'''
Deletes the specified Azure CDN profile in the specified subscription and resource group.
:return: True
'''
self.log("Deleting the CDN profile {0}".format(self.name))
try:
poller = self.cdn_client.profiles.begin_delete(
self.resource_group, self.name)
self.get_poller_result(poller)
return True
except Exception as e:
self.log('Error attempting to delete the CDN profile.')
self.fail("Error deleting the CDN profile: {0}".format(e.message))
return False
def get_cdnprofile(self):
'''
Gets the properties of the specified CDN profile.
:return: deserialized CDN profile state dictionary
'''
self.log(
"Checking if the CDN profile {0} is present".format(self.name))
try:
response = self.cdn_client.profiles.get(self.resource_group, self.name)
self.log("Response : {0}".format(response))
self.log("CDN profile : {0} found".format(response.name))
return cdnprofile_to_dict(response)
except Exception:
self.log('Did not find the CDN profile.')
return False
def get_cdn_client(self):
if not self.cdn_client:
self.cdn_client = self.get_mgmt_svc_client(CdnManagementClient,
base_url=self._cloud_environment.endpoints.resource_manager,
api_version='2017-04-02')
return self.cdn_client
def main():
"""Main execution"""
AzureRMCdnprofile()
if __name__ == '__main__':
main()
|