Adding DevTest Lab Artifact facts (#53195)

This commit is contained in:
Zim Kalinowski 2019-03-04 12:53:44 +08:00 committed by Yunge Zhu
parent d96543880b
commit c0c6eebed6
2 changed files with 290 additions and 0 deletions

View file

@ -0,0 +1,245 @@
#!/usr/bin/python
#
# Copyright (c) 2019 Zim Kalinowski, (@zikalino)
#
# 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
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = '''
---
module: azure_rm_devtestlabartifact_facts
version_added: "2.8"
short_description: Get Azure DevTest Lab Artifact facts.
description:
- Get facts of Azure DevTest Lab Artifact.
options:
resource_group:
description:
- The name of the resource group.
required: True
lab_name:
description:
- The name of the lab.
required: True
artifact_source_name:
description:
- The name of the artifact source.
required: True
name:
description:
- The name of the artifact.
extends_documentation_fragment:
- azure
author:
- "Zim Kalinowski (@zikalino)"
'''
EXAMPLES = '''
- name: Get instance of DevTest Lab Artifact
azure_rm_devtestlabartifact_facts:
resource_group: myResourceGroup
lab_name: myLab
artifact_source_name: myArtifactSource
name: myArtifact
'''
RETURN = '''
artifacts:
description: A list of dictionaries containing facts for DevTest Lab Artifact.
returned: always
type: complex
contains:
id:
description:
- The identifier of the artifact.
returned: always
type: str
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.DevTestLab/labs/myLab/ar
tifactSources/myArtifactSource/artifacts/myArtifact"
resource_group:
description:
- Name of the resource group.
returned: always
type: str
sample: myResourceGroup
lab_name:
description:
- Name of the lab.
returned: always
type: str
sample: myLab
artifact_source_name:
description:
- The name of the artifact source.
returned: always
type: str
sample: myArtifactSource
name:
description:
- The name of the artifact.
returned: always
type: str
sample: myArtifact
description:
description:
- Description of the artifact.
returned: always
type: str
sample: Installs My Software
file_path:
description:
- "Artifact's path in the repo."
returned: always
type: str
sample: Artifacts/myArtifact
publisher:
description:
- Publisher name.
returned: always
type: str
sample: MyPublisher
target_os_type:
description:
- Target OS type.
returned: always
type: str
sample: Linux
title:
description:
- Title of the artifact.
returned: always
type: str
sample: My Software
parameters:
description:
- A dictionary containing parameters definition of the artifact.
returned: always
type: complex
sample: {}
'''
from ansible.module_utils.azure_rm_common import AzureRMModuleBase
try:
from msrestazure.azure_exceptions import CloudError
from azure.mgmt.devtestlabs import DevTestLabsClient
from msrest.serialization import Model
except ImportError:
# This is handled in azure_rm_common
pass
class AzureRMArtifactFacts(AzureRMModuleBase):
def __init__(self):
# define user inputs into argument
self.module_arg_spec = dict(
resource_group=dict(
type='str',
required=True
),
lab_name=dict(
type='str',
required=True
),
artifact_source_name=dict(
type='str',
required=True
),
name=dict(
type='str'
)
)
# store the results of the module operation
self.results = dict(
changed=False
)
self.mgmt_client = None
self.resource_group = None
self.lab_name = None
self.artifact_source_name = None
self.name = None
super(AzureRMArtifactFacts, self).__init__(self.module_arg_spec, supports_tags=False)
def exec_module(self, **kwargs):
for key in self.module_arg_spec:
setattr(self, key, kwargs[key])
self.mgmt_client = self.get_mgmt_svc_client(DevTestLabsClient,
base_url=self._cloud_environment.endpoints.resource_manager)
if self.name:
self.results['artifacts'] = self.get()
else:
self.results['artifacts'] = self.list()
return self.results
def get(self):
response = None
results = []
try:
response = self.mgmt_client.artifacts.get(resource_group_name=self.resource_group,
lab_name=self.lab_name,
artifact_source_name=self.artifact_source_name,
name=self.name)
self.log("Response : {0}".format(response))
except CloudError as e:
self.log('Could not get facts for Artifact.')
if response:
results.append(self.format_response(response))
return results
def list(self):
response = None
results = []
try:
response = self.mgmt_client.artifacts.list(resource_group_name=self.resource_group,
lab_name=self.lab_name,
artifact_source_name=self.artifact_source_name)
self.log("Response : {0}".format(response))
except CloudError as e:
self.log('Could not get facts for Artifact.')
if response is not None:
for item in response:
results.append(self.format_response(item))
return results
def format_response(self, item):
d = item.as_dict()
d = {
'resource_group': self.parse_resource_to_dict(d.get('id')).get('resource_group'),
'lab_name': self.parse_resource_to_dict(d.get('id')).get('name'),
'artifact_source_name': self.parse_resource_to_dict(d.get('id')).get('child_name_1'),
'id': d.get('id'),
'description': d.get('description'),
'file_path': d.get('file_path'),
'name': d.get('name'),
'parameters': d.get('parameters'),
'publisher': d.get('publisher'),
'target_os_type': d.get('target_os_type'),
'title': d.get('title')
}
return d
def main():
AzureRMArtifactFacts()
if __name__ == '__main__':
main()

View file

@ -439,6 +439,51 @@
- output.changed - output.changed
when: "github_token | length > 0" when: "github_token | length > 0"
- name: Get Artifact facts
azure_rm_devtestlabartifact_facts:
resource_group: "{{ resource_group }}"
lab_name: "{{ lab_name }}"
artifact_source_name: "public repo"
register: output
- name: Assert that facts are returned
assert:
that:
- output.changed == False
- output.artifacts[0]['id'] != None
- output.artifacts[0]['resource_group'] != None
- output.artifacts[0]['lab_name'] != None
- output.artifacts[0]['artifact_source_name'] != None
- output.artifacts[0]['name'] != None
- output.artifacts[0]['description'] != None
- output.artifacts[0]['file_path'] != None
- output.artifacts[0]['publisher'] != None
- output.artifacts[0]['target_os_type'] != None
- output.artifacts[0]['publisher'] != None
- "output.artifacts | length > 1"
- name: Get Artifact facts
azure_rm_devtestlabartifact_facts:
resource_group: "{{ resource_group }}"
lab_name: "{{ lab_name }}"
artifact_source_name: "public repo"
name: windows-webdeploy
register: output
- name: Assert that facts are returned
assert:
that:
- output.changed == False
- output.artifacts[0]['id'] != None
- output.artifacts[0]['resource_group'] != None
- output.artifacts[0]['lab_name'] != None
- output.artifacts[0]['artifact_source_name'] != None
- output.artifacts[0]['name'] != None
- output.artifacts[0]['description'] != None
- output.artifacts[0]['file_path'] != None
- output.artifacts[0]['publisher'] != None
- output.artifacts[0]['target_os_type'] != None
- output.artifacts[0]['publisher'] != None
- "output.artifacts | length == 1"
- name: Delete instance of Lab -- check mode - name: Delete instance of Lab -- check mode
azure_rm_devtestlab: azure_rm_devtestlab:
resource_group: "{{ resource_group }}" resource_group: "{{ resource_group }}"