Skip to content

Commit

Permalink
VirtualMachineSize facts module (ansible-collections#605)
Browse files Browse the repository at this point in the history
* Add azure_rm_virtualmachinesize_info module

* Add integration tests for virtualmachinesize_info

* Fix visual indentation in azure_rm_virtualmachinesize_info

* azure_rm_virtualmachinesize_info add option types in docstring

Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>

* azure_rm_virtualmachinesize_info fix whitespaces

Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>

Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com>
Co-authored-by: xuzhang3 <57888764+xuzhang3@users.noreply.github.com>
  • Loading branch information
3 people committed Aug 31, 2021
1 parent e1148d7 commit c5eaaa6
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 0 deletions.
158 changes: 158 additions & 0 deletions plugins/modules/azure_rm_virtualmachinesize_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#!/usr/bin/python
#
# Copyright (c) 2021
# Maxence Ardouin <max@23.tf>
#
# 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


DOCUMENTATION = '''
---
module: azure_rm_virtualmachinesize_info
version_added: "1.8.0"
short_description: Get facts for virtual machine sizes
description:
- Get available virtual machine size profiles for a location
options:
location:
description:
- Location for which to list the available virtual machine size profiles
required: true
type: str
name:
description:
- Name of a size to get information about
type: str
extends_documentation_fragment:
- azure.azcollection.azure
author:
- Maxence Ardouin (@nbr23)
'''

EXAMPLES = '''
- name: Get all virtual machine size info in eastus
azure_rm_virtualmachinesize_info:
location: eastus
- name: Get virtual machine size info for eastus for Standard_A1_v2
azure_rm_virtualmachinesize_info:
location: eastus
name: Standard_A1_v2
'''

RETURN = '''
sizes:
description:
- List of virtual machine size profiles available for the location.
returned: always
type: complex
contains:
name:
description:
- The name of the virtual machine size
type: str
sample: Standard_A1_v2
memoryInMB:
description:
- The amount of memory, in MB, supported by the virtual machine size
type: int
sample: 2048
numberOfCores:
description:
- The number of cores supported by the virtual machine size
type: int
sample: 1
maxDataDiskCount:
description:
- The maximum number of data disks that can be attached to the virtual machine size
type: int
sample: 2
osDiskSizeInMB:
description:
- The OS disk size, in MB, allowed by the virtual machine size
type: int
sample: 1047552
resourceDiskSizeInMB:
description:
- The resource disk size, in MB, allowed by the virtual machine size
type: int
sample: 10240
'''

try:
from msrestazure.azure_exceptions import CloudError
except Exception:
# This is handled in azure_rm_common
pass

from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase

AZURE_OBJECT_CLASS = 'VirtualMachineSize'

AZURE_ENUM_MODULES = ['azure.mgmt.compute.models']


class AzureRMVirtualMachineSizeInfo(AzureRMModuleBase):

def __init__(self):

self.module_arg_spec = dict(
location=dict(type='str', required=True),
name=dict(type='str')
)

self.results = dict(
changed=False,
sizes=[]
)

self.location = None
self.name = None

super(AzureRMVirtualMachineSizeInfo, self).__init__(self.module_arg_spec,
supports_check_mode=True,
supports_tags=False,
facts_module=True)

def exec_module(self, **kwargs):
for key in self.module_arg_spec:
setattr(self, key, kwargs[key])

self.results['sizes'] = self.list_items_by_location()
return self.results

def list_items_by_location(self):
self.log('List items by location')
try:
items = self.compute_client.virtual_machine_sizes.list(location=self.location)
except CloudError as exc:
self.fail("Failed to list items - {0}".format(str(exc)))
return [self.serialize_size(item) for item in items if self.name is None or self.name == item.name]

def serialize_size(self, size):
'''
Convert a VirtualMachineSize object to dict.
:param size: VirtualMachineSize object
:return: dict
'''

return self.serialize_obj(size, AZURE_OBJECT_CLASS, enum_modules=AZURE_ENUM_MODULES)


def main():
AzureRMVirtualMachineSizeInfo()


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions pr-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ parameters:
- "azure_rm_virtualmachineextension"
- "azure_rm_virtualmachineimage_info"
- "azure_rm_virtualmachinescaleset"
- "azure_rm_virtualmachinesize_info"
- "azure_rm_virtualnetwork"
- "azure_rm_virtualnetworkgateway"
- "azure_rm_virtualnetworkpeering"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cloud/azure
shippable/azure/group3
destructive
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dependencies:
- setup_azure
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
- name: set location
set_fact:
location: eastus

- name: Get specific size information for a specific location
azure_rm_virtualmachinesize_info:
location: "{{ location }}"
name: Standard_A1_v2
register: output

- assert:
that: output['sizes'] | length == 1

- name: Get available sizes for a specific location
azure_rm_virtualmachinesize_info:
location: "{{ location }}"
register: output

- assert:
that: output['sizes'] | length > 0

0 comments on commit c5eaaa6

Please sign in to comment.