Beispiel #1
0
 def parse_categories(self, headers):
     kind = action = None
     mixins = collections.Counter()
     schemes = collections.defaultdict(list)
     try:
         categories = headers["Category"]
     except KeyError:
         raise exception.OCCIInvalidSchema("No categories")
     for ctg in _quoted_split(categories):
         ll = _quoted_split(ctg, "; ")
         d = {"term": ll[0]}  # assumes 1st element => term's value
         try:
             d.update(dict([_split_unquote(i) for i in ll[1:]]))
         except ValueError:
             raise exception.OCCIInvalidSchema("Unable to parse category")
         ctg_class = d.get("class", None)
         ctg_type = '%(scheme)s%(term)s' % d
         if ctg_class == "kind":
             if kind is not None:
                 raise exception.OCCIInvalidSchema("Duplicated Kind")
             kind = ctg_type
         elif ctg_class == "action":
             if action is not None:
                 raise exception.OCCIInvalidSchema("Duplicated action")
             action = ctg_type
         elif ctg_class == "mixin":
             mixins[ctg_type] += 1
         schemes[d["scheme"]].append(d["term"])
     if action and kind:
         raise exception.OCCIInvalidSchema("Action and kind together?")
     return {
         "category": kind or action,
         "mixins": mixins,
         "schemes": schemes,
     }
Beispiel #2
0
def snf_allocate_floating_ip(cls, req, network_id, device_id, pool=None):
    """Synnefo floating ip allocation method"""
    floating_ips = snf_list_floating_ips(cls, req)
    floating_ip_address = None
    floating_network_id = None

    for floating_ip in floating_ips:
        if floating_ip['port_id'] is None:
            floating_ip_address = floating_ip['floating_ip_address']
            floating_network_id = floating_ip['floating_network_id']

    if not all((
            floating_ip_address,
            floating_network_id,
    )):
        print 'Could not find any available floating ip, creating one...'
        floating_ip = _snf_create_floating_ip(cls, req)
        floating_network_id = floating_ip['floating_network_id']
        floating_ip_address = floating_ip['floating_ip_address']

    _snf_create_port_public_net(cls, req, floating_network_id, device_id,
                                floating_ip_address)
    try:
        link_public = OpenStackHelper._build_link(floating_network_id,
                                                  device_id,
                                                  floating_ip_address,
                                                  floating_network_id)
    except Exception:
        raise exception.OCCIInvalidSchema()

    return link_public
Beispiel #3
0
    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
Beispiel #4
0
    def assign_floating_ip_deprecated(self,
                                      req,
                                      network_id,
                                      device_id,
                                      pool=None):
        """assign floating ip to a server

        :param req: the incoming request
        :param network_id: network id
        :param device_id: device id
        """
        # net_id it is not needed if
        # there is just one port of the VM
        # FIXME(jorgesece): raise an error if the first port has
        # already a floating-ip
        ip = self.allocate_floating_ip(req, pool)
        # Add it to server
        self.associate_floating_ip(req, device_id, ip["ip"])

        try:
            link_public = self._build_link(network_id,
                                           device_id,
                                           ip["ip"],
                                           ip_id=ip["id"],
                                           pool=ip["pool"])
        except Exception:
            raise exception.OCCIInvalidSchema()
        return link_public
Beispiel #5
0
 def _convert_to_headers(self):
     if not self.body:
         raise exception.OCCIInvalidSchema("No schema found")
     hdrs = collections.defaultdict(list)
     for l in self.body.splitlines():
         hdr, content = l.split(":", 1)
         hdrs[hdr].append(content)
     return {hdr: ','.join(hdrs[hdr]) for hdr in hdrs}
Beispiel #6
0
 def parse_links(self, headers):
     links = {}
     try:
         header_links = headers["Link"]
     except KeyError:
         return links
     for link in _quoted_split(header_links):
         ll = _quoted_split(link, "; ")
         # remove the "<" and ">"
         if ll[0][1] != "<" and ll[0][-1] != ">":
             raise exception.OCCIInvalidSchema("Unable to parse link")
         link_dest = ll[0][1:-1]
         try:
             d = dict([_split_unquote(i) for i in ll[1:]])
         except ValueError:
             raise exception.OCCIInvalidSchema("Unable to parse link")
         links[link_dest] = d
     return links
Beispiel #7
0
 def parse(self):
     try:
         obj = json.loads(self.body or "")
     except ValueError:
         raise exception.OCCIInvalidSchema("Unable to parse JSON")
     r = self.parse_categories(obj)
     r['attributes'] = self.parse_attributes(obj)
     r['links'] = self.parse_links(obj)
     return r
Beispiel #8
0
 def parse_attributes(self, headers):
     attrs = {}
     try:
         header_attrs = headers["X-OCCI-Attribute"]
         for attr in _quoted_split(header_attrs):
             try:
                 n, v = attr.split("=", 1)
                 attrs[n.strip()] = self.parse_attribute_value(v)
             except ValueError:
                 raise exception.OCCIInvalidSchema("Unable to parse")
     except KeyError:
         pass
     return attrs
Beispiel #9
0
 def parse_links(self, obj):
     links = collections.defaultdict(list)
     for l in obj.get("links", []):
         try:
             d = {
                 "target": l["target"]["location"],
                 "attributes": copy.copy(l.get("attributes", {})),
             }
             if "id" in l:
                 d["id"] = l["id"]
             links[l["target"]["kind"]].append(d)
         except KeyError:
             raise exception.OCCIInvalidSchema("Unable to parse link")
     return links
Beispiel #10
0
 def parse_links(self, headers):
     links = collections.defaultdict(list)
     try:
         header_links = headers["Link"]
     except KeyError:
         return links
     for link in _quoted_split(header_links):
         ll = _quoted_split(link, "; ")
         # remove the "<" and ">"
         if ll[0][1] != "<" and ll[0][-1] != ">":
             raise exception.OCCIInvalidSchema("Unable to parse link")
         link_id = ll[0][1:-1]
         target_location = None
         target_kind = None
         attrs = {}
         try:
             for attr in ll[1:]:
                 n, v = attr.split("=", 1)
                 n = n.strip().strip('"')
                 v = self.parse_attribute_value(v)
                 if n == "rel":
                     target_kind = v
                     continue
                 elif n == "occi.core.target":
                     target_location = v
                     continue
                 attrs[n] = v
         except ValueError:
             raise exception.OCCIInvalidSchema("Unable to parse link")
         if not (target_kind and target_location):
             raise exception.OCCIInvalidSchema("Unable to parse link")
         links[target_kind].append({
             "target": target_location,
             "attributes": attrs,
             "id": link_id,
         })
     return links
Beispiel #11
0
    def assign_floating_ip(self, req, floatingip_id, device_id):
        """assign floating ip to a server

        :param req: the incoming request
        :param floatingip_id: floating ip id
        :param device_id: device id
        """
        ip = self.get_floating_ip(req, floatingip_id)

        self.associate_floating_ip(req, device_id, ip['ip'])

        try:
            link_public = self._build_link(floatingip_id,
                                           device_id,
                                           ip['ip'],
                                           ip_id=floatingip_id,
                                           public_ip=True)
        except Exception:
            raise exception.OCCIInvalidSchema()
        return link_public
Beispiel #12
0
 def parse_categories(self, obj):
     kind = action = None
     mixins = collections.Counter()
     schemes = collections.defaultdict(list)
     if "kind" in obj:
         sch, term = urllib.parse.urldefrag(obj["kind"])
         schemes[sch + "#"].append(term)
         kind = obj["kind"]
         for m in obj.get("mixins", []):
             mixins[m] += 1
             sch, term = urllib.parse.urldefrag(m)
             schemes[sch + "#"].append(term)
     if "action" in obj:
         action = obj["action"]
         sch, term = urllib.parse.urldefrag(obj["action"])
         schemes[sch + "#"].append(term)
     if action and kind:
         raise exception.OCCIInvalidSchema("Action and kind together?")
     return {
         "category": kind or action,
         "mixins": mixins,
         "schemes": schemes,
     }