Partial handling for automatic conversion

With basic dictionary available, we can now
do automatic conversion for direct property
conversion.  Still need to update
to handle hierarchy.

Change-Id: Idafce7cf6d6cd539c8724b64939d402497a9e074
This commit is contained in:
Travis Tripp 2014-05-10 23:53:57 -06:00
parent 09c98674f0
commit 7630b0443c
2 changed files with 80 additions and 28 deletions

View File

@ -20,6 +20,7 @@ import glob
import json import json
import os import os
from graffiti.api.model.v1.capability import Capability
from graffiti.api.model.v1.derived_type import DerivedType from graffiti.api.model.v1.derived_type import DerivedType
@ -139,3 +140,36 @@ def get_qualifier(property_name, property_value):
return property_dict[key2] return property_dict[key2]
return None return None
def resolve_capability(key, value, target_resource,
default_namespace=None,
default_name=None):
resolved_type = None
resolved_namespace = None
cap_and_namespace = get_qualifier(
key,
value
)
if cap_and_namespace:
resolved_type = cap_and_namespace.name
resolved_namespace = cap_and_namespace.namespace
else:
resolved_namespace = default_namespace
resolved_type = default_name
resolved_capability = None
for capability in target_resource.capabilities:
if capability.capability_type_namespace == resolved_namespace \
and capability.capability_type == resolved_type:
resolved_capability = capability
if not resolved_capability:
resolved_capability = Capability()
resolved_capability.capability_type_namespace = resolved_namespace
resolved_capability.capability_type = resolved_type
resolved_capability.properties = {}
target_resource.capabilities.append(resolved_capability)
resolved_capability.properties[key] = value

View File

@ -16,6 +16,7 @@
from graffiti.api.model.v1.capability import Capability from graffiti.api.model.v1.capability import Capability
from graffiti.api.model.v1.resource import Resource from graffiti.api.model.v1.resource import Resource
from graffiti.common import exception from graffiti.common import exception
from graffiti.common import utils
from graffiti.drivers import base from graffiti.drivers import base
from novaclient.v1_1 import client from novaclient.v1_1 import client
@ -33,7 +34,6 @@ class NovaResourceDriver(base.ResourceInterface):
self.endpoint_type = 'publicURL' self.endpoint_type = 'publicURL'
self.default_namespace = "OS::COMPUTE::CPU" self.default_namespace = "OS::COMPUTE::CPU"
self.default_resource_type = "OS::Nova::Flavor" self.default_resource_type = "OS::Nova::Flavor"
self.default_extraspec_key = "capabilities:cpu_info"
def get_resource(self, resource_type, resource_id, auth_token, def get_resource(self, resource_type, resource_id, auth_token,
endpoint_id=None, **kwargs): endpoint_id=None, **kwargs):
@ -202,47 +202,65 @@ class NovaResourceDriver(base.ResourceInterface):
property_name = None property_name = None
property_value = None property_value = None
if key in extraspecs_key_map: if not key.startswith("capabilities:cpu_info"):
capability_type, property_name, property_value = \ utils.resolve_capability(
extraspecs_key_map[key](key, extra_spec_keys[key]) key, extra_spec_keys[key], flavor_resource)
else: else:
capability_type, property_name, property_value = \ #TODO(Facundo)This can be automatic using dictionary
extractSimpleValue(key, extra_spec_keys[key]) if key in extraspecs_key_map:
capability_type, property_name, property_value = \
extraspecs_key_map[key](key, extra_spec_keys[key])
else:
capability_type, property_name, property_value = \
extractSimpleValue(key, extra_spec_keys[key])
capability_property = {property_name: property_value} capability = Capability()
capability.capability_type_namespace = capability_namespace
capability.capability_type = capability_type
capability.properties = {property_name: property_value}
flavor_capability = Capability() flavor_resource.capabilities.append(capability)
flavor_capability.properties = {}
flavor_capability.capability_type_namespace = capability_namespace
flavor_capability.capability_type = capability_type
flavor_capability.properties.update(capability_property)
flavor_resource.capabilities.append(flavor_capability)
return flavor_resource return flavor_resource
def transform_resource_to_extraspecs(self, resource): def transform_resource_to_extraspecs(self, resource):
topology_properties = {
"cores": "topology:cores",
"threads": "topology:threads",
"sockets": "topology:sockets",
}
extraspec = {} extraspec = {}
for capability in resource.capabilities: for capability in resource.capabilities:
topology_properties = {
"cores": "topology:cores",
"threads": "topology:threads",
"sockets": "topoloy:sockets",
}
base_key = "" base_key = ""
if capability.capability_type_namespace == self.default_namespace: if capability.capability_type_namespace == self.default_namespace:
base_key = "capabilities:cpu_info" base_key = "capabilities:cpu_info"
for property_name, property_value in capability.properties.items(): for property_name, property_value \
if property_name == "features": in capability.properties.items():
if property_name == "features":
key = base_key + ':' + property_name
extraspec[key] = '<in> ' + property_value
continue
if property_name in topology_properties:
key = base_key + ':' \
+ topology_properties[property_name]
extraspec[key] = property_value
continue
key = base_key + ':' + property_name key = base_key + ':' + property_name
extraspec[key] = '<in> ' + property_value
continue
if property_name in topology_properties:
key = base_key + ':' + topology_properties[property_name]
extraspec[key] = property_value extraspec[key] = property_value
continue else:
key = base_key + ':' + property_name if not capability.properties:
extraspec[key] = property_value #if capability doesnt have properties add as TAG
key = capability.capability_type
extraspec[key] = utils.TAG_IDENTIFIER
else:
for property_name, property_value \
in capability.properties.items():
key = property_name
extraspec[key] = str(property_value)
return extraspec return extraspec