def _flavor_create(context, values): specs = values.get('extra_specs') db_specs = [] if specs: for k, v in specs.items(): db_spec = api_models.FlavorExtraSpecs() db_spec['key'] = k db_spec['value'] = v db_specs.append(db_spec) projects = values.get('projects') db_projects = [] if projects: for project in set(projects): db_project = api_models.FlavorProjects() db_project['project_id'] = project db_projects.append(db_project) values['extra_specs'] = db_specs values['projects'] = db_projects db_flavor = api_models.Flavors() db_flavor.update(values) try: db_flavor.save(context.session) except db_exc.DBDuplicateEntry as e: if 'flavorid' in e.columns: raise exception.FlavorIdExists(flavor_id=values['flavorid']) raise exception.FlavorExists(name=values['name']) except Exception as e: raise db_exc.DBError(e) return _dict_with_extra_specs(db_flavor)
def test_tables_flavor_instance_type_extra_specs(self): flavor_extra_specs = api_models.FlavorExtraSpecs() instance_type_extra_specs = models.InstanceTypeExtraSpecs() columns_flavor_extra_specs = self._get_columns_list(flavor_extra_specs) columns_instance_type_extra_specs = self._get_columns_list( instance_type_extra_specs) columns_flavor_extra_specs.remove('flavor_id') columns_instance_type_extra_specs.remove('instance_type_id') self.assertTrue( self._check_column_list(columns_flavor_extra_specs, columns_instance_type_extra_specs))
def _create_api_flavor(context, altid=None): fake_db_flavor = dict(fake_flavor) del fake_db_flavor['extra_specs'] del fake_db_flavor['id'] flavor = api_models.Flavors() flavor.update(fake_db_flavor) if altid: flavor.update({'flavorid': altid, 'name': altid}) flavor.save(context.session) fake_db_extra_spec = { 'flavor_id': flavor['id'], 'key': 'foo', 'value': 'bar' } flavor_es = api_models.FlavorExtraSpecs() flavor_es.update(fake_db_extra_spec) flavor_es.save(context.session) return flavor
def _flavor_extra_specs_add(context, flavor_id, specs, max_retries=10): writer = api_db_api.context_manager.writer for attempt in range(max_retries): try: spec_refs = context.session.query( api_models.FlavorExtraSpecs).\ filter_by(flavor_id=flavor_id).\ filter(api_models.FlavorExtraSpecs.key.in_( specs.keys())).\ all() existing_keys = set() for spec_ref in spec_refs: key = spec_ref["key"] existing_keys.add(key) with writer.savepoint.using(context): spec_ref.update({"value": specs[key]}) for key, value in specs.items(): if key in existing_keys: continue spec_ref = api_models.FlavorExtraSpecs() with writer.savepoint.using(context): spec_ref.update({ "key": key, "value": value, "flavor_id": flavor_id }) context.session.add(spec_ref) return specs except db_exc.DBDuplicateEntry: # a concurrent transaction has been committed, # try again unless this was the last attempt if attempt == max_retries - 1: raise exception.FlavorExtraSpecUpdateCreateFailed( id=flavor_id, retries=max_retries)