def __delete_snapshot(self, resource): if 'snapshotParameters' not in self.data: raise HPOneViewResourceNotFound(self.MSG_NO_OPTIONS_PROVIDED) snapshot = self.__get_snapshot_by_name(resource, self.data) if not snapshot: raise HPOneViewResourceNotFound(self.MSG_SNAPSHOT_NOT_FOUND) else: self.resource_client.delete_snapshot(snapshot) return dict(changed=True, msg=self.MSG_SNAPSHOT_DELETED)
def execute_module(self): resource = self.__get_by_name(self.data) if self.state == 'present': changed, msg, resource = self.__present(resource, self.data) elif self.state == 'absent': return self.resource_absent(resource, 'remove') else: if not resource: raise HPOneViewResourceNotFound(self.MSG_ENCLOSURE_NOT_FOUND) if self.state == 'reconfigured': changed, msg, resource = self.__reconfigure(resource) elif self.state == 'refreshed': changed, msg, resource = self.__refresh(resource, self.data) elif self.state == 'support_data_collection_set': changed, msg, resource = self.__support_data_collection_set( resource, self.data) else: changed, msg, resource = self.__patch(resource, self.data) return dict(changed=changed, msg=msg, ansible_facts=dict(enclosure=resource))
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(self.MSG_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 __update_resource_assignments(self, resource): if not resource: raise HPOneViewResourceNotFound(self.MSG_RESOURCE_NOT_FOUND) if self.oneview_client.api_version < 500: scope = self.resource_client.update_resource_assignments( resource['uri'], self.data.get('resourceAssignments')) else: add_resources = self.data.get('resourceAssignments').get( 'addedResourceUris') is not None remove_resources = self.data.get('resourceAssignments').get( 'removedResourceUris') is not None if add_resources: scope = self.resource_client.patch( resource['uri'], 'replace', '/addedResourceUris', self.data.get('resourceAssignments').get( 'addedResourceUris')) if remove_resources: scope = self.resource_client.patch( resource['uri'], 'replace', '/removedResourceUris', self.data.get('resourceAssignments').get( 'removedResourceUris')) if not add_resources and not remove_resources: return dict(changed=False, msg=self.MSG_RESOURCE_ASSIGNMENTS_NOT_UPDATED, ansible_facts=dict(scope=resource)) return dict(changed=True, msg=self.MSG_RESOURCE_ASSIGNMENTS_UPDATED, ansible_facts=dict(scope=scope))
def __create_snapshot(self, resource): if 'snapshotParameters' not in self.data: raise HPOneViewResourceNotFound(self.MSG_NO_OPTIONS_PROVIDED) self.resource_client.create_snapshot(resource['uri'], self.data['snapshotParameters']) return dict(changed=True, msg=self.MSG_SNAPSHOT_CREATED)
def __present(self, data, resource): changed = False msg = self.MSG_ALREADY_PRESENT if not resource: if self.oneview_client.api_version >= 500: raise HPOneViewResourceNotFound(self.MSG_RESOURCE_NOT_FOUND) else: resource = self.oneview_client.storage_pools.add(data) changed = True msg = self.MSG_CREATED else: merged_data = resource.copy() merged_data.update(self.data) if ResourceComparator.compare(resource, merged_data): msg = self.MSG_ALREADY_PRESENT else: resource = self.resource_client.update(merged_data) changed = True msg = self.MSG_UPDATED return dict(changed=changed, msg=msg, ansible_facts=dict(storage_pool=resource))
def execute_module(self): changed, msg, ansible_facts = False, '', {} logical_enclosure = self.oneview_client.logical_enclosures.get_by_name( self.data['name']) if self.state == 'present': changed, msg, ansible_facts = self.__present( self.data, logical_enclosure) elif self.state == 'absent': changed, msg, ansible_facts = self.__absent(logical_enclosure) else: if not logical_enclosure: raise HPOneViewResourceNotFound(self.MSG_REQUIRED) if self.state == 'firmware_updated': changed, msg, ansible_facts = self.__update_firmware( self.data, logical_enclosure) elif self.state == 'script_updated': changed, msg, ansible_facts = self.__update_script( self.data, logical_enclosure) elif self.state == 'dumped': changed, msg, ansible_facts = self.__support_dump( self.data, logical_enclosure) elif self.state == 'reconfigured': changed, msg, ansible_facts = self.__reconfigure( logical_enclosure) elif self.state == 'updated_from_group': changed, msg, ansible_facts = self.__update_from_group( logical_enclosure) return dict(changed=changed, msg=msg, ansible_facts=ansible_facts)
def __get_by_name(self, name): sas_interconnects = self.resource_client.get_by('name', name) if not sas_interconnects: raise HPOneViewResourceNotFound(self.MSG_NOT_FOUND) return sas_interconnects[0]
def __refresh(self, data, resource): if not resource: raise HPOneViewResourceNotFound(self.MSG_LOGICAL_SWITCH_NOT_FOUND) else: logical_switch = self.oneview_client.logical_switches.refresh( resource['uri']) return True, self.MSG_REFRESHED, dict( logical_switch=logical_switch)
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(self.MSG_ETHERNET_NETWORK_NOT_FOUND + network_name_or_uri)
def __resolve_log_interconnect_names(self, interconnectNames): uris = [] for name in interconnectNames: li = self.get_by_name(name) if not li: raise HPOneViewResourceNotFound(self.MSG_NOT_FOUND) uris.append(li['uri']) return uris
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.MSG_APPLIANCE_NOT_FOUND.format(name)) return appliance['uri']
def __absent(self, data, resource): if self.oneview_client.api_version >= 500: if resource: raise HPOneViewResourceNotFound(self.MSG_RESOURCE_FOUND) else: return dict(changed=False, msg=self.MSG_ALREADY_ABSENT, ansible_facts=dict(storage_pool=None)) else: return self.resource_absent(resource, 'remove')
def __get_drive_enclosure(self, data): name = data.get('name') if not name: raise HPOneViewValueError(self.MSG_NAME_REQUIRED) else: result = self.oneview_client.drive_enclosures.get_by('name', name) if not result: raise HPOneViewResourceNotFound(self.MSG_NOT_FOUND) return result[0]
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(self.PROFILE_NOT_FOUND)
def __replace_logical_interconnect_name_by_uri(self): if 'logicalInterconnectName' in self.data: name = self.data.pop('logicalInterconnectName') logical_interconnect = self.oneview_client.logical_interconnects.get_by_name( name) if logical_interconnect: self.data['logicalInterconnectUri'] = logical_interconnect[ 'uri'] else: raise HPOneViewResourceNotFound( self.MSG_LOGICAL_INTERCONNECT_NOT_FOUND)
def execute_module(self): resource = self.get_by_name(self.module.params.get('name')) if self.state == 'absent': return self.resource_absent(resource) if not resource: raise HPOneViewResourceNotFound(self.MSG_NOT_FOUND) else: if self.state == 'present': return self.__present(resource) elif self.state == 'ports_updated': return self.__update_ports(resource)
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( self.MSG_LOGICAL_SWITCH_GROUP_NOT_FOUND)
def __get_by_name(self, name): logical_interconnect = self.resource_client.get_by_name(name=name) if not logical_interconnect: raise HPOneViewResourceNotFound(self.MSG_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 __default_bandwidth_reset(self, resource): if not resource: raise HPOneViewResourceNotFound( self.MSG_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, self.MSG_CONNECTION_TEMPLATE_RESET, dict( ethernet_network_connection_template=connection_template)
def __reserved_vlan_range_updated(self): resource = self.get_by_name(self.data['name']) if not resource: raise HPOneViewResourceNotFound(self.MSG_NOT_FOUND) resource_vlan_range = resource.get('reservedVlanRange') merged_data = resource_vlan_range.copy() merged_data.update(self.data['reservedVlanRangeParameters']) if ResourceComparator.compare(resource_vlan_range, merged_data): return dict(changed=False, msg=self.MSG_ALREADY_PRESENT, ansible_facts=dict(fabric=resource)) else: return self.__update_vlan_range(self.data, resource)
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(self.MSG_MISSING_KEY) if not interconnects: raise HPOneViewResourceNotFound(self.MSG_INTERCONNECT_NOT_FOUND) return interconnects[0]
def execute_module(self): resource = self.__get_resource(self.data) if not resource: raise HPOneViewResourceNotFound(self.MSG_NOT_FOUND) if self.state == 'present': exit_status = self.__update(self.data, resource) elif self.state == 'refresh_state_set': exit_status = self.__set_refresh_state(self.data, resource) elif self.state == 'endpoints_csv_file_created': exit_status = self.__create_endpoints_csv_file(resource) elif self.state == 'issues_report_created': exit_status = self.__create_issue_report(resource) return dict(exit_status)
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.MSG_NETWORK_NOT_FOUND.format(name)) return fcoe_networks[0]['uri']
def __present(self, resource): changed, msg = False, self.MSG_ALREADY_PRESENT if not resource: raise HPOneViewResourceNotFound(self.MSG_RESOURCE_NOT_FOUND) if "newName" in self.data: self.data["name"] = self.data.pop("newName") different = resource.get('name') != self.data.get('name') different |= resource.get('description') != self.data.get('description') if different: resource = self.resource_client.update(self.data, resource['uri']) changed = True msg = self.MSG_UPDATED return dict(changed=changed, msg=msg, ansible_facts=dict(server_hardware_type=resource))
def execute_module(self): resource = self.get_by_name(self.data['name']) if self.state == 'present': changed, msg, ansible_facts = self.__present(self.data, resource) elif self.state == 'absent': return self.resource_absent(resource) else: if not resource: raise HPOneViewResourceNotFound(self.MSG_WAS_NOT_FOUND) if self.state == 'downloaded': changed, msg, ansible_facts = self.__download(self.data, resource) elif self.state == 'archive_downloaded': changed, msg, ansible_facts = self.__download_archive(self.data, resource) return dict(changed=changed, msg=msg, ansible_facts=ansible_facts)
def execute_module(self): if self.state == 'compliant': changed, msg, ansible_facts = self.__compliance() else: resource = self.get_by_name(self.data['name']) if not resource: raise HPOneViewResourceNotFound(self.MSG_NOT_FOUND) if self.state == 'configuration_updated': changed, msg, ansible_facts = self.__update_configuration(resource['uri']) elif self.state == 'firmware_updated': changed, msg, ansible_facts = self.__update_firmware(resource['uri']) elif self.state == 'drive_enclosure_replaced': changed, msg, ansible_facts = self.__replace_drive_enclosure(resource['uri']) return dict(changed=changed, msg=msg, ansible_facts=ansible_facts)
def __present(self, data, resource): changed = False msg = '' if not resource: raise HPOneViewResourceNotFound(self.MSG_NOT_FOUND) else: if 'newName' in data: data['name'] = data.pop('newName') merged_data = resource.copy() merged_data.update(data) if not ResourceComparator.compare(resource, merged_data): resource = self.resource_client.update(merged_data) changed = True msg = self.MSG_UPDATED else: msg = self.MSG_ALREADY_PRESENT return changed, msg, dict(connection_template=resource)
def __update(self, data, resource): if not resource: raise HPOneViewResourceNotFound(self.MSG_LOGICAL_SWITCH_NOT_FOUND) else: data['logicalSwitch']['uri'] = resource['uri'] if 'logicalSwitchGroupUri' not in data['logicalSwitch']: data['logicalSwitch']['logicalSwitchGroupUri'] = resource[ 'logicalSwitchGroupUri'] if 'switchCredentialConfiguration' not in data['logicalSwitch']: data['logicalSwitch'][ 'switchCredentialConfiguration'] = resource[ 'switchCredentialConfiguration'] if 'newName' in data['logicalSwitch']: data['logicalSwitch']['name'] = data['logicalSwitch'].pop( 'newName') self.__replace_group_name_by_uri(data) updated_resource = self.oneview_client.logical_switches.update( data) return True, self.MSG_UPDATED, dict( logical_switch=updated_resource)
def execute_module(self): if self.data.get('deviceVolumeName'): name = self.data.get('deviceVolumeName') else: name = self.data.get('name') or self.data.get('properties').get( 'name') resource = self.get_by_name(name) if self.state == 'present': return self.__present(resource) if self.state == 'managed': return self.__managed(resource) elif self.state == 'absent': return self.__absent(resource) else: if not resource: raise HPOneViewResourceNotFound(self.MSG_NOT_FOUND) if self.state == 'repaired': return self.__repair(resource) elif self.state == 'snapshot_created': return self.__create_snapshot(resource) elif self.state == 'snapshot_deleted': return self.__delete_snapshot(resource)