-
Notifications
You must be signed in to change notification settings - Fork 23.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding missing Azure facts modules #54838
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
220 changes: 220 additions & 0 deletions
220
lib/ansible/modules/cloud/azure/azure_rm_devtestlabcustomimage_facts.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
#!/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_devtestlabcustomimage_facts | ||
version_added: "2.8" | ||
short_description: Get Azure DevTest Lab Custom Image facts. | ||
description: | ||
- Get facts of Azure Azure DevTest Lab Custom Image. | ||
|
||
options: | ||
resource_group: | ||
description: | ||
- The name of the resource group. | ||
required: True | ||
lab_name: | ||
description: | ||
- The name of the lab. | ||
required: True | ||
name: | ||
description: | ||
- The name of the custom image. | ||
tags: | ||
description: | ||
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. | ||
|
||
extends_documentation_fragment: | ||
- azure | ||
|
||
author: | ||
- "Zim Kalinowski (@zikalino)" | ||
|
||
''' | ||
|
||
EXAMPLES = ''' | ||
- name: Get instance of Custom Image | ||
azure_rm_devtestlabcustomimage_facts: | ||
resource_group: myResourceGroup | ||
lab_name: myLab | ||
name: myImage | ||
|
||
- name: List instances of Custom Image in the lab | ||
azure_rm_devtestlabcustomimage_facts: | ||
resource_group: myResourceGroup | ||
lab_name: myLab | ||
name: myImage | ||
''' | ||
|
||
RETURN = ''' | ||
custom_images: | ||
description: A list of dictionaries containing facts for Custom Image. | ||
returned: always | ||
type: complex | ||
contains: | ||
id: | ||
description: | ||
- The identifier of the artifact source. | ||
returned: always | ||
type: str | ||
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.DevTestLab/labs/myLab/cu | ||
stomimages/myImage" | ||
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 | ||
name: | ||
description: | ||
- The name of the image. | ||
returned: always | ||
type: str | ||
sample: myImage | ||
managed_shapshot_id: | ||
description: | ||
- Managed snapshot id. | ||
returned: always | ||
type: str | ||
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/microsoft.compute/snapshots/myImage" | ||
source_vm_id: | ||
description: | ||
- Source VM id. | ||
returned: always | ||
type: str | ||
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx//resourcegroups/myResourceGroup/providers/microsoft.devtestlab/labs/myLab/v | ||
irtualmachines/myLabVm" | ||
tags: | ||
description: | ||
- The tags of the resource. | ||
returned: always | ||
type: complex | ||
sample: "{ 'MyTag': 'MyValue' }" | ||
''' | ||
|
||
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 AzureRMDtlCustomImageFacts(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 | ||
), | ||
name=dict( | ||
type='str', | ||
required=True | ||
), | ||
tags=dict( | ||
type='list' | ||
) | ||
) | ||
# 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.name = None | ||
self.tags = None | ||
super(AzureRMDtlCustomImageFacts, 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['custom_images'] = self.get() | ||
else: | ||
self.results['custom_images'] = self.list() | ||
return self.results | ||
|
||
def get(self): | ||
response = None | ||
results = [] | ||
try: | ||
response = self.mgmt_client.custom_images.get(resource_group_name=self.resource_group, | ||
lab_name=self.lab_name, | ||
name=self.name) | ||
self.log("Response : {0}".format(response)) | ||
except CloudError as e: | ||
self.log('Could not get facts for Custom Image.') | ||
|
||
if response and self.has_tags(response.tags, self.tags): | ||
results.append(self.format_response(response)) | ||
|
||
return results | ||
|
||
def list(self): | ||
response = None | ||
results = [] | ||
try: | ||
response = self.mgmt_client.custom_images.list(resource_group_name=self.resource_group, | ||
lab_name=self.lab_name) | ||
self.log("Response : {0}".format(response)) | ||
except CloudError as e: | ||
self.log('Could not get facts for Custom Image.') | ||
|
||
if response is not None: | ||
for item in response: | ||
if self.has_tags(item.tags, self.tags): | ||
results.append(self.format_response(item)) | ||
|
||
return results | ||
|
||
def format_response(self, item): | ||
d = item.as_dict() | ||
d = { | ||
'resource_group': self.resource_group, | ||
'lab_name': self.lab_name, | ||
'name': d.get('name'), | ||
'id': d.get('id'), | ||
'managed_snapshot_id': d.get('managed_snapshot_id'), | ||
'source_vm_id': d.get('vm', {}).get('source_vm_id'), | ||
'tags': d.get('tags') | ||
} | ||
return d | ||
|
||
|
||
def main(): | ||
AzureRMDtlCustomImageFacts() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it has tags, can facts be filtered by tags?