def delete_host_services_by_name(token, host_name, host_uuid, only_if_changed=False): """ Asks OpenStack Neutron to delete a host by name """ url = token.get_service_url(OPENSTACK_SERVICE.NEUTRON) if url is None: raise ValueError("OpenStack Neutron URL is invalid") api_cmd = url + "/v2.0/hosts.json?fields=id&fields=name" api_cmd_headers = dict() api_cmd_headers['wrs-header'] = 'true' api_cmd_headers['Content-Type'] = "application/json" response = rest_api_request(token, "GET", api_cmd) for host in response.result_data['hosts']: if host['name'] == host_name: if only_if_changed: if host['id'] != host_uuid: api_cmd = url + "/v2.0/hosts/%s" % host['id'] rest_api_request(token, "DELETE", api_cmd) else: api_cmd = url + "/v2.0/hosts/%s" % host['id'] rest_api_request(token, "DELETE", api_cmd)
def swact_from_host(token, host_uuid): """ Ask System Inventory to swact from a host """ url = token.get_service_url(PLATFORM_SERVICE.SYSINV) if url is None: raise ValueError("OpenStack SysInv URL is invalid") api_cmd = url + "/ihosts/%s" % host_uuid api_cmd_headers = dict() api_cmd_headers['Content-Type'] = "application/json" api_cmd_headers['User-Agent'] = "vim/1.0" host_data = dict() host_data['path'] = "/action" host_data['value'] = "swact" host_data['op'] = "replace" api_cmd_payload = list() api_cmd_payload.append(host_data) response = rest_api_request(token, "PATCH", api_cmd, api_cmd_headers, json.dumps(api_cmd_payload)) return response
def notify_host_services_delete_failed(token, host_uuid, reason): """ Notify System Inventory that host services delete failed """ url = token.get_service_url(PLATFORM_SERVICE.SYSINV) if url is None: raise ValueError("OpenStack SysInv URL is invalid") api_cmd = url + "/ihosts/%s" % host_uuid api_cmd_headers = dict() api_cmd_headers['Content-Type'] = "application/json" api_cmd_headers['User-Agent'] = "vim/1.0" api_cmd_payload_action = dict() api_cmd_payload_action['path'] = '/action' api_cmd_payload_action['value'] = 'services-delete-failed' api_cmd_payload_action['op'] = 'replace' api_cmd_payload_reason = dict() api_cmd_payload_reason['path'] = '/vim_progress_status' api_cmd_payload_reason['value'] = str(reason) api_cmd_payload_reason['op'] = 'replace' api_cmd_list = list() api_cmd_list.append(api_cmd_payload_action) api_cmd_list.append(api_cmd_payload_reason) response = rest_api_request(token, "PATCH", api_cmd, api_cmd_headers, json.dumps(api_cmd_list)) return response
def get_alarm_history(token, start=None, end=None): """ Asks Fault Management for customer alarm history """ url = token.get_service_url(PLATFORM_SERVICE.FM) if url is None: raise ValueError("OpenStack FM URL is invalid") api_cmd = url + "/event_log?alarms=True" if start is not None and end is not None: api_cmd += ("&q.field=start&q.field=end&q.op=eq&q.op=eq" "&q.value=%s&q.value=%s" % (str(start).replace(' ', 'T').replace(':', '%3A'), str(end).replace(' ', 'T').replace(':', '%3A'))) elif start is not None: api_cmd += ("&q.field=start;q.op=eq;q.value='%s'" % str(start).replace(' ', 'T').replace(':', '%3A')) elif end is not None: api_cmd += ("&q.field=end;q.op=eq;q.value='%s'" % str(end).replace(' ', 'T').replace(':', '%3A')) api_cmd += '&limit=100' response = rest_api_request(token, "GET", api_cmd) return response
def notify_host_services_disable_extend(token, host_uuid): """ Notify System Inventory that host services disable needs to be extended """ url = token.get_service_url(OPENSTACK_SERVICE.SYSINV) if url is None: raise ValueError("OpenStack SysInv URL is invalid") api_cmd = url + "/ihosts/%s" % host_uuid api_cmd_headers = dict() api_cmd_headers['Content-Type'] = "application/json" api_cmd_headers['User-Agent'] = "vim/1.0" api_cmd_payload_action = dict() api_cmd_payload_action['path'] = '/action' api_cmd_payload_action['value'] = 'services-disable-extend' api_cmd_payload_action['op'] = 'replace' api_cmd_list = list() api_cmd_list.append(api_cmd_payload_action) response = rest_api_request(token, "PATCH", api_cmd, api_cmd_headers, json.dumps(api_cmd_list)) return response
def create_network(token, network_name, network_type, segmentation_id, physical_network, shared): """ Asks OpenStack Neutron to create a network """ url = token.get_service_url(OPENSTACK_SERVICE.NEUTRON) if url is None: raise ValueError("OpenStack Neutron URL is invalid") api_cmd = url + "/v2.0/networks" api_cmd_headers = dict() api_cmd_headers['wrs-header'] = 'true' api_cmd_headers['Content-Type'] = "application/json" network = dict() network['name'] = network_name network['shared'] = shared if 'flat' == network_type: network['provider:network_type'] = network_type network['provider:physical_network'] = physical_network else: network['provider:network_type'] = network_type network['provider:segmentation_id'] = segmentation_id network['provider:physical_network'] = physical_network api_cmd_payload = dict() api_cmd_payload['network'] = network response = rest_api_request(token, "POST", api_cmd, api_cmd_headers, json.dumps(api_cmd_payload)) return response
def create_image(token, image_name, image_description, container_format, disk_format, min_disk_size_gb, min_memory_size_mb, visibility, protected, properties): """ Ask OpenStack Glance to create an image """ url = token.get_service_url(OPENSTACK_SERVICE.GLANCE, strip_version=True) if url is None: raise ValueError("OpenStack Glance URL is invalid") api_cmd = url + "/v2/images" api_cmd_headers = dict() api_cmd_headers['Content-Type'] = "application/json" api_cmd_payload = dict() api_cmd_payload['name'] = image_name api_cmd_payload['description'] = image_description api_cmd_payload['container_format'] = container_format api_cmd_payload['disk_format'] = disk_format api_cmd_payload['min_disk'] = min_disk_size_gb api_cmd_payload['min_ram'] = min_memory_size_mb api_cmd_payload['visibility'] = visibility api_cmd_payload['protected'] = protected if properties is not None: for property in properties: api_cmd_payload[property] = properties[property] response = rest_api_request(token, "POST", api_cmd, api_cmd_headers, json.dumps(api_cmd_payload)) return response
def upgrade_activate(token): """ Ask System Inventory to activate an upgrade """ url = token.get_service_url(PLATFORM_SERVICE.SYSINV) if url is None: raise ValueError("OpenStack SysInv URL is invalid") api_cmd = url + "/upgrade" api_cmd_headers = dict() api_cmd_headers['Content-Type'] = "application/json" api_cmd_headers['User-Agent'] = "vim/1.0" host_data = dict() host_data['path'] = "/state" host_data['value'] = "activation-requested" host_data['op'] = "replace" api_cmd_payload = list() api_cmd_payload.append(host_data) response = rest_api_request(token, "PATCH", api_cmd, api_cmd_headers, json.dumps(api_cmd_payload)) return response
def update_network(token, network_id, admin_state=None, shared=None): """ Asks OpenStack Neutron to update a network """ url = token.get_service_url(OPENSTACK_SERVICE.NEUTRON) if url is None: raise ValueError("OpenStack Neutron URL is invalid") api_cmd = url + "/v2.0/networks/%s" % network_id api_cmd_headers = dict() api_cmd_headers['wrs-header'] = 'true' api_cmd_headers['Content-Type'] = "application/json" network = dict() if admin_state is not None: if NETWORK_ADMIN_STATE.UP == admin_state: network['admin_state_up'] = True else: network['admin_state_up'] = False if shared is not None: network['shared'] = shared api_cmd_payload = dict() api_cmd_payload['network'] = network response = rest_api_request(token, "PUT", api_cmd, api_cmd_headers, json.dumps(api_cmd_payload)) return response
def create_host_services(token, hostname, host_uuid): """ Asks OpenStack Neutron to create a host """ url = token.get_service_url(OPENSTACK_SERVICE.NEUTRON) if url is None: raise ValueError("OpenStack Neutron URL is invalid") api_cmd = url + "/v2.0/hosts.json" api_cmd_headers = dict() api_cmd_headers['wrs-header'] = 'true' api_cmd_headers['Content-Type'] = "application/json" payload = dict() payload['availability'] = 'down' payload['id'] = host_uuid payload['name'] = hostname api_cmd_payload = dict() api_cmd_payload['host'] = payload response = rest_api_request(token, "POST", api_cmd, api_cmd_headers, json.dumps(api_cmd_payload)) return response
def create_subnet(token, network_id, subnet_name, ip_version, cidr, gateway_ip, dhcp_enabled): """ Ask OpenStack Neutron to create a subnet """ url = token.get_service_url(OPENSTACK_SERVICE.NEUTRON) if url is None: raise ValueError("OpenStack Neutron URL is invalid") api_cmd = url + "/v2.0/subnets" api_cmd_headers = dict() api_cmd_headers['wrs-header'] = 'true' api_cmd_headers['Content-Type'] = "application/json" subnet = dict() if subnet_name is not None: subnet['name'] = subnet_name subnet['network_id'] = network_id subnet['ip_version'] = ip_version subnet['cidr'] = cidr subnet['enable_dhcp'] = dhcp_enabled if gateway_ip is not None: subnet['gateway_ip'] = gateway_ip api_cmd_payload = dict() api_cmd_payload['subnet'] = subnet response = rest_api_request(token, "POST", api_cmd, api_cmd_headers, json.dumps(api_cmd_payload)) return response
def update_subnet(token, subnet_id, gateway_ip=None, delete_gateway=False, dhcp_enabled=None): """ Ask OpenStack Neutron to update a subnet """ url = token.get_service_url(OPENSTACK_SERVICE.NEUTRON) if url is None: raise ValueError("OpenStack Neutron URL is invalid") api_cmd = url + "/v2.0/subnets/%s" % subnet_id api_cmd_headers = dict() api_cmd_headers['wrs-header'] = 'true' api_cmd_headers['Content-Type'] = "application/json" subnet = dict() if gateway_ip is not None: subnet['gateway_ip'] = gateway_ip elif delete_gateway: subnet['gateway_ip'] = None if dhcp_enabled is not None: subnet['enable_dhcp'] = dhcp_enabled api_cmd_payload = dict() api_cmd_payload['subnet'] = subnet response = rest_api_request(token, "PUT", api_cmd, api_cmd_headers, json.dumps(api_cmd_payload)) return response
def upload_image_data_by_url(token, image_id, image_data_url): """ Ask OpenStack Glance to upload image data using a url """ url = token.get_service_url(OPENSTACK_SERVICE.GLANCE, strip_version=True) if url is None: raise ValueError("OpenStack Glance URL is invalid") api_cmd = url + "/v2/images/%s" % image_id api_cmd_headers = dict() api_cmd_headers['Content-Type'] = "application/" + \ "openstack-images-v2.1-json-patch" operations = list() operation = dict() operation['op'] = 'add' operation['path'] = '/locations/0' operation['value'] = {'url': image_data_url, 'metadata': {}} operations.append(operation) api_cmd_payload = operations response = rest_api_request(token, "PATCH", api_cmd, api_cmd_headers, json.dumps(api_cmd_payload)) return response
def publish_meter_sample(token, resource, meter_name, type, unit, sample, timestamp): """ Publish a sample for a meter to OpenStack Ceilometer """ url = token.get_service_url(OPENSTACK_SERVICE.CEILOMETER) if url is None: raise ValueError("OpenStack Ceilometer URL is invalid") api_cmd = url + "/v2/meters/" + meter_name api_cmd_headers = dict() api_cmd_headers['Content-Type'] = "application/json" api_cmd_payload = dict() api_cmd_payload['resource_id'] = resource api_cmd_payload['project_id'] = 'none' api_cmd_payload['user_id'] = 'none' api_cmd_payload['counter_name'] = meter_name api_cmd_payload['counter_type'] = type api_cmd_payload['counter_unit'] = unit api_cmd_payload['counter_volume'] = sample api_cmd_payload['timestamp'] = timestamp response = rest_api_request(token, "POST", api_cmd) return response
def create_volume(token, volume_name, volume_description, size_gb, image_id=None, bootable=None): """ Asks OpenStack Cinder to create a volume """ url = token.get_service_url(OPENSTACK_SERVICE.CINDER) if url is None: raise ValueError("OpenStack Cinder URL is invalid") api_cmd = url + "/volumes" api_cmd_headers = dict() api_cmd_headers['wrs-header'] = 'true' api_cmd_headers['Content-Type'] = "application/json" volume = dict() volume['name'] = volume_name volume['description'] = volume_description volume['size'] = size_gb if image_id is not None: volume['imageRef'] = image_id if bootable is not None: volume['bootable'] = bootable api_cmd_payload = dict() api_cmd_payload['volume'] = volume response = rest_api_request(token, "POST", api_cmd, api_cmd_headers, json.dumps(api_cmd_payload)) return response
def get_volumes(token, page_limit=None, next_page=None, all_tenants=True): """ Asks OpenStack Cinder for a list of volumes """ if next_page is None: url = token.get_service_url(OPENSTACK_SERVICE.CINDER) if url is None: raise ValueError("OpenStack Cinder URL is invalid") api_cmd = url + "/volumes" if page_limit is not None: api_cmd += "?limit=%s" % page_limit if all_tenants: api_cmd += "&all_tenants=1" elif all_tenants: api_cmd += "?all_tenants=1" else: api_cmd = next_page api_cmd_headers = dict() api_cmd_headers['wrs-header'] = 'true' response = rest_api_request(token, "GET", api_cmd, api_cmd_headers) return response
def delete_network_agents(token, host_name): """ Asks OpenStack Neutron to delete agents for a host """ try: url, api_cmd, api_cmd_headers, result_data = _get_network_agents( token, host_name) num_agents_found = 0 supported_agents = [AGENT_TYPE.L3, AGENT_TYPE.DHCP] for agent in result_data: if (agent['host'] == host_name and agent['agent_type'] in supported_agents): api_cmd = url + "/v2.0/agents/%s" % agent['id'] response = rest_api_request(token, "DELETE", api_cmd, api_cmd_headers) num_agents_found = num_agents_found + 1 except Exception as e: DLOG.exception("Caught exception trying to delete host %s agent: %s" % (host_name, e)) return False if num_agents_found != len(supported_agents): DLOG.warn("Host %s, did not find expected agents to delete" % host_name) return True
def query_host_services(token, host_name): """ Asks OpenStack Neutron for the state of services on a host """ url = token.get_service_url(OPENSTACK_SERVICE.NEUTRON) if url is None: raise ValueError("OpenStack Neutron URL is invalid") api_cmd = url + "/v2.0/hosts.json?fields=id&name=%s" % host_name api_cmd_headers = dict() api_cmd_headers['wrs-header'] = 'true' api_cmd_headers['Content-Type'] = "application/json" response = rest_api_request(token, "GET", api_cmd) if 0 == len(response.result_data['hosts']): return None result_data = response.result_data['hosts'][0] host_id = result_data['id'] # Deal with getting an invalid uuid try: uuid.UUID(host_id) except (TypeError, ValueError, AttributeError): DLOG.error("Neutron host query failed for %s, defaulting return " "to down." % host_name) return 'down' api_cmd = url + "/v2.0/hosts/%s" % host_id response = rest_api_request(token, "GET", api_cmd) result_data = response.result_data['host'] host_state = result_data['availability'] # Deal with invalid return value if not (host_state == 'up' or host_state == 'down'): DLOG.error( "Neutron availability state query failed for %s, defaulting " "return to down." % host_name) host_state = 'down' return host_state
def query_hosts(token): """ Asks Patch Controller for information about the hosts in the system """ url = token.get_service_url(PLATFORM_SERVICE.PATCHING, strip_version=True) if url is None: raise ValueError("OpenStack Patching URL is invalid") api_cmd = url + "/v1/query_hosts" response = rest_api_request(token, "GET", api_cmd) return response
def get_hosts(token): """ Asks System Inventory for a list of hosts """ url = token.get_service_url(PLATFORM_SERVICE.SYSINV) if url is None: raise ValueError("OpenStack SysInv URL is invalid") api_cmd = url + "/ihosts" response = rest_api_request(token, "GET", api_cmd) return response
def host_install_async(token, host_name): """ Asks Patch Controller to perform a software upgrade on a host """ url = token.get_service_url(PLATFORM_SERVICE.PATCHING, strip_version=True) if url is None: raise ValueError("OpenStack Patching URL is invalid") api_cmd = url + "/v1/host_install_async/%s" % str(host_name) response = rest_api_request(token, "POST", api_cmd) return response
def get_versions(token): """ Asks OpenStack Heat for a list of versions """ url = token.get_service_url(OPENSTACK_SERVICE.HEAT) if url is None: raise ValueError("OpenStack Heat URL is invalid") api_cmd = url + "/" response = rest_api_request(token, "GET", api_cmd) return response
def get_stack(token, stack_id): """ Asks OpenStack Heat for stack details """ url = token.get_service_url(OPENSTACK_SERVICE.HEAT) if url is None: raise ValueError("OpenStack Heat URL is invalid") api_cmd = url + "/stacks/%s" % stack_id response = rest_api_request(token, "GET", api_cmd) return response
def get_alarms(token): """ Asks OpenStack Ceilometer for a list of alarms """ url = token.get_service_url(OPENSTACK_SERVICE.CEILOMETER) if url is None: raise ValueError("OpenStack Ceilometer URL is invalid") api_cmd = url + "/v2/alarms" response = rest_api_request(token, "GET", api_cmd) return response
def get_image(token, image_id): """ Ask OpenStack Glance for image details """ url = token.get_service_url(OPENSTACK_SERVICE.GLANCE, strip_version=True) if url is None: raise ValueError("OpenStack Glance URL is invalid") api_cmd = url + "/v2/images/%s" % image_id response = rest_api_request(token, "GET", api_cmd) return response
def get_alarms(token): """ Asks Fault Management for customer alarms """ url = token.get_service_url(PLATFORM_SERVICE.FM) if url is None: raise ValueError("OpenStack FM URL is invalid") api_cmd = url + "/alarms?include_suppress=True" response = rest_api_request(token, "GET", api_cmd) return response
def get_upgrade(token): """ Asks System Inventory for information about the upgrade """ url = token.get_service_url(PLATFORM_SERVICE.SYSINV) if url is None: raise ValueError("OpenStack SysInv URL is invalid") api_cmd = url + "/upgrade" response = rest_api_request(token, "GET", api_cmd) return response
def delete_alarm(token, alarm_id): """ Asks OpenStack Ceilometer to delete an alarm """ url = token.get_service_url(OPENSTACK_SERVICE.CEILOMETER) if url is None: raise ValueError("OpenStack Ceilometer URL is invalid") api_cmd = url + "/v2/alarms/%s" % alarm_id response = rest_api_request(token, "DELETE", api_cmd) return response
def delete_stack(token, stack_name, stack_id): """ Asks OpenStack Heat to delete a stack """ url = token.get_service_url(OPENSTACK_SERVICE.HEAT) if url is None: raise ValueError("OpenStack Heat URL is invalid") api_cmd = url + "/stacks/%s/%s" % (stack_name, stack_id) response = rest_api_request(token, "DELETE", api_cmd) return response
def get_host_labels(token, host_uuid): """ Asks System Inventory for host label details """ url = token.get_service_url(PLATFORM_SERVICE.SYSINV) if url is None: raise ValueError("OpenStack SysInv URL is invalid") api_cmd = url + "/ihosts/%s/labels" % host_uuid response = rest_api_request(token, "GET", api_cmd) return response