def register_item(self, context, instance_name, nic, addr, addr_type, name): if not nic: msg = _("Network interface is invalid or empty") raise exception.InvalidRequest(msg) if addr_type is None: addr_type = self.DEFAULT_ACCESS_CONFIG_TYPE elif addr_type != self.DEFAULT_ACCESS_CONFIG_TYPE: msg = _("Only '%s' type of access config currently supported.")\ % self.DEFAULT_ACCESS_CONFIG_TYPE raise exception.InvalidRequest(msg) if name is None: name = self.DEFAULT_ACCESS_CONFIG_NAME if not addr: msg = _("There is no address to assign.") raise exception.InvalidRequest(msg) new_item = { "id": instance_name + "-" + addr, "instance_name": instance_name, "nic": nic, "name": name, "type": addr_type, "addr": addr } new_item = self._add_db_item(context, new_item) return new_item
def add_item(self, context, instance_name, params, source, name, auto_delete, scope): # NOTE(apavlov): name is a 'device_name' here if not name: msg = _("There is no name to assign.") raise exception.InvalidRequest(msg) nova_client = clients.nova(context) instances = nova_client.servers.list( search_opts={"name": instance_name}) if not instances or len(instances) != 1: raise exception.NotFound instance = instances[0] devices = list() volumes_client = nova_client.volumes for server_volume in volumes_client.get_server_volumes(instance.id): devices.append(server_volume.device) device_name = None for letter in string.ascii_lowercase[1:]: device_name = "vd" + letter for device in devices: if device_name in device: break else: break else: raise exception.OverQuota context.operation_data["device_name"] = device_name if source: volume_name = utils._extract_name_from_url(source) if not volume_name: msg = _("There is no volume to assign.") raise exception.NotFound(msg) volume = disk_api.API().get_item(context, volume_name, scope) context.operation_data["volume_id"] = volume["id"] elif params: params.setdefault("diskName", instance_name) context.operation_data["params"] = params context.operation_data["scope"] = scope else: msg = _('Disk config must contain either "source" or ' '"initializeParams".') raise exception.InvalidRequest(msg) context.operation_data["instance_id"] = instance.id context.operation_data["register_args"] = [ instance_name, name, auto_delete ] operation_util.start_operation( context, base_api.API._get_complex_operation_progress) operation_util.continue_operation(context, lambda: self._attach_volume(context), timeout=0)
def _check_rules(self, firewall): if not (firewall.get('sourceRanges') or firewall.get('sourceTags')): msg = _("Not 'sourceRange' neither 'sourceTags' is provided") raise exception.InvalidRequest(msg) for allowed in firewall.get('allowed', []): proto = allowed.get('IPProtocol') proto = PROTOCOL_MAP.get(proto, proto) if not proto or proto not in PROTOCOL_MAP.values(): msg = _("Invlaid protocol") raise exception.InvalidRequest(msg) if proto == 'icmp' and allowed.get('ports'): msg = _("Invalid options for icmp protocol") raise exception.InvalidRequest(msg)
def register_item(self, context, instance_name, volume_id, name, auto_delete): if not name: msg = _("There is no name to assign.") raise exception.InvalidRequest(msg) if not volume_id: msg = _("There is no volume_id to assign.") raise exception.InvalidRequest(msg) new_item = { "id": instance_name + "-" + volume_id, "instance_name": instance_name, "volume_id": volume_id, "name": name, "auto_delete": auto_delete } new_item = self._add_db_item(context, new_item) return new_item
def add_item(self, context, instance_name, nic, addr, addr_type, name): if not nic: msg = _("Network interface is invalid or empty") raise exception.InvalidRequest(msg) if addr_type is None: addr_type = self.DEFAULT_ACCESS_CONFIG_TYPE elif addr_type != self.DEFAULT_ACCESS_CONFIG_TYPE: msg = _("Only '%s' type of access config currently supported.")\ % self.DEFAULT_ACCESS_CONFIG_TYPE raise exception.InvalidRequest(msg) client = clients.nova(context) instances = client.servers.list(search_opts={"name": instance_name}) if not instances or len(instances) != 1: raise exception.NotFound instance = instances[0] fixed_ip = None for network in instance.addresses: if nic != network: continue for address in instance.addresses[network]: atype = address["OS-EXT-IPS:type"] if atype == "floating": msg = _('At most one access config currently supported.') raise exception.InvalidRequest(msg) if atype == "fixed": fixed_ip = address["addr"] if not fixed_ip: msg = _('Network interface not found') raise exception.InvalidRequest(msg) floating_ips = client.floating_ips.list() if addr is None: # NOTE(apavlov): try to find unused for floating_ip in floating_ips: if floating_ip.instance_id is None: addr = floating_ip.ip break else: msg = _('There is no unused floating ips.') raise exception.InvalidRequest(msg) else: for floating_ip in floating_ips: if floating_ip.ip != addr: continue if floating_ip.instance_id is None: break msg = _("Floating ip '%s' is already associated") % floating_ip raise exception.InvalidRequest(msg) else: msg = _("There is no such floating ip '%s'.") % addr raise exception.InvalidRequest(msg) operation_util.start_operation(context) instance.add_floating_ip(addr, fixed_ip) return self.register_item(context, instance_name, nic, addr, addr_type, name)