def test_pool_info(self): one.set_fixture_unit_test("test_pool_info") hostpool = one.hostpool.info() self.assertGreater(len(hostpool.HOST), 0) host = hostpool.HOST[0] self.assertIn(HOST_STATES(host.STATE), [HOST_STATES.MONITORED, HOST_STATES.INIT])
def wait_for_host_state(self, host, target_states): """ Utility method that waits for a host state. Args: host: target_states: """ return self.wait_for_state('host', lambda: self.one.host.info(host.ID).STATE, lambda s: HOST_STATES(s).name, target_states, invalid_states=[HOST_STATES.ERROR, HOST_STATES.MONITORING_ERROR])
def run(self, one, module, result): # Get the list of hosts host_name = self.get_parameter("name") host = self.get_host_by_name(host_name) # manage host state desired_state = self.get_parameter('state') if bool(host): current_state = host.STATE current_state_name = HOST_STATES(host.STATE).name else: current_state = HOST_ABSENT current_state_name = "ABSENT" # apply properties if desired_state == 'present': if current_state == HOST_ABSENT: self.allocate_host() host = self.get_host_by_name(host_name) self.wait_for_host_state(host, [HOST_STATES.MONITORED]) elif current_state in [HOST_STATES.ERROR, HOST_STATES.MONITORING_ERROR]: self.fail(msg="invalid host state %s" % current_state_name) elif desired_state == 'enabled': if current_state == HOST_ABSENT: self.allocate_host() host = self.get_host_by_name(host_name) self.wait_for_host_state(host, [HOST_STATES.MONITORED]) elif current_state in [HOST_STATES.DISABLED, HOST_STATES.OFFLINE]: if one.host.status(host.ID, HOST_STATUS.ENABLED): self.wait_for_host_state(host, [HOST_STATES.MONITORED]) result['changed'] = True else: self.fail(msg="could not enable host") elif current_state in [HOST_STATES.MONITORED]: pass else: self.fail(msg="unknown host state %s, cowardly refusing to change state to enable" % current_state_name) elif desired_state == 'disabled': if current_state == HOST_ABSENT: self.fail(msg='absent host cannot be put in disabled state') elif current_state in [HOST_STATES.MONITORED, HOST_STATES.OFFLINE]: if one.host.status(host.ID, HOST_STATUS.DISABLED): self.wait_for_host_state(host, [HOST_STATES.DISABLED]) result['changed'] = True else: self.fail(msg="could not disable host") elif current_state in [HOST_STATES.DISABLED]: pass else: self.fail(msg="unknown host state %s, cowardly refusing to change state to disable" % current_state_name) elif desired_state == 'offline': if current_state == HOST_ABSENT: self.fail(msg='absent host cannot be placed in offline state') elif current_state in [HOST_STATES.MONITORED, HOST_STATES.DISABLED]: if one.host.status(host.ID, HOST_STATUS.OFFLINE): self.wait_for_host_state(host, [HOST_STATES.OFFLINE]) result['changed'] = True else: self.fail(msg="could not set host offline") elif current_state in [HOST_STATES.OFFLINE]: pass else: self.fail(msg="unknown host state %s, cowardly refusing to change state to offline" % current_state_name) elif desired_state == 'absent': if current_state != HOST_ABSENT: if one.host.delete(host.ID): result['changed'] = True else: self.fail(msg="could not delete host from cluster") # if we reach this point we can assume that the host was taken to the desired state if desired_state != "absent": # manipulate or modify the template desired_template_changes = self.get_parameter('template') if desired_template_changes is None: desired_template_changes = dict() # complete the template with specific ansible parameters if self.is_parameter('labels'): desired_template_changes['LABELS'] = self.get_parameter('labels') if self.requires_template_update(host.TEMPLATE, desired_template_changes): # setup the root element so that pyone will generate XML instead of attribute vector desired_template_changes = {"TEMPLATE": desired_template_changes} if one.host.update(host.ID, desired_template_changes, 1): # merge the template result['changed'] = True else: self.fail(msg="failed to update the host template") # the cluster if host.CLUSTER_ID != self.get_parameter('cluster_id'): if one.cluster.addhost(self.get_parameter('cluster_id'), host.ID): result['changed'] = True else: self.fail(msg="failed to update the host cluster") # return self.exit()