Skip to content

Commit

Permalink
Merge branch 'test' into main
Browse files Browse the repository at this point in the history
# Conflicts:
#	generate_spec.py
#	nutanix/ncp/plugins/module_utils/prism/vms.py
#	nutanix/ncp/plugins/modules/nutanix_vms.py
  • Loading branch information
Gevorg-Khachatryaan authored and Gevorg-Khachatryaan committed Jan 20, 2022
2 parents aa7a58e + fcc9247 commit 0508932
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
6 changes: 5 additions & 1 deletion nutanix/ncp/plugins/module_utils/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
51 changes: 46 additions & 5 deletions nutanix/ncp/plugins/module_utils/prism/vms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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:
Expand Down Expand Up @@ -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()
Expand All @@ -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]}]
Expand All @@ -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)
7 changes: 6 additions & 1 deletion nutanix/ncp/plugins/modules/nutanix_vms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 0508932

Please sign in to comment.