예제 #1
0
    def post(self, flavor_profile_):
        """Creates a flavor Profile."""
        flavorprofile = flavor_profile_.flavorprofile
        context = pecan_request.context.get('octavia_context')
        self._auth_validate_action(context, context.project_id,
                                   constants.RBAC_POST)
        # Do a basic JSON validation on the metadata
        try:
            flavor_data_dict = jsonutils.loads(flavorprofile.flavor_data)
        except Exception as e:
            raise exceptions.InvalidOption(
                value=flavorprofile.flavor_data,
                option=constants.FLAVOR_DATA) from e

        # Validate that the provider driver supports the metadata
        driver = driver_factory.get_driver(flavorprofile.provider_name)
        driver_utils.call_provider(driver.name, driver.validate_flavor,
                                   flavor_data_dict)

        lock_session = db_api.get_session(autocommit=False)
        try:
            flavorprofile_dict = flavorprofile.to_dict(render_unsets=True)
            flavorprofile_dict['id'] = uuidutils.generate_uuid()
            db_flavor_profile = self.repositories.flavor_profile.create(
                lock_session, **flavorprofile_dict)
            lock_session.commit()
        except odb_exceptions.DBDuplicateEntry as e:
            lock_session.rollback()
            raise exceptions.IDAlreadyExists() from e
        except Exception:
            with excutils.save_and_reraise_exception():
                lock_session.rollback()
        result = self._convert_db_to_type(
            db_flavor_profile, profile_types.FlavorProfileResponse)
        return profile_types.FlavorProfileRootResponse(flavorprofile=result)
예제 #2
0
 def get_one(self, id, fields=None):
     """Gets a flavor profile's detail."""
     context = pecan.request.context.get('octavia_context')
     self._auth_validate_action(context, context.project_id,
                                constants.RBAC_GET_ONE)
     db_flavor_profile = self._get_db_flavor_profile(context.session, id)
     result = self._convert_db_to_type(db_flavor_profile,
                                       profile_types.FlavorProfileResponse)
     if fields is not None:
         result = self._filter_fields([result], fields)[0]
     return profile_types.FlavorProfileRootResponse(flavorprofile=result)
예제 #3
0
    def put(self, id, flavor_profile_):
        """Updates a flavor Profile."""
        flavorprofile = flavor_profile_.flavorprofile
        context = pecan.request.context.get('octavia_context')
        self._auth_validate_action(context, context.project_id,
                                   constants.RBAC_PUT)

        # Don't allow changes to the flavor_data or provider_name if it
        # is in use.
        if (not isinstance(flavorprofile.flavor_data, wtypes.UnsetType) or
                not isinstance(flavorprofile.provider_name, wtypes.UnsetType)):
            if self.repositories.flavor.count(context.session,
                                              flavor_profile_id=id) > 0:
                raise exceptions.ObjectInUse(object='Flavor profile', id=id)

        if not isinstance(flavorprofile.flavor_data, wtypes.UnsetType):
            # Do a basic JSON validation on the metadata
            try:
                flavor_data_dict = jsonutils.loads(flavorprofile.flavor_data)
            except Exception:
                raise exceptions.InvalidOption(value=flavorprofile.flavor_data,
                                               option=constants.FLAVOR_DATA)

            if isinstance(flavorprofile.provider_name, wtypes.UnsetType):
                db_flavor_profile = self._get_db_flavor_profile(
                    context.session, id)
                provider_driver = db_flavor_profile.provider_name
            else:
                provider_driver = flavorprofile.provider_name

            # Validate that the provider driver supports the metadata
            driver = driver_factory.get_driver(provider_driver)
            driver_utils.call_provider(driver.name, driver.validate_flavor,
                                       flavor_data_dict)

        lock_session = db_api.get_session(autocommit=False)
        try:
            flavorprofile_dict = flavorprofile.to_dict(render_unsets=False)
            if flavorprofile_dict:
                self.repositories.flavor_profile.update(
                    lock_session, id, **flavorprofile_dict)
            lock_session.commit()
        except Exception:
            with excutils.save_and_reraise_exception():
                lock_session.rollback()

        # Force SQL alchemy to query the DB, otherwise we get inconsistent
        # results
        context.session.expire_all()
        db_flavor_profile = self._get_db_flavor_profile(context.session, id)
        result = self._convert_db_to_type(db_flavor_profile,
                                          profile_types.FlavorProfileResponse)
        return profile_types.FlavorProfileRootResponse(flavorprofile=result)