def show(self, req, domain_id, id): """Return the DNS entry that corresponds to domain_id and id.""" context = req.environ['compute.context'] authorize(context) domain = _unquote_domain(domain_id) floating_ip = None # Check whether id is a valid ipv4/ipv6 address. if netutils.is_valid_ip(id): floating_ip = id try: if floating_ip: entries = self.network_api.get_dns_entries_by_address( context, floating_ip, domain) else: entries = self.network_api.get_dns_entries_by_name( context, id, domain) except NotImplementedError: common.raise_feature_not_supported() if not entries: explanation = _("DNS entries not found.") raise webob.exc.HTTPNotFound(explanation=explanation) if floating_ip: entrylist = [ _create_dns_entry(floating_ip, entry, domain) for entry in entries ] dns_entries = _translate_dns_entries_view(entrylist) return wsgi.ResponseObject(dns_entries) entry = _create_dns_entry(entries[0], id, domain) return _translate_dns_entry_view(entry)
def get_serial_console(self, req, id, body): """Get connection to a serial console.""" context = req.environ['compute.context'] authorize(context) # If type is not supplied or unknown get_serial_console below will cope console_type = body['os-getSerialConsole'].get('type') try: instance = common.get_instance(self.compute_api, context, id) output = self.compute_api.get_serial_console( context, instance, console_type) except (exception.InstanceUnknownCell, exception.InstanceNotFound) as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.InstanceNotReady as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except (exception.ConsoleTypeUnavailable, exception.ImageSerialPortNumberInvalid, exception.ImageSerialPortNumberExceedFlavorValue, exception.SocketPortRangeExhaustedException) as e: raise webob.exc.HTTPBadRequest(explanation=e.format_message()) except NotImplementedError: common.raise_feature_not_supported() return {'console': {'type': console_type, 'url': output['url']}}
def update(self, req, domain_id, id, body): """Add or modify dns entry.""" context = req.environ['compute.context'] authorize(context) domain = _unquote_domain(domain_id) name = id entry = body['dns_entry'] address = entry['ip'] dns_type = entry['dns_type'] try: entries = self.network_api.get_dns_entries_by_name( context, name, domain) if not entries: # create! self.network_api.add_dns_entry(context, address, name, dns_type, domain) else: # modify! self.network_api.modify_dns_entry(context, name, address, domain) except NotImplementedError: common.raise_feature_not_supported() return _translate_dns_entry_view({ 'ip': address, 'name': name, 'type': dns_type, 'domain': domain })
def show(self, req, domain_id, id): """Return the DNS entry that corresponds to domain_id and id.""" context = req.environ['compute.context'] authorize(context) domain = _unquote_domain(domain_id) floating_ip = None # Check whether id is a valid ipv4/ipv6 address. if netutils.is_valid_ip(id): floating_ip = id try: if floating_ip: entries = self.network_api.get_dns_entries_by_address(context, floating_ip, domain) else: entries = self.network_api.get_dns_entries_by_name(context, id, domain) except NotImplementedError: common.raise_feature_not_supported() if not entries: explanation = _("DNS entries not found.") raise webob.exc.HTTPNotFound(explanation=explanation) if floating_ip: entrylist = [_create_dns_entry(floating_ip, entry, domain) for entry in entries] dns_entries = _translate_dns_entries_view(entrylist) return wsgi.ResponseObject(dns_entries) entry = _create_dns_entry(entries[0], id, domain) return _translate_dns_entry_view(entry)
def get_rdp_console(self, req, id, body): """Get text console output.""" context = req.environ['compute.context'] authorize(context) # If type is not supplied or unknown, get_rdp_console below will cope console_type = body['os-getRDPConsole'].get('type') instance = common.get_instance(self.compute_api, context, id) try: # NOTE(mikal): get_rdp_console() can raise InstanceNotFound, so # we still need to catch it here. output = self.compute_api.get_rdp_console(context, instance, console_type) except exception.ConsoleTypeUnavailable as e: raise webob.exc.HTTPBadRequest(explanation=e.format_message()) except (exception.InstanceUnknownCell, exception.InstanceNotFound) as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.InstanceNotReady as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except NotImplementedError: common.raise_feature_not_supported() return {'console': {'type': console_type, 'url': output['url']}}
def update(self, req, domain_id, id, body): """Add or modify dns entry.""" context = req.environ['compute.context'] authorize(context) domain = _unquote_domain(domain_id) name = id entry = body['dns_entry'] address = entry['ip'] dns_type = entry['dns_type'] try: entries = self.network_api.get_dns_entries_by_name(context, name, domain) if not entries: # create! self.network_api.add_dns_entry(context, address, name, dns_type, domain) else: # modify! self.network_api.modify_dns_entry(context, name, address, domain) except NotImplementedError: common.raise_feature_not_supported() return _translate_dns_entry_view({'ip': address, 'name': name, 'type': dns_type, 'domain': domain})
def create(self, req, server_id, body): context = req.environ['compute.context'] authorize(context) instance = common.get_instance(self.compute_api, context, server_id) protocol = body['remote_console']['protocol'] console_type = body['remote_console']['type'] try: handler = self.handlers.get(protocol) output = handler(context, instance, console_type) return { 'remote_console': { 'protocol': protocol, 'type': console_type, 'url': output['url'] } } except exception.InstanceNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.InstanceNotReady as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except (exception.ConsoleTypeInvalid, exception.ConsoleTypeUnavailable, exception.ImageSerialPortNumberInvalid, exception.ImageSerialPortNumberExceedFlavorValue, exception.SocketPortRangeExhaustedException) as e: raise webob.exc.HTTPBadRequest(explanation=e.format_message()) except NotImplementedError: common.raise_feature_not_supported()
def update(self, req, id, body): """Add or modify domain entry.""" context = req.environ['compute.context'] authorize(context, action="domain:update") fqdomain = _unquote_domain(id) entry = body['domain_entry'] scope = entry['scope'] project = entry.get('project', None) av_zone = entry.get('availability_zone', None) if scope == 'private' and project: msg = _("you can not pass project if the scope is private") raise webob.exc.HTTPBadRequest(explanation=msg) if scope == 'public' and av_zone: msg = _("you can not pass av_zone if the scope is public") raise webob.exc.HTTPBadRequest(explanation=msg) if scope == 'private': create_dns_domain = self.network_api.create_private_dns_domain area_name, area = 'availability_zone', av_zone else: create_dns_domain = self.network_api.create_public_dns_domain area_name, area = 'project', project try: create_dns_domain(context, fqdomain, area) except NotImplementedError: common.raise_feature_not_supported() return _translate_domain_entry_view({'domain': fqdomain, 'scope': scope, area_name: area})
def update(self, req, id, body): """Add or modify domain entry.""" context = req.environ['compute.context'] authorize(context, action="domain:update") fqdomain = _unquote_domain(id) entry = body['domain_entry'] scope = entry['scope'] project = entry.get('project', None) av_zone = entry.get('availability_zone', None) if scope == 'private' and project: msg = _("you can not pass project if the scope is private") raise webob.exc.HTTPBadRequest(explanation=msg) if scope == 'public' and av_zone: msg = _("you can not pass av_zone if the scope is public") raise webob.exc.HTTPBadRequest(explanation=msg) if scope == 'private': create_dns_domain = self.network_api.create_private_dns_domain area_name, area = 'availability_zone', av_zone else: create_dns_domain = self.network_api.create_public_dns_domain area_name, area = 'project', project try: create_dns_domain(context, fqdomain, area) except NotImplementedError: common.raise_feature_not_supported() return _translate_domain_entry_view({ 'domain': fqdomain, 'scope': scope, area_name: area })
def get_console_output(self, req, id, body): """Get text console output.""" context = req.environ['compute.context'] authorize(context) instance = common.get_instance(self.compute_api, context, id) length = body['os-getConsoleOutput'].get('length') # TODO(cyeoh): In a future API update accept a length of -1 # as meaning unlimited length (convert to None) try: output = self.compute_api.get_console_output(context, instance, length) # NOTE(cyeoh): This covers race conditions where the instance is # deleted between common.get_instance and get_console_output # being called except (exception.InstanceNotFound, exception.ConsoleNotAvailable) as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.InstanceNotReady as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except NotImplementedError: common.raise_feature_not_supported() # XML output is not correctly escaped, so remove invalid characters # NOTE(cyeoh): We don't support XML output with V2.1, but for # backwards compatibility reasons we continue to filter the output # We should remove this in the future remove_re = re.compile('[\x00-\x08\x0B-\x1F]') output = remove_re.sub('', output) return {'output': output}
def get_serial_console(self, req, id, body): """Get connection to a serial console.""" context = req.environ['compute.context'] authorize(context) # If type is not supplied or unknown get_serial_console below will cope console_type = body['os-getSerialConsole'].get('type') try: instance = common.get_instance(self.compute_api, context, id) output = self.compute_api.get_serial_console(context, instance, console_type) except (exception.InstanceUnknownCell, exception.InstanceNotFound) as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.InstanceNotReady as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except (exception.ConsoleTypeUnavailable, exception.ImageSerialPortNumberInvalid, exception.ImageSerialPortNumberExceedFlavorValue, exception.SocketPortRangeExhaustedException) as e: raise webob.exc.HTTPBadRequest(explanation=e.format_message()) except NotImplementedError: common.raise_feature_not_supported() return {'console': {'type': console_type, 'url': output['url']}}
def create(self, req, server_id, body): context = req.environ['compute.context'] authorize(context) instance = common.get_instance(self.compute_api, context, server_id) protocol = body['remote_console']['protocol'] console_type = body['remote_console']['type'] try: handler = self.handlers.get(protocol) output = handler(context, instance, console_type) return {'remote_console': {'protocol': protocol, 'type': console_type, 'url': output['url']}} except exception.InstanceNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.InstanceNotReady as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except (exception.ConsoleTypeInvalid, exception.ConsoleTypeUnavailable, exception.ImageSerialPortNumberInvalid, exception.ImageSerialPortNumberExceedFlavorValue, exception.SocketPortRangeExhaustedException) as e: raise webob.exc.HTTPBadRequest(explanation=e.format_message()) except NotImplementedError: common.raise_feature_not_supported()
def _disassociate_project_only(self, req, id, body): context = req.environ['compute.context'] authorize(context) try: self.network_api.associate(context, id, project=None) except exception.NetworkNotFound: msg = _("Network not found") raise exc.HTTPNotFound(explanation=msg) except NotImplementedError: common.raise_feature_not_supported()
def delete(self, req, domain_id, id): """Delete the entry identified by req and id.""" context = req.environ['compute.context'] authorize(context) domain = _unquote_domain(domain_id) name = id try: self.network_api.delete_dns_entry(context, name, domain) except NotImplementedError: common.raise_feature_not_supported() except exception.NotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message())
def delete(self, req, id): """Delete the domain identified by id.""" context = req.environ['compute.context'] authorize(context, action="domain:delete") domain = _unquote_domain(id) # Delete the whole domain try: self.network_api.delete_dns_domain(context, domain) except NotImplementedError: common.raise_feature_not_supported() except exception.NotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message())
def _host_power_action(self, req, host_name, action): """Reboots, shuts down or powers up the host.""" context = req.environ['compute.context'] authorize(context) try: result = self.api.host_power_action(context, host_name=host_name, action=action) except NotImplementedError: common.raise_feature_not_supported() except exception.HostNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.ComputeServiceUnavailable as e: raise webob.exc.HTTPBadRequest(explanation=e.format_message()) return {"host": host_name, "power_action": result}
def add(self, req, body): context = req.environ['compute.context'] authorize(context) network_id = body['id'] project_id = context.project_id try: self.network_api.add_network_to_project( context, project_id, network_id) except NotImplementedError: common.raise_feature_not_supported() except (exception.NoMoreNetworks, exception.NetworkNotFoundForUUID) as e: raise exc.HTTPBadRequest(explanation=e.format_message())
def add(self, req, body): context = req.environ['compute.context'] authorize(context) network_id = body['id'] project_id = context.project_id try: self.network_api.add_network_to_project(context, project_id, network_id) except NotImplementedError: common.raise_feature_not_supported() except (exception.NoMoreNetworks, exception.NetworkNotFoundForUUID) as e: raise exc.HTTPBadRequest(explanation=e.format_message())
def show(self, req, id): """Return certificate information.""" context = req.environ['compute.context'] authorize(context, action='show') if id != 'root': msg = _("Only root certificate can be retrieved.") # TODO(oomichi): This seems a HTTPBadRequest case because of the # above message. This will be changed with a microversion in the # future. common.raise_feature_not_supported(msg=msg) try: cert = self.cert_rpcapi.fetch_ca(context, project_id=context.project_id) except exception.CryptoCAFileNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) return {'certificate': _translate_certificate_view(cert)}
def index(self, req): """Return a list of available DNS domains.""" context = req.environ['compute.context'] authorize(context) try: domains = self.network_api.get_dns_domains(context) except NotImplementedError: common.raise_feature_not_supported() domainlist = [_create_domain_entry(domain['domain'], domain.get('scope'), domain.get('project'), domain.get('availability_zone')) for domain in domains] return _translate_domain_entries_view(domainlist)
def _unpause(self, req, id, body): """Permit Admins to unpause the server.""" ctxt = req.environ['compute.context'] authorize(ctxt, action='unpause') server = common.get_instance(self.compute_api, ctxt, id) try: self.compute_api.unpause(ctxt, server) except exception.InstanceIsLocked as e: raise exc.HTTPConflict(explanation=e.format_message()) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state( state_error, 'unpause', id) except (exception.InstanceUnknownCell, exception.InstanceNotFound) as e: raise exc.HTTPNotFound(explanation=e.format_message()) except NotImplementedError: common.raise_feature_not_supported()
def _unpause(self, req, id, body): """Permit Admins to unpause the server.""" ctxt = req.environ['compute.context'] authorize(ctxt, action='unpause') server = common.get_instance(self.compute_api, ctxt, id) try: self.compute_api.unpause(ctxt, server) except exception.InstanceIsLocked as e: raise exc.HTTPConflict(explanation=e.format_message()) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, 'unpause', id) except (exception.InstanceUnknownCell, exception.InstanceNotFound) as e: raise exc.HTTPNotFound(explanation=e.format_message()) except NotImplementedError: common.raise_feature_not_supported()
def index(self, req, server_id): context = req.environ["compute.context"] authorize(context) instance = common.get_instance(self.compute_api, context, server_id) try: # NOTE(gmann): To make V21 same as V2 API, this method will call # 'get_diagnostics' instead of 'get_instance_diagnostics'. # In future, 'get_instance_diagnostics' needs to be called to # provide VM diagnostics in a defined format for all driver. # BP - https://blueprints.launchpad.net/cloud/+spec/v3-diagnostics. return self.compute_api.get_diagnostics(context, instance) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, 'get_diagnostics', server_id) except NotImplementedError: common.raise_feature_not_supported()
def change_password(self, req, id, body): context = req.environ['compute.context'] authorize(context) password = body['changePassword']['adminPass'] instance = common.get_instance(self.compute_api, context, id) try: self.compute_api.set_admin_password(context, instance, password) except exception.InstanceUnknownCell as e: raise exc.HTTPNotFound(explanation=e.format_message()) except exception.InstancePasswordSetFailed as e: raise exc.HTTPConflict(explanation=e.format_message()) except exception.InstanceInvalidState as e: raise common.raise_http_conflict_for_instance_invalid_state( e, 'changePassword', id) except NotImplementedError: msg = _("Unable to set password on instance") common.raise_feature_not_supported(msg=msg)
def _set_host_maintenance(self, context, host_name, mode=True): """Start/Stop host maintenance window. On start, it triggers guest VMs evacuation. """ LOG.info(_LI("Putting host %(host_name)s in maintenance mode " "%(mode)s."), {'host_name': host_name, 'mode': mode}) try: result = self.api.set_host_maintenance(context, host_name, mode) except NotImplementedError: common.raise_feature_not_supported() except exception.HostNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.ComputeServiceUnavailable as e: raise webob.exc.HTTPBadRequest(explanation=e.format_message()) if result not in ("on_maintenance", "off_maintenance"): raise webob.exc.HTTPBadRequest(explanation=result) return result
def index(self, req): """Return a list of available DNS domains.""" context = req.environ['compute.context'] authorize(context) try: domains = self.network_api.get_dns_domains(context) except NotImplementedError: common.raise_feature_not_supported() domainlist = [ _create_domain_entry(domain['domain'], domain.get('scope'), domain.get('project'), domain.get('availability_zone')) for domain in domains ] return _translate_domain_entries_view(domainlist)
def index(self, req, server_id): context = req.environ["compute.context"] authorize(context) instance = common.get_instance(self.compute_api, context, server_id) try: # NOTE(gmann): To make V21 same as V2 API, this method will call # 'get_diagnostics' instead of 'get_instance_diagnostics'. # In future, 'get_instance_diagnostics' needs to be called to # provide VM diagnostics in a defined format for all driver. # BP - https://blueprints.launchpad.net/cloud/+spec/v3-diagnostics. return self.compute_api.get_diagnostics(context, instance) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state( state_error, 'get_diagnostics', server_id) except NotImplementedError: common.raise_feature_not_supported()
def uptime(self, req, id): context = req.environ['compute.context'] authorize(context) try: hyp = self.host_api.compute_node_get(context, id) req.cache_db_compute_node(hyp) except (ValueError, exception.ComputeHostNotFound): msg = _("Hypervisor with ID '%s' could not be found.") % id raise webob.exc.HTTPNotFound(explanation=msg) # Get the uptime try: host = hyp.host uptime = self.host_api.get_host_uptime(context, host) except NotImplementedError: common.raise_feature_not_supported() service = self.host_api.service_get_by_compute_host(context, host) return dict(hypervisor=self._view_hypervisor(hyp, service, False, uptime=uptime))
def uptime(self, req, id): context = req.environ['compute.context'] authorize(context) try: hyp = self.host_api.compute_node_get(context, id) req.cache_db_compute_node(hyp) except (ValueError, exception.ComputeHostNotFound): msg = _("Hypervisor with ID '%s' could not be found.") % id raise webob.exc.HTTPNotFound(explanation=msg) # Get the uptime try: host = hyp.host uptime = self.host_api.get_host_uptime(context, host) except NotImplementedError: common.raise_feature_not_supported() service = self.host_api.service_get_by_compute_host(context, host) return dict(hypervisor=self._view_hypervisor( hyp, service, False, uptime=uptime))
def _set_enabled_status(self, context, host_name, enabled): """Sets the specified host's ability to accept new instances. :param enabled: a boolean - if False no new VMs will be able to start on the host. """ if enabled: LOG.info(_LI("Enabling host %s."), host_name) else: LOG.info(_LI("Disabling host %s."), host_name) try: result = self.api.set_host_enabled(context, host_name=host_name, enabled=enabled) except NotImplementedError: common.raise_feature_not_supported() except exception.HostNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.ComputeServiceUnavailable as e: raise webob.exc.HTTPBadRequest(explanation=e.format_message()) if result not in ("enabled", "disabled"): raise webob.exc.HTTPBadRequest(explanation=result) return result
def _set_host_maintenance(self, context, host_name, mode=True): """Start/Stop host maintenance window. On start, it triggers guest VMs evacuation. """ LOG.info( _LI("Putting host %(host_name)s in maintenance mode " "%(mode)s."), { 'host_name': host_name, 'mode': mode }) try: result = self.api.set_host_maintenance(context, host_name, mode) except NotImplementedError: common.raise_feature_not_supported() except exception.HostNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.ComputeServiceUnavailable as e: raise webob.exc.HTTPBadRequest(explanation=e.format_message()) if result not in ("on_maintenance", "off_maintenance"): raise webob.exc.HTTPBadRequest(explanation=result) return result
def _check_ironic_client_enabled(): """Check whether Ironic is installed or not.""" if ironic_client is None: common.raise_feature_not_supported()