def generate_schema(): definitions = collections.OrderedDict() type_enum = [] top_properties = {"type": {"type": "string", "enum": type_enum}} base = collections.OrderedDict([("$schema", schema_spec), ("type", "object"), ("title", "AciObjectSpec"), ("properties", top_properties), ("definitions", definitions)]) for klass in writable_classes: required = klass.identity_attributes.keys() properties = {} for attr in [ klass.identity_attributes, klass.other_attributes, klass.db_attributes ]: for k, v in attr.iteritems(): properties[k] = v title = klass.__name__ name = utils.camel_to_snake(title) definitions[utils.camel_to_snake(title)] = collections.OrderedDict([ ("type", "object"), ("title", title), ("properties", properties) ]) if required: definitions[utils.camel_to_snake(title)]["required"] = required type_enum.append(name) top_properties[name] = {"$ref": "#/definitions/%s" % name} return base
def generate_schema(): definitions = collections.OrderedDict() type_enum = [] top_properties = {"type": {"type": "string", "enum": type_enum}} base = collections.OrderedDict( [("$schema", schema_spec), ("type", "object"), ("title", "AciObjectSpec"), ("properties", top_properties), ("definitions", definitions)]) for klass in writable_classes: required = klass.identity_attributes.keys() properties = {} for attr in [klass.identity_attributes, klass.other_attributes, klass.db_attributes]: for k, v in attr.iteritems(): properties[k] = v title = klass.__name__ name = utils.camel_to_snake(title) definitions[utils.camel_to_snake(title)] = collections.OrderedDict( [("type", "object"), ("title", title), ("properties", properties)]) if required: definitions[utils.camel_to_snake(title)]["required"] = required type_enum.append(name) top_properties[name] = {"$ref": "#/definitions/%s" % name} return base
def build_selectors(self, aim_klass, filters): result = 'aim_type=%s' % utils.camel_to_snake(aim_klass.__name__) for fltr in filters: if fltr in aim_klass.identity_attributes: result += ',%s=%s' % (fltr, utils.sanitize_name(filters[fltr])) selectors = self._get_aim_id_selector(filters) selectors['label_selector'] = result return selectors
def __init__(self, config): self.cfg = config self.ctx = context.AimContext(store=api.get_store()) self.mgr = aim_manager.AimManager() self.sneak_name_to_klass = { utils.camel_to_snake(x.__name__): x for x in self.mgr.aim_resources }
def build_selectors(self, aim_klass, filters): result = 'aim_type=%s' % utils.camel_to_snake(aim_klass.__name__) for fltr in filters: if fltr in aim_klass.identity_attributes: result += ',%s=%s' % ( fltr, utils.sanitize_name(filters[fltr])) selectors = self._get_aim_id_selector(filters) selectors['label_selector'] = result return selectors
def from_attr(self, resource_klass, attribute_dict): name = utils.camel_to_snake(resource_klass.__name__) self.setdefault('spec', {'type': name, name: {}}) self.setdefault('metadata', {'labels': {'aim_type': name}}) labels = self['metadata']['labels'] attrs = self['spec'][name] for k, v in attribute_dict.iteritems(): if k in resource_klass.identity_attributes: labels[k] = utils.sanitize_name(v) attrs[k] = v if 'name' not in self['metadata']: self['metadata']['name'] = self._build_name(name, resource_klass)
def from_attr(self, resource_klass, attribute_dict): name = utils.camel_to_snake(resource_klass.__name__) self.setdefault('spec', {'type': name, name: {}}) self.setdefault('metadata', {'labels': {'aim_type': name}}) labels = self['metadata']['labels'] attrs = self['spec'][name] for k, v in attribute_dict.iitems(): if k in resource_klass.identity_attributes: labels[k] = utils.sanitize_name(v) attrs[k] = v if 'name' not in self['metadata']: self['metadata']['name'] = self._build_name(name, resource_klass)
from aim.agent.aid.universes import constants as lcon from aim import aim_manager from aim.api import resource from aim.api import status from aim.api import tree as aim_tree from aim.common.hashtree import structured_tree from aim.common import utils from aim import context from aim.db import api from aim.k8s import api_v1 from aim import tree_manager LOG = logging.getLogger(__name__) serving_tenants = {} name_to_res = {utils.camel_to_snake(x.__name__): x for x in aim_manager.AimManager.aim_resources} OBSERVER_LOOP_MAX_WAIT = 10 OBSERVER_LOOP_MAX_RETRIES = 5 BUILDER_LOOP_MAX_WAIT = 10 BUILDER_LOOP_MAX_RETRIES = 5 MONITOR_LOOP_MAX_WAIT = 5 MONITOR_LOOP_MAX_RETRIES = 5 WARM_BUILD_TIME = 0.2 ACTION_CREATED = 'added' ACTION_MODIFIED = 'modified' ACTION_DELETED = 'deleted' ACTION_ERROR = 'error'
def __init__(self, config): self.cfg = config self.ctx = context.AimContext(store=api.get_store()) self.mgr = aim_manager.AimManager() self.sneak_name_to_klass = {utils.camel_to_snake(x.__name__): x for x in self.mgr.aim_resources}
def _generate_data_item(self, aim_resource): return {'type': utils.camel_to_snake(type(aim_resource).__name__), 'aim_id': aim_resource.__dict__.pop('_aim_id', ''), 'attributes': aim_resource.__dict__}
def _test_aim_server_per_type(self): exclude = { api_res.Configuration, # There are many already status_res.AciFault, # Tested by status test status_res.AciStatus, # Tested by status test tree_res.ActionLog, # Never created by API api_res.Topology, # Can create only 1 api_res.Agent, # Created only by AID # REVISIT: Instead of avaoiding classes # with enum data types, we should introspect the # object and create valid identifiers for the test infra_res.HostDomainMappingV2, # Avoiding enums for now } # For debugging purposes, only test one type test_only = {} # For test performance purposes, only test N klasses klass_num = 10 # Run for a limited set of types, or it will timeout for res_type in test_only or list( (self.mgr.aim_resources - exclude))[:klass_num]: for cardinality in [0, 1, 3]: fail_msg = ('Test Failed for type %s with ' 'cardinality %s' % (res_type, cardinality)) to_create = [ self.generate_aim_object(res_type) for _ in range(cardinality) ] # Create objects data = [ self.root.aimc._generate_data_item(x) for x in to_create ] resp = self.POST('aim', data=json.dumps(data)) self.assertEqual(200, resp.status_code, fail_msg) # Set objects' status status_supported = False if to_create and self.mgr.get_status( self.ctx, to_create[0], create_if_absent=True): status_supported = True for aim_obj in to_create: self.mgr.set_resource_sync_pending(self.ctx, aim_obj) # Test GET all resp = self.GET('aim') self.assertEqual(200, resp.status_code, fail_msg) self.assertEqual(len(to_create), resp.json()['count'], fail_msg) # There are AIM IDs now for item in data: item.update({'aim_id': mock.ANY}) sorting_key = lambda x: x['attributes'] data_resp = resp.json()['data'] for to_compare in [data, data_resp]: for x in to_compare: for non_user in res_type.non_user_attributes(): x['attributes'].pop(non_user, None) self.assertEqual(sorted(data, key=sorting_key), sorted(data_resp, key=sorting_key), fail_msg) # GET with status included resp = self.GET('aim?include-status=true') self.assertEqual(200, resp.status_code, fail_msg) self.assertEqual( len(to_create) * (2 if status_supported else 1), resp.json()['count'], fail_msg) # Create some with a PUT, with a common attribute comm_attr = {} for set_attr, schema_type in ( res_type.other_attributes.iteritems()): if schema_type['type'] == 'string' and ( 'enum' not in schema_type): comm_attr[set_attr] = utils.generate_uuid() break to_put = [ self.generate_aim_object(res_type, **comm_attr) for _ in range(cardinality) ] put_data = [ self.root.aimc._generate_data_item(x) for x in to_put ] self.PUT('aim', data=json.dumps(put_data)) resp = self.GET('aim') self.assertEqual(200, resp.status_code, fail_msg) # Objects added self.assertEqual( len(to_create) + len(to_put), resp.json()['count'], fail_msg) if comm_attr: uri = ('aim?object-type=%s&%s=%s' % (utils.camel_to_snake(res_type.__name__), comm_attr.keys()[0], comm_attr.values()[0])) resp = self.GET(uri) self.assertEqual(200, resp.status_code, fail_msg) self.assertEqual(len(to_put), resp.json()['count'], fail_msg) # Delete objects just created for item in to_put: uri = ('aim?object-type=%s&%s' % (utils.camel_to_snake(res_type.__name__), '&'.join([ '%s=%s' % (k, getattr(item, k)) for k in item.identity_attributes.keys() ]))) self.DELETE(uri) resp = self.GET('aim') self.assertEqual(200, resp.status_code, fail_msg) self.assertEqual(len(to_create), resp.json()['count'], fail_msg) # Delete all resp = self.DELETE('aim') self.assertEqual(200, resp.status_code, fail_msg) resp = self.GET('aim') self.assertEqual(200, resp.status_code, fail_msg) self.assertEqual(0, resp.json()['count'], fail_msg)
def _generate_data_item(self, aim_resource): return { 'type': utils.camel_to_snake(type(aim_resource).__name__), 'aim_id': aim_resource.__dict__.pop('_aim_id', ''), 'attributes': aim_resource.__dict__ }
from aim.agent.aid.universes import constants as lcon from aim import aim_manager from aim.api import resource from aim.api import status from aim.api import tree as aim_tree from aim.common.hashtree import structured_tree from aim.common import utils from aim import context from aim.db import api from aim.k8s import api_v1 from aim import tree_manager LOG = logging.getLogger(__name__) serving_tenants = {} name_to_res = { utils.camel_to_snake(x.__name__): x for x in aim_manager.AimManager.aim_resources } OBSERVER_LOOP_MAX_WAIT = 10 OBSERVER_LOOP_MAX_RETRIES = 5 BUILDER_LOOP_MAX_WAIT = 10 BUILDER_LOOP_MAX_RETRIES = 5 MONITOR_LOOP_MAX_WAIT = 5 MONITOR_LOOP_MAX_RETRIES = 5 WARM_BUILD_TIME = 0.2 ACTION_CREATED = 'added' ACTION_MODIFIED = 'modified' ACTION_DELETED = 'deleted' ACTION_ERROR = 'error'
self.config_parse() def _check_call_list(self, expected, mocked, check_all=True): observed = mocked.call_args_list for call in expected: self.assertTrue(call in observed, msg='Call not found, expected:\n%s\nobserved:' '\n%s' % (str(call), str(observed))) observed.remove(call) if check_all: self.assertFalse( len(observed), msg='There are more calls than expected: %s' % str(observed)) name_to_res = {utils.camel_to_snake(x.__name__): x for x in aim_manager.AimManager.aim_resources} k8s_watcher_instance = None def _k8s_post_create(self, created): if created: w = k8s_watcher_instance w.klient.get_new_watch() event = {'type': 'ADDED', 'object': created} w.klient.watch.stream = mock.Mock(return_value=[event]) w._reset_trees = mock.Mock() w.q.put(event) w._persistence_loop(save_on_empty=True, warmup_wait=0)