Skip to content

Commit

Permalink
functionality to update owner of vms (#412)
Browse files Browse the repository at this point in the history
* functionality to update owner of vm

* sanity fix

* add test

---------

Co-authored-by: Gevorg-Khachatryaan <Gevorg1050.1>
Co-authored-by: Alaa Bishtawi <[email protected]>
  • Loading branch information
Gevorg-Khachatryan-97 and alaa-bish authored Oct 25, 2023
1 parent 0054cc9 commit 8579162
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
15 changes: 15 additions & 0 deletions plugins/doc_fragments/ntnx_vms_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
3 changes: 3 additions & 0 deletions plugins/module_utils/prism/spec/vms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
Expand Down
17 changes: 17 additions & 0 deletions plugins/module_utils/prism/vms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
35 changes: 35 additions & 0 deletions tests/integration/targets/nutanix_vms/tasks/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] ] }}'
##################################################################################
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/targets/nutanix_vms/tasks/vm_update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 8579162

Please sign in to comment.