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

migrate cluster info module from community #80

Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions changelogs/fragments/80-migrate-cluster-info-module.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- cluster_info - Migrate cluster_info module from the community.vmware collection to here
1 change: 1 addition & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ action_groups:
- cluster_dpm
- cluster_drs
- cluster_drs_recommendations
- cluster_info
- cluster_vcls
- content_template
- folder_template_from_vm
Expand Down
135 changes: 132 additions & 3 deletions plugins/module_utils/_vmware_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@

PYVMOMI_IMP_ERR = None
try:
from pyVmomi import vim, VmomiSupport
from pyVmomi import vim, VmomiSupport, vmodl
except ImportError:
pass

from ansible.module_utils._text import to_text
from ansible.module_utils.six import integer_types, string_types, iteritems
import ansible.module_utils.common._collections_compat as collections_compat
from ansible_collections.vmware.vmware.plugins.module_utils._vmware_folder_paths import get_folder_path_of_vm
from ansible_collections.vmware.vmware.plugins.module_utils._vmware_folder_paths import get_folder_path_of_vsphere_object


class VmFacts():
Expand Down Expand Up @@ -167,7 +167,7 @@

def hw_folder_facts(self):
try:
hw_folder = get_folder_path_of_vm(self.vm)
hw_folder = get_folder_path_of_vsphere_object(self.vm)

Check warning on line 170 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L170

Added line #L170 was not covered by tests
except Exception:
hw_folder = None

Expand Down Expand Up @@ -281,6 +281,135 @@
}


class ClusterFacts():
DPM_DEFAULT_RATE = 3
DRS_DEFAULT_RATE = 3

def __init__(self, cluster):
self.cluster = cluster

Check warning on line 289 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L289

Added line #L289 was not covered by tests

@staticmethod
def reverse_drs_or_dpm_rate(input_rate):
return 6 - int(input_rate)

Check warning on line 293 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L293

Added line #L293 was not covered by tests

def all_facts(self):
return {

Check warning on line 296 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L296

Added line #L296 was not covered by tests
**self.host_facts(),
**self.ha_facts(),
**self.identifier_facts(),
**self.drs_facts(),
**self.vsan_facts(),
**self.resource_usage_facts(),
**self.dpm_facts()
}

def identifier_facts(self):
return {

Check warning on line 307 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L307

Added line #L307 was not covered by tests
"moid": self.cluster._moId,
"datacenter": self.cluster.parent.parent.name
}

def host_facts(self):
hosts = []

Check warning on line 313 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L313

Added line #L313 was not covered by tests
for host in self.cluster.host:
hosts.append({

Check warning on line 315 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L315

Added line #L315 was not covered by tests
'name': host.name,
'folder': get_folder_path_of_vsphere_object(host),
})
return {"hosts": hosts}

Check warning on line 319 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L319

Added line #L319 was not covered by tests

def ha_facts(self):
fact_keys = [

Check warning on line 322 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L322

Added line #L322 was not covered by tests
"ha_enabled", "ha_failover_level", "ha_vm_monitoring", "ha_admission_control_enabled",
"ha_restart_priority", "ha_vm_tools_monitoring", "ha_vm_min_up_time", "ha_vm_max_failures",
"ha_vm_max_failure_window", "ha_vm_failure_interval"
]
ha_facts = dict.fromkeys(fact_keys, None)
das_config = self.cluster.configurationEx.dasConfig

Check warning on line 328 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L327-L328

Added lines #L327 - L328 were not covered by tests
if not das_config:
ha_facts['ha_enabled'] = False
return ha_facts

Check warning on line 331 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L330-L331

Added lines #L330 - L331 were not covered by tests

ha_facts["ha_enabled"] = das_config.enabled
ha_facts["ha_vm_monitoring"] = das_config.vmMonitoring
ha_facts["ha_host_monitoring"] = das_config.hostMonitoring
ha_facts["ha_admission_control_enabled"] = das_config.admissionControlEnabled

Check warning on line 336 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L333-L336

Added lines #L333 - L336 were not covered by tests

if getattr(das_config, "admissionControlPolicy"):
ha_facts['ha_failover_level'] = das_config.admissionControlPolicy.failoverLevel

Check warning on line 339 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L339

Added line #L339 was not covered by tests
if getattr(das_config, "defaultVmSettings"):
ha_facts['ha_restart_priority'] = das_config.defaultVmSettings.restartPriority
ha_facts['ha_vm_tools_monitoring'] = das_config.defaultVmSettings.vmToolsMonitoringSettings.vmMonitoring
ha_facts['ha_vm_min_up_time'] = das_config.defaultVmSettings.vmToolsMonitoringSettings.minUpTime
ha_facts['ha_vm_max_failures'] = das_config.defaultVmSettings.vmToolsMonitoringSettings.maxFailures
ha_facts['ha_vm_max_failure_window'] = das_config.defaultVmSettings.vmToolsMonitoringSettings.maxFailureWindow
ha_facts['ha_vm_failure_interval'] = das_config.defaultVmSettings.vmToolsMonitoringSettings.failureInterval

Check warning on line 346 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L341-L346

Added lines #L341 - L346 were not covered by tests

return ha_facts

Check warning on line 348 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L348

Added line #L348 was not covered by tests

def dpm_facts(self):
fact_keys = ["dpm_enabled", "dpm_default_dpm_behavior", "dpm_host_power_action_rate"]
output_facts = dict.fromkeys(fact_keys, None)
dpm_config = self.cluster.configurationEx.dpmConfigInfo

Check warning on line 353 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L351-L353

Added lines #L351 - L353 were not covered by tests
if not dpm_config:
output_facts["dpm_enabled"] = False
return output_facts

Check warning on line 356 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L355-L356

Added lines #L355 - L356 were not covered by tests

output_facts["dpm_enabled"] = dpm_config.enabled
output_facts["dpm_default_dpm_behavior"] = getattr(dpm_config, "defaultDpmBehavior", None)

Check warning on line 359 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L358-L359

Added lines #L358 - L359 were not covered by tests
# dpm host power rate is reversed by the vsphere API. so a 1 in the API is really a 5 in the UI
try:
output_facts["dpm_host_power_action_rate"] = ClusterFacts.reverse_drs_or_dpm_rate(dpm_config.hostPowerActionRate)
except (TypeError, AttributeError):
output_facts["dpm_host_power_action_rate"] = ClusterFacts.DPM_DEFAULT_RATE

Check warning on line 364 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L361-L364

Added lines #L361 - L364 were not covered by tests

return output_facts

Check warning on line 366 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L366

Added line #L366 was not covered by tests

def drs_facts(self):
fact_keys = ["drs_enabled", "drs_enable_vm_behavior_overrides", "drs_default_vm_behavior", "drs_vmotion_rate"]
output_facts = dict.fromkeys(fact_keys, None)
drs_config = self.cluster.configurationEx.drsConfig

Check warning on line 371 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L369-L371

Added lines #L369 - L371 were not covered by tests
if not drs_config:
output_facts["drs_enabled"] = False
return output_facts

Check warning on line 374 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L373-L374

Added lines #L373 - L374 were not covered by tests

output_facts["drs_enabled"] = drs_config.enabled
output_facts["drs_enable_vm_behavior_overrides"] = getattr(drs_config, "enableVmBehaviorOverrides", None)

Check warning on line 377 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L376-L377

Added lines #L376 - L377 were not covered by tests

# docs call one option 'automatic' but the API calls it 'automated'. So we adjust here to match docs
_drs_default_vm_behavior = getattr(drs_config, "defaultVmBehavior", None)
output_facts["drs_default_vm_behavior"] = 'automatic' if _drs_default_vm_behavior == 'automated' else _drs_default_vm_behavior

Check warning on line 381 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L380-L381

Added lines #L380 - L381 were not covered by tests

# drs vmotion rate is reversed by the vsphere API. so a 1 in the API is really a 5 in the UI
try:
output_facts["drs_vmotion_rate"] = ClusterFacts.reverse_drs_or_dpm_rate(drs_config.vmotionRate)
except (TypeError, AttributeError):
output_facts["drs_vmotion_rate"] = ClusterFacts.DRS_DEFAULT_RATE

Check warning on line 387 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L384-L387

Added lines #L384 - L387 were not covered by tests

return output_facts

Check warning on line 389 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L389

Added line #L389 was not covered by tests

def vsan_facts(self):
vsan_config = getattr(self.cluster.configurationEx, 'vsanConfigInfo', None)
vsan_facts = {

Check warning on line 393 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L392-L393

Added lines #L392 - L393 were not covered by tests
"vsan_enabled": False,
"vsan_auto_claim_storage": None
}
if vsan_config:
vsan_facts['vsan_enabled'] = vsan_config.enabled
vsan_facts['vsan_auto_claim_storage'] = vsan_config.defaultConfig.autoClaimStorage
return vsan_facts

Check warning on line 400 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L398-L400

Added lines #L398 - L400 were not covered by tests

def resource_usage_facts(self):
try:
resource_summary = vmware_obj_to_json(self.cluster.GetResourceUsage())
except vmodl.fault.MethodNotFound:
return {'resource_summary': {}}

Check warning on line 406 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L403-L406

Added lines #L403 - L406 were not covered by tests

if '_vimtype' in resource_summary:
del resource_summary['_vimtype']
return {'resource_summary': resource_summary}

Check warning on line 410 in plugins/module_utils/_vmware_facts.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_facts.py#L409-L410

Added lines #L409 - L410 were not covered by tests


def get_vm_prop_or_none(vm, attributes):
"""Safely get a property or return None"""
result = vm
Expand Down
11 changes: 5 additions & 6 deletions plugins/module_utils/_vmware_folder_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,16 @@
return __prepend_datacenter_and_folder_type(folder_path, datacenter_name, folder_type='datastore')


def get_folder_path_of_vm(vm):
def get_folder_path_of_vsphere_object(vsphere_obj):
"""
Find the path of virtual machine.
Find the path of an object in vsphere.
Args:
content: VMware content object
vm_name: virtual machine managed object
vsphere_obj: VMware content object

Returns: Folder of virtual machine if exists, else None
Returns: Folder of object if exists, else None

"""
_folder = vm.parent
_folder = vsphere_obj.parent

Check warning on line 84 in plugins/module_utils/_vmware_folder_paths.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_folder_paths.py#L84

Added line #L84 was not covered by tests
folder_path = [_folder.name]
while getattr(_folder, 'parent', None) is not None:
_folder = _folder.parent
Expand Down
14 changes: 13 additions & 1 deletion plugins/module_utils/_vmware_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,26 @@
"""
Get a list of tag objects attached to a virtual machine
Args:
vm_mid: the VM MOID to use to gather tags
vm_moid: the VM MOID to use to gather tags

Returns:
List of tag object associated with the given virtual machine
"""
dobj = DynamicID(type='VirtualMachine', id=vm_moid)
return self.get_tags_for_dynamic_id_obj(dobj=dobj)

def get_tags_by_cluster_moid(self, cluster_moid):
"""
Get a list of tag objects attached to a cluster
Args:
cluster_moid: the cluster MOID to use to gather tags

Returns:
List of tag object associated with the given cluster
"""
dobj = DynamicID(type='ClusterComputeResource', id=cluster_moid)
return self.get_tags_for_dynamic_id_obj(dobj=dobj)

Check warning on line 453 in plugins/module_utils/_vmware_rest_client.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/_vmware_rest_client.py#L452-L453

Added lines #L452 - L453 were not covered by tests

def format_tag_identity_as_dict(self, tag_obj):
"""
Takes a tag object and outputs a dictionary with identifying details about the tag,
Expand Down
7 changes: 5 additions & 2 deletions plugins/modules/cluster_dpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
TaskError,
RunningTaskMonitor
)
from ansible_collections.vmware.vmware.plugins.module_utils._vmware_facts import (
ClusterFacts
)

from ansible.module_utils._text import to_native

Expand Down Expand Up @@ -148,7 +151,7 @@
We present the scale seen in the docs/UI to the user and then adjust the value here to ensure
vCenter behaves as intended.
"""
return 6 - self.params['recommendation_priority_threshold']
return ClusterFacts.reverse_drs_or_dpm_rate(self.params['recommendation_priority_threshold'])

Check warning on line 154 in plugins/modules/cluster_dpm.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/cluster_dpm.py#L154

Added line #L154 was not covered by tests

def check_dpm_config_diff(self):
"""
Expand Down Expand Up @@ -214,7 +217,7 @@
choices=['automatic', 'manual'],
default='automatic'
),
recommendation_priority_threshold=dict(type='int', choices=[1, 2, 3, 4, 5], default=3)
recommendation_priority_threshold=dict(type='int', choices=[1, 2, 3, 4, 5], default=ClusterFacts.DPM_DEFAULT_RATE)
)
},
supports_check_mode=True,
Expand Down
8 changes: 5 additions & 3 deletions plugins/modules/cluster_drs.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@
TaskError,
RunningTaskMonitor
)

from ansible_collections.vmware.vmware.plugins.module_utils._vmware_facts import (
ClusterFacts
)
from ansible_collections.vmware.vmware.plugins.module_utils._vmware_type_utils import (
diff_dict_and_vmodl_options_set
)
Expand Down Expand Up @@ -175,7 +177,7 @@
We present the scale seen in the docs/UI to the user and then adjust the value here to ensure
vCenter behaves as intended.
"""
return 6 - self.params.get('drs_vmotion_rate')
return ClusterFacts.reverse_drs_or_dpm_rate(self.params.get('drs_vmotion_rate'))

Check warning on line 180 in plugins/modules/cluster_drs.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/cluster_drs.py#L180

Added line #L180 was not covered by tests

def check_drs_config_diff(self):
"""
Expand Down Expand Up @@ -254,7 +256,7 @@
choices=['fullyAutomated', 'manual', 'partiallyAutomated'],
default='fullyAutomated'
),
drs_vmotion_rate=dict(type='int', choices=[1, 2, 3, 4, 5], default=3),
drs_vmotion_rate=dict(type='int', choices=[1, 2, 3, 4, 5], default=ClusterFacts.DRS_DEFAULT_RATE),
advanced_settings=dict(type='dict', required=False, default=dict()),
predictive_drs=dict(type='bool', required=False, default=False),
)
Expand Down
Loading
Loading