Skip to content
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

vmware_first_class_disk_info - Add a module to gather infos about first class disks #1996

Merged
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelogs/fragments/1996 - vmware_first_class_disk_info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
minor_changes:
- vmware_first_class_disk_info - Add a module to gather informations about first class disks.
(https://github.com/ansible-collections/community.vmware/pull/1996).
(https://github.com/ansible-collections/community.vmware/issues/1988).
24 changes: 24 additions & 0 deletions plugins/module_utils/vmware.py
Original file line number Diff line number Diff line change
@@ -1874,6 +1874,30 @@ def find_first_class_disk_by_name(self, disk_name, datastore_obj):

return None

def find_first_class_disks(self, datastore_obj):
"""
Get first-class disks managed object
Args:
datastore_obj: Managed object of datastore

Returns: First-class disks managed object if found else None

"""

disks = []

if self.is_vcenter():
for id in self.content.vStorageObjectManager.ListVStorageObject(datastore_obj):
disks.append(self.content.vStorageObjectManager.RetrieveVStorageObject(id, datastore_obj))

else:
for id in self.content.vStorageObjectManager.HostListVStorageObject(datastore_obj):
disks.append(self.content.vStorageObjectManager.HostRetrieveVStorageObject(id, datastore_obj))

if disks == []:
return None
else:
return disks
#
# Conversion to JSON
#
143 changes: 143 additions & 0 deletions plugins/modules/vmware_first_class_disk_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright: (c) 2024, Nina Loser <nina.loser@muenchen.de>
# Copyright: (c) 2021, Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import absolute_import, division, print_function
__metaclass__ = type

DOCUMENTATION = r'''
---
module: vmware_first_class_disk_info
short_description: Gather info about VMware vSphere First Class Disks
description:
- This module can be used to gather information about VMware vSphere First Class Disks.
author:
- Nina Loser (@nina2244)
options:
datacenter_name:
description: The name of the datacenter.
type: str
datastore_name:
description: Name of datastore or datastore cluster of the disk.
required: true
type: str
disk_name:
description: The name of the disk. If not set return all found.
type: str
extends_documentation_fragment: vmware.documentation
'''

EXAMPLES = r'''
- name: Gather info of 1GBDisk
community.vmware.vmware_first_class_disk_info:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datastore_name: '{{ datastore_name }}'
disk_name: '1GBDisk'
register: disk_info
delegate_to: localhost

- debug:
var: disk_info.first_class_disks

- name: Gather info of all first class disks
community.vmware.vmware_first_class_disk_info:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datastore_name: '{{ datastore_name }}'
register: disk_info
delegate_to: localhost

- debug:
var: disk_info.first_class_disks
'''

RETURN = r'''
first_class_disks:
description: list of dictionary of First-class disk and their information
returned: success
type: list
sample: [
{
"name": "1GBDisk",
"datastore_name": "DS0",
"size_mb": "1024",
"consumption_type": "disk",
"descriptor_version": 0,
"consumer_ids": []
}
]
'''

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, vmware_argument_spec


class FirstClassDiskInfo(PyVmomi):
def __init__(self, module):
super(FirstClassDiskInfo, self).__init__(module)
self.datacenter_name = self.params['datacenter_name']
self.datastore_name = self.params['datastore_name']
self.disk_name = self.params['disk_name']

def gather_first_class_disk_info(self):
self.datastore_obj = self.find_datastore_by_name(datastore_name=self.datastore_name, datacenter_name=self.datacenter_name)
if not self.datastore_obj:
self.module.fail_json(msg='Failed to find datastore %s.' % self.datastore_name)

if self.disk_name:
self.disk_obj = self.find_first_class_disk_by_name(self.disk_name, self.datastore_obj)
if not self.disk_obj:
self.module.fail_json(msg='Failed to find disk %s.' % self.disk_name)

self.disks = [self.disk_obj]

else:
self.disks = self.find_first_class_disks(self.datastore_obj)
if not self.disks:
self.module.fail_json(msg='Failed to find any disk first class disk.')

disk_infos = list()
for disk in self.disks:
disk_info = dict(
name=disk.config.name,
id=disk.config.id.id,
datastore_name=disk.config.backing.datastore.name,
size_mb=disk.config.capacityInMB,
consumption_type=disk.config.consumptionType,
descriptor_version=disk.config.descriptorVersion,
consumer_ids=list(id.id for id in disk.config.consumerId)
)
disk_infos.append(disk_info)

return disk_infos


def main():
argument_spec = vmware_argument_spec()
argument_spec.update(
dict(
datacenter_name=dict(type='str'),
datastore_name=dict(required=True, type='str'),
disk_name=dict(type='str')
)
)

module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True)

first_class_disk = FirstClassDiskInfo(module)

_first_class_disks = first_class_disk.gather_first_class_disk_info()

module.exit_json(changed=False, first_class_disks=_first_class_disks)


if __name__ == '__main__':
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cloud/vcenter
needs/target/prepare_vmware_tests
zuul/vmware/vcenter_1esxi
105 changes: 105 additions & 0 deletions tests/integration/targets/vmware_first_class_disk_info/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Test code for the vmware_first_class_disk_info module.
# Copyright: (c) 2021, Mario Lenz <m@riolenz.de>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

- import_role:
name: prepare_vmware_tests
vars:
setup_attach_host: true
setup_datastore: true

- name: Gather Info if no first-class disk exists
vmware_first_class_disk_info: &disk_info
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datastore_name: "{{ rw_datastore }}"
validate_certs: false
register: disk_info
delegate_to: localhost

- debug:
var: disk_info

- assert:
that:
- "disk_info['first_class_disks'] == []"

- name: Create first-class disk through vCenter
vmware_first_class_disk:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
disk_name: "firstclassdisk"
size: 1GB
datastore_name: "{{ rw_datastore }}"
state: present
validate_certs: false

- name: Gather Info of the generated first-class disk
vmware_first_class_disk_info:
<<: *disk_info
register: disk_info

- debug:
var: result_create_vcenter

- name: assert for first-class disk info
assert:
that:
- "(disk_info['first_class_disks'] | length) == 1"
- "disk_info['first_class_disks'][0]['name'] == 'firstclassdisk'"
- "disk_info['first_class_disks'][0]['datastore_name'] == rw_datastore"
- "disk_info['first_class_disks'][0]['size_mb'] == '1024'"
- "disk_info['first_class_disks'][0]['consumption_type'] is defined"
- "disk_info['first_class_disks'][0]['descriptor_version'] is defined"
- "disk_info['first_class_disks'][0]['consumer_ids'] is defined"

- name: Create a second first-class disk through vCenter
vmware_first_class_disk:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
disk_name: "secondclassdisk"
size: 1GB
datastore_name: "{{ rw_datastore }}"
state: present
validate_certs: false

- name: Gather Info of the two generated first-class disk
vmware_first_class_disk_info:
<<: *disk_info
register: disk_info

- debug:
var: result_create_vcenter

- name: assert for first-class disk info
assert:
that:
- "(disk_info['first_class_disks'] | length) == 2"

- name: Gather Info of first-class disk 'firstclassdisk'
vmware_first_class_disk_info:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datastore_name: "{{ rw_datastore }}"
disk_name: "firstclassdisk"
validate_certs: false
register: disk_info
delegate_to: localhost

- debug:
var: result_create_vcenter

- name: assert for first-class disk info
assert:
that:
- "(disk_info['first_class_disks'] | length) == 1"
- "disk_info['first_class_disks'][0]['name'] == 'firstclassdisk'"
- "disk_info['first_class_disks'][0]['datastore_name'] == rw_datastore"
- "disk_info['first_class_disks'][0]['size_mb'] == '1024'"
- "disk_info['first_class_disks'][0]['consumption_type'] is defined"
- "disk_info['first_class_disks'][0]['descriptor_version'] is defined"
- "disk_info['first_class_disks'][0]['consumer_ids'] is defined"
Loading