def _set_specs_for_instance(self, context, instance_db, spec_id_list):
     if not spec_id_list:
         instance_db.spec_ids = []
         return
     with context.session.begin(subtransactions=True):
         filters = {'id': spec_id_list}
         specs_in_db = self._get_collection_query(context, ServiceChainSpec,
                                                  filters=filters)
         specs_list = set(spec_db['id'] for spec_db in specs_in_db)
         for spec_id in spec_id_list:
             if spec_id not in specs_list:
                 # Do not update if spec ID is invalid
                 raise schain.ServiceChainSpecNotFound(sc_spec_id=spec_id)
         # Reset the existing list and then add each spec in order. The list
         # could be empty in which case we clear the existing specs.
         instance_db.specs = []
         for spec_id in spec_id_list:
             assoc = InstanceSpecAssociation(
                                 servicechain_instance_id=instance_db.id,
                                 servicechain_spec_id=spec_id)
             instance_db.specs.append(assoc)
 def _get_servicechain_spec(self, context, spec_id):
     try:
         return self._get_by_id(context, ServiceChainSpec, spec_id)
     except exc.NoResultFound:
         raise schain.ServiceChainSpecNotFound(sc_spec_id=spec_id)