def run(self): state = self.module.params['state'] data = self.module.params['data'] try: resource = self.__get_by_name(data) if state == 'present': self.__present(resource, data) elif state == 'absent': self.__absent(resource) else: if not resource: raise HPOneViewResourceNotFound(ENCLOSURE_NOT_FOUND) if state == 'reconfigured': changed, msg, resource = self.__reconfigure(resource) elif state == 'refreshed': changed, msg, resource = self.__refresh(resource, data) elif state == 'support_data_collection_set': changed, msg, resource = self.__support_data_collection_set( resource, data) else: changed, msg, resource = self.__patch(resource, data) self.module.exit_json(changed=changed, msg=msg, ansible_facts=dict(enclosure=resource)) except HPOneViewException as exception: self.module.fail_json(msg='; '.join( str(e) for e in exception.args))
def __set_ethernet_uris(self, ethernet_names, operation="add"): """Updates network uris.""" if not isinstance(ethernet_names, list): ethernet_names = [ethernet_names] associated_enets = self.data.get('networkUris', []) ethernet_uris = [] for i, enet in enumerate(ethernet_names): enet_exists = self._ethernet_networks.get_by_name(enet) if enet_exists: ethernet_uris.append(enet_exists.data['uri']) else: raise HPOneViewResourceNotFound( "Ethernet: {} does not exist".foramt(enet)) if operation == "remove": enets_to_update = sorted( list(set(associated_enets) - set(ethernet_uris))) elif operation == "add": enets_to_update = sorted( list(set(associated_enets).union(set(ethernet_uris)))) else: raise ValueError( "Value {} is not supported as operation. The supported values are: ['add', 'remove']" ) if set(enets_to_update) != set(associated_enets): updated_network = {'networkUris': enets_to_update} self.update(updated_network)
def test_oneview_resource_not_found_inheritance(self): exception = HPOneViewResourceNotFound("The resource was not found!") self.assertIsInstance(exception, HPOneViewException) self.assertEqual(exception.msg, "The resource was not found!") self.assertEqual(exception.oneview_response, None) self.assertEqual(exception.args[0], "The resource was not found!")
def __get_current_property_value(self, state_name, state, resource, data): property_name = state['path'].split('/')[1] sub_property_name = state['path'].split('/')[-1] if sub_property_name == property_name: sub_property_name = None if state_name == 'appliance_bays_powered_on': sub_property_name = 'poweredOn' filter = set(data.keys()) - set(["name"]) if filter: filter = filter.pop() property_current_value = None if filter: sub_resource = None if resource.get(property_name): sub_resource = next( (item for item in resource[property_name] if str(item[filter]) == str(data[filter])), None) if not sub_resource: # Resource doesn't have that property or subproperty raise HPOneViewResourceNotFound(BAY_NOT_FOUND) property_current_value = sub_resource.get(sub_property_name) state['path'] = state['path'].format(**data) else: property_current_value = resource[property_name] return property_current_value
def run(self): state = self.module.params['state'] data = self.module.params['data'] try: changed, msg, ansible_facts = False, '', {} if state == 'compliant': changed, msg, ansible_facts = self.__compliance(data) else: resource = self.__get_log_interconnect_by_name(data['name']) if not resource: raise HPOneViewResourceNotFound(SAS_LOGICAL_INTERCONNECT_NOT_FOUND) uri = resource['uri'] if state == 'configuration_updated': changed, msg, ansible_facts = self.__update_configuration(uri) elif state == 'firmware_updated': changed, msg, ansible_facts = self.__update_firmware(uri, data) elif state == 'drive_enclosure_replaced': changed, msg, ansible_facts = self.__replace_drive_enclosure(uri, data) self.module.exit_json(changed=changed, msg=msg, ansible_facts=ansible_facts) except HPOneViewException as exception: self.module.fail_json(msg='; '.join(str(e) for e in exception.args))
def __get_by_name(self, name): sas_interconnects = self.resource_client.get_by('name', name) if not sas_interconnects: raise HPOneViewResourceNotFound(SAS_INTERCONNECT_NOT_FOUND) return sas_interconnects[0]
def run(self): try: state = self.module.params['state'] data = self.module.params['data'] changed, msg, ansible_facts = False, '', {} resource = (self.i3s_client.golden_images.get_by("name", data['name']) or [None])[0] if state == 'present': changed, msg, ansible_facts = self.__present(data, resource) elif state == 'absent': changed, msg, ansible_facts = self.__absent(resource) else: if not resource: raise HPOneViewResourceNotFound(GOLDEN_IMAGE_WAS_NOT_FOUND) if state == 'downloaded': changed, msg, ansible_facts = self.__download(data, resource) elif state == 'archive_downloaded': changed, msg, ansible_facts = self.__download_archive(data, resource) self.module.exit_json(changed=changed, msg=msg, ansible_facts=ansible_facts) except HPOneViewException as exception: self.module.fail_json(msg='; '.join(str(e) for e in exception.args))
def __get_appliance_by_name(self, name): appliances = self.oneview_client.os_deployment_servers.get_appliances() if appliances: for appliance in appliances: if appliance['name'] == name: return appliance['uri'] raise HPOneViewResourceNotFound(self.APPLIANCE_NOT_FOUND.format(name))
def run(self): state = self.module.params['state'] data = self.module.params['data'] try: if not self.module.params.get('validate_etag'): self.oneview_client.connection.disable_etag_validation() resource = self.__get_by_name(data) if not resource: raise HPOneViewResourceNotFound(LOGICAL_INTERCONNECT_NOT_FOUND) uri = resource['uri'] if state == 'compliant': changed, msg, ansible_facts = self.__compliance(uri) elif state == 'ethernet_settings_updated': changed, msg, ansible_facts = self.__update_ethernet_settings( resource, data) elif state == 'internal_networks_updated': changed, msg, ansible_facts = self.__update_internal_networks( uri, data) elif state == 'settings_updated': changed, msg, ansible_facts = self.__update_settings( resource, data) elif state == 'forwarding_information_base_generated': changed, msg, ansible_facts = self.__generate_forwarding_information_base( uri) elif state == 'qos_aggregated_configuration_updated': changed, msg, ansible_facts = self.__update_qos_configuration( uri, data) elif state == 'snmp_configuration_updated': changed, msg, ansible_facts = self.__update_snmp_configuration( uri, data) elif state == 'port_monitor_updated': changed, msg, ansible_facts = self.__update_port_monitor( uri, data) elif state == 'configuration_updated': changed, msg, ansible_facts = self.__update_configuration(uri) elif state == 'firmware_installed': changed, msg, ansible_facts = self.__install_firmware( uri, data) elif state == 'telemetry_configuration_updated': changed, msg, ansible_facts = self.__update_telemetry_configuration( resource, data) else: changed, msg, ansible_facts = False, '', dict() if ansible_facts: self.module.exit_json(changed=changed, msg=msg, ansible_facts=ansible_facts) else: self.module.exit_json(changed=changed, msg=msg) except HPOneViewException as exception: self.module.fail_json(msg='; '.join( str(e) for e in exception.args))
def __replace_name_by_uri(self, data, attr_name, message, resource_client): attr_uri = attr_name.replace("Name", "Uri") if attr_name in data: name = data.pop(attr_name) resource_by_name = resource_client.get_by('name', name) if not resource_by_name: raise HPOneViewResourceNotFound(message + name) data[attr_uri] = resource_by_name[0]['uri']
def __resolve_log_interconnect_names(self, interconnectNames): uris = [] for name in interconnectNames: li = self.__get_log_interconnect_by_name(name) if not li: raise HPOneViewResourceNotFound(SAS_LOGICAL_INTERCONNECT_NOT_FOUND) uris.append(li['uri']) return uris
def __get_network_by_name(self, name): fc_networks = self.oneview_client.fc_networks.get_by('name', name) if fc_networks: return fc_networks[0] ethernet_networks = self.oneview_client.ethernet_networks.get_by('name', name) if not ethernet_networks: raise HPOneViewResourceNotFound(self.SERVER_PROFILE_NETWORK_NOT_FOUND + name) return ethernet_networks[0]
def __get_appliance_by_name(self, name): appliance = self.oneview_client.os_deployment_servers.get_appliance_by_name( name) if not appliance: raise HPOneViewResourceNotFound( self.APPLIANCE_NOT_FOUND.format(name)) return appliance['uri']
def __set_refresh_state(self, data, resource): if not resource: raise HPOneViewResourceNotFound(POWER_DEVICE_NOT_FOUND) resource = self.oneview_client.power_devices.update_refresh_state( resource['uri'], data['refreshStateData']) return True, POWER_DEVICE_REFRESH_STATE_UPDATED, dict( power_device=resource)
def __refresh(self, data): resource = self.__get_by_name(data) if resource: logical_switch = self.oneview_client.logical_switches.refresh(resource['uri']) self.module.exit_json(changed=True, msg=LOGICAL_SWITCH_REFRESHED, ansible_facts=dict(logical_switch=logical_switch)) else: raise HPOneViewResourceNotFound(LOGICAL_SWITCH_NOT_FOUND)
def __replace_group_name_by_uri(self, data): if 'logicalSwitch' in data and 'logicalSwitchGroupName' in data['logicalSwitch']: group_name = data['logicalSwitch']['logicalSwitchGroupName'] logical_switch_group = self.oneview_client.logical_switch_groups.get_by('name', group_name) if logical_switch_group: data['logicalSwitch'].pop('logicalSwitchGroupName') data['logicalSwitch']['logicalSwitchGroupUri'] = logical_switch_group[0]['uri'] else: raise HPOneViewResourceNotFound(LOGICAL_SWITCH_GROUP_NOT_FOUND)
def __get_server_profile_uri(self, server_profile): if "/" in server_profile: return server_profile else: profile = self.oneview_client.server_profiles.get_by_name( server_profile) if profile: return profile['uri'] else: raise HPOneViewResourceNotFound(PROFILE_NOT_FOUND)
def __get_drive_enclosure(self, data): name = data.get('name') if not name: raise HPOneViewValueError(DRIVE_ENCLOSURE_NAME_REQUIRED) else: result = self.oneview_client.drive_enclosures.get_by('name', name) if not result: raise HPOneViewResourceNotFound(DRIVE_ENCLOSURE_NOT_FOUND) return result[0]
def __update_resource_assignments(self, data): resource = self.__get_by_name(data) if not resource: raise HPOneViewResourceNotFound(SCOPE_NOT_FOUND) else: scope = self.oneview_client.scopes.update_resource_assignments( resource['uri'], data.get('resourceAssignments')) self.module.exit_json(changed=True, msg=SCOPE_RESOURCE_ASSIGNMENTS_UPDATED, ansible_facts=dict(scope=scope))
def __get_network_uri(self, network_name_or_uri): if network_name_or_uri.startswith('/rest/ethernet-networks'): return network_name_or_uri else: enet_network = self.__get_ethernet_network_by_name( network_name_or_uri) if enet_network: return enet_network['uri'] else: raise HPOneViewResourceNotFound( NETWORK_SET_ENET_NETWORK_NOT_FOUND + network_name_or_uri)
def __create_snapshot(self, data): if 'snapshotParameters' not in data: raise HPOneViewResourceNotFound(VOLUME_NO_OPTIONS_PROVIDED) resource = self.__get_by_name(data['name']) if resource: self.oneview_client.volumes.create_snapshot(resource['uri'], data['snapshotParameters']) self.module.exit_json(changed=True, msg=VOLUME_SNAPSHOT_CREATED) else: self.module.fail_json(msg=VOLUME_NOT_FOUND)
def __replace_logical_interconnect_name_by_uri(self, data): if 'logicalInterconnectName' in data: name = data['logicalInterconnectName'] logical_interconnect = self.oneview_client.logical_interconnects.get_by_name( name) if logical_interconnect: del data['logicalInterconnectName'] data['logicalInterconnectUri'] = logical_interconnect['uri'] else: raise HPOneViewResourceNotFound( UPLINK_SET_LOGICAL_INTERCONNECT_NOT_FOUND)
def __default_bandwidth_reset(self, data): resource = self.__get_by_name(data) if not resource: raise HPOneViewResourceNotFound(ETHERNET_NETWORK_NOT_FOUND) default_connection_template = self.oneview_client.connection_templates.get_default() changed, connection_template = self.__update_connection_template(resource, default_connection_template['bandwidth']) return changed, ETHERNET_NETWORK_CONNECTION_TEMPLATE_RESET, dict( ethernet_network_connection_template=connection_template)
def __get_network_uri_by_name(self, name): ethernet_networks = self.oneview_client.ethernet_networks.get_by('name', name) if ethernet_networks: return ethernet_networks[0]['uri'] fc_networks = self.oneview_client.fc_networks.get_by('name', name) if fc_networks: return fc_networks[0]['uri'] fcoe_networks = self.oneview_client.fcoe_networks.get_by('name', name) if not fcoe_networks: raise HPOneViewResourceNotFound(self.NETWORK_NOT_FOUND.format(name)) return fcoe_networks[0]['uri']
def __get_by_name(self, name): logical_interconnect = self.resource_client.get_by_name(name=name) if not logical_interconnect: raise HPOneViewResourceNotFound(LOGICAL_INTERCONNECT_NOT_FOUND) facts = dict(logical_interconnects=logical_interconnect) options = self.module.params["options"] if options: options_facts = self.__get_options(logical_interconnect, options) facts.update(options_facts) return facts
def run(self): try: state = self.module.params['state'] data = self.module.params['data'] if not self.module.params.get('validate_etag'): self.oneview_client.connection.disable_etag_validation() if state == 'present': changed, msg, ansible_facts = self.__present(data) else: if not data.get('name'): raise HPOneViewValueError( SERVER_HARDWARE_MANDATORY_FIELD_MISSING.format( "data.name")) resource = self.__get_server_hardware(data['name']) if state == 'absent': changed, msg, ansible_facts = self.__absent(resource) else: if not resource: raise HPOneViewResourceNotFound( SERVER_HARDWARE_NOT_FOUND) if state == 'power_state_set': changed, msg, ansible_facts = self.__set_power_state( data, resource) elif state == 'refresh_state_set': changed, msg, ansible_facts = self.__set_refresh_state( data, resource) elif state == 'ilo_firmware_version_updated': changed, msg, ansible_facts = self.__update_mp_firmware_version( resource) elif state == 'environmental_configuration_set': changed, msg, ansible_facts = self.__set_environmental_configuration( data, resource) else: changed, msg, ansible_facts = self.__patch( state, resource) self.module.exit_json(changed=changed, msg=msg, ansible_facts=ansible_facts) except HPOneViewException as exception: self.module.fail_json(msg=exception.args[0])
def __get_interconnect(self): interconnect_ip = self.module.params['ip'] interconnect_name = self.module.params['name'] if interconnect_ip: interconnects = self.oneview_client.interconnects.get_by('interconnectIP', interconnect_ip) or [] elif interconnect_name: interconnects = self.oneview_client.interconnects.get_by('name', interconnect_name) or [] else: raise HPOneViewValueError(MISSING_KEY_MSG) if not interconnects: raise HPOneViewResourceNotFound(INTERCONNECT_WAS_NOT_FOUND) return interconnects[0]
def __delete_snapshot(self, data): if 'snapshotParameters' not in data: raise HPOneViewResourceNotFound(VOLUME_NO_OPTIONS_PROVIDED) resource = self.__get_by_name(data['name']) if not resource: self.module.fail_json(msg=VOLUME_NOT_FOUND) else: snapshot = self.__get_snapshot_by_name(resource, data) if not snapshot: self.module.fail_json(msg=VOLUME_SNAPSHOT_NOT_FOUND) else: self.oneview_client.volumes.delete_snapshot(snapshot) self.module.exit_json(changed=True, msg=VOLUME_SNAPSHOT_DELETED)
def __update_internal_networks(self, uri, data): self.__validate_options('internalNetworks', data) networks = [] for network_uri_or_name in data['internalNetworks']: if 'name' in network_uri_or_name: ethernet_network = self.__get_ethernet_network_by_name( network_uri_or_name['name']) if not ethernet_network: msg = LOGICAL_INTERCONNECT_ETH_NETWORK_NOT_FOUND + network_uri_or_name[ 'name'] raise HPOneViewResourceNotFound(msg) networks.append(ethernet_network['uri']) elif 'uri' in network_uri_or_name: networks.append(network_uri_or_name['uri']) li = self.oneview_client.logical_interconnects.update_internal_networks( uri, networks) return True, LOGICAL_INTERCONNECT_INTERNAL_NETWORKS_UPDATED, dict( logical_interconnect=li)
def __present(self, data, resource): changed = False if "newName" in data: data["name"] = data.pop("newName") if not resource: raise HPOneViewResourceNotFound(SERVER_HARDWARE_TYPE_NOT_FOUND) different = resource.get('name') != data.get('name') different |= resource.get('description') != data.get('description') if different: # update resource changed = True resource = self.oneview_client.server_hardware_types.update( data, resource['uri']) msg = SERVER_HARDWARE_TYPE_UPDATED else: msg = SERVER_HARDWARE_TYPE_ALREADY_UPDATED return changed, msg, dict(server_hardware_type=resource)