| 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_devtestlabartifact_info |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Get Azure DevTest Lab Artifact facts |
| 16 |
description: |
| 17 |
- Get facts of Azure DevTest Lab Artifact. |
| 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 the lab. |
| 28 |
required: True |
| 29 |
type: str |
| 30 |
artifact_source_name: |
| 31 |
description: |
| 32 |
- The name of the artifact source. |
| 33 |
required: True |
| 34 |
type: str |
| 35 |
name: |
| 36 |
description: |
| 37 |
- The name of the artifact. |
| 38 |
type: 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 |
| 50 |
azure_rm_devtestlabartifact_info: |
| 51 |
resource_group: myResourceGroup |
| 52 |
lab_name: myLab |
| 53 |
artifact_source_name: myArtifactSource |
| 54 |
name: myArtifact |
| 55 |
''' |
| 56 |
|
| 57 |
RETURN = ''' |
| 58 |
artifacts: |
| 59 |
description: |
| 60 |
- A list of dictionaries containing facts for DevTest Lab Artifact. |
| 61 |
returned: always |
| 62 |
type: complex |
| 63 |
contains: |
| 64 |
id: |
| 65 |
description: |
| 66 |
- The identifier of the artifact. |
| 67 |
returned: always |
| 68 |
type: str |
| 69 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.DevTestLab/labs/myLab/ar |
| 70 |
tifactSources/myArtifactSource/artifacts/myArtifact" |
| 71 |
resource_group: |
| 72 |
description: |
| 73 |
- Name of the resource group. |
| 74 |
returned: always |
| 75 |
type: str |
| 76 |
sample: myResourceGroup |
| 77 |
lab_name: |
| 78 |
description: |
| 79 |
- Name of the lab. |
| 80 |
returned: always |
| 81 |
type: str |
| 82 |
sample: myLab |
| 83 |
artifact_source_name: |
| 84 |
description: |
| 85 |
- The name of the artifact source. |
| 86 |
returned: always |
| 87 |
type: str |
| 88 |
sample: myArtifactSource |
| 89 |
name: |
| 90 |
description: |
| 91 |
- The name of the artifact. |
| 92 |
returned: always |
| 93 |
type: str |
| 94 |
sample: myArtifact |
| 95 |
description: |
| 96 |
description: |
| 97 |
- Description of the artifact. |
| 98 |
returned: always |
| 99 |
type: str |
| 100 |
sample: Installs My Software |
| 101 |
file_path: |
| 102 |
description: |
| 103 |
- Artifact's path in the repo. |
| 104 |
returned: always |
| 105 |
type: str |
| 106 |
sample: Artifacts/myArtifact |
| 107 |
publisher: |
| 108 |
description: |
| 109 |
- Publisher name. |
| 110 |
returned: always |
| 111 |
type: str |
| 112 |
sample: MyPublisher |
| 113 |
target_os_type: |
| 114 |
description: |
| 115 |
- Target OS type. |
| 116 |
returned: always |
| 117 |
type: str |
| 118 |
sample: Linux |
| 119 |
title: |
| 120 |
description: |
| 121 |
- Title of the artifact. |
| 122 |
returned: always |
| 123 |
type: str |
| 124 |
sample: My Software |
| 125 |
parameters: |
| 126 |
description: |
| 127 |
- A dictionary containing parameters definition of the artifact. |
| 128 |
returned: always |
| 129 |
type: dict |
| 130 |
sample: {} |
| 131 |
''' |
| 132 |
|
| 133 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 134 |
|
| 135 |
try: |
| 136 |
from azure.core.exceptions import ResourceNotFoundError |
| 137 |
from azure.mgmt.devtestlabs import DevTestLabsClient |
| 138 |
except ImportError: |
| 139 |
|
| 140 |
pass |
| 141 |
|
| 142 |
|
| 143 |
class AzureRMArtifactInfo(AzureRMModuleBase): |
| 144 |
def __init__(self): |
| 145 |
|
| 146 |
self.module_arg_spec = dict( |
| 147 |
resource_group=dict( |
| 148 |
type='str', |
| 149 |
required=True |
| 150 |
), |
| 151 |
lab_name=dict( |
| 152 |
type='str', |
| 153 |
required=True |
| 154 |
), |
| 155 |
artifact_source_name=dict( |
| 156 |
type='str', |
| 157 |
required=True |
| 158 |
), |
| 159 |
name=dict( |
| 160 |
type='str' |
| 161 |
) |
| 162 |
) |
| 163 |
|
| 164 |
self.results = dict( |
| 165 |
changed=False |
| 166 |
) |
| 167 |
self.mgmt_client = None |
| 168 |
self.resource_group = None |
| 169 |
self.lab_name = None |
| 170 |
self.artifact_source_name = None |
| 171 |
self.name = None |
| 172 |
super(AzureRMArtifactInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False) |
| 173 |
|
| 174 |
def exec_module(self, **kwargs): |
| 175 |
for key in self.module_arg_spec: |
| 176 |
setattr(self, key, kwargs[key]) |
| 177 |
self.mgmt_client = self.get_mgmt_svc_client(DevTestLabsClient, |
| 178 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 179 |
|
| 180 |
if self.name: |
| 181 |
self.results['artifacts'] = self.get() |
| 182 |
else: |
| 183 |
self.results['artifacts'] = self.list() |
| 184 |
|
| 185 |
return self.results |
| 186 |
|
| 187 |
def get(self): |
| 188 |
response = None |
| 189 |
results = [] |
| 190 |
try: |
| 191 |
response = self.mgmt_client.artifacts.get(resource_group_name=self.resource_group, |
| 192 |
lab_name=self.lab_name, |
| 193 |
artifact_source_name=self.artifact_source_name, |
| 194 |
name=self.name) |
| 195 |
self.log("Response : {0}".format(response)) |
| 196 |
except ResourceNotFoundError as e: |
| 197 |
self.log('Could not get facts for Artifact.') |
| 198 |
|
| 199 |
if response: |
| 200 |
results.append(self.format_response(response)) |
| 201 |
|
| 202 |
return results |
| 203 |
|
| 204 |
def list(self): |
| 205 |
response = None |
| 206 |
results = [] |
| 207 |
try: |
| 208 |
response = self.mgmt_client.artifacts.list(resource_group_name=self.resource_group, |
| 209 |
lab_name=self.lab_name, |
| 210 |
artifact_source_name=self.artifact_source_name) |
| 211 |
self.log("Response : {0}".format(response)) |
| 212 |
except Exception as e: |
| 213 |
self.log('Could not get facts for Artifact.') |
| 214 |
|
| 215 |
if response is not None: |
| 216 |
for item in response: |
| 217 |
results.append(self.format_response(item)) |
| 218 |
|
| 219 |
return results |
| 220 |
|
| 221 |
def format_response(self, item): |
| 222 |
d = item.as_dict() |
| 223 |
d = { |
| 224 |
'resource_group': self.parse_resource_to_dict(d.get('id')).get('resource_group'), |
| 225 |
'lab_name': self.parse_resource_to_dict(d.get('id')).get('name'), |
| 226 |
'artifact_source_name': self.parse_resource_to_dict(d.get('id')).get('child_name_1'), |
| 227 |
'id': d.get('id'), |
| 228 |
'description': d.get('description'), |
| 229 |
'file_path': d.get('file_path'), |
| 230 |
'name': d.get('name'), |
| 231 |
'parameters': d.get('parameters'), |
| 232 |
'publisher': d.get('publisher'), |
| 233 |
'target_os_type': d.get('target_os_type'), |
| 234 |
'title': d.get('title') |
| 235 |
} |
| 236 |
return d |
| 237 |
|
| 238 |
|
| 239 |
def main(): |
| 240 |
AzureRMArtifactInfo() |
| 241 |
|
| 242 |
|
| 243 |
if __name__ == '__main__': |
| 244 |
main() |