| 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_info |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Get Azure DevTest Lab Artifact Source facts |
| 16 |
description: |
| 17 |
- Get facts of Azure DevTest Lab Artifact Source. |
| 18 |
|
| 19 |
options: |
| 20 |
resource_group: |
| 21 |
description: |
| 22 |
- The name of the resource group. |
| 23 |
required: True |
| 24 |
type: str |
| 25 |
lab_name: |
| 26 |
description: |
| 27 |
- The name of DevTest Lab. |
| 28 |
required: True |
| 29 |
type: str |
| 30 |
name: |
| 31 |
description: |
| 32 |
- The name of DevTest Lab Artifact Source. |
| 33 |
type: str |
| 34 |
tags: |
| 35 |
description: |
| 36 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 37 |
type: list |
| 38 |
elements: str |
| 39 |
|
| 40 |
extends_documentation_fragment: |
| 41 |
- azure.azcollection.azure |
| 42 |
|
| 43 |
author: |
| 44 |
- Zim Kalinowski (@zikalino) |
| 45 |
|
| 46 |
''' |
| 47 |
|
| 48 |
EXAMPLES = ''' |
| 49 |
- name: Get instance of DevTest Lab Artifact Source |
| 50 |
azure_rm_devtestlabartifactsource_info: |
| 51 |
resource_group: myResourceGroup |
| 52 |
lab_name: myLab |
| 53 |
name: myArtifactSource |
| 54 |
''' |
| 55 |
|
| 56 |
RETURN = ''' |
| 57 |
artifactsources: |
| 58 |
description: |
| 59 |
- A list of dictionaries containing facts for DevTest Lab Artifact Source. |
| 60 |
returned: always |
| 61 |
type: complex |
| 62 |
contains: |
| 63 |
id: |
| 64 |
description: |
| 65 |
- The identifier of the artifact source. |
| 66 |
returned: always |
| 67 |
type: str |
| 68 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.DevTestLab/labs/myLab/ar |
| 69 |
tifactSources/myArtifactSource" |
| 70 |
resource_group: |
| 71 |
description: |
| 72 |
- Name of the resource group. |
| 73 |
returned: always |
| 74 |
type: str |
| 75 |
sample: myResourceGroup |
| 76 |
lab_name: |
| 77 |
description: |
| 78 |
- Name of the lab. |
| 79 |
returned: always |
| 80 |
type: str |
| 81 |
sample: myLab |
| 82 |
name: |
| 83 |
description: |
| 84 |
- The name of the artifact source. |
| 85 |
returned: always |
| 86 |
type: str |
| 87 |
sample: myArtifactSource |
| 88 |
display_name: |
| 89 |
description: |
| 90 |
- The artifact source's display name. |
| 91 |
returned: always |
| 92 |
type: str |
| 93 |
sample: Public Artifact Repo |
| 94 |
source_type: |
| 95 |
description: |
| 96 |
- The artifact source's type. |
| 97 |
returned: always |
| 98 |
type: str |
| 99 |
sample: github |
| 100 |
is_enabled: |
| 101 |
description: |
| 102 |
- Is the artifact source enabled. |
| 103 |
returned: always |
| 104 |
type: str |
| 105 |
sample: True |
| 106 |
uri: |
| 107 |
description: |
| 108 |
- URI of the artifact source. |
| 109 |
returned: always |
| 110 |
type: str |
| 111 |
sample: https://github.com/Azure/azure-devtestlab.git |
| 112 |
folder_path: |
| 113 |
description: |
| 114 |
- The folder containing artifacts. |
| 115 |
returned: always |
| 116 |
type: str |
| 117 |
sample: /Artifacts |
| 118 |
arm_template_folder_path: |
| 119 |
description: |
| 120 |
- The folder containing Azure Resource Manager templates. |
| 121 |
returned: always |
| 122 |
type: str |
| 123 |
sample: /Environments |
| 124 |
provisioning_state: |
| 125 |
description: |
| 126 |
- Provisioning state of artifact source. |
| 127 |
returned: always |
| 128 |
type: str |
| 129 |
sample: Succeeded |
| 130 |
tags: |
| 131 |
description: |
| 132 |
- The tags of the resource. |
| 133 |
returned: always |
| 134 |
type: dict |
| 135 |
sample: "{ 'MyTag': 'MyValue' }" |
| 136 |
''' |
| 137 |
|
| 138 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 139 |
|
| 140 |
try: |
| 141 |
from azure.core.exceptions import ResourceNotFoundError |
| 142 |
from azure.mgmt.devtestlabs import DevTestLabsClient |
| 143 |
except ImportError: |
| 144 |
|
| 145 |
pass |
| 146 |
|
| 147 |
|
| 148 |
class AzureRMDtlArtifactSourceInfo(AzureRMModuleBase): |
| 149 |
def __init__(self): |
| 150 |
|
| 151 |
self.module_arg_spec = dict( |
| 152 |
resource_group=dict( |
| 153 |
type='str', |
| 154 |
required=True |
| 155 |
), |
| 156 |
lab_name=dict( |
| 157 |
type='str', |
| 158 |
required=True |
| 159 |
), |
| 160 |
name=dict( |
| 161 |
type='str' |
| 162 |
), |
| 163 |
tags=dict( |
| 164 |
type='list', |
| 165 |
elements='str' |
| 166 |
) |
| 167 |
) |
| 168 |
|
| 169 |
self.results = dict( |
| 170 |
changed=False |
| 171 |
) |
| 172 |
self.mgmt_client = None |
| 173 |
self.resource_group = None |
| 174 |
self.lab_name = None |
| 175 |
self.name = None |
| 176 |
self.tags = None |
| 177 |
super(AzureRMDtlArtifactSourceInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True) |
| 178 |
|
| 179 |
def exec_module(self, **kwargs): |
| 180 |
|
| 181 |
for key in self.module_arg_spec: |
| 182 |
setattr(self, key, kwargs[key]) |
| 183 |
self.mgmt_client = self.get_mgmt_svc_client(DevTestLabsClient, |
| 184 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 185 |
|
| 186 |
if self.name: |
| 187 |
self.results['artifactsources'] = self.get() |
| 188 |
else: |
| 189 |
self.results['artifactsources'] = self.list() |
| 190 |
|
| 191 |
return self.results |
| 192 |
|
| 193 |
def get(self): |
| 194 |
response = None |
| 195 |
results = [] |
| 196 |
try: |
| 197 |
response = self.mgmt_client.artifact_sources.get(resource_group_name=self.resource_group, |
| 198 |
lab_name=self.lab_name, |
| 199 |
name=self.name) |
| 200 |
self.log("Response : {0}".format(response)) |
| 201 |
except ResourceNotFoundError as e: |
| 202 |
self.fail('Could not get facts for Artifact Source.') |
| 203 |
|
| 204 |
if response and self.has_tags(response.tags, self.tags): |
| 205 |
results.append(self.format_response(response)) |
| 206 |
|
| 207 |
return results |
| 208 |
|
| 209 |
def list(self): |
| 210 |
response = None |
| 211 |
results = [] |
| 212 |
try: |
| 213 |
response = self.mgmt_client.artifact_sources.list(resource_group_name=self.resource_group, |
| 214 |
lab_name=self.lab_name) |
| 215 |
self.log("Response : {0}".format(response)) |
| 216 |
except Exception as e: |
| 217 |
self.fail('Could not get facts for Artifact Source.') |
| 218 |
|
| 219 |
if response is not None: |
| 220 |
for item in response: |
| 221 |
if self.has_tags(item.tags, self.tags): |
| 222 |
results.append(self.format_response(item)) |
| 223 |
|
| 224 |
return results |
| 225 |
|
| 226 |
def format_response(self, item): |
| 227 |
d = item.as_dict() |
| 228 |
d = { |
| 229 |
'id': d.get('id'), |
| 230 |
'resource_group': self.parse_resource_to_dict(d.get('id')).get('resource_group'), |
| 231 |
'lab_name': self.parse_resource_to_dict(d.get('id')).get('name'), |
| 232 |
'name': d.get('name'), |
| 233 |
'display_name': d.get('display_name'), |
| 234 |
'tags': d.get('tags'), |
| 235 |
'source_type': d.get('source_type').lower(), |
| 236 |
'is_enabled': d.get('status') == 'Enabled', |
| 237 |
'uri': d.get('uri'), |
| 238 |
'arm_template_folder_path': d.get('arm_template_folder_path'), |
| 239 |
'folder_path': d.get('folder_path'), |
| 240 |
'provisioning_state': d.get('provisioning_state') |
| 241 |
} |
| 242 |
return d |
| 243 |
|
| 244 |
|
| 245 |
def main(): |
| 246 |
AzureRMDtlArtifactSourceInfo() |
| 247 |
|
| 248 |
|
| 249 |
if __name__ == '__main__': |
| 250 |
main() |