diff --git a/nutanix/ncp/plugins/module_utils/entity.py b/nutanix/ncp/plugins/module_utils/entity.py index f907b1e99..8ee8ae72d 100644 --- a/nutanix/ncp/plugins/module_utils/entity.py +++ b/nutanix/ncp/plugins/module_utils/entity.py @@ -215,8 +215,12 @@ def clean_spec(self, spec): for key, value in spec.copy().items(): if isinstance(value, str): if value.startswith('{{') and value.endswith('}}'): - value = getattr(self, value[2:-2], None) + value = value[2:-2] + + value = getattr(self, value, None) if value: + if key == 'guest_customization': + value = self.get_attr_spec(key, value) spec[key] = value else: spec.pop(key) diff --git a/nutanix/ncp/plugins/module_utils/prism/vms.py b/nutanix/ncp/plugins/module_utils/prism/vms.py index 7c01bbb34..0b327451d 100644 --- a/nutanix/ncp/plugins/module_utils/prism/vms.py +++ b/nutanix/ncp/plugins/module_utils/prism/vms.py @@ -3,7 +3,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type from copy import deepcopy - +from base64 import b64encode from .prism import Prism @@ -15,7 +15,8 @@ class VM(Prism): def get_attr_spec(self, param, param_spec, **kwargs): param_method_spec = { "disk_list": VMDisk, - "nic_list": VMNetwork + "nic_list": VMNetwork, + "guest_customization": GuestCustomizationSpec } if param in param_method_spec: @@ -207,7 +208,6 @@ def __get_subnet_ref(self, name, **kwargs): def _get_api_spec(self, param_spec, **kwargs): - _di_map = {} final_nic_list = [] for nic_param in param_spec: nic_final = self.get_default_spec() @@ -224,8 +224,7 @@ def _get_api_spec(self, param_spec, **kwargs): "uuid": v } elif k == "subnet_name" and not nic_param.get("subnet_uuid"): - nic_final["subnet_reference"] = self.__get_subnet_ref( - v, **kwargs) + nic_final["subnet_reference"] = self.__get_subnet_ref(v, **kwargs) elif k == "ip_endpoint_list" and bool(v): nic_final[k] = [{"ip": v[0]}] @@ -237,3 +236,45 @@ def _get_api_spec(self, param_spec, **kwargs): def __call__(self, param_spec, **kwargs): return self._get_api_spec(param_spec, **kwargs) + + +class GuestCustomizationSpec(VMSpec): + + @staticmethod + def get_default_spec(): + return deepcopy( + { + "sysprep": { + "install_type": "", + "unattend_xml": "", + "custom_key_values": {}, + }, + "cloud_init": { + "meta_data": "", + "user_data": "", + "custom_key_values": {}, + }, + "is_overridable": "" + } + ) + + def _get_api_spec(self, param_spec, **kwargs): + + gc_spec = self.get_default_spec() + script_file_path = param_spec["script_path"] + with open(script_file_path, "r") as f: + content = f.read() + content = b64encode(content) + type = param_spec["type"] + if type == "sysprep": + gc_spec[type]["unattend_xml"] = content + elif type == "cloud_init": + gc_spec[type]["user_data"] = content + gc_spec["is_overridable"] = param_spec.get("is_overridable") + + self.remove_null_references(gc_spec) + + return gc_spec + + def __call__(self, param_spec, **kwargs): + return self._get_api_spec(param_spec, **kwargs) diff --git a/nutanix/ncp/plugins/modules/nutanix_vms.py b/nutanix/ncp/plugins/modules/nutanix_vms.py index 51af7ce69..663e84d5f 100755 --- a/nutanix/ncp/plugins/modules/nutanix_vms.py +++ b/nutanix/ncp/plugins/modules/nutanix_vms.py @@ -110,7 +110,12 @@ def run_module(): 'memory_size_mib', 'memory_gb']), metadata__categories_mapping=dict(type='dict', aliases=['categories']), metadata__use_categories_mapping=dict( - type='bool', aliases=['use_categories_mapping'], default=False) + type='bool', aliases=['use_categories_mapping'], default=False), + spec__resources__guest_customization=dict(type='dict', aliases=['guest_customization'], options=dict( + type=dict(type="str", choices=["sysprep", "cloud_init"], default="sysprep"), + script_path=dict(type="str", required=True), + is_overridable=dict(type="bool", default=False) + )) )) module = BaseModule()