| 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_gallery |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Manage Azure Shared Image Gallery instance |
| 16 |
description: |
| 17 |
- Create, update and delete instance of Azure Shared Image Gallery (SIG). |
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- The name of the resource group. |
| 22 |
required: true |
| 23 |
type: str |
| 24 |
name: |
| 25 |
description: |
| 26 |
- The name of the Shared Image Gallery. |
| 27 |
- Valid names consist of less than 80 alphanumeric characters, underscores and periods. |
| 28 |
required: true |
| 29 |
type: str |
| 30 |
location: |
| 31 |
description: |
| 32 |
- Resource location. |
| 33 |
type: str |
| 34 |
description: |
| 35 |
description: |
| 36 |
- The description of this Shared Image Gallery resource. This property is updatable. |
| 37 |
type: str |
| 38 |
state: |
| 39 |
description: |
| 40 |
- Assert the state of the Gallery. |
| 41 |
- Use C(present) to create or update an Gallery and C(absent) to delete it. |
| 42 |
default: present |
| 43 |
type: str |
| 44 |
choices: |
| 45 |
- absent |
| 46 |
- present |
| 47 |
extends_documentation_fragment: |
| 48 |
- azure.azcollection.azure |
| 49 |
- azure.azcollection.azure_tags |
| 50 |
author: |
| 51 |
- Zim Kalinowski (@zikalino) |
| 52 |
|
| 53 |
''' |
| 54 |
|
| 55 |
EXAMPLES = ''' |
| 56 |
- name: Create or update a simple gallery. |
| 57 |
azure_rm_gallery: |
| 58 |
resource_group: myResourceGroup |
| 59 |
name: myGallery1283 |
| 60 |
location: West US |
| 61 |
description: This is the gallery description. |
| 62 |
''' |
| 63 |
|
| 64 |
RETURN = ''' |
| 65 |
id: |
| 66 |
description: |
| 67 |
- Resource ID. |
| 68 |
returned: always |
| 69 |
type: str |
| 70 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/galleries/myGallery1283" |
| 71 |
''' |
| 72 |
|
| 73 |
import time |
| 74 |
import json |
| 75 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 76 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient |
| 77 |
|
| 78 |
|
| 79 |
class Actions: |
| 80 |
NoAction, Create, Update, Delete = range(4) |
| 81 |
|
| 82 |
|
| 83 |
class AzureRMGalleries(AzureRMModuleBaseExt): |
| 84 |
def __init__(self): |
| 85 |
self.module_arg_spec = dict( |
| 86 |
resource_group=dict( |
| 87 |
type='str', |
| 88 |
required=True |
| 89 |
), |
| 90 |
name=dict( |
| 91 |
type='str', |
| 92 |
required=True |
| 93 |
), |
| 94 |
location=dict( |
| 95 |
type='str' |
| 96 |
), |
| 97 |
description=dict( |
| 98 |
type='str', |
| 99 |
), |
| 100 |
state=dict( |
| 101 |
type='str', |
| 102 |
default='present', |
| 103 |
choices=['present', 'absent'] |
| 104 |
) |
| 105 |
) |
| 106 |
|
| 107 |
self.resource_group = None |
| 108 |
self.name = None |
| 109 |
self.gallery = None |
| 110 |
|
| 111 |
self.results = dict(changed=False) |
| 112 |
self.mgmt_client = None |
| 113 |
self.state = None |
| 114 |
self.url = None |
| 115 |
self.status_code = [200, 201, 202] |
| 116 |
self.to_do = Actions.NoAction |
| 117 |
|
| 118 |
self.body = {} |
| 119 |
self.body['properties'] = {} |
| 120 |
self.query_parameters = {} |
| 121 |
self.query_parameters['api-version'] = '2019-07-01' |
| 122 |
self.header_parameters = {} |
| 123 |
self.header_parameters['Content-Type'] = 'application/json; charset=utf-8' |
| 124 |
|
| 125 |
super(AzureRMGalleries, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 126 |
supports_check_mode=True, |
| 127 |
supports_tags=True) |
| 128 |
|
| 129 |
def exec_module(self, **kwargs): |
| 130 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 131 |
if hasattr(self, key): |
| 132 |
setattr(self, key, kwargs[key]) |
| 133 |
elif kwargs[key] is not None: |
| 134 |
if key == 'description': |
| 135 |
self.body['properties']['description'] = kwargs[key] |
| 136 |
else: |
| 137 |
self.body[key] = kwargs[key] |
| 138 |
|
| 139 |
old_response = None |
| 140 |
response = None |
| 141 |
|
| 142 |
self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient, |
| 143 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 144 |
|
| 145 |
resource_group = self.get_resource_group(self.resource_group) |
| 146 |
|
| 147 |
if 'location' not in self.body: |
| 148 |
self.body['location'] = resource_group.location |
| 149 |
|
| 150 |
self.url = ('/subscriptions' + |
| 151 |
'/{{ subscription_id }}' + |
| 152 |
'/resourceGroups' + |
| 153 |
'/{{ resource_group }}' + |
| 154 |
'/providers' + |
| 155 |
'/Microsoft.Compute' + |
| 156 |
'/galleries' + |
| 157 |
'/{{ gallery_name }}') |
| 158 |
self.url = self.url.replace('{{ subscription_id }}', self.subscription_id) |
| 159 |
self.url = self.url.replace('{{ resource_group }}', self.resource_group) |
| 160 |
self.url = self.url.replace('{{ gallery_name }}', self.name) |
| 161 |
|
| 162 |
old_response = self.get_resource() |
| 163 |
|
| 164 |
if not old_response: |
| 165 |
self.log("Gallery instance doesn't exist") |
| 166 |
|
| 167 |
if self.state == 'absent': |
| 168 |
self.log("Old instance didn't exist") |
| 169 |
else: |
| 170 |
self.to_do = Actions.Create |
| 171 |
else: |
| 172 |
self.log('Gallery instance already exists') |
| 173 |
|
| 174 |
if self.state == 'absent': |
| 175 |
self.to_do = Actions.Delete |
| 176 |
else: |
| 177 |
if self.body['properties'].get('description') is not None and\ |
| 178 |
self.body['properties']['description'] != old_response['properties'].get('description'): |
| 179 |
self.to_do = Actions.Update |
| 180 |
else: |
| 181 |
self.body['properties']['description'] = old_response['properties'].get('description') |
| 182 |
|
| 183 |
update_tags, new_tags = self.update_tags(old_response.get('tags')) |
| 184 |
if update_tags: |
| 185 |
self.to_do = Actions.Update |
| 186 |
self.body['tags'] = new_tags |
| 187 |
|
| 188 |
if self.to_do == Actions.Create: |
| 189 |
self.log('Need to Create the Gallery instance') |
| 190 |
if self.check_mode: |
| 191 |
self.results['changed'] = True |
| 192 |
return self.results |
| 193 |
response = self.create_resource() |
| 194 |
|
| 195 |
self.results['changed'] = True |
| 196 |
|
| 197 |
|
| 198 |
self.log('Creation done') |
| 199 |
elif self.to_do == Actions.Update: |
| 200 |
self.log('Need to Update the Gallery instance') |
| 201 |
if self.check_mode: |
| 202 |
self.results['changed'] = True |
| 203 |
return self.results |
| 204 |
response = self.update_resource() |
| 205 |
|
| 206 |
self.results['changed'] = True |
| 207 |
self.log('Update done') |
| 208 |
elif self.to_do == Actions.Delete: |
| 209 |
self.log('Gallery instance deleted') |
| 210 |
self.results['changed'] = True |
| 211 |
|
| 212 |
if self.check_mode: |
| 213 |
return self.results |
| 214 |
|
| 215 |
self.delete_resource() |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
while self.get_resource(): |
| 220 |
time.sleep(20) |
| 221 |
else: |
| 222 |
self.log('Gallery instance unchanged') |
| 223 |
self.results['changed'] = False |
| 224 |
response = old_response |
| 225 |
|
| 226 |
if response: |
| 227 |
self.results["id"] = response["id"] |
| 228 |
|
| 229 |
return self.results |
| 230 |
|
| 231 |
def update_resource(self): |
| 232 |
|
| 233 |
|
| 234 |
try: |
| 235 |
response = self.mgmt_client.query(self.url, |
| 236 |
'PATCH', |
| 237 |
self.query_parameters, |
| 238 |
self.header_parameters, |
| 239 |
self.body, |
| 240 |
self.status_code, |
| 241 |
600, |
| 242 |
30) |
| 243 |
except Exception as exc: |
| 244 |
self.log('Error attempting to update the Gallery instance.') |
| 245 |
self.fail('Error updating the Gallery instance: {0}'.format(str(exc))) |
| 246 |
|
| 247 |
if hasattr(response, 'body'): |
| 248 |
response = json.loads(response.body()) |
| 249 |
elif hasattr(response, 'context'): |
| 250 |
response = response.context['deserialized_data'] |
| 251 |
else: |
| 252 |
self.fail("Updating fail, no match message return, return info as {0}".format(response)) |
| 253 |
|
| 254 |
return response |
| 255 |
|
| 256 |
def create_resource(self): |
| 257 |
|
| 258 |
|
| 259 |
try: |
| 260 |
response = self.mgmt_client.query(self.url, |
| 261 |
'PUT', |
| 262 |
self.query_parameters, |
| 263 |
self.header_parameters, |
| 264 |
self.body, |
| 265 |
self.status_code, |
| 266 |
600, |
| 267 |
30) |
| 268 |
except Exception as exc: |
| 269 |
self.log('Error attempting to create the Gallery instance.') |
| 270 |
self.fail('Error creating the Gallery instance: {0}'.format(str(exc))) |
| 271 |
|
| 272 |
if hasattr(response, 'body'): |
| 273 |
response = json.loads(response.body()) |
| 274 |
elif hasattr(response, 'context'): |
| 275 |
response = response.context['deserialized_data'] |
| 276 |
else: |
| 277 |
self.fail("Create fail, no match message return, return info as {0}".format(response)) |
| 278 |
|
| 279 |
return response |
| 280 |
|
| 281 |
def delete_resource(self): |
| 282 |
|
| 283 |
try: |
| 284 |
response = self.mgmt_client.query(self.url, |
| 285 |
'DELETE', |
| 286 |
self.query_parameters, |
| 287 |
self.header_parameters, |
| 288 |
None, |
| 289 |
self.status_code, |
| 290 |
600, |
| 291 |
30) |
| 292 |
except Exception as e: |
| 293 |
self.log('Error attempting to delete the Gallery instance.') |
| 294 |
self.fail('Error deleting the Gallery instance: {0}'.format(str(e))) |
| 295 |
|
| 296 |
return True |
| 297 |
|
| 298 |
def get_resource(self): |
| 299 |
|
| 300 |
found = False |
| 301 |
try: |
| 302 |
response = self.mgmt_client.query(self.url, |
| 303 |
'GET', |
| 304 |
self.query_parameters, |
| 305 |
self.header_parameters, |
| 306 |
None, |
| 307 |
self.status_code, |
| 308 |
600, |
| 309 |
30) |
| 310 |
response = json.loads(response.body()) |
| 311 |
found = True |
| 312 |
self.log("Response : {0}".format(response)) |
| 313 |
|
| 314 |
except Exception as e: |
| 315 |
self.log('Did not find the AzureFirewall instance.') |
| 316 |
if found is True: |
| 317 |
return response |
| 318 |
|
| 319 |
return False |
| 320 |
|
| 321 |
|
| 322 |
def main(): |
| 323 |
AzureRMGalleries() |
| 324 |
|
| 325 |
|
| 326 |
if __name__ == '__main__': |
| 327 |
main() |