Refactored compute_flavor_info module

Change-Id: Ic598a60c2dd6fb465965fa8beee0ea973385bbcf
This commit is contained in:
Jakob Meng 2023-01-13 09:04:50 +01:00
parent e34f259566
commit 0c75f19e4c
2 changed files with 123 additions and 168 deletions

View File

@ -238,7 +238,7 @@
register: flavor_info register: flavor_info
- assert: - assert:
that: item in flavor_info.openstack_flavors[0] that: item in flavor_info.flavors[0]
loop: "{{ expected_fields }}" loop: "{{ expected_fields }}"
- name: List flavors with filter - name: List flavors with filter
@ -250,5 +250,5 @@
- name: Check output of list flavors with filter - name: Check output of list flavors with filter
assert: assert:
that: that:
- flavor.openstack_flavors | length == 1 - flavor.flavors | length == 1
- flavor.openstack_flavors.0.name == "m1.tiny" - flavor.flavors.0.name == "m1.tiny"

View File

@ -4,185 +4,155 @@
# Copyright (c) 2015 IBM # Copyright (c) 2015 IBM
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
DOCUMENTATION = ''' DOCUMENTATION = r'''
--- ---
module: compute_flavor_info module: compute_flavor_info
short_description: Retrieve information about one or more flavors short_description: Fetch compute flavors from OpenStack cloud
author: OpenStack Ansible SIG author: OpenStack Ansible SIG
description: description:
- Retrieve information about available OpenStack instance flavors. By default, - Fetch OpenStack compute flavors.
information about ALL flavors are retrieved. Filters can be applied to get
information for only matching flavors. For example, you can filter on the
amount of RAM available to the flavor, or the number of virtual CPUs
available to the flavor, or both. When specifying multiple filters,
*ALL* filters must match on a flavor before that flavor is returned as
a fact.
notes:
- The result contains a list of unsorted flavors.
options: options:
name: ephemeral:
description: description:
- A flavor name. Cannot be used with I(ram) or I(vcpus) or I(ephemeral). - Filter flavors based on the amount of ephemeral storage.
type: str - I(ephemeral) supports same format as I(ram) option.
ram: type: str
description: limit:
- "A string used for filtering flavors based on the amount of RAM description:
- Limits number of flavors to I(limit) results.
- By default all matching flavors are returned.
type: int
name:
description:
- Flavor name.
type: str
ram:
description:
- "A string used for filtering flavors based on the amount of RAM
(in MB) desired. This string accepts the following special values: (in MB) desired. This string accepts the following special values:
'MIN' (return flavors with the minimum amount of RAM), and 'MAX' 'MIN' (return flavors with the minimum amount of RAM), and 'MAX'
(return flavors with the maximum amount of RAM)." (return flavors with the maximum amount of RAM)."
- "A specific amount of RAM may also be specified. Any flavors with this
- "A specific amount of RAM may also be specified. Any flavors with this
exact amount of RAM will be returned." exact amount of RAM will be returned."
- "A range of acceptable RAM may be given using a special syntax. Simply
- "A range of acceptable RAM may be given using a special syntax. Simply
prefix the amount of RAM with one of these acceptable range values: prefix the amount of RAM with one of these acceptable range values:
'<', '>', '<=', '>='. These values represent less than, greater than, '<', '>', '<=', '>='. These values represent less than, greater than,
less than or equal to, and greater than or equal to, respectively." less than or equal to, and greater than or equal to, respectively."
type: str type: str
vcpus: vcpus:
description: description:
- A string used for filtering flavors based on the number of virtual - Filter flavors based on the number of virtual CPUs.
CPUs desired. Format is the same as the I(ram) parameter. - I(vcpus) supports same format as I(ram) option.
type: str type: str
limit:
description:
- Limits the number of flavors returned. All matching flavors are
returned by default.
type: int
ephemeral:
description:
- A string used for filtering flavors based on the amount of ephemeral
storage. Format is the same as the I(ram) parameter
type: str
requirements: requirements:
- "python >= 3.6" - "python >= 3.6"
- "openstacksdk" - "openstacksdk"
extends_documentation_fragment: extends_documentation_fragment:
- openstack.cloud.openstack - openstack.cloud.openstack
''' '''
EXAMPLES = ''' EXAMPLES = r'''
# Gather information about all available flavors - name: Gather information about all available flavors
- openstack.cloud.compute_flavor_info: openstack.cloud.compute_flavor_info:
cloud: mycloud cloud: mycloud
register: result
- debug: - name: Gather information for the flavor named "xlarge-flavor"
msg: "{{ result.openstack_flavors }}" openstack.cloud.compute_flavor_info:
# Gather information for the flavor named "xlarge-flavor"
- openstack.cloud.compute_flavor_info:
cloud: mycloud cloud: mycloud
name: "xlarge-flavor" name: "xlarge-flavor"
# Get all flavors that have exactly 512 MB of RAM. - name: Get all flavors with 512 MB of RAM
- openstack.cloud.compute_flavor_info: openstack.cloud.compute_flavor_info:
cloud: mycloud cloud: mycloud
ram: "512" ram: "512"
# Get all flavors that have 1024 MB or more of RAM. - name: Get all flavors with >= 1024 MB RAM
- openstack.cloud.compute_flavor_info: openstack.cloud.compute_flavor_info:
cloud: mycloud cloud: mycloud
ram: ">=1024" ram: ">=1024"
# Get a single flavor that has the minimum amount of RAM. Using the 'limit' - name: Get a single flavor with minimum amount of RAM
# option will guarantee only a single flavor is returned. openstack.cloud.compute_flavor_info:
- openstack.cloud.compute_flavor_info:
cloud: mycloud cloud: mycloud
ram: "MIN" ram: "MIN"
limit: 1 limit: 1
# Get all flavors with 1024 MB of RAM or more, AND exactly 2 virtual CPUs. - name: Get all flavors with >=1024 MB RAM and 2 vCPUs
- openstack.cloud.compute_flavor_info: openstack.cloud.compute_flavor_info:
cloud: mycloud cloud: mycloud
ram: ">=1024" ram: ">=1024"
vcpus: "2" vcpus: "2"
# Get all flavors with 1024 MB of RAM or more, exactly 2 virtual CPUs, and - name: Get flavors with >= 1024 MB RAM 2 vCPUs and < 30gb ephemeral storage
# less than 30gb of ephemeral storage. openstack.cloud.compute_flavor_info:
- openstack.cloud.compute_flavor_info:
cloud: mycloud cloud: mycloud
ram: ">=1024" ram: ">=1024"
vcpus: "2" vcpus: "2"
ephemeral: "<30" ephemeral: "<30"
''' '''
RETURN = r'''
RETURN = ''' flavors:
openstack_flavors: description: List of dictionaries describing the compute flavors.
description: Dictionary describing the flavors. returned: always
returned: On success. type: list
type: complex elements: dict
contains: contains:
id: description:
description: Flavor ID. description: Description of the flavor
returned: success type: str
type: str sample: "Small flavor"
sample: "515256b8-7027-4d73-aa54-4e30a4a4a339" disk:
name: description: Size of local disk, in GB.
description: Flavor name. type: int
returned: success sample: 10
type: str ephemeral:
sample: "tiny" description: Ephemeral space size, in GB.
original_name: type: int
description: Original flavor name sample: 10
returned: success extra_specs:
type: str description: Optional parameters to configure different flavors
sample: "tiny" options.
description: type: dict
description: Description of the flavor sample: "{'hw_rng:allowed': True}"
returned: success id:
type: str description: Flavor ID.
sample: "Small flavor" type: str
is_disabled: sample: "515256b8-7027-4d73-aa54-4e30a4a4a339"
description: Wether the flavor is enabled or not is_disabled:
returned: success description: Wether the flavor is enabled or not
type: bool type: bool
sample: False sample: False
rxtx_factor: is_public:
description: Factor to be multiplied by the rxtx_base property of description: Make flavor accessible to the public.
the network it is attached to in order to have a type: bool
different bandwidth cap. sample: true
returned: success name:
type: float description: Flavor name.
sample: 1.0 type: str
extra_specs: sample: "tiny"
description: Optional parameters to configure different flavors original_name:
options. description: Original flavor name
returned: success type: str
type: dict sample: "tiny"
sample: "{'hw_rng:allowed': True}" ram:
disk: description: Amount of memory, in MB.
description: Size of local disk, in GB. type: int
returned: success sample: 1024
type: int rxtx_factor:
sample: 10 description: Factor to be multiplied by the rxtx_base property of
ephemeral: the network it is attached to in order to have a
description: Ephemeral space size, in GB. different bandwidth cap.
returned: success type: float
type: int sample: 1.0
sample: 10 swap:
ram: description: Swap space size, in MB.
description: Amount of memory, in MB. type: int
returned: success sample: 100
type: int vcpus:
sample: 1024 description: Number of virtual CPUs.
swap: type: int
description: Swap space size, in MB. sample: 2
returned: success
type: int
sample: 100
vcpus:
description: Number of virtual CPUs.
returned: success
type: int
sample: 2
is_public:
description: Make flavor accessible to the public.
returned: success
type: bool
sample: true
''' '''
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
@ -190,54 +160,39 @@ from ansible_collections.openstack.cloud.plugins.module_utils.openstack import O
class ComputeFlavorInfoModule(OpenStackModule): class ComputeFlavorInfoModule(OpenStackModule):
argument_spec = dict( argument_spec = dict(
ephemeral=dict(),
limit=dict(type='int'),
name=dict(), name=dict(),
ram=dict(), ram=dict(),
vcpus=dict(), vcpus=dict(),
limit=dict(type='int'),
ephemeral=dict(),
) )
module_kwargs = dict( module_kwargs = dict(
mutually_exclusive=[
['name', 'ram'],
['name', 'vcpus'],
['name', 'ephemeral']
],
supports_check_mode=True supports_check_mode=True
) )
def run(self): def run(self):
name = self.params['name'] name = self.params['name']
vcpus = self.params['vcpus']
ram = self.params['ram']
ephemeral = self.params['ephemeral']
limit = self.params['limit']
filters = {} filters = dict((k, self.params[k])
if vcpus: for k in ['ephemeral', 'ram', 'vcpus']
filters['vcpus'] = vcpus if self.params[k] is not None)
if ram:
filters['ram'] = ram
if ephemeral:
filters['ephemeral'] = ephemeral
if name: if name:
# extra_specs are exposed in the flavor representation since Rocky, so we do not
# need get_extra_specs=True which is not available in OpenStack SDK 0.36 (Train)
# Ref.: https://docs.openstack.org/nova/latest/reference/api-microversion-history.html
flavor = self.conn.compute.find_flavor(name) flavor = self.conn.compute.find_flavor(name)
flavors = [flavor] if flavor else [] flavors = [flavor] if flavor else []
else: else:
flavors = list(self.conn.compute.flavors()) flavors = list(self.conn.compute.flavors())
if filters:
flavors = self.conn.range_search(flavors, filters)
if filters:
flavors = self.conn.range_search(flavors, filters)
limit = self.params['limit']
if limit is not None: if limit is not None:
flavors = flavors[:limit] flavors = flavors[:limit]
# Transform entries to dict self.exit_json(changed=False,
flavors = [flavor.to_dict(computed=False) for flavor in flavors] flavors=[f.to_dict(computed=False) for f in flavors])
self.exit_json(changed=False, openstack_flavors=flavors)
def main(): def main():