Refactor code for find_host_by_cluster_datacenter (#26859)

Fix adds refactor of code for find_host_by_cluster_datacenter
API in vmware_host and vmware_resource_pool.
Also, adds strict check for cluster and datacenter object find.
Testcase for vmware_resource_pool.

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/28743/head
Abhijeet Kasurde 7 years ago committed by Dag Wieers
parent 62e01aecfb
commit 3e20acbd79

@ -624,3 +624,18 @@ def serialize_spec(clonespec):
data[x] = str(xt)
return data
def find_host_by_cluster_datacenter(module, content, datacenter_name, cluster_name, host_name):
dc = find_datacenter_by_name(content, datacenter_name)
if dc is None:
module.fail_json(msg="Unable to find datacenter with name %s" % datacenter_name)
cluster = find_cluster_by_name(content, cluster_name, datacenter=dc)
if cluster is None:
module.fail_json(msg="Unable to find cluster with name %s" % cluster_name)
for host in cluster.host:
if host.name == host_name:
return host, cluster
return None, cluster

@ -84,10 +84,9 @@ from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.vmware import (
TaskError,
connect_to_api,
find_cluster_by_name,
find_datacenter_by_name,
vmware_argument_spec,
wait_for_task,
find_host_by_cluster_datacenter,
)
@ -128,19 +127,6 @@ class VMwareHost(object):
except Exception as e:
self.module.fail_json(msg=str(e))
def find_host_by_cluster_datacenter(self):
self.dc = find_datacenter_by_name(self.content, self.datacenter_name)
self.cluster = find_cluster_by_name(self.content, self.cluster_name, self.dc)
if self.cluster is None:
self.module.fail_json(msg="Unable to find cluster %(cluster_name)s" % self.module.params)
for host in self.cluster.host:
if host.name == self.esxi_hostname:
return host, self.cluster
return None, self.cluster
def add_host_to_vcenter(self):
host_connect_spec = vim.host.ConnectSpec()
host_connect_spec.hostName = self.esxi_hostname
@ -200,7 +186,8 @@ class VMwareHost(object):
self.module.exit_json(changed=changed, result=str(result))
def check_host_state(self):
self.host, self.cluster = self.find_host_by_cluster_datacenter()
self.host, self.cluster = find_host_by_cluster_datacenter(self.module, self.content, self.datacenter_name,
self.cluster_name, self.esxi_hostname)
if self.host is None:
return 'absent'

@ -137,9 +137,9 @@ try:
except ImportError:
HAS_PYVMOMI = False
from ansible.module_utils.vmware import get_all_objs, connect_to_api, vmware_argument_spec, find_datacenter_by_name, \
find_cluster_by_name, wait_for_task, find_host_by_cluster_datacenter
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.vmware import (get_all_objs, connect_to_api, vmware_argument_spec,
find_datacenter_by_name, find_cluster_by_name_datacenter, wait_for_task)
class VMwareResourcePool(object):
@ -169,17 +169,6 @@ class VMwareResourcePool(object):
self.resource_pool_obj = None
self.content = connect_to_api(module)
def find_host_by_cluster_datacenter(self):
self.dc_obj = find_datacenter_by_name(self.content, self.datacenter)
self.cluster_obj = find_cluster_by_name_datacenter(
self.dc_obj, self.cluster)
for host in self.cluster_obj.host:
if host.name == self.hostname:
return host, self.cluster
return None, self.cluster
def select_resource_pool(self, host):
pool_obj = None
@ -275,8 +264,12 @@ class VMwareResourcePool(object):
rp_spec.memoryAllocation = mem_alloc
self.dc_obj = find_datacenter_by_name(self.content, self.datacenter)
self.cluster_obj = find_cluster_by_name_datacenter(
self.dc_obj, self.cluster)
if self.dc_obj is None:
self.module.fail_json(msg="Unable to find datacenter with name %s" % self.datacenter)
self.cluster_obj = find_cluster_by_name(self.content, self.cluster, datacenter=self.dc_obj)
if self.cluster_obj is None:
self.module.fail_json(msg="Unable to find cluster with name %s" % self.cluster)
rootResourcePool = self.cluster_obj.resourcePool
rootResourcePool.CreateResourcePool(self.resource_pool, rp_spec)
@ -284,7 +277,8 @@ class VMwareResourcePool(object):
def check_rp_state(self):
self.host_obj, self.cluster_obj = self.find_host_by_cluster_datacenter()
self.host_obj, self.cluster_obj = find_host_by_cluster_datacenter(self.module, self.content, self.datacenter,
self.cluster, self.hostname)
self.resource_pool_obj = self.select_resource_pool(self.host_obj)
if self.resource_pool_obj is None:
@ -298,10 +292,6 @@ def main():
argument_spec.update(dict(datacenter=dict(required=True, type='str'),
cluster=dict(required=True, type='str'),
resource_pool=dict(required=True, type='str'),
hostname=dict(required=True, type='str'),
username=dict(required=True, type='str'),
password=dict(
required=True, type='str', no_log=True),
mem_shares=dict(type='str', default="normal", choices=[
'high', 'custom', 'normal', 'low']),
mem_limit=dict(type='int', default="-1"),

@ -0,0 +1,2 @@
posix/ci/cloud/vcenter
cloud/vcenter

@ -0,0 +1,169 @@
# Test code for the vmware_resource_pool module.
# (c) 2017, Abhijeet Kasurde <akasurde@redhat.com>
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
- name: make sure pyvmomi is installed
pip:
name: pyvmomi
state: latest
- name: store the vcenter container ip
set_fact:
vcsim: "{{ lookup('env', 'vcenter_host') }}"
- debug: var=vcsim
- name: Wait for Flask controller to come up online
wait_for:
host: "{{ vcsim }}"
port: 5000
state: started
- name: kill vcsim
uri:
url: "{{ 'http://' + vcsim + ':5000/killall' }}"
- name: start vcsim
uri:
url: "{{ 'http://' + vcsim + ':5000/spawn?cluster=2' }}"
register: vcsim_instance
- debug: var=vcsim_instance
- name: Wait for vcsim server to come up online
wait_for:
host: "{{ vcsim }}"
port: 443
state: started
- name: get a list of Datacenters from vcsim
uri:
url: "{{ 'http://' + vcsim + ':5000/govc_find?filter=DC' }}"
register: datacenters
- name: get a datacenter
set_fact: dc1="{{ datacenters['json'][0] | basename }}"
- debug: var=dc1
- name: get a list of Clusters from vcsim
uri:
url: "{{ 'http://' + vcsim + ':5000/govc_find?filter=CCR' }}"
register: clusters
- name: get a cluster
set_fact: ccr1="{{ clusters['json'][0] | basename }}"
- debug: var=ccr1
# Testcase 0001: Add Resource pool
- name: add resource pool
vmware_resource_pool:
validate_certs: False
hostname: "{{ vcsim }}"
username: "{{ vcsim_instance['json']['username'] }}"
password: "{{ vcsim_instance['json']['password'] }}"
datacenter: "{{ dc1 }}"
cluster: "{{ ccr1 }}"
resource_pool: test_resource_0001
mem_shares: normal
mem_limit: -1
mem_reservation: 0
mem_expandable_reservations: True
cpu_shares: normal
cpu_limit: -1
cpu_reservation: 0
cpu_expandable_reservations: True
state: present
register: resource_result_0001
- name: get a list of resources from vcsim after adding a resource pool
uri:
url: "{{ 'http://' + vcsim + ':5000/govc_find?filter=RP' }}"
register: new_rp_list
- set_fact: new_resource="{% for rp in new_rp_list['json'] %} {{ True if (rp | basename) == 'test_resource_0001' else False }} {% endfor %}"
- name: ensure a resource pool is present
assert:
that:
- "{{ resource_result_0001.changed == true }}"
- "{{ 'True' in new_resource }}"
# Testcase 0002: Add Resource pool again
- name: add resource pool again
vmware_resource_pool:
validate_certs: False
hostname: "{{ vcsim }}"
username: "{{ vcsim_instance['json']['username'] }}"
password: "{{ vcsim_instance['json']['password'] }}"
datacenter: "{{ dc1 }}"
cluster: "{{ ccr1 }}"
resource_pool: test_resource_0001
mem_shares: normal
mem_limit: -1
mem_reservation: 0
mem_expandable_reservations: True
cpu_shares: normal
cpu_limit: -1
cpu_reservation: 0
cpu_expandable_reservations: True
state: present
register: resource_result_0002
- name: check if nothing is changed
assert:
that:
- "{{ resource_result_0002.changed == false }}"
# Testcase 0003: Remove Resource pool
- name: add resource pool again
vmware_resource_pool:
validate_certs: False
hostname: "{{ vcsim }}"
username: "{{ vcsim_instance['json']['username'] }}"
password: "{{ vcsim_instance['json']['password'] }}"
datacenter: "{{ dc1 }}"
cluster: "{{ ccr1 }}"
resource_pool: test_resource_0001
state: absent
register: resource_result_0003
- name: check if resource pool is removed
assert:
that:
- "{{ resource_result_0003.changed == true }}"
# Testcase 0004: Remove Resource pool again
- name: add resource pool again
vmware_resource_pool:
validate_certs: False
hostname: "{{ vcsim }}"
username: "{{ vcsim_instance['json']['username'] }}"
password: "{{ vcsim_instance['json']['password'] }}"
datacenter: "{{ dc1 }}"
cluster: "{{ ccr1 }}"
resource_pool: test_resource_0001
state: absent
register: resource_result_0004
- name: check if resource pool is already removed
assert:
that:
- "{{ resource_result_0004.changed == false }}"
Loading…
Cancel
Save