diff --git a/plugins/doc_fragments/ntnx_vms_base.py b/plugins/doc_fragments/ntnx_vms_base.py index fed985866..635168013 100644 --- a/plugins/doc_fragments/ntnx_vms_base.py +++ b/plugins/doc_fragments/ntnx_vms_base.py @@ -194,4 +194,19 @@ class ModuleDocFragment(object): - CDROM - DISK - NETWORK + owner: + description: Name or UUID of the owner + required: false + type: dict + suboptions: + name: + description: + - Owner Name + - Mutually exclusive with C(uuid) + type: str + uuid: + description: + - Owner UUID + - Mutually exclusive with C(name) + type: str """ diff --git a/plugins/module_utils/prism/spec/vms.py b/plugins/module_utils/prism/spec/vms.py index 760aeb89b..11f66384f 100644 --- a/plugins/module_utils/prism/spec/vms.py +++ b/plugins/module_utils/prism/spec/vms.py @@ -50,6 +50,9 @@ class DefaultVMSpec: project=dict( type="dict", options=entity_by_spec, mutually_exclusive=mutually_exclusive ), + owner=dict( + type="dict", options=entity_by_spec, mutually_exclusive=mutually_exclusive + ), cluster=dict( type="dict", options=entity_by_spec, mutually_exclusive=mutually_exclusive ), diff --git a/plugins/module_utils/prism/vms.py b/plugins/module_utils/prism/vms.py index a1ca89876..0ecd0ea9e 100644 --- a/plugins/module_utils/prism/vms.py +++ b/plugins/module_utils/prism/vms.py @@ -17,6 +17,7 @@ from .projects import Project from .spec.categories_mapping import CategoriesMapping from .subnets import get_subnet_uuid +from .users import User class VM(Prism): @@ -32,6 +33,7 @@ def __init__(self, module): "name": self._build_spec_name, "desc": self._build_spec_desc, "project": self._build_spec_project, + "owner": self._build_spec_owner, "cluster": self._build_spec_cluster, "vcpus": self._build_spec_vcpus, "cores_per_vcpu": self._build_spec_cores, @@ -220,6 +222,21 @@ def _build_spec_project(self, payload, param): ) return payload, None + def _build_spec_owner(self, payload, param): + if "name" in param: + owner = User(self.module) + name = param["name"] + uuid = owner.get_uuid(name, key="username") + if not uuid: + error = "Owner {0} not found.".format(name) + return None, error + + elif "uuid" in param: + uuid = param["uuid"] + + payload["metadata"].update({"owner_reference": {"uuid": uuid, "kind": "user"}}) + return payload, None + def _build_spec_cluster(self, payload, param): uuid, err = get_cluster_uuid(param, self.module) if err: diff --git a/tests/integration/targets/nutanix_vms/tasks/create.yml b/tests/integration/targets/nutanix_vms/tasks/create.yml index f8f43736a..afe4c5d7c 100644 --- a/tests/integration/targets/nutanix_vms/tasks/create.yml +++ b/tests/integration/targets/nutanix_vms/tasks/create.yml @@ -39,6 +39,41 @@ fail_msg: 'Unable to Create VM with none values ' success_msg: 'VM with none values created successfully ' + - set_fact: + todelete: '{{ todelete + [ result["response"]["metadata"]["uuid"] ] }}' +# ################################################################################## + - name: VM with owner name + ntnx_vms: + state: present + name: none + timezone: GMT + project: + uuid: "{{ project.uuid }}" + cluster: + name: "{{ cluster.name }}" + categories: + AppType: + - Apache_Spark + owner: + name: "{{ vm_owner.name }}" + disks: + - type: DISK + size_gb: 5 + bus: SCSI + register: result + ignore_errors: true + + - name: Creation Status + assert: + that: + - result.response is defined + - result.response.status.state == 'COMPLETE' + - result.response.metadata.owner_reference.name == "{{ vm_owner.name }}" + - result.response.metadata.owner_reference.uuid == "{{ vm_owner.uuid }}" + - result.response.metadata.owner_reference.kind == "user" + fail_msg: 'Unable to Create VM with owner' + success_msg: 'VM with owner created successfully ' + - set_fact: todelete: '{{ todelete + [ result["response"]["metadata"]["uuid"] ] }}' ################################################################################## diff --git a/tests/integration/targets/nutanix_vms/tasks/vm_update.yml b/tests/integration/targets/nutanix_vms/tasks/vm_update.yml index d8ee4048f..8ce3b8bc7 100644 --- a/tests/integration/targets/nutanix_vms/tasks/vm_update.yml +++ b/tests/integration/targets/nutanix_vms/tasks/vm_update.yml @@ -26,6 +26,28 @@ fail_msg: ' Unable to create VM with minimum requiremnts ' success_msg: ' VM with minimum requiremnts created successfully ' #################################################################### +- name: update vm by set owner by uuid + ntnx_vms: + vm_uuid: "{{ result.vm_uuid }}" + owner: + uuid: "{{vm_owner.uuid}}" + register: result + ignore_errors: true + +- name: Update Status + assert: + that: + - result.response is defined + - result.vm_uuid + - result.task_uuid + - result.response.status.state == "COMPLETE" + - result.response.metadata.owner_reference.name == "{{ vm_owner.name }}" + - result.response.metadata.owner_reference.uuid == "{{ vm_owner.uuid }}" + - result.response.metadata.owner_reference.kind == "user" + fail_msg: ' Unable to update vm by setting owner ' + success_msg: ' VM updated successfully by setting owner ' +#################################################################### + - debug: msg: Start update tests for memory vcpus cores_per_vcpu