def create_resource_of_volume_if_instance_created(sender, instance, created=False, **kwargs): resource = instance if not created or not resource.scope or not resource.offering.scope: return if resource.offering.type != INSTANCE_TYPE: return instance = resource.scope volume_offering = utils.get_offering(VOLUME_TYPE, resource.offering.scope) if not volume_offering: return for volume in instance.volumes.all(): if marketplace_models.Resource.objects.filter(scope=volume).exists(): continue volume_resource = marketplace_models.Resource( project=resource.project, offering=volume_offering, created=resource.created, name=volume.name, scope=volume, ) volume_resource.init_cost() volume_resource.save() utils.import_volume_metadata(volume_resource) volume_resource.init_quotas()
def create_marketplace_resource_for_imported_resources( sender, instance, offering=None, plan=None, **kwargs ): resource = marketplace_models.Resource( project=instance.service_project_link.project, state=get_resource_state(instance.state), name=instance.name, scope=instance, created=instance.created, plan=plan, offering=offering, ) if isinstance(instance, openstack_tenant_models.Instance): offering = offering or utils.get_offering( INSTANCE_TYPE, instance.service_settings ) if not offering: return resource.offering = offering resource.init_cost() resource.save() utils.import_instance_metadata(resource) resource.init_quotas() if isinstance(instance, openstack_tenant_models.Volume): offering = offering or utils.get_offering( VOLUME_TYPE, instance.service_settings ) if not offering: return resource.offering = offering resource.init_cost() resource.save() utils.import_volume_metadata(resource) resource.init_quotas() if isinstance(instance, openstack_models.Tenant): offering = offering or utils.get_offering( PACKAGE_TYPE, instance.service_settings ) if not offering: return resource.offering = offering resource.init_cost() resource.save() utils.import_resource_metadata(resource) resource.init_quotas() create_offerings_for_volume_and_instance(instance)
def process_order_item(self, user): with transaction.atomic(): resource = marketplace_models.Resource( project=self.order_item.order.project, offering=self.order_item.offering, plan=self.order_item.plan, limits=self.order_item.limits, attributes=self.order_item.attributes, name=self.order_item.attributes.get('name') or '', state=marketplace_models.Resource.States.CREATING, ) resource.init_cost() resource.save() resource.init_quotas() self.order_item.resource = resource self.order_item.save(update_fields=['resource'])
def process_order_item(self, user): scope = self.send_request(user) with transaction.atomic(): resource = models.Resource( project=self.order_item.order.project, offering=self.order_item.offering, plan=self.order_item.plan, limits=self.order_item.limits, attributes=self.order_item.attributes, name=self.order_item.attributes.get('name') or '', scope=scope, ) resource.init_cost() resource.save() resource.init_quotas() self.order_item.resource = resource self.order_item.save(update_fields=['resource'])
def create_marketplace_resource_for_imported_cluster( sender, instance, offering=None, plan=None, **kwargs ): if not offering or not plan: # When cluster is imported directly (ie without marketplace), # marketplace resources are not created. return resource = marketplace_models.Resource( project=instance.service_project_link.project, state=get_resource_state(instance.state), name=instance.name, scope=instance, created=instance.created, plan=plan, offering=offering, ) resource.init_cost() resource.save() resource.init_quotas()
def process_order_item(self, user): post_data = self.get_post_data() view = self.get_viewset().as_view({'post': 'create'}) response = common_utils.create_request(view, user, post_data) if response.status_code != status.HTTP_201_CREATED: raise serializers.ValidationError(response.data) with transaction.atomic(): scope = self.get_scope_from_response(response) resource = models.Resource( project=self.order_item.order.project, offering=self.order_item.offering, plan=self.order_item.plan, limits=self.order_item.limits, attributes=self.order_item.attributes, scope=scope, ) resource.init_cost() resource.save() resource.init_quotas() self.order_item.resource = resource self.order_item.save(update_fields=['resource'])
def create_marketplace_resource_for_imported_resources( sender, instance, offering=None, plan=None, **kwargs ): resource = marketplace_models.Resource( # backend_id is None if instance is being restored from backup because # on database level there's uniqueness constraint enforced for backend_id # but in marketplace resource backend_is not nullable backend_id=instance.backend_id or '', project=instance.project, state=get_resource_state(instance.state), name=instance.name, scope=instance, created=instance.created, plan=plan, offering=offering, ) if isinstance(instance, openstack_tenant_models.Instance): offering = offering or utils.get_offering( INSTANCE_TYPE, instance.service_settings ) if not offering: return resource.offering = offering resource.init_cost() resource.save() utils.import_instance_metadata(resource) resource.init_quotas() if isinstance(instance, openstack_tenant_models.Volume): offering = offering or utils.get_offering( VOLUME_TYPE, instance.service_settings ) if not offering: return resource.offering = offering resource.init_cost() resource.save() utils.import_volume_metadata(resource) resource.init_quotas() if isinstance(instance, openstack_models.Tenant): offering = offering or utils.get_offering( TENANT_TYPE, instance.service_settings ) if not offering: return resource.offering = offering resource.init_cost() resource.save() utils.import_resource_metadata(resource) resource.init_quotas() create_offerings_for_volume_and_instance(instance)