def test_update(self): mapping1 = {"occi.foo.1": attribute.Attribute("occi.foo.1", "bar")} mapping2 = {"occi.foo.2": attribute.Attribute("occi.foo.2", "baz")} col1 = attribute.AttributeCollection(mapping1) col2 = attribute.AttributeCollection(mapping2) self.assertEqual(mapping1, col1.attributes) self.assertEqual(mapping2, col2.attributes) col1.update(col2) mapping1.update(mapping2) self.assertEqual(mapping1, col1.attributes)
class StorageResource(resource.Resource): attributes = attr.AttributeCollection( ["occi.storage.size", "occi.storage.state"]) actions = (online, offline, backup, snapshot, resize) kind = kind.Kind(helpers.build_scheme('infrastructure'), 'storage', 'storage resource', attributes, 'storage/', actions=actions, related=[resource.Resource.kind]) def __init__(self, title, summary=None, id=None, size=None, state=None): mixins = [] super(StorageResource, self).__init__(title, mixins, summary=summary, id=id) self.attributes["occi.storage.size"] = attr.MutableAttribute( "occi.storage.size", size) self.attributes["occi.storage.state"] = attr.InmutableAttribute( "occi.storage.state", state) @property def size(self): return self.attributes["occi.storage.size"].value @size.setter def size(self, value): self.attributes["occi.storage.size"].value = value @property def state(self): return self.attributes["occi.storage.state"].value
class Link(entity.Entity): """OCCI Resoure. The Resource type is complemented by the Link type which associates one Resource instance with another. """ attributes = attribute.AttributeCollection( ["occi.core.source", "occi.core.target"]) kind = kind.Kind(helpers.build_scheme("core"), 'link', 'link', attributes, 'link/') def __init__(self, title, mixins, source, target, id=None): super(Link, self).__init__(title, mixins, id) self.attributes["occi.core.source"] = attribute.MutableAttribute( "occi.core.source", source) self.attributes["occi.core.target"] = attribute.MutableAttribute( "occi.core.target", target) @property def source(self): return self.attributes["occi.core.source"].value @source.setter def source(self, value): self.attributes["occi.core.source"].value = value @property def target(self): return self.attributes["occi.core.target"].value @target.setter def target(self, value): self.attributes["occi.core.target"].value = value
class SecurityGroupLink(link.Link): attributes = attr.AttributeCollection({ "occi.securitygrouplink.state": attr.InmutableAttribute("occi.securitygrouplink.state", description="Current state of the instance", attr_type=attr.AttributeType.string_type) }) kind = kind.Kind(helpers.build_scheme('infrastructure'), 'securitygrouplink', 'security group link resource', attributes, 'securitygrouplink/', parent=link.Link.kind) def __init__(self, source, target, state=None): link_id = '_'.join([source.id, target.id]) super(SecurityGroupLink, self).__init__(None, [], source, target, link_id) self.attributes["occi.securitygrouplink.state"] = ( attr.InmutableAttribute.from_attr( self.attributes["occi.securitygrouplink.state"], state)) @property def state(self): return self.attributes["occi.securitygrouplink.state"].value
class IPReservation(network.NetworkResource): attributes = attr.AttributeCollection({ "occi.ipreservation.address": attr.MutableAttribute("occi.ipreservation.address", description="Internet Protocol(IP) network" " address re-served for Compute instances" " linking with this IPReservation instance", attr_type=attr.AttributeType.string_type), "occi.ipreservation.used": attr.InmutableAttribute("occi.ipreservation.used", description="Indication whether" " the reserved address is currently in use.", attr_type=attr.AttributeType.boolean_type), "occi.ipreservation.state": attr.InmutableAttribute( "occi.ipreservation.state", description="Indicates the state of the resource.", attr_type=attr.AttributeType.string_type), }) kind = kind.Kind(helpers.build_scheme('infrastructure'), 'ipreservation', 'IPReservation', attributes, 'ipreservation/', parent=network.NetworkResource.kind) def __init__(self, title, address, id=None, used=False, state=None, mixins=[]): super(IPReservation, self).__init__(title, id=id, mixins=mixins) self.address = address self.attributes["occi.ipreservation.used"] = ( attr.InmutableAttribute.from_attr( self.attributes["occi.ipreservation.used"], used)) self.attributes["occi.ipreservation.state"] = ( attr.InmutableAttribute.from_attr( self.attributes["occi.ipreservation.state"], state)) @property def address(self): return self.attributes["occi.ipreservation.address"].value @address.setter def address(self, value): self.attributes["occi.ipreservation.address"].value = value @property def used(self): return self.attributes["occi.ipreservation.used"].value @property def state(self): return self.attributes["occi.ipreservation.state"].value
class Entity(object): """OCCI Entity. Entity is an abstract type, which both Resource and Link inherit. Each sub-type of Entity is identified by a unique Kind instance """ attributes = attribute.AttributeCollection({ "occi.core.id": attribute.InmutableAttribute( "occi.core.id", description="A unique identifier", attr_type=attribute.AttributeType.string_type), "occi.core.title": attribute.MutableAttribute( "occi.core.title", description="The display name of the instance", attr_type=attribute.AttributeType.string_type), }) kind = kind.Kind(helpers.build_scheme('core'), 'entity', 'entity', attributes, 'entity/') actions = None def __init__(self, title, mixins, id=None): helpers.check_type(mixins, mixin.Mixin) self.mixins = mixins # NOTE(aloga): we need a copy of the attributes, otherwise we will be # using the class ones instead of the object ones. self.attributes = self.attributes.copy() # damn, we're shading a builtin if id is None: id = uuid.uuid4().hex self.attributes["occi.core.id"] = ( attribute.InmutableAttribute.from_attr( self.attributes["occi.core.id"], id)) self.title = title @property def id(self): return self.attributes["occi.core.id"].value @property def title(self): return self.attributes["occi.core.title"].value @title.setter def title(self, value): self.attributes["occi.core.title"].value = value @property def location(self): return utils.join_url(self.kind.location, self.id)
class Resource(entity.Entity): """OCCI Resource. The heart of the OCCI Core Model is the Resource type. Any resource exposed through OCCI is a Resource or a sub-type thereof. A resource can be e.g. a virtual machine, a job in a job submission system, a user, etc. The Resource type is complemented by the Link type which associates one Resource instance with another. The Link type contains a number of common attributes that Link sub-types inherit. """ attributes = attribute.AttributeCollection({ "occi.core.summary": attribute.MutableAttribute( "occi.core.summary", description=("A summarizing description of " "the resource instance."), attr_type=attribute.AttributeType.string_type), }) kind = kind.Kind(helpers.build_scheme('core'), 'resource', 'resource', attributes, 'resource/', parent=entity.Entity.kind) def __init__(self, title, mixins, id=None, summary=None): super(Resource, self).__init__(title, mixins, id=id) self.summary = summary self._links = [] def __eq__(self, other): return all([ self.attributes[i].value == other.attributes[i].value for i in self.attributes ]) @property def links(self): return self._links def link(self, target, mixins=[]): l = link.Link("", mixins, self, target) self._links.append(l) def add_link(self, link): self._links.append(link) @property def summary(self): return self.attributes["occi.core.summary"].value @summary.setter def summary(self, value): self.attributes["occi.core.summary"].value = value
def __init__(self, pool=None): term = "osnetwork" title = "openstack network" super(OSNetwork, self).__init__(scheme=self.scheme, term=term, title=title, attributes=attr.AttributeCollection( ["org.openstack.network.ip_version"]))
def test_kind_attributes(self): attr = attribute.MutableAttribute("org.example", "foo", description="bar", default="baz") knd = kind.Kind("scheme", "term", "title", attributes=attribute.AttributeCollection({ "org.example": attr} )) r = self.renderer.get_renderer(knd) observed = r.render() self.assertKindAttr(knd, attr, observed)
def __init__(self, scheme, term, title, attributes=None, location=None): self.scheme = scheme self.term = term self.title = title if attributes is None: self.attributes = attribute.AttributeCollection() elif not isinstance(attributes, attribute.AttributeCollection): raise TypeError("attributes must be an AttributeCollection") self.attributes = attributes self.location = location
def __init__(self, user_data=None): attrs = [ attribute.InmutableAttribute("org.openstack.compute.user_data", user_data), ] attrs = attribute.AttributeCollection({a.name: a for a in attrs}) super(OpenStackUserData, self).__init__(OpenStackUserData.scheme, OpenStackUserData.term, "Contextualization extension - user_data", attributes=attrs)
class NetworkResource(resource.Resource): attributes = attr.AttributeCollection( ["occi.network.vlan", "occi.network.label", "occi.network.state"]) actions = (up, down) kind = kind.Kind(helpers.build_scheme('infrastructure'), 'network', 'network resource', attributes, '/network/', actions=actions, related=[resource.Resource.kind]) def __init__(self, title, summary=None, id=None, vlan=None, label=None, state=None, mixins=[]): super(NetworkResource, self).__init__(title, mixins, summary=summary, id=id) self.attributes["occi.network.vlan"] = attr.MutableAttribute( "occi.network.vlan", vlan) self.attributes["occi.network.label"] = attr.MutableAttribute( "occi.network.label", label) self.attributes["occi.network.state"] = attr.InmutableAttribute( "occi.network.state", state) @property def vlan(self): return self.attributes["occi.network.vlan"].value @vlan.setter def vlan(self, value): self.attributes["occi.network.vlan"].value = value @property def label(self): return self.attributes["occi.network.label"].value @label.setter def label(self, value): self.attributes["occi.network.label"].value = value @property def state(self): return self.attributes["occi.network.state"].value
class StorageLink(link.Link): attributes = attr.AttributeCollection([ "occi.storagelink.deviceid", "occi.storagelink.mountpoint", "occi.storagelink.state" ]) kind = kind.Kind(helpers.build_scheme('infrastructure'), 'storagelink', 'storage link resource', attributes, '/storagelink/', related=[link.Link.kind]) def __init__(self, source, target, deviceid=None, mountpoint=None, state=None): # TODO(enolfc): is this a valid link id? link_id = '_'.join([source.id, target.id]) super(StorageLink, self).__init__(None, [], source, target, link_id) self.attributes["occi.storagelink.deviceid"] = attr.MutableAttribute( "occi.storagelink.deviceid", deviceid) self.attributes["occi.storagelink.mountpoint"] = attr.MutableAttribute( "occi.storagelink.mountpoint", mountpoint) self.attributes["occi.storagelink.state"] = attr.InmutableAttribute( "occi.storagelink.state", state) @property def deviceid(self): return self.attributes["occi.storagelink.deviceid"].value @deviceid.setter def deviceid(self, value): self.attributes["occi.storagelink.deviceid"].value = value @property def mountpoint(self): return self.attributes["occi.storagelink.mountpoint"].value @mountpoint.setter def mountpoint(self, value): self.attributes["occi.storagelink.mountpoint"].value = value @property def state(self): return self.attributes["occi.storagelink.state"].value
def __init__(self, name=None, data=None): attrs = [ attribute.InmutableAttribute( "org.openstack.credentials.publickey.name", name), attribute.InmutableAttribute( "org.openstack.credentials.publickey.data", data), ] attrs = attribute.AttributeCollection({a.name: a for a in attrs}) super(OpenStackPublicKey, self).__init__(OpenStackPublicKey.scheme, OpenStackPublicKey.term, "Contextualization extension - public_key", attributes=attrs)
class NetworkInterface(link.Link): attributes = attr.AttributeCollection([ "occi.networkinterface.interface", "occi.networkinterface.mac", "occi.networkinterface.state" ]) kind = kind.Kind(helpers.build_scheme('infrastructure'), 'networkinterface', 'network link resource', attributes, 'networklink/', related=[link.Link.kind]) def __init__(self, mixins, source, target, id=None, interface=None, mac=None, state=None): super(NetworkInterface, self).__init__(None, mixins, source, target, id) self.attributes["occi.networkinterface.interface"] = ( attr.InmutableAttribute("occi.networkinterface.interface", interface)) self.attributes["occi.networkinterface.mac"] = attr.MutableAttribute( "occi.networkinterface.mac", mac) self.attributes["occi.networkinterface.state"] = ( attr.InmutableAttribute("occi.networkinterface.state", state)) @property def interface(self): return self.attributes["occi.networkinterface.interface"].value @property def mac(self): return self.attributes["occi.networkinterface.mac"].value @mac.setter def mac(self, value): self.attributes["occi.networkinterface.mac"].value = value @property def state(self): return self.attributes["occi.networkinterface.state"].value
class Link(entity.Entity): """OCCI Resoure. The Resource type is complemented by the Link type which associates one Resource instance with another. """ attributes = attribute.AttributeCollection({ "occi.core.source": attribute.MutableAttribute( "occi.core.source", required=True, description="The Resource instance the link originates from", attr_type=attribute.AttributeType.object_type), "occi.core.target": attribute.MutableAttribute( "occi.core.target", required=True, description=("The unique identifier of an Object this Link " "instance points to"), attr_type=attribute.AttributeType.object_type), }) kind = kind.Kind(helpers.build_scheme("core"), 'link', 'link', attributes, 'link/') def __init__(self, title, mixins, source, target, id=None): super(Link, self).__init__(title, mixins, id) self.source = source self.target = target @property def source(self): return self.attributes["occi.core.source"].value @source.setter def source(self, value): self.attributes["occi.core.source"].value = value @property def target(self): return self.attributes["occi.core.target"].value @target.setter def target(self, value): self.attributes["occi.core.target"].value = value
class OSNetworkInterface(network_link.NetworkInterface): attributes = attr.AttributeCollection(["occi.networkinterface.address", "occi.networkinterface.gateway", "occi.networkinterface.allocation"]) def __init__(self, source, target, mac, address, ip_id=None, pool=None, state='active'): link_id = '_'.join([source.id, target.id, address]) mixins = [network_link.ip_network_interface] if pool: mixins.append(OSFloatingIPPool(pool)) super(OSNetworkInterface, self).__init__(mixins, source, target, link_id, "eth0", mac, state) self.ip_id = ip_id self.attributes["occi.networkinterface.address"] = ( attr.MutableAttribute("occi.networkinterface.address", address)) self.attributes["occi.networkinterface.gateway"] = ( attr.MutableAttribute("occi.networkinterface.gateway", None)) self.attributes["occi.networkinterface.allocation"] = ( attr.MutableAttribute("occi.networkinterface.allocation", "dynamic")) @property def address(self): return self.attributes["occi.networkinterface.address"].value @address.setter def address(self, value): self.attributes["occi.networkinterface.address"].value = value @property def gateway(self): return self.attributes["occi.networkinterface.gateway"].value @gateway.setter def gateway(self, value): self.attributes["occi.networkinterface.gateway"].value = value @property def allocation(self): return self.attributes["occi.networkinterface.allocation"].value @allocation.setter def allocation(self, value): self.attributes["occi.networkinterface.allocation"].value = value
class SecurityGroupResource(resource.Resource): attributes = attr.AttributeCollection({ "occi.securitygroup.rules": attr.MutableAttribute("occi.securitygroup.rules", description="Security Rules", attr_type=attr.AttributeType.list_type), "occi.securitygroup.state": attr.InmutableAttribute("occi.securitygroup.state", description="Current state of the instance", attr_type=attr.AttributeType.string_type) }) kind = kind.Kind(helpers.build_scheme('infrastructure'), 'securitygroup', 'securitygroup resource', attributes, 'securitygroup/', parent=resource.Resource.kind) def __init__(self, title, id=None, rules=None, summary=None, state=None, mixins=[]): super(SecurityGroupResource, self).__init__(title, mixins, summary=summary, id=id) self.rules = rules self.attributes["occi.securitygroup.state"] = ( attr.InmutableAttribute.from_attr( self.attributes["occi.securitygroup.state"], state)) @property def rules(self): return self.attributes["occi.securitygroup.rules"].value @rules.setter def rules(self, value): self.attributes["occi.securitygroup.rules"].value = value @property def state(self): return self.attributes["occi.securitygroup.state"].value
def __init__(self, id, name, cores, memory, disk, ephemeral=0, swap=0): attrs = [ attribute.InmutableAttribute("occi.compute.cores", cores), attribute.InmutableAttribute("occi.compute.memory", memory), attribute.InmutableAttribute("occi.compute.disk", disk), attribute.InmutableAttribute("occi.compute.ephemeral", ephemeral), attribute.InmutableAttribute("occi.compute.swap", swap), attribute.InmutableAttribute("org.openstack.flavor.name", name) ] attrs = attribute.AttributeCollection({a.name: a for a in attrs}) super(OpenStackResourceTemplate, self).__init__(OpenStackResourceTemplate.scheme, id, "Flavor: %s" % name, related=[templates.resource_tpl], attributes=attrs)
def __init__(self, ssh_key=None): attrs = [ attribute.MutableAttribute( "occi.crendentials.ssh.publickey", ssh_key, required=True, description=("The contents of the public key file to be " "injected into the Compute Resource"), attr_type=attribute.AttributeType.string_type), ] attrs = attribute.AttributeCollection({a.name: a for a in attrs}) super(SSHKey, self).__init__(SSHKey.scheme, SSHKey.term, "Credentials mixin", attributes=attrs, applies=[compute.ComputeResource.kind])
def __init__(self, user_data=None): attrs = [ attribute.MutableAttribute( "occi.compute.userdata", user_data, required=True, description=("Contextualization data (e.g., script, " "executable) that the client supplies once and " "only once. It cannot be updated."), attr_type=attribute.AttributeType.string_type), ] attrs = attribute.AttributeCollection({a.name: a for a in attrs}) super(UserData, self).__init__(UserData.scheme, UserData.term, "Contextualization mixin", attributes=attrs, applies=[compute.ComputeResource.kind])
def __init__(self, id, name, cores, memory, disk, ephemeral=0, swap=0): attrs = [ attribute.InmutableAttribute( "occi.compute.cores", cores, attr_type=attribute.AttributeType.number_type), attribute.InmutableAttribute( "occi.compute.memory", memory, attr_type=attribute.AttributeType.number_type), attribute.InmutableAttribute( "org.openstack.flavor.disk", disk, attr_type=attribute.AttributeType.number_type), attribute.InmutableAttribute( "org.openstack.flavor.ephemeral", ephemeral, attr_type=attribute.AttributeType.number_type), attribute.InmutableAttribute( "org.openstack.flavor.swap", swap, attr_type=attribute.AttributeType.number_type), attribute.InmutableAttribute( "org.openstack.flavor.name", name, attr_type=attribute.AttributeType.string_type), ] attrs = attribute.AttributeCollection({a.name: a for a in attrs}) location = "%s/%s" % (self._location, id) super(OpenStackResourceTemplate, self).__init__(id, "Flavor: %s" % name, depends=[templates.resource_tpl], attributes=attrs, location=location)
class NetworkResource(resource.Resource): attributes = attr.AttributeCollection({ "occi.network.vlan": attr.MutableAttribute("occi.network.vlan", description="802.1q VLAN identifier", attr_type=attr.AttributeType.string_type), "occi.network.label": attr.MutableAttribute("occi.network.label", description="Tag based VLANs", attr_type=attr.AttributeType.string_type), "occi.network.state": attr.InmutableAttribute("occi.network.state", description="Current state of the instance", attr_type=attr.AttributeType.string_type), "occi.network.state.message": attr.InmutableAttribute( "occi.network.state.message", description=("Human-readable explanation of the current instance " "state"), attr_type=attr.AttributeType.string_type), }) actions = (up, down) kind = kind.Kind(helpers.build_scheme('infrastructure'), 'network', 'network resource', attributes, 'network/', actions=actions, parent=resource.Resource.kind) def __init__(self, title, summary=None, id=None, vlan=None, label=None, state=None, message=None, mixins=[]): super(NetworkResource, self).__init__(title, mixins, summary=summary, id=id) self.vlan = vlan self.label = label self.attributes["occi.network.state"] = ( attr.InmutableAttribute.from_attr( self.attributes["occi.network.state"], state)) self.attributes["occi.network.state.message"] = ( attr.InmutableAttribute( self.attributes["occi.network.state.message"], message)) @property def vlan(self): return self.attributes["occi.network.vlan"].value @vlan.setter def vlan(self, value): self.attributes["occi.network.vlan"].value = value @property def label(self): return self.attributes["occi.network.label"].value @label.setter def label(self, value): self.attributes["occi.network.label"].value = value @property def state(self): return self.attributes["occi.network.state"].value @property def message(self): return self.attributes["occi.network.state.message"].value
def test_collection(self): col = attribute.AttributeCollection() self.assertEqual({}, col.attributes)
def state(self): return self.attributes["occi.network.state"].value @property def message(self): return self.attributes["occi.network.state.message"].value ip_network = mixin.Mixin( helpers.build_scheme("infrastructure/network"), "ipnetwork", "IP Networking Mixin", attributes=attr.AttributeCollection({ "occi.network.address": attr.MutableAttribute( "occi.network.address", description="Internet Protocol (IP) network address", attr_type=attr.AttributeType.string_type), "occi.network.gateway": attr.MutableAttribute( "occi.network.gateway", description="Internet Protocol (IP) network address", attr_type=attr.AttributeType.string_type), "occi.network.allocation": attr.MutableAttribute( "occi.network.allocation", description="Address allocation mechanism: dynamic, static", attr_type=attr.AttributeType.string_type), }), applies=[NetworkResource.kind])
self.attributes["occi.networkinterface.mac"] = attr.MutableAttribute( "occi.networkinterface.mac", mac) self.attributes["occi.networkinterface.state"] = ( attr.InmutableAttribute("occi.networkinterface.state", state)) @property def interface(self): return self.attributes["occi.networkinterface.interface"].value @property def mac(self): return self.attributes["occi.networkinterface.mac"].value @mac.setter def mac(self, value): self.attributes["occi.networkinterface.mac"].value = value @property def state(self): return self.attributes["occi.networkinterface.state"].value ip_network_interface = mixin.Mixin( helpers.build_scheme("infrastructure/networkinterface"), "ipnetworkinterface", "IP Network interface Mixin", attributes=attr.AttributeCollection([ "occi.networkinterface.address", "occi.networkinterface.gateway", "occi.networkinterface.allocation" ]))
def test_collection_raises_if_not_set(self): col = attribute.AttributeCollection(["foo"]) self.assertRaises(AttributeError, col.__getitem__, "foo")
def test_update_invalid(self): mapping = {"occi.foo.1": attribute.Attribute("occi.foo.1", "bar")} col = attribute.AttributeCollection(mapping) self.assertRaises(TypeError, col.update, {"foo": "bar"})
def test_collection_from_seq(self): seq = ["foo", "bar"] col = attribute.AttributeCollection(seq) self.assertItemsEqual(seq, col.attributes.keys())
def test_collection_from_map(self): mapping = {"foo": attribute.Attribute("occi.foo.bar", "crap")} col = attribute.AttributeCollection(mapping) self.assertEqual(mapping, col.attributes)