| 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_devtestlabartifactsource |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Manage Azure DevTest Labs Artifacts Source instance |
| 16 |
description: |
| 17 |
- Create, update and delete instance of Azure DevTest Labs Artifacts Source. |
| 18 |
|
| 19 |
options: |
| 20 |
resource_group: |
| 21 |
description: |
| 22 |
- The name of the resource group. |
| 23 |
type: str |
| 24 |
required: True |
| 25 |
lab_name: |
| 26 |
description: |
| 27 |
- The name of the lab. |
| 28 |
type: str |
| 29 |
required: True |
| 30 |
name: |
| 31 |
description: |
| 32 |
- The name of the artifact source. |
| 33 |
type: str |
| 34 |
required: True |
| 35 |
display_name: |
| 36 |
description: |
| 37 |
- The artifact source's display name. |
| 38 |
type: str |
| 39 |
uri: |
| 40 |
description: |
| 41 |
- The artifact source's URI. |
| 42 |
type: str |
| 43 |
source_type: |
| 44 |
description: |
| 45 |
- The artifact source's type. |
| 46 |
type: str |
| 47 |
choices: |
| 48 |
- 'vso' |
| 49 |
- 'github' |
| 50 |
folder_path: |
| 51 |
description: |
| 52 |
- The folder containing artifacts. |
| 53 |
type: str |
| 54 |
arm_template_folder_path: |
| 55 |
description: |
| 56 |
- The folder containing Azure Resource Manager templates. |
| 57 |
type: str |
| 58 |
branch_ref: |
| 59 |
description: |
| 60 |
- The artifact source's branch reference. |
| 61 |
type: str |
| 62 |
security_token: |
| 63 |
description: |
| 64 |
- The security token to authenticate to the artifact source. |
| 65 |
type: str |
| 66 |
is_enabled: |
| 67 |
description: |
| 68 |
- Indicates whether the artifact source is enabled. |
| 69 |
type: bool |
| 70 |
state: |
| 71 |
description: |
| 72 |
- Assert the state of the DevTest Labs Artifacts Source. |
| 73 |
- Use C(present) to create or update an DevTest Labs Artifacts Source and C(absent) to delete it. |
| 74 |
default: present |
| 75 |
type: str |
| 76 |
choices: |
| 77 |
- absent |
| 78 |
- present |
| 79 |
|
| 80 |
extends_documentation_fragment: |
| 81 |
- azure.azcollection.azure |
| 82 |
- azure.azcollection.azure_tags |
| 83 |
|
| 84 |
author: |
| 85 |
- Zim Kalinowski (@zikalino) |
| 86 |
|
| 87 |
''' |
| 88 |
|
| 89 |
EXAMPLES = ''' |
| 90 |
- name: Create (or update) DevTest Labs Artifacts Source |
| 91 |
azure_rm_devtestlabartifactsource: |
| 92 |
resource_group: myrg |
| 93 |
lab_name: mylab |
| 94 |
name: myartifacts |
| 95 |
uri: https://github.com/myself/myrepo.git |
| 96 |
source_type: github |
| 97 |
folder_path: / |
| 98 |
security_token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
| 99 |
''' |
| 100 |
|
| 101 |
RETURN = ''' |
| 102 |
id: |
| 103 |
description: |
| 104 |
- The identifier of the resource. |
| 105 |
returned: always |
| 106 |
type: str |
| 107 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/myrg/providers/microsoft.devtestlab/labs/mylab/artifactsources/myartifacts |
| 108 |
is_enabled: |
| 109 |
description: |
| 110 |
- Indicates whether the artifact source is enabled. |
| 111 |
returned: always |
| 112 |
type: bool |
| 113 |
sample: true |
| 114 |
''' |
| 115 |
|
| 116 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 117 |
|
| 118 |
try: |
| 119 |
from azure.core.polling import LROPoller |
| 120 |
from azure.core.exceptions import ResourceNotFoundError |
| 121 |
from azure.mgmt.devtestlabs import DevTestLabsClient |
| 122 |
except ImportError: |
| 123 |
|
| 124 |
pass |
| 125 |
|
| 126 |
|
| 127 |
class Actions: |
| 128 |
NoAction, Create, Update, Delete = range(4) |
| 129 |
|
| 130 |
|
| 131 |
class AzureRMDevTestLabArtifactsSource(AzureRMModuleBase): |
| 132 |
"""Configuration class for an Azure RM DevTest Labs Artifacts Source resource""" |
| 133 |
|
| 134 |
def __init__(self): |
| 135 |
self.module_arg_spec = dict( |
| 136 |
resource_group=dict( |
| 137 |
type='str', |
| 138 |
required=True |
| 139 |
), |
| 140 |
lab_name=dict( |
| 141 |
type='str', |
| 142 |
required=True |
| 143 |
), |
| 144 |
name=dict( |
| 145 |
type='str', |
| 146 |
required=True |
| 147 |
), |
| 148 |
display_name=dict( |
| 149 |
type='str' |
| 150 |
), |
| 151 |
uri=dict( |
| 152 |
type='str' |
| 153 |
), |
| 154 |
source_type=dict( |
| 155 |
type='str', |
| 156 |
choices=['vso', |
| 157 |
'github'] |
| 158 |
), |
| 159 |
folder_path=dict( |
| 160 |
type='str' |
| 161 |
), |
| 162 |
arm_template_folder_path=dict( |
| 163 |
type='str' |
| 164 |
), |
| 165 |
branch_ref=dict( |
| 166 |
type='str' |
| 167 |
), |
| 168 |
security_token=dict( |
| 169 |
type='str', |
| 170 |
no_log=True |
| 171 |
), |
| 172 |
is_enabled=dict( |
| 173 |
type='bool' |
| 174 |
), |
| 175 |
state=dict( |
| 176 |
type='str', |
| 177 |
default='present', |
| 178 |
choices=['present', 'absent'] |
| 179 |
) |
| 180 |
) |
| 181 |
|
| 182 |
self.resource_group = None |
| 183 |
self.lab_name = None |
| 184 |
self.name = None |
| 185 |
self.artifact_source = dict() |
| 186 |
|
| 187 |
self.results = dict(changed=False) |
| 188 |
self.mgmt_client = None |
| 189 |
self.state = None |
| 190 |
self.to_do = Actions.NoAction |
| 191 |
|
| 192 |
required_if = [ |
| 193 |
('state', 'present', [ |
| 194 |
'source_type', 'uri', 'security_token']) |
| 195 |
] |
| 196 |
|
| 197 |
super(AzureRMDevTestLabArtifactsSource, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 198 |
supports_check_mode=True, |
| 199 |
supports_tags=True, |
| 200 |
required_if=required_if) |
| 201 |
|
| 202 |
def exec_module(self, **kwargs): |
| 203 |
"""Main module execution method""" |
| 204 |
|
| 205 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 206 |
if hasattr(self, key): |
| 207 |
setattr(self, key, kwargs[key]) |
| 208 |
elif kwargs[key] is not None: |
| 209 |
self.artifact_source[key] = kwargs[key] |
| 210 |
|
| 211 |
if self.artifact_source.get('source_type') == 'github': |
| 212 |
self.artifact_source['source_type'] = 'GitHub' |
| 213 |
elif self.artifact_source.get('source_type') == 'vso': |
| 214 |
self.artifact_source['source_type'] = 'VsoGit' |
| 215 |
|
| 216 |
if self.artifact_source.get('status') is not None: |
| 217 |
self.artifact_source['status'] = 'Enabled' if self.artifact_source.get('status') else 'Disabled' |
| 218 |
|
| 219 |
response = None |
| 220 |
|
| 221 |
self.mgmt_client = self.get_mgmt_svc_client(DevTestLabsClient, |
| 222 |
base_url=self._cloud_environment.endpoints.resource_manager, |
| 223 |
api_version='2018-10-15') |
| 224 |
|
| 225 |
old_response = self.get_devtestlabartifactssource() |
| 226 |
|
| 227 |
if not old_response: |
| 228 |
self.log("DevTest Labs Artifacts Source instance doesn't exist") |
| 229 |
if self.state == 'absent': |
| 230 |
self.log("Old instance didn't exist") |
| 231 |
else: |
| 232 |
self.to_do = Actions.Create |
| 233 |
else: |
| 234 |
self.log("DevTest Labs Artifacts Source instance already exists") |
| 235 |
if self.state == 'absent': |
| 236 |
self.to_do = Actions.Delete |
| 237 |
elif self.state == 'present': |
| 238 |
self.results['old_response'] = old_response |
| 239 |
|
| 240 |
if self.artifact_source.get('display_name') is not None: |
| 241 |
if self.artifact_source.get('display_name') != old_response.get('display_name'): |
| 242 |
self.to_do = Actions.Update |
| 243 |
else: |
| 244 |
self.artifact_source['display_name'] = old_response.get('display_name') |
| 245 |
|
| 246 |
if self.artifact_source.get('source_type').lower() != old_response.get('source_type').lower(): |
| 247 |
self.to_do = Actions.Update |
| 248 |
|
| 249 |
if self.artifact_source.get('uri') != old_response.get('uri'): |
| 250 |
self.to_do = Actions.Update |
| 251 |
|
| 252 |
if self.artifact_source.get('branch_ref') is not None: |
| 253 |
if self.artifact_source.get('branch_ref') != old_response.get('branch_ref'): |
| 254 |
self.to_do = Actions.Update |
| 255 |
else: |
| 256 |
self.artifact_source['branch_ref'] = old_response.get('branch_ref') |
| 257 |
|
| 258 |
if self.artifact_source.get('status') is not None: |
| 259 |
if self.artifact_source.get('status') != old_response.get('status'): |
| 260 |
self.to_do = Actions.Update |
| 261 |
else: |
| 262 |
self.artifact_source['status'] = old_response.get('status') |
| 263 |
|
| 264 |
if self.artifact_source.get('folder_path') is not None: |
| 265 |
if self.artifact_source.get('folder_path') != old_response.get('folder_path'): |
| 266 |
self.to_do = Actions.Update |
| 267 |
else: |
| 268 |
self.artifact_source['folder_path'] = old_response.get('folder_path') |
| 269 |
|
| 270 |
if self.artifact_source.get('arm_template_folder_path') is not None: |
| 271 |
if self.artifact_source.get('arm_template_folder_path') != old_response.get('arm_template_folder_path'): |
| 272 |
self.to_do = Actions.Update |
| 273 |
else: |
| 274 |
self.artifact_source['arm_template_folder_path'] = old_response.get('arm_template_folder_path') |
| 275 |
|
| 276 |
if (self.to_do == Actions.Create) or (self.to_do == Actions.Update): |
| 277 |
self.log("Need to Create / Update the DevTest Labs Artifacts Source instance") |
| 278 |
self.results['changed'] = True |
| 279 |
if self.check_mode: |
| 280 |
return self.results |
| 281 |
response = self.create_update_devtestlabartifactssource() |
| 282 |
self.log("Creation / Update done") |
| 283 |
elif self.to_do == Actions.Delete: |
| 284 |
self.log("DevTest Labs Artifacts Source instance deleted") |
| 285 |
self.results['changed'] = True |
| 286 |
if self.check_mode: |
| 287 |
return self.results |
| 288 |
self.delete_devtestlabartifactssource() |
| 289 |
else: |
| 290 |
self.log("DevTest Labs Artifacts Source instance unchanged") |
| 291 |
self.results['changed'] = False |
| 292 |
response = old_response |
| 293 |
|
| 294 |
if self.state == 'present': |
| 295 |
self.results.update({ |
| 296 |
'id': response.get('id', None), |
| 297 |
'is_enabled': (response.get('status', None).lower() == 'enabled') |
| 298 |
}) |
| 299 |
return self.results |
| 300 |
|
| 301 |
def create_update_devtestlabartifactssource(self): |
| 302 |
''' |
| 303 |
Creates or updates DevTest Labs Artifacts Source with the specified configuration. |
| 304 |
|
| 305 |
:return: deserialized DevTest Labs Artifacts Source instance state dictionary |
| 306 |
''' |
| 307 |
self.log("Creating / Updating the DevTest Labs Artifacts Source instance {0}".format(self.name)) |
| 308 |
|
| 309 |
try: |
| 310 |
response = self.mgmt_client.artifact_sources.create_or_update(resource_group_name=self.resource_group, |
| 311 |
lab_name=self.lab_name, |
| 312 |
name=self.name, |
| 313 |
artifact_source=self.artifact_source) |
| 314 |
if isinstance(response, LROPoller): |
| 315 |
response = self.get_poller_result(response) |
| 316 |
|
| 317 |
except Exception as exc: |
| 318 |
self.log('Error attempting to create the DevTest Labs Artifacts Source instance.') |
| 319 |
self.fail("Error creating the DevTest Labs Artifacts Source instance: {0}".format(str(exc))) |
| 320 |
return response.as_dict() |
| 321 |
|
| 322 |
def delete_devtestlabartifactssource(self): |
| 323 |
''' |
| 324 |
Deletes specified DevTest Labs Artifacts Source instance in the specified subscription and resource group. |
| 325 |
|
| 326 |
:return: True |
| 327 |
''' |
| 328 |
self.log("Deleting the DevTest Labs Artifacts Source instance {0}".format(self.name)) |
| 329 |
try: |
| 330 |
response = self.mgmt_client.artifact_sources.delete(resource_group_name=self.resource_group, |
| 331 |
lab_name=self.lab_name, |
| 332 |
name=self.name) |
| 333 |
except Exception as e: |
| 334 |
self.log('Error attempting to delete the DevTest Labs Artifacts Source instance.') |
| 335 |
self.fail("Error deleting the DevTest Labs Artifacts Source instance: {0}".format(str(e))) |
| 336 |
|
| 337 |
return True |
| 338 |
|
| 339 |
def get_devtestlabartifactssource(self): |
| 340 |
''' |
| 341 |
Gets the properties of the specified DevTest Labs Artifacts Source. |
| 342 |
|
| 343 |
:return: deserialized DevTest Labs Artifacts Source instance state dictionary |
| 344 |
''' |
| 345 |
self.log("Checking if the DevTest Labs Artifacts Source instance {0} is present".format(self.name)) |
| 346 |
found = False |
| 347 |
try: |
| 348 |
response = self.mgmt_client.artifact_sources.get(resource_group_name=self.resource_group, |
| 349 |
lab_name=self.lab_name, |
| 350 |
name=self.name) |
| 351 |
found = True |
| 352 |
self.log("Response : {0}".format(response)) |
| 353 |
self.log("DevTest Labs Artifacts Source instance : {0} found".format(response.name)) |
| 354 |
except ResourceNotFoundError as e: |
| 355 |
self.log('Did not find the DevTest Labs Artifacts Source instance.') |
| 356 |
if found is True: |
| 357 |
return response.as_dict() |
| 358 |
|
| 359 |
return False |
| 360 |
|
| 361 |
|
| 362 |
def main(): |
| 363 |
"""Main execution""" |
| 364 |
AzureRMDevTestLabArtifactsSource() |
| 365 |
|
| 366 |
|
| 367 |
if __name__ == '__main__': |
| 368 |
main() |