Пример #1
0
    def create(self, req, body, tenant_id):
        # TODO(hub-cap): turn this into middleware
        LOG.info(_("Creating a database instance for tenant '%s'") % tenant_id)
        LOG.info(logging.mask_password(_("req : '%s'\n\n") % req))
        LOG.info(logging.mask_password(_("body : '%s'\n\n") % body))
        context = req.environ[wsgi.CONTEXT_KEY]
        datastore_args = body['instance'].get('datastore', {})
        datastore, datastore_version = (
            datastore_models.get_datastore_version(**datastore_args))
        image_id = datastore_version.image_id
        name = body['instance']['name']
        flavor_ref = body['instance']['flavorRef']
        flavor_id = utils.get_id_from_href(flavor_ref)

        configuration = self._configuration_parse(context, body)
        databases = populate_validated_databases(
            body['instance'].get('databases', []))
        database_names = [database.get('_name', '') for database in databases]
        users = None
        try:
            users = populate_users(body['instance'].get('users', []),
                                   database_names)
        except ValueError as ve:
            raise exception.BadRequest(msg=ve)

        if 'volume' in body['instance']:
            volume_size = int(body['instance']['volume']['size'])
        else:
            volume_size = None

        if 'restorePoint' in body['instance']:
            backupRef = body['instance']['restorePoint']['backupRef']
            backup_id = utils.get_id_from_href(backupRef)
        else:
            backup_id = None

        if 'availability_zone' in body['instance']:
            availability_zone = body['instance']['availability_zone']
        else:
            availability_zone = None

        if 'nics' in body['instance']:
            nics = body['instance']['nics']
        else:
            nics = None

        if 'slave_of' in body['instance']:
            slave_of_id = body['instance']['slave_of']
        else:
            slave_of_id = None

        instance = models.Instance.create(context, name, flavor_id,
                                          image_id, databases, users,
                                          datastore, datastore_version,
                                          volume_size, backup_id,
                                          availability_zone, nics,
                                          configuration, slave_of_id)

        view = views.InstanceDetailView(instance, req=req)
        return wsgi.Result(view.data(), 200)
Пример #2
0
 def show(self, req, tenant_id, datastore, id, name):
     self.authorize_request(req, 'show')
     ds, ds_version = ds_models.get_datastore_version(
         type=datastore, version=id)
     rule = models.DatastoreConfigurationParameters.load_parameter_by_name(
         ds_version.id, name)
     return wsgi.Result(views.ConfigurationParameterView(rule).data(), 200)
Пример #3
0
    def create(self, req, body, tenant_id):
        LOG.debug(("Creating a Cluster for Tenant '%(tenant_id)s'\n"
                   "req : '%(req)s'\n\nbody : '%(body)s'\n\n") % {
                       "tenant_id": tenant_id,
                       "req": req,
                       "body": body
                   })

        context = req.environ[wsgi.CONTEXT_KEY]
        name = body['cluster']['name']
        datastore_args = body['cluster'].get('datastore', {})
        datastore, datastore_version = (datastore_models.get_datastore_version(
            **datastore_args))

        # TODO(saurabhs): add extended_properties to apischema
        extended_properties = body['cluster'].get('extended_properties', {})

        try:
            clusters_enabled = (CONF.get(
                datastore_version.manager).get('cluster_support'))
        except NoSuchOptError:
            clusters_enabled = False

        if not clusters_enabled:
            raise exception.ClusterDatastoreNotSupported(
                datastore=datastore.name,
                datastore_version=datastore_version.name)

        nodes = body['cluster']['instances']
        instances = []
        for node in nodes:
            flavor_id = utils.get_id_from_href(node['flavorRef'])
            volume_size = volume_type = nics = availability_zone = None
            if 'volume' in node:
                volume_size = int(node['volume']['size'])
                volume_type = node['volume'].get('volume_type')
            if 'nics' in node:
                nics = node['nics']
            if 'availability_zone' in node:
                availability_zone = node['availability_zone']

            instances.append({
                "flavor_id": flavor_id,
                "volume_size": volume_size,
                "volume_type": volume_type,
                "nics": nics,
                "availability_zone": availability_zone
            })

        context.notification = notification.DBaaSClusterCreate(context,
                                                               request=req)
        with StartNotification(context,
                               name=name,
                               datastore=datastore.name,
                               datastore_version=datastore_version.name):
            cluster = models.Cluster.create(context, name, datastore,
                                            datastore_version, instances,
                                            extended_properties)
        view = views.load_view(cluster, req=req, load_servers=False)
        return wsgi.Result(view.data(), 200)
Пример #4
0
    def create(self, req, body, tenant_id):
        # TODO(hub-cap): turn this into middleware
        LOG.info(_LI("Creating a database instance for tenant '%s'"),
                 tenant_id)
        LOG.debug("req : '%s'\n\n", strutils.mask_password(req))
        LOG.debug("body : '%s'\n\n", strutils.mask_password(body))
        context = req.environ[wsgi.CONTEXT_KEY]
        context.notification = notification.DBaaSInstanceCreate(context,
                                                                request=req)
        datastore_args = body['instance'].get('datastore', {})
        datastore, datastore_version = (
            datastore_models.get_datastore_version(**datastore_args))
        image_id = datastore_version.image_id
        name = body['instance']['name']
        flavor_ref = body['instance']['flavorRef']
        flavor_id = utils.get_id_from_href(flavor_ref)

        configuration = self._configuration_parse(context, body)
        databases = populate_validated_databases(
            body['instance'].get('databases', []))
        database_names = [database.get('_name', '') for database in databases]
        users = None
        try:
            users = populate_users(body['instance'].get('users', []),
                                   database_names)
        except ValueError as ve:
            raise exception.BadRequest(msg=ve)

        if 'volume' in body['instance']:
            volume_info = body['instance']['volume']
            volume_size = int(volume_info['size'])
            volume_type = volume_info.get('type')
        else:
            volume_size = None
            volume_type = None

        if 'restorePoint' in body['instance']:
            backupRef = body['instance']['restorePoint']['backupRef']
            backup_id = utils.get_id_from_href(backupRef)
        else:
            backup_id = None

        availability_zone = body['instance'].get('availability_zone')
        nics = body['instance'].get('nics')

        slave_of_id = body['instance'].get('replica_of',
                                           # also check for older name
                                           body['instance'].get('slave_of'))
        replica_count = body['instance'].get('replica_count')
        instance = models.Instance.create(context, name, flavor_id,
                                          image_id, databases, users,
                                          datastore, datastore_version,
                                          volume_size, backup_id,
                                          availability_zone, nics,
                                          configuration, slave_of_id,
                                          replica_count=replica_count,
                                          volume_type=volume_type)

        view = views.InstanceDetailView(instance, req=req)
        return wsgi.Result(view.data(), 200)
Пример #5
0
 def index(self, req, tenant_id, datastore, id):
     ds, ds_version = ds_models.get_datastore_version(type=datastore,
                                                      version=id)
     rules = models.DatastoreConfigurationParameters.load_parameters(
         ds_version.id)
     return wsgi.Result(
         views.ConfigurationParametersView(rules).data(), 200)
Пример #6
0
 def index(self, req, tenant_id, datastore, id):
     ds, ds_version = ds_models.get_datastore_version(
         type=datastore, version=id)
     rules = models.DatastoreConfigurationParameters.load_parameters(
         ds_version.id)
     return wsgi.Result(views.ConfigurationParametersView(rules).data(),
                        200)
Пример #7
0
    def create(self, req, body, tenant_id):
        LOG.debug("req : '%s'\n\n" % req)
        LOG.debug("body : '%s'\n\n" % req)

        name = body['configuration']['name']
        description = body['configuration'].get('description')
        values = body['configuration']['values']

        datastore_args = body['configuration'].get('datastore', {})
        datastore, datastore_version = (
            ds_models.get_datastore_version(**datastore_args))

        configItems = []
        if values:
            # validate that the values passed in are permitted by the operator.
            ConfigurationsController._validate_configuration(
                body['configuration']['values'],
                datastore_manager=datastore_version.manager)

            for k, v in values.iteritems():
                configItems.append(ConfigurationParameter(
                    configuration_key=k,
                    configuration_value=v))

        cfg_group = models.Configuration.create(name, description, tenant_id,
                                                datastore.id,
                                                datastore_version.id)
        cfg_group_items = models.Configuration.create_items(cfg_group.id,
                                                            values)
        view_data = views.DetailedConfigurationView(cfg_group,
                                                    cfg_group_items)
        return wsgi.Result(view_data.data(), 200)
Пример #8
0
    def create(self, req, body, tenant_id):
        LOG.debug("req : '%s'\n\n" % req)
        LOG.debug("body : '%s'\n\n" % req)

        name = body['configuration']['name']
        description = body['configuration'].get('description')
        values = body['configuration']['values']

        msg = _("Creating configuration group on tenant "
                "%(tenant_id)s with name: %(cfg_name)s")
        LOG.info(msg % {"tenant_id": tenant_id, "cfg_name": name})

        datastore_args = body['configuration'].get('datastore', {})
        datastore, datastore_version = (ds_models.get_datastore_version(
            **datastore_args))

        configItems = []
        if values:
            # validate that the values passed in are permitted by the operator.
            ConfigurationsController._validate_configuration(
                body['configuration']['values'],
                datastore_version=datastore_version)

            for k, v in values.iteritems():
                configItems.append(
                    DBConfigurationParameter(configuration_key=k,
                                             configuration_value=v))

        cfg_group = models.Configuration.create(name, description, tenant_id,
                                                datastore.id,
                                                datastore_version.id)
        cfg_group_items = models.Configuration.create_items(
            cfg_group.id, values)
        view_data = views.DetailedConfigurationView(cfg_group, cfg_group_items)
        return wsgi.Result(view_data.data(), 200)
Пример #9
0
 def index(self, req, tenant_id, datastore, id):
     ds, ds_version = ds_models.get_datastore_version(
         type=datastore, version=id)
     rules = configurations.get_validation_rules(
         datastore_manager=ds_version.manager)
     return wsgi.Result(views.ConfigurationParametersView(rules).data(),
                        200)
Пример #10
0
def remove_datastore_configuration_parameters(datastore, datastore_version):
    get_db_api().configure_db(CONF)
    (ds, ds_version) = dstore_models.get_datastore_version(
        type=datastore, version=datastore_version, return_inactive=True)
    db_params = DatastoreConfigurationParameters.load_parameters(ds_version.id)
    for db_param in db_params:
        db_param.delete()
Пример #11
0
 def index(self, req, tenant_id, datastore, id):
     ds, ds_version = ds_models.get_datastore_version(
         type=datastore, version=id)
     rules = configurations.get_validation_rules(
         datastore_manager=ds_version.manager)
     return wsgi.Result(views.ConfigurationParametersView(rules).data(),
                        200)
Пример #12
0
 def index(self, req, tenant_id):
     context = req.environ[wsgi.CONTEXT_KEY]
     datastore = req.GET.get('datastore', '')
     if datastore and datastore.lower() != models.Modules.MATCH_ALL_NAME:
         ds, ds_ver = datastore_models.get_datastore_version(type=datastore)
         datastore = ds.id
     modules = models.Modules.load(context, datastore=datastore)
     view = views.ModulesView(modules)
     return wsgi.Result(view.data(), 200)
Пример #13
0
def load_datastore_configuration_parameters(datastore, datastore_version, config_file):
    get_db_api().configure_db(CONF)
    (ds, ds_v) = dstore_models.get_datastore_version(type=datastore, version=datastore_version)
    with open(config_file) as f:
        config = json.load(f)
        for param in config["configuration-parameters"]:
            create_or_update_datastore_configuration_parameter(
                param["name"], ds_v.id, param["restart_required"], param["type"], param.get("max"), param.get("min")
            )
Пример #14
0
 def show(self, req, tenant_id, datastore, id, name):
     ds, ds_version = ds_models.get_datastore_version(
         type=datastore, version=id)
     rules = configurations.get_validation_rules(
         datastore_manager=ds_version.manager)
     for rule in rules['configuration-parameters']:
         if rule['name'] == name:
             return wsgi.Result(
                 views.ConfigurationParametersView(rule).data(), 200)
     raise exception.ConfigKeyNotFound(key=name)
Пример #15
0
 def index(self, req, tenant_id):
     context = req.environ[wsgi.CONTEXT_KEY]
     datastore = req.GET.get('datastore', '')
     if datastore and datastore.lower() != models.Modules.MATCH_ALL_NAME:
         ds, ds_ver = datastore_models.get_datastore_version(
             type=datastore)
         datastore = ds.id
     modules = models.Modules.load(context, datastore=datastore)
     view = views.ModulesView(modules)
     return wsgi.Result(view.data(), 200)
Пример #16
0
 def show(self, req, tenant_id, datastore, id, name):
     ds, ds_version = ds_models.get_datastore_version(
         type=datastore, version=id)
     rules = configurations.get_validation_rules(
         datastore_manager=ds_version.manager)
     for rule in rules['configuration-parameters']:
         if rule['name'] == name:
             return wsgi.Result(
                 views.ConfigurationParametersView(rule).data(), 200)
     raise exception.ConfigKeyNotFound(key=name)
Пример #17
0
    def create(self, req, body, tenant_id):
        # TODO(hub-cap): turn this into middleware
        LOG.info(_("Creating a database instance for tenant '%s'") % tenant_id)
        LOG.info(logging.mask_password(_("req : '%s'\n\n") % req))
        LOG.info(logging.mask_password(_("body : '%s'\n\n") % body))
        context = req.environ[wsgi.CONTEXT_KEY]
        datastore_args = body['instance'].get('datastore', {})
        datastore, datastore_version = (
            datastore_models.get_datastore_version(**datastore_args))
        image_id = datastore_version.image_id
        name = body['instance']['name']
        flavor_ref = body['instance']['flavorRef']
        flavor_id = utils.get_id_from_href(flavor_ref)

        configuration = self._configuration_parse(context, body)
        databases = populate_validated_databases(
            body['instance'].get('databases', []))
        database_names = [database.get('_name', '') for database in databases]
        users = None
        try:
            users = populate_users(body['instance'].get('users', []),
                                   database_names)
        except ValueError as ve:
            raise exception.BadRequest(msg=ve)

        if 'volume' in body['instance']:
            volume_size = int(body['instance']['volume']['size'])
        else:
            volume_size = None

        if 'restorePoint' in body['instance']:
            backupRef = body['instance']['restorePoint']['backupRef']
            backup_id = utils.get_id_from_href(backupRef)
        else:
            backup_id = None

        if 'availability_zone' in body['instance']:
            availability_zone = body['instance']['availability_zone']
        else:
            availability_zone = None

        if 'nics' in body['instance']:
            nics = body['instance']['nics']
        else:
            nics = None

        instance = models.Instance.create(context, name, flavor_id,
                                          image_id, databases, users,
                                          datastore, datastore_version,
                                          volume_size, backup_id,
                                          availability_zone, nics,
                                          configuration)

        view = views.InstanceDetailView(instance, req=req)
        return wsgi.Result(view.data(), 200)
Пример #18
0
    def create(self, req, body, tenant_id):
        LOG.debug("Creating a Cluster for Tenant '%s'" % tenant_id)
        LOG.info(_("req : '%s'\n\n") % req)
        LOG.info(_("body : '%s'\n\n") % body)

        context = req.environ[wsgi.CONTEXT_KEY]
        name = body['cluster']['name']
        datastore_args = body['cluster'].get('datastore', {})
        datastore, datastore_version = (
            datastore_models.get_datastore_version(**datastore_args))

        # TODO(saurabhs): add extended_properties to apischema
        extended_properties = body['cluster'].get('extended_properties', {})

        try:
            clusters_enabled = (CONF.get(datastore_version.manager)
                                .get('cluster_support'))
        except NoSuchOptError:
            clusters_enabled = False

        if not clusters_enabled:
            raise exception.ClusterDatastoreNotSupported(
                datastore=datastore.name,
                datastore_version=datastore_version.name)

        nodes = body['cluster']['instances']
        instances = []
        for node in nodes:
            flavor_id = utils.get_id_from_href(node['flavorRef'])
            volume_size = volume_type = nics = availability_zone = None
            if 'volume' in node:
                volume_size = int(node['volume']['size'])
                volume_type = node['volume'].get('volume_type')
            if 'nics' in node:
                nics = node['nics']
            if 'availability_zone' in node:
                availability_zone = node['availability_zone']

            instances.append({"flavor_id": flavor_id,
                              "volume_size": volume_size,
                              "volume_type": volume_type,
                              "nics": nics,
                              "availability_zone": availability_zone})

        context.notification = notification.DBaaSClusterCreate(context,
                                                               request=req)
        with StartNotification(context, name=name, datastore=datastore.name,
                               datastore_version=datastore_version.name):
            cluster = models.Cluster.create(context, name, datastore,
                                            datastore_version, instances,
                                            extended_properties)
        view = views.load_view(cluster, req=req, load_servers=False)
        return wsgi.Result(view.data(), 200)
Пример #19
0
    def update(self, req, body, tenant_id, id):
        LOG.info(_("Updating configuration for tenant id %s") % tenant_id)
        context = req.environ[wsgi.CONTEXT_KEY]
        group = models.Configuration.load(context, id)
        datastore_args = body["configuration"].get("datastore", {})
        datastore, datastore_version = ds_models.get_datastore_version(**datastore_args)
        instances = instances_models.DBInstance.find_all(
            tenant_id=context.tenant, configuration_id=id, deleted=False
        ).all()

        # if name/description are provided in the request body, update the
        # model with these values as well.
        if "name" in body["configuration"]:
            group.name = body["configuration"]["name"]

        if "description" in body["configuration"]:
            group.description = body["configuration"]["description"]

        """validate if params are valid"""
        is_configuration_valid = False

        validation_result = KSC_ConfigurationsController.validate_configuration(
            context, body["configuration"]["values"], datastore_manager=datastore_version.manager
        )

        if instances:
            current_config = KSC_Configuration.load(context, id)
            validation_dynamic_result = KSC_Configuration.validate_dynamic_params(
                context, current_config, instances, body["configuration"]["values"], dynamic_param=True
            )
            if validation_result or validation_dynamic_result:
                if validation_dynamic_result:
                    validation_result.update(validation_dynamic_result)
            elif not validation_result and not validation_dynamic_result:
                is_configuration_valid = True
        else:
            if not validation_result:
                is_configuration_valid = True

        result = {"is_validate_pass": is_configuration_valid, "validation_result": validation_result}

        if validation_result:
            return wsgi.Result(result, 400)
        else:
            deleted_at = datetime.utcnow()
            models.Configuration.remove_all_items(context, group.id, deleted_at)
            LOG.info(_("loaded configuration instances: %s") % instances)
            items = self.configuration_items_list(group, body["configuration"])
            models.Configuration.save(context, group, items, instances)

            result["is_validate_pass"] = is_configuration_valid
            return wsgi.Result(result, 202)
Пример #20
0
 def validate_datastore(datastore, datastore_version):
     datastore_id = None
     datastore_version_id = None
     if datastore:
         ds, ds_ver = datastore_models.get_datastore_version(
             type=datastore, version=datastore_version)
         datastore_id = ds.id
         if datastore_version:
             datastore_version_id = ds_ver.id
     elif datastore_version:
         msg = _("Cannot specify version without datastore")
         raise exception.BadRequest(message=msg)
     return datastore_id, datastore_version_id
Пример #21
0
 def validate_datastore(datastore, datastore_version):
     datastore_id = None
     datastore_version_id = None
     if datastore:
         ds, ds_ver = datastore_models.get_datastore_version(
             type=datastore, version=datastore_version)
         datastore_id = ds.id
         if datastore_version:
             datastore_version_id = ds_ver.id
     elif datastore_version:
         msg = _("Cannot specify version without datastore")
         raise exception.BadRequest(message=msg)
     return datastore_id, datastore_version_id
Пример #22
0
    def create(self, req, body, tenant_id):
        LOG.debug("req : '%s'\n\n" % req)
        LOG.debug("body : '%s'\n\n" % req)

        context = req.environ[wsgi.CONTEXT_KEY]
        policy.authorize_on_tenant(context, 'configuration:create')
        context.notification = notification.DBaaSConfigurationCreate(
            context, request=req)
        name = body['configuration']['name']
        description = body['configuration'].get('description')
        values = body['configuration']['values']

        msg = _("Creating configuration group on tenant "
                "%(tenant_id)s with name: %(cfg_name)s")
        LOG.info(msg % {"tenant_id": tenant_id, "cfg_name": name})

        datastore_args = body['configuration'].get('datastore', {})
        datastore, datastore_version = (
            ds_models.get_datastore_version(**datastore_args))

        with StartNotification(context, name=name, datastore=datastore.name,
                               datastore_version=datastore_version.name):
            configItems = []
            if values:
                # validate that the values passed in are permitted by the
                # operator.
                ConfigurationsController._validate_configuration(
                    body['configuration']['values'],
                    datastore_version,
                    models.DatastoreConfigurationParameters.load_parameters(
                        datastore_version.id))

                for k, v in values.items():
                    configItems.append(DBConfigurationParameter(
                        configuration_key=k,
                        configuration_value=v))

            cfg_group = models.Configuration.create(name, description,
                                                    tenant_id, datastore.id,
                                                    datastore_version.id)
            with EndNotification(context, configuration_id=cfg_group.id):
                cfg_group_items = models.Configuration.create_items(
                    cfg_group.id, values)

        view_data = views.DetailedConfigurationView(cfg_group,
                                                    cfg_group_items)
        return wsgi.Result(view_data.data(), 200)
Пример #23
0
def load_datastore_configuration_parameters(datastore,
                                            datastore_version,
                                            config_file):
    get_db_api().configure_db(CONF)
    (ds, ds_v) = dstore_models.get_datastore_version(
        type=datastore, version=datastore_version, return_inactive=True)
    with open(config_file) as f:
        config = json.load(f)
        for param in config['configuration-parameters']:
            create_or_update_datastore_configuration_parameter(
                param['name'],
                ds_v.id,
                param['restart_required'],
                param['type'],
                param.get('max'),
                param.get('min'),
            )
Пример #24
0
def load_datastore_configuration_parameters(datastore, datastore_version,
                                            config_file):
    get_db_api().configure_db(CONF)
    (ds, ds_v) = dstore_models.get_datastore_version(type=datastore,
                                                     version=datastore_version,
                                                     return_inactive=True)
    with open(config_file) as f:
        config = json.load(f)
        for param in config['configuration-parameters']:
            create_or_update_datastore_configuration_parameter(
                param['name'],
                ds_v.id,
                param['restart_required'],
                param['type'],
                param.get('max'),
                param.get('min'),
            )
Пример #25
0
    def data(self):
        module_dict = dict(
            id=self.module.id,
            name=self.module.name,
            type=self.module.type,
            description=self.module.description,
            tenant_id=self.module.tenant_id,
            datastore_id=self.module.datastore_id,
            datastore_version_id=self.module.datastore_version_id,
            auto_apply=bool(self.module.auto_apply),
            priority_apply=bool(self.module.priority_apply),
            apply_order=self.module.apply_order,
            is_admin=bool(self.module.is_admin),
            md5=self.module.md5,
            visible=bool(self.module.visible),
            created=self.module.created,
            updated=self.module.updated)
        # add extra data to make results more legible
        if self.module.tenant_id:
            # This should be the tenant name, but until we figure out where
            # to get it from, use the tenant_id
            tenant = self.module.tenant_id
        else:
            tenant = models.Modules.MATCH_ALL_NAME
        module_dict["tenant"] = tenant
        datastore = self.module.datastore_id
        datastore_version = self.module.datastore_version_id
        if datastore:
            if datastore_version:
                ds, ds_ver = (
                    datastore_models.get_datastore_version(
                        type=datastore, version=datastore_version))
                datastore = ds.name
                datastore_version = ds_ver.name
            else:
                ds = datastore_models.Datastore.load(datastore)
                datastore = ds.name
                datastore_version = models.Modules.MATCH_ALL_NAME
        else:
            datastore = models.Modules.MATCH_ALL_NAME
            datastore_version = models.Modules.MATCH_ALL_NAME
        module_dict["datastore"] = datastore
        module_dict["datastore_version"] = datastore_version

        return {"module": module_dict}
Пример #26
0
    def create(self, req, body, tenant_id):
        LOG.debug("Creating a Cluster for Tenant '%s'" % tenant_id)
        LOG.info(_("req : '%s'\n\n") % req)
        LOG.info(_("body : '%s'\n\n") % body)

        context = req.environ[wsgi.CONTEXT_KEY]
        name = body['cluster']['name']
        datastore_args = body['cluster'].get('datastore', {})
        datastore, datastore_version = (datastore_models.get_datastore_version(
            **datastore_args))

        try:
            clusters_enabled = (CONF.get(
                datastore_version.manager).get('cluster_support'))
        except NoSuchOptError:
            clusters_enabled = False

        if not clusters_enabled:
            raise exception.ClusterDatastoreNotSupported(
                datastore=datastore.name,
                datastore_version=datastore_version.name)

        nodes = body['cluster']['instances']
        instances = []
        for node in nodes:
            flavor_id = utils.get_id_from_href(node['flavorRef'])
            volume_size = nics = availability_zone = None
            if 'volume' in node:
                volume_size = int(node['volume']['size'])
            if 'nics' in node:
                nics = node['nics']
            if 'availability_zone' in node:
                availability_zone = node['availability_zone']

            instances.append({
                "flavor_id": flavor_id,
                "volume_size": volume_size,
                "nics": nics,
                "availability_zone": availability_zone
            })

        cluster = models.Cluster.create(context, name, datastore,
                                        datastore_version, instances)
        view = views.load_view(cluster, req=req, load_servers=False)
        return wsgi.Result(view.data(), 200)
Пример #27
0
    def data(self):
        module_dict = dict(
            id=self.module.id,
            name=self.module.name,
            type=self.module.type,
            description=self.module.description,
            tenant_id=self.module.tenant_id,
            datastore_id=self.module.datastore_id,
            datastore_version_id=self.module.datastore_version_id,
            auto_apply=self.module.auto_apply,
            priority_apply=self.module.priority_apply,
            apply_order=self.module.apply_order,
            is_admin=self.module.is_admin,
            md5=self.module.md5,
            visible=self.module.visible,
            created=self.module.created,
            updated=self.module.updated)
        # add extra data to make results more legible
        if self.module.tenant_id:
            # This should be the tenant name, but until we figure out where
            # to get it from, use the tenant_id
            tenant = self.module.tenant_id
        else:
            tenant = models.Modules.MATCH_ALL_NAME
        module_dict["tenant"] = tenant
        datastore = self.module.datastore_id
        datastore_version = self.module.datastore_version_id
        if datastore:
            if datastore_version:
                ds, ds_ver = (
                    datastore_models.get_datastore_version(
                        type=datastore, version=datastore_version))
                datastore = ds.name
                datastore_version = ds_ver.name
            else:
                ds = datastore_models.Datastore.load(datastore)
                datastore = ds.name
                datastore_version = models.Modules.MATCH_ALL_NAME
        else:
            datastore = models.Modules.MATCH_ALL_NAME
            datastore_version = models.Modules.MATCH_ALL_NAME
        module_dict["datastore"] = datastore
        module_dict["datastore_version"] = datastore_version

        return {"module": module_dict}
Пример #28
0
    def create(self, req, body, tenant_id):
        LOG.debug("Creating a Cluster for Tenant '%s'" % tenant_id)
        LOG.info(_("req : '%s'\n\n") % req)
        LOG.info(_("body : '%s'\n\n") % body)

        context = req.environ[wsgi.CONTEXT_KEY]
        name = body["cluster"]["name"]
        datastore_args = body["cluster"].get("datastore", {})
        datastore, datastore_version = datastore_models.get_datastore_version(**datastore_args)

        try:
            clusters_enabled = CONF.get(datastore_version.manager).get("cluster_support")
        except NoSuchOptError:
            clusters_enabled = False

        if not clusters_enabled:
            raise exception.ClusterDatastoreNotSupported(
                datastore=datastore.name, datastore_version=datastore_version.name
            )

        nodes = body["cluster"]["instances"]
        instances = []
        for node in nodes:
            flavor_id = utils.get_id_from_href(node["flavorRef"])
            volume_size = nics = availability_zone = None
            if "volume" in node:
                volume_size = int(node["volume"]["size"])
            if "nics" in node:
                nics = node["nics"]
            if "availability_zone" in node:
                availability_zone = node["availability_zone"]

            instances.append(
                {
                    "flavor_id": flavor_id,
                    "volume_size": volume_size,
                    "nics": nics,
                    "availability_zone": availability_zone,
                }
            )

        cluster = models.Cluster.create(context, name, datastore, datastore_version, instances)
        view = views.load_view(cluster, req=req, load_servers=False)
        return wsgi.Result(view.data(), 200)
Пример #29
0
    def edit(self, req, body, tenant_id, id):
        context = req.environ[wsgi.CONTEXT_KEY]
        group = models.Configuration.load(context, id)
        datastore_args = body["configuration"].get("datastore", {})
        datastore, datastore_version = ds_models.get_datastore_version(**datastore_args)
        instances = instances_models.DBInstance.find_all(
            tenant_id=context.tenant, configuration_id=id, deleted=False
        ).all()
        LOG.info(_("loaded configuration instances: %s") % instances)

        """validate if params are valid"""
        is_configuration_valid = False

        validation_result = KSC_ConfigurationsController.validate_configuration(
            context, body["configuration"]["values"], datastore_manager=datastore_version.manager
        )

        if instances:
            current_config = KSC_Configuration.load(context, id)
            validation_dynamic_result = (
                KSC_Configuration.validate_dynamic_params(
                    context, current_config, instances, body["configuration"]["values"], dynamic_param=True
                )
                or {}
            )
            if validation_result or validation_dynamic_result:
                validation_result.update(validation_dynamic_result)
            elif not validation_result and not validation_dynamic_result:
                is_configuration_valid = True
        else:
            if not validation_result:
                is_configuration_valid = True

        result = {"is_validate_pass": is_configuration_valid, "validation_result": validation_result}

        if validation_result:
            return wsgi.Result(result, 400)
        else:
            items = self.configuration_items_list(group, body["configuration"])
            models.Configuration.save(context, group, items, instances)

            result["is_validate_pass"] = is_configuration_valid

            return wsgi.Result(result, 202)
Пример #30
0
    def update(self, req, id, body, tenant_id):
        """Updates the instance to attach/detach configuration."""
        LOG.info(_("Updating instance for tenant id %s") % tenant_id)
        LOG.info(_("req: %s") % req)
        LOG.info(_("body: %s") % body)
        context = req.environ[wsgi.CONTEXT_KEY]

        instance = models.Instance.load(context, id)
        # if configuration is set, then we will update the instance to use
        # the new configuration.  If configuration is empty, we want to
        # disassociate the instance from the configuration group and remove the
        # active overrides file.

        configuration_id = self._configuration_parse(context, body)

        if configuration_id:
            need_restart, result = instance.assign_configuration(configuration_id)
        else:
            #restore instance config  default
            service_type = instance.service_type
            datastore, datastore_version = ds_models.get_datastore_version(service_type)
            template_config_id = datastore_version.default_configuration_id
            LOG.info(_("restore instance config %s") % template_config_id)
            need_restart, result = instance.assign_configuration(template_config_id)
            
            #raise exception.Forbidden("the instance can't unassign_config")
            '''
            instance.unassign_configuration()
            if instance.type == DBInstanceType.MASTER:
                standby = InstanceGroupItem.get_by_gid_type(context, instance.group_id,DBInstanceType.STANDBY)
                if standby:
                    standby_instance = models.Instance.load(context, standby.instance_id)
                    standby_instance.unassign_configuration()
            '''
        if result['is_validate_pass']:
            data = {"instance":{"need_restart":need_restart}, 'validation_result': result}
            return wsgi.Result(data, 202)
        else:             
            return wsgi.Result(result, 400)
Пример #31
0
    def create(self, req, body, tenant_id):
        LOG.debug("req : '%s'\n\n" % req)
        LOG.debug("body : '%s'\n\n" % req)

        context = req.environ[wsgi.CONTEXT_KEY]
        name = body["configuration"]["name"]
        description = body["configuration"].get("description")
        values = body["configuration"]["values"]

        datastore_args = body["configuration"].get("datastore", {})
        datastore, datastore_version = ds_models.get_datastore_version(**datastore_args)

        configItems = []
        validation_result = {}
        is_configuration_valid = True

        if values:
            # validate that the values passed in are permitted by the operator.
            """validate if params are valid"""
            validation_result = KSC_ConfigurationsController.validate_configuration(
                context, body["configuration"]["values"], datastore_manager=datastore_version.manager
            )

            for k, v in values.iteritems():
                configItems.append(ConfigurationParameter(configuration_key=k, configuration_value=v))

        if len(validation_result):
            is_configuration_valid = False
            result = {"is_validate_pass": is_configuration_valid, "validation_result": validation_result}
            return wsgi.Result(result, 400)
        else:
            result = {"is_validate_pass": is_configuration_valid, "validation_result": validation_result}

            cfg_group = models.Configuration.create(name, description, tenant_id, datastore.id, datastore_version.id)
            cfg_group_items = models.Configuration.create_items(cfg_group.id, values)
            view_data = views.DetailedConfigurationView(cfg_group, cfg_group_items)
            result.update(view_data.data())

            return wsgi.Result(result, 200)
Пример #32
0
    def create(self, req, body, tenant_id):
        LOG.debug("req : '%s'\n\n" % req)
        LOG.debug("body : '%s'\n\n" % req)

        name = body['configuration']['name']
        description = body['configuration'].get('description')
        values = body['configuration']['values']

        msg = _("Creating configuration group on tenant "
                "%(tenant_id)s with name: %(cfg_name)s")
        LOG.info(msg % {"tenant_id": tenant_id, "cfg_name": name})

        datastore_args = body['configuration'].get('datastore', {})
        datastore, datastore_version = (
            ds_models.get_datastore_version(**datastore_args))

        configItems = []
        if values:
            # validate that the values passed in are permitted by the operator.
            ConfigurationsController._validate_configuration(
                body['configuration']['values'],
                datastore_version,
                models.DatastoreConfigurationParameters.load_parameters(
                    datastore_version.id))

            for k, v in values.iteritems():
                configItems.append(DBConfigurationParameter(
                    configuration_key=k,
                    configuration_value=v))

        cfg_group = models.Configuration.create(name, description, tenant_id,
                                                datastore.id,
                                                datastore_version.id)
        cfg_group_items = models.Configuration.create_items(cfg_group.id,
                                                            values)
        view_data = views.DetailedConfigurationView(cfg_group,
                                                    cfg_group_items)
        return wsgi.Result(view_data.data(), 200)
Пример #33
0
    def create(self, req, body, tenant_id):
        LOG.debug("Creating a Cluster for Tenant '%s'" % tenant_id)
        LOG.info(_("req : '%s'\n\n") % req)
        LOG.info(_("body : '%s'\n\n") % body)

        context = req.environ[wsgi.CONTEXT_KEY]
        name = body['cluster']['name']
        datastore_args = body['cluster'].get('datastore', {})
        datastore, datastore_version = (
            datastore_models.get_datastore_version(**datastore_args))

        try:
            clusters_enabled = (CONF.get(datastore_version.manager)
                                .get('cluster_support'))
        except NoSuchOptError:
            clusters_enabled = False

        if not clusters_enabled:
            raise exception.ClusterDatastoreNotSupported(
                datastore=datastore.name,
                datastore_version=datastore_version.name)

        nodes = body['cluster']['instances']
        instances = []
        for node in nodes:
            flavor_id = utils.get_id_from_href(node['flavorRef'])
            if 'volume' in node:
                volume_size = int(node['volume']['size'])
            else:
                volume_size = None
            instances.append({"flavor_id": flavor_id,
                              "volume_size": volume_size})

        cluster = models.Cluster.create(context, name, datastore,
                                        datastore_version, instances)
        view = views.load_view(cluster, req=req, load_servers=False)
        return wsgi.Result(view.data(), 200)
Пример #34
0
    def create(self, req, body, tenant_id):
        # TODO(hub-cap): turn this into middleware
        LOG.info(_LI("Creating a database instance for tenant '%s'"),
                 tenant_id)
        LOG.debug("req : '%s'\n\n", strutils.mask_password(req))
        LOG.debug("body : '%s'\n\n", strutils.mask_password(body))
        context = req.environ[wsgi.CONTEXT_KEY]
        datastore_args = body['instance'].get('datastore', {})
        datastore, datastore_version = (
            datastore_models.get_datastore_version(**datastore_args))
        image_id = datastore_version.image_id
        name = body['instance']['name']
        flavor_ref = body['instance']['flavorRef']
        flavor_id = utils.get_id_from_href(flavor_ref)

        configuration = self._configuration_parse(context, body)
        databases = populate_validated_databases(
            body['instance'].get('databases', []))
        database_names = [database.get('_name', '') for database in databases]
        users = None
        try:
            users = populate_users(body['instance'].get('users', []),
                                   database_names)
        except ValueError as ve:
            raise exception.BadRequest(msg=ve)

        if 'volume' in body['instance']:
            volume_size = int(body['instance']['volume']['size'])
        else:
            volume_size = None

        if 'restorePoint' in body['instance']:
            backupRef = body['instance']['restorePoint']['backupRef']
            backup_id = utils.get_id_from_href(backupRef)
        else:
            backup_id = None

        availability_zone = body['instance'].get('availability_zone')
        nics = body['instance'].get('nics')
        #rds-start
        if CONF.private_network:
            from trove.common.remote import create_neutron_client
            from neutronclient.common import exceptions as neutron_exceptions
            client = create_neutron_client(context)
            try:
                net_info = client.list_networks(name=CONF.private_network)
                private_id = net_info['networks'][0]['id']
            except neutron_exceptions.NotFound:
                raise neutron_exceptions.NotFound((_("failed geting private"
                                                     " net id for: %s") %
                                                     CONF.private_network))
            else:
                nics.insert(1,{'net-id': private_id})
                LOG.info(_("attach network is %s") % nics)
        #rds-end
			

        slave_of_id = body['instance'].get('replica_of',
                                           # also check for older name
                                           body['instance'].get('slave_of'))
        replica_count = body['instance'].get('replica_count')
        instance = models.Instance.create(context, name, flavor_id,
                                          image_id, databases, users,
                                          datastore, datastore_version,
                                          volume_size, backup_id,
                                          availability_zone, nics,
                                          configuration, slave_of_id,
                                          replica_count=replica_count)

        view = views.InstanceDetailView(instance, req=req)
        return wsgi.Result(view.data(), 200)
Пример #35
0
    def create(self, req, body, tenant_id):
        # TODO(hub-cap): turn this into middleware
        LOG.info("Creating a database instance for tenant '%s'",
                 tenant_id)
        LOG.debug("req : '%s'\n\n", strutils.mask_password(req))
        LOG.debug("body : '%s'\n\n", strutils.mask_password(body))
        context = req.environ[wsgi.CONTEXT_KEY]
        policy.authorize_on_tenant(context, 'instance:create')
        context.notification = notification.DBaaSInstanceCreate(context,
                                                                request=req)
        datastore_args = body['instance'].get('datastore', {})
        datastore, datastore_version = (
            datastore_models.get_datastore_version(**datastore_args))
        image_id = datastore_version.image_id
        name = body['instance']['name']
        flavor_ref = body['instance']['flavorRef']
        flavor_id = utils.get_id_from_href(flavor_ref)

        configuration = self._configuration_parse(context, body)
        databases = populate_validated_databases(
            body['instance'].get('databases', []))
        database_names = [database.get('_name', '') for database in databases]
        users = None
        try:
            users = populate_users(body['instance'].get('users', []),
                                   database_names)
        except ValueError as ve:
            raise exception.BadRequest(message=ve)

        modules = body['instance'].get('modules')

        # The following operations have their own API calls.
        # We need to make sure the same policies are enforced when
        # creating an instance.
        # i.e. if attaching configuration group to an existing instance is not
        # allowed, it should not be possible to create a new instance with the
        # group attached either
        if configuration:
            policy.authorize_on_tenant(context, 'instance:update')
        if modules:
            policy.authorize_on_tenant(context, 'instance:module_apply')
        if users:
            policy.authorize_on_tenant(
                context, 'instance:extension:user:create')
        if databases:
            policy.authorize_on_tenant(
                context, 'instance:extension:database:create')

        if 'volume' in body['instance']:
            volume_info = body['instance']['volume']
            volume_size = int(volume_info['size'])
            volume_type = volume_info.get('type')
        else:
            volume_size = None
            volume_type = None

        if 'restorePoint' in body['instance']:
            backupRef = body['instance']['restorePoint']['backupRef']
            backup_id = utils.get_id_from_href(backupRef)
        else:
            backup_id = None

        availability_zone = body['instance'].get('availability_zone')
        nics = body['instance'].get('nics', [])

        slave_of_id = body['instance'].get('replica_of',
                                           # also check for older name
                                           body['instance'].get('slave_of'))
        replica_count = body['instance'].get('replica_count')
        locality = body['instance'].get('locality')
        if locality:
            locality_domain = ['affinity', 'anti-affinity']
            locality_domain_msg = ("Invalid locality '%s'. "
                                   "Must be one of ['%s']" %
                                   (locality,
                                    "', '".join(locality_domain)))
            if locality not in locality_domain:
                raise exception.BadRequest(message=locality_domain_msg)
            if slave_of_id:
                dupe_locality_msg = (
                    'Cannot specify locality when adding replicas to existing '
                    'master.')
                raise exception.BadRequest(message=dupe_locality_msg)

        region_name = body['instance'].get(
            'region_name', CONF.service_credentials.region_name
        )
        access = body['instance'].get('access', None)

        instance = models.Instance.create(context, name, flavor_id,
                                          image_id, databases, users,
                                          datastore, datastore_version,
                                          volume_size, backup_id,
                                          availability_zone, nics,
                                          configuration, slave_of_id,
                                          replica_count=replica_count,
                                          volume_type=volume_type,
                                          modules=modules,
                                          locality=locality,
                                          region_name=region_name,
                                          access=access)

        view = views.InstanceDetailView(instance, req=req)
        return wsgi.Result(view.data(), 200)
Пример #36
0
    def create(self, req, body, tenant_id):
        LOG.debug(("Creating a Cluster for Tenant '%(tenant_id)s'\n"
                   "req : '%(req)s'\n\nbody : '%(body)s'\n\n") % {
                       "tenant_id": tenant_id,
                       "req": req,
                       "body": body
                   })

        context = req.environ[wsgi.CONTEXT_KEY]
        policy.authorize_on_tenant(context, 'cluster:create')

        name = body['cluster']['name']
        datastore_args = body['cluster'].get('datastore', {})
        datastore, datastore_version = (datastore_models.get_datastore_version(
            **datastore_args))

        # TODO(saurabhs): add extended_properties to apischema
        extended_properties = body['cluster'].get('extended_properties', {})

        try:
            clusters_enabled = (CONF.get(
                datastore_version.manager).get('cluster_support'))
        except NoSuchOptError:
            clusters_enabled = False

        if not clusters_enabled:
            raise exception.ClusterDatastoreNotSupported(
                datastore=datastore.name,
                datastore_version=datastore_version.name)

        nodes = body['cluster']['instances']
        instances = []
        for node in nodes:
            flavor_id = utils.get_id_from_href(node['flavorRef'])
            volume_size = volume_type = nics = availability_zone = None
            modules = None
            if 'volume' in node:
                volume_size = int(node['volume']['size'])
                volume_type = node['volume'].get('type')
            if 'nics' in node:
                nics = node['nics']
            if 'availability_zone' in node:
                availability_zone = node['availability_zone']
            if 'modules' in node:
                modules = node['modules']

            instances.append({
                "flavor_id": flavor_id,
                "volume_size": volume_size,
                "volume_type": volume_type,
                "nics": nics,
                "availability_zone": availability_zone,
                'region_name': node.get('region_name'),
                "modules": modules
            })

        locality = body['cluster'].get('locality')
        if locality:
            locality_domain = ['affinity', 'anti-affinity']
            locality_domain_msg = ("Invalid locality '%s'. "
                                   "Must be one of ['%s']" %
                                   (locality, "', '".join(locality_domain)))
            if locality not in locality_domain:
                raise exception.BadRequest(msg=locality_domain_msg)

        configuration = body['cluster'].get('configuration')

        context.notification = notification.DBaaSClusterCreate(context,
                                                               request=req)
        with StartNotification(context,
                               name=name,
                               datastore=datastore.name,
                               datastore_version=datastore_version.name):
            cluster = models.Cluster.create(context, name, datastore,
                                            datastore_version, instances,
                                            extended_properties, locality,
                                            configuration)
        cluster.locality = locality
        view = views.load_view(cluster, req=req, load_servers=False)
        return wsgi.Result(view.data(), 200)
Пример #37
0
    def create(self, req, body, tenant_id):
        # TODO(hub-cap): turn this into middleware
        LOG.info(_LI("Creating a database instance for tenant '%s'"),
                 tenant_id)
        LOG.debug("req : '%s'\n\n", strutils.mask_password(req))
        LOG.debug("body : '%s'\n\n", strutils.mask_password(body))
        context = req.environ[wsgi.CONTEXT_KEY]
        context.notification = notification.DBaaSInstanceCreate(context,
                                                                request=req)
        datastore_args = body['instance'].get('datastore', {})
        datastore, datastore_version = (datastore_models.get_datastore_version(
            **datastore_args))
        image_id = datastore_version.image_id
        name = body['instance']['name']
        flavor_ref = body['instance']['flavorRef']
        flavor_id = utils.get_id_from_href(flavor_ref)

        configuration = self._configuration_parse(context, body)
        databases = populate_validated_databases(body['instance'].get(
            'databases', []))
        database_names = [database.get('_name', '') for database in databases]
        users = None
        try:
            users = populate_users(body['instance'].get('users', []),
                                   database_names)
        except ValueError as ve:
            raise exception.BadRequest(msg=ve)

        if 'volume' in body['instance']:
            volume_info = body['instance']['volume']
            volume_size = int(volume_info['size'])
            volume_type = volume_info.get('type')
        else:
            volume_size = None
            volume_type = None

        if 'restorePoint' in body['instance']:
            backupRef = body['instance']['restorePoint']['backupRef']
            backup_id = utils.get_id_from_href(backupRef)
        else:
            backup_id = None

        availability_zone = body['instance'].get('availability_zone')
        nics = body['instance'].get('nics')

        slave_of_id = body['instance'].get(
            'replica_of',
            # also check for older name
            body['instance'].get('slave_of'))
        replica_count = body['instance'].get('replica_count')
        modules = body['instance'].get('modules')
        locality = body['instance'].get('locality')
        if locality:
            locality_domain = ['affinity', 'anti-affinity']
            locality_domain_msg = ("Invalid locality '%s'. "
                                   "Must be one of ['%s']" %
                                   (locality, "', '".join(locality_domain)))
            if locality not in locality_domain:
                raise exception.BadRequest(msg=locality_domain_msg)
            if slave_of_id:
                dupe_locality_msg = (
                    'Cannot specify locality when adding replicas to existing '
                    'master.')
                raise exception.BadRequest(msg=dupe_locality_msg)

        instance = models.Instance.create(context,
                                          name,
                                          flavor_id,
                                          image_id,
                                          databases,
                                          users,
                                          datastore,
                                          datastore_version,
                                          volume_size,
                                          backup_id,
                                          availability_zone,
                                          nics,
                                          configuration,
                                          slave_of_id,
                                          replica_count=replica_count,
                                          volume_type=volume_type,
                                          modules=modules,
                                          locality=locality)

        view = views.InstanceDetailView(instance, req=req)
        return wsgi.Result(view.data(), 200)
Пример #38
0
    def create(self, req, body, tenant_id):
        # TODO(hub-cap): turn this into middleware
        LOG.info(_LI("Creating a database instance for tenant '%s'"),
                 tenant_id)
        LOG.debug("req : '%s'\n\n", strutils.mask_password(req))
        LOG.debug("body : '%s'\n\n", strutils.mask_password(body))
        context = req.environ[wsgi.CONTEXT_KEY]
        policy.authorize_on_tenant(context, 'instance:create')
        context.notification = notification.DBaaSInstanceCreate(context,
                                                                request=req)
        datastore_args = body['instance'].get('datastore', {})
        datastore, datastore_version = (
            datastore_models.get_datastore_version(**datastore_args))
        image_id = datastore_version.image_id

        name = body['instance']['name']
        flavor_ref = body['instance']['flavorRef']
        flavor_id = utils.get_id_from_href(flavor_ref)

        configuration = self._configuration_parse(context, body)
        users, databases = self._parse_users_and_databases(
            datastore_version.manager, body)

        modules = body['instance'].get('modules')

        # The following operations have their own API calls.
        # We need to make sure the same policies are enforced when
        # creating an instance.
        # i.e. if attaching configuration group to an existing instance is not
        # allowed, it should not be possible to create a new instance with the
        # group attached either
        if configuration:
            policy.authorize_on_tenant(context, 'instance:update')
        if modules:
            policy.authorize_on_tenant(context, 'instance:module_apply')
        if users:
            policy.authorize_on_tenant(
                context, 'instance:extension:user:create')
        if databases:
            policy.authorize_on_tenant(
                context, 'instance:extension:database:create')

        modules = body['instance'].get('modules')

        # The following operations have their own API calls.
        # We need to make sure the same policies are enforced when
        # creating an instance.
        # i.e. if attaching configuration group to an existing instance is not
        # allowed, it should not be possible to create a new instance with the
        # group attached either
        if configuration:
            policy.authorize_on_tenant(context, 'instance:update')
        if modules:
            policy.authorize_on_tenant(context, 'instance:module_apply')
        if users:
            policy.authorize_on_tenant(
                context, 'instance:extension:user:create')
        if databases:
            policy.authorize_on_tenant(
                context, 'instance:extension:database:create')

        if 'volume' in body['instance']:
            volume_info = body['instance']['volume']
            volume_size = int(volume_info['size'])
            volume_type = volume_info.get('type')
        else:
            volume_size = None
            volume_type = None

        if 'restorePoint' in body['instance']:
            backupRef = body['instance']['restorePoint']['backupRef']
            backup_id = utils.get_id_from_href(backupRef)
        else:
            backup_id = None

        availability_zone = body['instance'].get('availability_zone')
        nics = body['instance'].get('nics')

        slave_of_id = body['instance'].get('replica_of',
                                           # also check for older name
                                           body['instance'].get('slave_of'))
        replica_count = body['instance'].get('replica_count')
        locality = body['instance'].get('locality')
        if locality:
            locality_domain = ['affinity', 'anti-affinity']
            locality_domain_msg = ("Invalid locality '%s'. "
                                   "Must be one of ['%s']" %
                                   (locality,
                                    "', '".join(locality_domain)))
            if locality not in locality_domain:
                raise exception.BadRequest(msg=locality_domain_msg)
            if slave_of_id:
                dupe_locality_msg = (
                    'Cannot specify locality when adding replicas to existing '
                    'master.')
                raise exception.BadRequest(msg=dupe_locality_msg)
        region_name = body['instance'].get('region_name', CONF.os_region_name)

        instance = models.Instance.create(context, name, flavor_id,
                                          image_id, databases, users,
                                          datastore, datastore_version,
                                          volume_size, backup_id,
                                          availability_zone, nics,
                                          configuration, slave_of_id,
                                          replica_count=replica_count,
                                          volume_type=volume_type,
                                          modules=modules,
                                          locality=locality,
                                          region_name=region_name)

        view = views.InstanceDetailView(instance, req=req)
        return wsgi.Result(view.data(), 200)
Пример #39
0
    def create(self, req, body, tenant_id):
        LOG.debug(
            ("Creating a Cluster for Tenant '%(tenant_id)s'\n" "req : '%(req)s'\n\nbody : '%(body)s'\n\n")
            % {"tenant_id": tenant_id, "req": req, "body": body}
        )

        context = req.environ[wsgi.CONTEXT_KEY]
        name = body["cluster"]["name"]
        datastore_args = body["cluster"].get("datastore", {})
        datastore, datastore_version = datastore_models.get_datastore_version(**datastore_args)

        # TODO(saurabhs): add extended_properties to apischema
        extended_properties = body["cluster"].get("extended_properties", {})

        try:
            clusters_enabled = CONF.get(datastore_version.manager).get("cluster_support")
        except NoSuchOptError:
            clusters_enabled = False

        if not clusters_enabled:
            raise exception.ClusterDatastoreNotSupported(
                datastore=datastore.name, datastore_version=datastore_version.name
            )

        nodes = body["cluster"]["instances"]
        instances = []
        for node in nodes:
            flavor_id = utils.get_id_from_href(node["flavorRef"])
            volume_size = volume_type = nics = availability_zone = None
            modules = None
            if "volume" in node:
                volume_size = int(node["volume"]["size"])
                volume_type = node["volume"].get("volume_type")
            if "nics" in node:
                nics = node["nics"]
            if "availability_zone" in node:
                availability_zone = node["availability_zone"]
            if "modules" in node:
                modules = node["modules"]

            instances.append(
                {
                    "flavor_id": flavor_id,
                    "volume_size": volume_size,
                    "volume_type": volume_type,
                    "nics": nics,
                    "availability_zone": availability_zone,
                    "region_name": node.get("region_name"),
                    "modules": modules,
                }
            )

        locality = body["cluster"].get("locality")
        if locality:
            locality_domain = ["affinity", "anti-affinity"]
            locality_domain_msg = "Invalid locality '%s'. " "Must be one of ['%s']" % (
                locality,
                "', '".join(locality_domain),
            )
            if locality not in locality_domain:
                raise exception.BadRequest(msg=locality_domain_msg)

        context.notification = notification.DBaaSClusterCreate(context, request=req)
        with StartNotification(context, name=name, datastore=datastore.name, datastore_version=datastore_version.name):
            cluster = models.Cluster.create(
                context, name, datastore, datastore_version, instances, extended_properties, locality
            )
        cluster.locality = locality
        view = views.load_view(cluster, req=req, load_servers=False)
        return wsgi.Result(view.data(), 200)
Пример #40
0
    def create(self, req, body, tenant_id):
        LOG.info("Creating a database instance for tenant '%s'", tenant_id)
        LOG.debug("req : '%s'\n\n", strutils.mask_password(req))
        LOG.debug("body : '%s'\n\n", strutils.mask_password(body))
        context = req.environ[wsgi.CONTEXT_KEY]
        policy.authorize_on_tenant(context, 'instance:create')
        context.notification = notification.DBaaSInstanceCreate(context,
                                                                request=req)

        name = body['instance']['name']
        slave_of_id = body['instance'].get('replica_of')
        replica_count = body['instance'].get('replica_count')
        flavor_ref = body['instance'].get('flavorRef')
        datastore_args = body['instance'].get('datastore', {})
        volume_info = body['instance'].get('volume', {})
        availability_zone = body['instance'].get('availability_zone')
        nics = body['instance'].get('nics', [])
        locality = body['instance'].get('locality')
        region_name = body['instance'].get(
            'region_name', CONF.service_credentials.region_name)
        access = body['instance'].get('access', None)

        if slave_of_id:
            if flavor_ref:
                msg = 'Cannot specify flavor when creating replicas.'
                raise exception.BadRequest(message=msg)
            if datastore_args:
                msg = 'Cannot specify datastore when creating replicas.'
                raise exception.BadRequest(message=msg)
            if volume_info:
                msg = 'Cannot specify volume when creating replicas.'
                raise exception.BadRequest(message=msg)
            if locality:
                msg = 'Cannot specify locality when creating replicas.'
                raise exception.BadRequest(message=msg)
            backup_model.verify_swift_auth_token(context)
        else:
            if replica_count and replica_count > 1:
                msg = (f"Replica count only valid when creating replicas. "
                       f"Cannot create {replica_count} instances.")
                raise exception.BadRequest(message=msg)

        flavor_id = utils.get_id_from_href(flavor_ref)

        if volume_info:
            volume_size = int(volume_info.get('size'))
            volume_type = volume_info.get('type')
        else:
            volume_size = None
            volume_type = None

        if slave_of_id:
            try:
                replica_source = models.DBInstance.find_by(context,
                                                           id=slave_of_id,
                                                           deleted=False)
                flavor_id = replica_source.flavor_id
            except exception.ModelNotFoundError:
                LOG.error(f"Cannot create a replica of {slave_of_id} as that "
                          f"instance could not be found.")
                raise exception.NotFound(uuid=slave_of_id)
            if replica_source.slave_of_id:
                raise exception.Forbidden(
                    f"Cannot create a replica of a replica {slave_of_id}")

            datastore_version = ds_models.DatastoreVersion.load_by_uuid(
                replica_source.datastore_version_id)
            datastore = ds_models.Datastore.load(
                datastore_version.datastore_id)
        else:
            datastore, datastore_version = ds_models.get_datastore_version(
                **datastore_args)

        # If only image_tags is configured in the datastore version, get
        # the image ID using the tags.
        glance_client = clients.create_glance_client(context)
        image_id = common_glance.get_image_id(glance_client,
                                              datastore_version.image_id,
                                              datastore_version.image_tags)
        LOG.info(f'Using image {image_id} for creating instance')

        databases = populate_validated_databases(body['instance'].get(
            'databases', []))
        database_names = [database.get('_name', '') for database in databases]
        users = None
        try:
            users = populate_users(body['instance'].get('users', []),
                                   database_names)
        except ValueError as ve:
            raise exception.BadRequest(message=str(ve))
        if slave_of_id and (databases or users):
            raise exception.ReplicaCreateWithUsersDatabasesError()

        configuration = self._configuration_parse(context, body)
        modules = body['instance'].get('modules')

        # The following operations have their own API calls.
        # We need to make sure the same policies are enforced when
        # creating an instance.
        # i.e. if attaching configuration group to an existing instance is not
        # allowed, it should not be possible to create a new instance with the
        # group attached either
        if configuration:
            policy.authorize_on_tenant(context, 'instance:update')
        if modules:
            policy.authorize_on_tenant(context, 'instance:module_apply')
        if users:
            policy.authorize_on_tenant(context,
                                       'instance:extension:user:create')
        if databases:
            policy.authorize_on_tenant(context,
                                       'instance:extension:database:create')

        if 'restorePoint' in body['instance']:
            backupRef = body['instance']['restorePoint']['backupRef']
            backup_id = utils.get_id_from_href(backupRef)
        else:
            backup_id = None

        # Only 1 nic is allowed as defined in API jsonschema.
        # Use list just for backward compatibility.
        if len(nics) > 0:
            nic = nics[0]
            LOG.info('Checking user provided instance network %s', nic)
            if slave_of_id and nic.get('ip_address'):
                msg = "Cannot specify IP address when creating replicas."
                raise exception.BadRequest(message=msg)
            self._check_nic(context, nic)

        if locality:
            locality_domain = ['affinity', 'anti-affinity']
            locality_domain_msg = ("Invalid locality '%s'. "
                                   "Must be one of ['%s']" %
                                   (locality, "', '".join(locality_domain)))
            if locality not in locality_domain:
                raise exception.BadRequest(message=locality_domain_msg)

        instance = models.Instance.create(context,
                                          name,
                                          flavor_id,
                                          image_id,
                                          databases,
                                          users,
                                          datastore,
                                          datastore_version,
                                          volume_size,
                                          backup_id,
                                          availability_zone,
                                          nics,
                                          configuration,
                                          slave_of_id,
                                          replica_count=replica_count,
                                          volume_type=volume_type,
                                          modules=modules,
                                          locality=locality,
                                          region_name=region_name,
                                          access=access)

        view = views.InstanceDetailView(instance, req=req)
        return wsgi.Result(view.data(), 200)
Пример #41
0
    def _get_create_param(self,req,body,instance_type):
        context = req.environ[wsgi.CONTEXT_KEY]
        master_id = None 
        extend = None
        template_config_id = body['instance'].get('template_config_id',None)
        
        if 'extend' in body['instance']:  
            extend = body['instance']['extend']
            extend['availability_zone'] = body['instance'].get('availability_zone', None)

        if instance_type==InstanceType.SG or instance_type==InstanceType.HA:
            service_type = (body['instance'].get('service_type') or CONF.service_type)
            #service = models.ServiceImage.find_by(service_name=service_type)
            #image_id = service['image_id']
            datastore, datastore_version = ds_models.get_datastore_version(service_type)
            image_id = datastore_version.image_id
            
            name = body['instance'].get('name', None)
            flavor_ref = body['instance']['flavorRef']
            flavor_id = utils.get_id_from_href(flavor_ref)
            databases = populate_validated_databases(
                body['instance'].get('databases', []))
            users = None
            try:
                users = populate_users(body['instance'].get('users', []))
            except ValueError as ve:          
                raise exception.BadRequest(msg=ve)

            if 'volume' in body['instance']:
                volume_size = int(body['instance']['volume']['size'])
            else:
                volume_size = None
        
            if 'restorePoint' in body['instance']:
                backupRef = body['instance']['restorePoint']['backupRef']
                backup_id = utils.get_id_from_href(backupRef)
            else:
                backup_id = None 
        
        if instance_type in [InstanceType.RR, InstanceType.STANDBY]:
            v_master_id = body['instance'].get('instance_id')
            master_id = instance_utils.virtual_instid_2_origin_instid(v_master_id, get_deleted = False)
            master_instance = models.BuiltInstance.load(context, id = master_id)
            service_type = master_instance.service_type
            #service = models.ServiceImage.find_by(service_name=service_type)
            #image_id = service['image_id']
            datastore, datastore_version = ds_models.get_datastore_version(service_type)
            image_id = datastore_version.image_id
            
            name = body['instance'].get('name', None)
            if name is None:
                name = master_instance.name + "_" + instance_type.lower()
                
            #flavor_id = master_instance.flavor_id
            flavor_ref = body['instance']['flavorRef']
            flavor_id = utils.get_id_from_href(flavor_ref)
            databases = []
            users = None
            volume_size = master_instance.volume_size
            backup_id = None
            
            if "1" == body['instance'].get('config_id_from_master', "0"):
                template_config_id = master_instance.db_info.configuration_id
         
        if  template_config_id is None:
            template_config_id = datastore_version.default_configuration_id
        
        
        return context, name, flavor_id,image_id, databases, users,\
            service_type, volume_size,backup_id, instance_type,\
            extend,master_id,template_config_id
Пример #42
0
 def version_show(self, req, tenant_id, datastore, id):
     datastore, datastore_version = models.get_datastore_version(
         datastore, id)
     return wsgi.Result(
         views.DatastoreVersionView(datastore_version, req).data(), 200)
Пример #43
0
    def create(self, req, body, tenant_id):
        # TODO(hub-cap): turn this into middleware
        LOG.info(_("Creating a database instance for tenant '%s'") % tenant_id)
        LOG.info(logging.mask_password(_("req : '%s'\n\n") % req))
        LOG.info(logging.mask_password(_("body : '%s'\n\n") % body))
        context = req.environ[wsgi.CONTEXT_KEY]
        datastore_args = body["instance"].get("datastore", {})
        datastore, datastore_version = datastore_models.get_datastore_version(**datastore_args)
        image_id = datastore_version.image_id
        name = body["instance"]["name"]
        flavor_ref = body["instance"]["flavorRef"]
        flavor_id = utils.get_id_from_href(flavor_ref)

        configuration = self._configuration_parse(context, body)
        databases = populate_validated_databases(body["instance"].get("databases", []))
        database_names = [database.get("_name", "") for database in databases]
        users = None
        try:
            users = populate_users(body["instance"].get("users", []), database_names)
        except ValueError as ve:
            raise exception.BadRequest(msg=ve)

        if "volume" in body["instance"]:
            volume_size = int(body["instance"]["volume"]["size"])
        else:
            volume_size = None

        if "restorePoint" in body["instance"]:
            backupRef = body["instance"]["restorePoint"]["backupRef"]
            backup_id = utils.get_id_from_href(backupRef)
        else:
            backup_id = None

        if "availability_zone" in body["instance"]:
            availability_zone = body["instance"]["availability_zone"]
        else:
            availability_zone = None

        if "nics" in body["instance"]:
            nics = body["instance"]["nics"]
        else:
            nics = None

        instance = models.Instance.create(
            context,
            name,
            flavor_id,
            image_id,
            databases,
            users,
            datastore,
            datastore_version,
            volume_size,
            backup_id,
            availability_zone,
            nics,
            configuration,
        )

        view = views.InstanceDetailView(instance, req=req)
        return wsgi.Result(view.data(), 200)
Пример #44
0
    def create(self, req, body, tenant_id):
        LOG.debug("Creating a Backup for tenant '%s'" % tenant_id)
        context = req.environ[wsgi.CONTEXT_KEY]
        data = body['backup']
        instance = data.get('instance',None)
        group = data.get('group',None)
        name = data['name']
        type = data.get("type", "snapshot")
        #expire_at = data.get("expire_after", 7)
        desc = data.get('description')
        parent_id = data.get('parent_id')
        LOG.info("parent_id:%s", parent_id)

        if group is None and instance is None:
            raise exception.BadRequest("you must specify group or instance")

        instance_id = None

        if group is not None:
            try:
                instance_id = InstanceGroupItem.get_by_gid_type(context, group, DBInstanceType.STANDBY).instance_id
            except:
                instance_id = InstanceGroupItem.get_by_gid_type(context, group, DBInstanceType.SINGLE).instance_id

        if instance_id is None and instance is not None:
            instance_id = inst_utils.virtual_instid_2_origin_instid(instance)

        _instance = DBInstance.find_by(context,id=instance_id)
        _type = _instance.service_type
        #_image = ServiceImage.find_by(context,service_name=_type)
        #service_image_id = _image.id
        ds,ds_version = ds_models.get_datastore_version(_type)
        service_image_id = ds_version.image_id

        grp_item = InstanceGroupItem.get_by_instance_id(context, _instance.id)
        group_id = grp_item.group_id

        # get this group's autobackup config and set the expire_after default
        _autobackup = AutoBackup.get_by_gid(context, group_id)
        expire_after = data.get("expire_after", _autobackup.expire_after)
        duration = _autobackup.duration
        expire_at = AutoBackup.calculate_expire_at(expire_after, duration)
        LOG.info("group_id %s, expire_at :%s", group_id, time.ctime(expire_at))

        if grp_item.type == DBInstanceType.MASTER:
            try:
                instance_id = InstanceGroupItem.get_by_gid_type(context, group_id, DBInstanceType.STANDBY).instance_id
            except Exception as e:
                LOG.error(e)

        backup = Backup.create(context, instance_id, name, description=desc,group_id=group_id,backup_type=type,expire_at=expire_at,service_image_id=service_image_id,parent_id=parent_id)
        try:
            #service = inst_models.ServiceImage.find_by(id=backup.service_image_id)
            #backup.db_type = service['service_name']
            ds,ds_version = ds_patch_models.find_datastore_by_image_id(backup.service_image_id)
            backup.db_type = ds.name
        except Exception as ex:
            backup.db_type = ""
            LOG.warn("Failed get db type information of backup %s, %s", backup.id, ex)
        chain = self._get_chain_ids(context, id)
        LOG.info(_("chain : '%s'") % chain)
        return wsgi.Result(views.BackupView(backup).data(), 202)
Пример #45
0
    def create(self, req, body, tenant_id):
        LOG.debug(("Creating a Cluster for Tenant '%(tenant_id)s'\n"
                   "req : '%(req)s'\n\nbody : '%(body)s'\n\n") %
                  {"tenant_id": tenant_id, "req": req, "body": body})

        context = req.environ[wsgi.CONTEXT_KEY]
        policy.authorize_on_tenant(context, 'cluster:create')

        name = body['cluster']['name']
        datastore_args = body['cluster'].get('datastore', {})
        datastore, datastore_version = (
            datastore_models.get_datastore_version(**datastore_args))

        # TODO(saurabhs): add extended_properties to apischema
        extended_properties = body['cluster'].get('extended_properties', {})

        try:
            clusters_enabled = (CONF.get(datastore_version.manager)
                                .get('cluster_support'))
        except NoSuchOptError:
            clusters_enabled = False

        if not clusters_enabled:
            raise exception.ClusterDatastoreNotSupported(
                datastore=datastore.name,
                datastore_version=datastore_version.name)

        nodes = body['cluster']['instances']
        instances = []
        for node in nodes:
            flavor_id = utils.get_id_from_href(node['flavorRef'])
            volume_size = volume_type = nics = availability_zone = None
            modules = None
            if 'volume' in node:
                volume_size = int(node['volume']['size'])
                volume_type = node['volume'].get('type')
            if 'nics' in node:
                nics = node['nics']
            if 'availability_zone' in node:
                availability_zone = node['availability_zone']
            if 'modules' in node:
                modules = node['modules']
            instance_type = None
            if 'type' in node:
                instance_type = node['type']
                if isinstance(instance_type, six.string_types):
                    instance_type = instance_type.split(',')

            instances.append({"flavor_id": flavor_id,
                              "volume_size": volume_size,
                              "volume_type": volume_type,
                              "nics": nics,
                              "availability_zone": availability_zone,
                              'region_name': node.get('region_name'),
                              "modules": modules,
                              "instance_type": instance_type})

        locality = body['cluster'].get('locality')
        if locality:
            locality_domain = ['affinity', 'anti-affinity']
            locality_domain_msg = ("Invalid locality '%s'. "
                                   "Must be one of ['%s']" %
                                   (locality,
                                    "', '".join(locality_domain)))
            if locality not in locality_domain:
                raise exception.BadRequest(msg=locality_domain_msg)

        configuration = body['cluster'].get('configuration')

        context.notification = notification.DBaaSClusterCreate(context,
                                                               request=req)
        with StartNotification(context, name=name, datastore=datastore.name,
                               datastore_version=datastore_version.name):
            cluster = models.Cluster.create(context, name, datastore,
                                            datastore_version, instances,
                                            extended_properties,
                                            locality, configuration)
        cluster.locality = locality
        view = views.load_view(cluster, req=req, load_servers=False)
        return wsgi.Result(view.data(), 200)
Пример #46
0
 def version_show(self, req, tenant_id, datastore, id):
     datastore, datastore_version = models.get_datastore_version(datastore,
                                                                 id)
     return wsgi.Result(views.DatastoreVersionView(datastore_version,
                                                   req).data(), 200)