def assign_floating_ip(self, req, device_id): """assign floating ip to a server :param req: the incoming request :param device_id: device id """ # net_id it is not needed if # there is just one port of the VM attributes_port = {'device_id': device_id} try: net_public = self._get_public_network(req) except Exception: raise exception.NetworkNotFound() try: ports = self.list_resources(req, 'ports', attributes_port) port_id = ports[0]['id'] # subnet_id = ports[0]['fixed_ips'][0]['subnet_id'] except Exception: raise exception.NotFound() response = self._add_floating_ip(req, net_public, port_id) try: link_public = self._build_link( ports[0]['network_id'], attributes_port['device_id'], response['floating_ip_address'], pool=response['floating_network_id']) except Exception: raise exception.OCCIInvalidSchema() return link_public
def get_compute_net_link(self, req, compute_id, network_id, ip): """Get a specific network/server link It shows a specific link (either private or public ip) :param req: the incoming request :param compute_id: server id :param network_id: network id :param ip: ip connected """ try: if network_id == os_helpers.PUBLIC_NETWORK: param = {'floating_ip_address': ip} flo_ips = self.list_resources(req, 'floatingips', param) for f_ip in flo_ips: link_public = self._build_link( network_id, compute_id, f_ip['floating_ip_address'], pool=f_ip['floating_network_id']) return link_public else: param_ports = { 'device_id': compute_id, 'network_id': network_id } ports = self.list_resources(req, 'ports', param_ports) for p in ports: if ip == p["fixed_ips"][0]["ip_address"]: link_private = self._build_link( p["network_id"], p['device_id'], p["fixed_ips"][0]["ip_address"], mac=p["mac_address"], state=p["status"]) return link_private raise exception.NotFound() except Exception: raise exception.NotFound()
def get_compute_net_link(self, req, compute_id, address): """Get a specific network/server link It shows a specific link (either private or public ip) :param req: the incoming request :param compute_id: server id :param address: ip connected """ compute = self.get_server(req, compute_id) server_addrs = compute.get("addresses", {}) compute_id = compute["id"] ports = self._get_ports(req, compute_id) floating_ips = self.get_floating_ips(req) for addr_set in server_addrs.values(): for addr in addr_set: if addr["addr"] == address: public_ip = False pool = None ip_id = None mac = addr["OS-EXT-IPS-MAC:mac_addr"] ip_type = addr["OS-EXT-IPS:type"] if ip_type == "fixed": network_id = "FIXED" for p in ports: if p['mac_addr'] == mac: ip_id = str(p['port_id']) network_id = str(p['net_id']) break else: network_id = "PUBLIC" for fp in floating_ips: if compute_id == fp['instance_id']: pool = fp['pool'] ip_id = str(fp['id']) network_id = str(fp['id']) public_ip = True break return self._build_link(network_id, compute_id, address, mac=mac, pool=pool, ip_id=ip_id, public_ip=public_ip) raise exception.NotFound()
def list_compute_net_links(self, req, network_id, device_id): """List the network and compute links It lists every private and public ip related to the servers of the tenant :param req: the incoming request :param network_id: id network :param device_id: id device """ param_port = { 'device_owner': 'compute:nova', 'device_id': device_id, 'network_id': network_id } link_list = [] try: ports = self.list_resources(req, 'ports', param_port) for port in ports: link_private = self._build_link( port["network_id"], port['device_id'], port["fixed_ips"][0]["ip_address"], mac=port["mac_address"], state=os_helpers.network_status(port["status"])) link_list.append(link_private) # Query public links associated to the port floating_ips = self.list_resources(req, 'floatingips', {"port_id": port['id']}) for f_ip in floating_ips: link_public = self._build_link( port["network_id"], port['device_id'], f_ip['floating_ip_address'], mac=port["mac_address"], pool=f_ip['floating_network_id']) link_list.append(link_public) except Exception: raise exception.NotFound() return link_list
def get_compute_net_link(self, req, compute_id, network_id, address): """Get a specific network/server link It shows a specific link (either private or public ip) :param req: the incoming request :param compute_id: server id :param network_id: network id :param address: ip connected """ s = self.get_server(req, compute_id) pool = None ip_id = None server_addrs = s.get("addresses", {}) for addr_set in server_addrs.values(): for addr in addr_set: if addr["addr"] == address: mac = addr["OS-EXT-IPS-MAC:mac_addr"] if network_id == os_helpers.PUBLIC_NETWORK: floating_ips = self.get_floating_ips(req) for ip in floating_ips: if address == ip['ip']: pool = ip['pool'] ip_id = ip['id'] else: ports = self._get_ports(req, compute_id) for p in ports: if p["net_id"] == network_id: for ip in p["fixed_ips"]: if ip['ip_address'] == address: ip_id = p['port_id'] return self._build_link(network_id, compute_id, address, mac=mac, pool=pool, ip_id=ip_id) raise exception.NotFound()
def create_server_security_link(self, req, server_id, securitygroup_id): """Create security group link in a server :param req: incoming request :param server_id: server id :param securitygroup_id: segurity group id :return: empty """ tenant_id = self.tenant_from_req(req) path = "/%s/servers/%s/action" % (tenant_id, server_id) sg = self._get_security_group(req, securitygroup_id) if "name" not in sg: raise exception.NotFound("Security group %s not found." % securitygroup_id) param = {"name": sg["name"]} body = utils.make_body('addSecurityGroup', param) os_req = self._get_req(req, path=path, content_type="application/json", body=json.dumps(body), method="POST") os_req.get_response(self.app) return []