def _request_with_skip_connection_extnet(self, skip_connection_extnet=False): payload = self.get_valid_payload() payload['skip_connection_extnet'] = skip_connection_extnet patch = mock.patch('waldur_mastermind.packages.views.executors') mock_executors = patch.start() common_utils.create_request(self.view, self.fixture.staff, payload) transmitted_skip = mock_executors.OpenStackPackageCreateExecutor.execute.call_args[ 1]['skip_connection_extnet'] mock.patch.stopall() return transmitted_skip
def test_logger_called_when_package_change_scheduled(self, logger_mock): common_utils.create_request(self.view, self.fixture.staff, self.get_valid_payload()) logger_mock.openstack_package.info.assert_called_once_with( 'Tenant package change has been scheduled. ' 'Old value: %s, new value: {package_template_name}' % self.fixture.openstack_package.template.name, event_type='openstack_package_change_scheduled', event_context={ 'tenant': self.package.tenant, 'package_template_name': self.new_template.name, 'service_settings': self.package.service_settings, })
def extend_package(self, total_price): self.new_template = packages_factories.PackageTemplateFactory( service_settings=self.fixture.openstack_service_settings ) component_price = ( total_price / 31.0 / len(self.new_template.get_required_component_types()) ) for component_type in self.new_template.get_required_component_types(): self.new_template.components.filter(type=component_type).update( price=component_price, amount=1 ) view = packages_views.OpenStackPackageViewSet.as_view({'post': 'change'}) response = common_utils.create_request( view, self.fixture.owner, {'template': self.new_template.uuid.hex, 'package': self.package.uuid.hex,}, ) if response.status_code == status.HTTP_202_ACCEPTED: packages_utils.run_openstack_package_change_executor( self.package, self.new_template ) return response
def test_if_only_staff_manages_services_other_user_can_not_create_package( self, user ): response = common_utils.create_request( self.view, getattr(self.fixture, user), self.get_valid_payload() ) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_user_cannot_extend_package_with_same_template(self): payload = self.get_valid_payload(template=self.package.template) response = common_utils.create_request(self.view, self.fixture.staff, payload) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual( response.data['non_field_errors'], ["New package template cannot be the same as package's current template."], )
def send_request(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) return self.get_scope_from_response(response)
def terminate_resource(serialized_resource, serialized_user): resource = core_utils.deserialize_instance(serialized_resource) user = core_utils.deserialize_instance(serialized_user) view = views.ResourceViewSet.as_view({'post': 'terminate'}) response = create_request(view, user, {}, uuid=resource.uuid.hex) if response.status_code != status.HTTP_200_OK: raise exceptions.ResourceTerminateException(response.rendered_content)
def test_user_cannot_extend_package_with_tenant_in_invalid_state(self): self.package.tenant.state = openstack_models.Tenant.States.ERRED self.package.tenant.save(update_fields=['state']) response = common_utils.create_request(self.view, self.fixture.staff, self.get_valid_payload()) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data['package'], ["Package's tenant must be in OK state."])
def test_user_can_decrease_package_if_tenant_usage_does_not_exceeds_new_limits( self, component_type ): self.set_usage_and_limit(component_type, usage=5, old_limit=20, new_limit=5) response = common_utils.create_request( self.view, self.fixture.staff, self.get_valid_payload() ) self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
def send_request(self, user): view = self.get_view() payload = self.get_post_data() response = common_utils.create_request(view, user, payload) if response.status_code != status.HTTP_202_ACCEPTED: raise serializers.ValidationError(response.data) # we expect all children to implement async update process, which will set state of resource back to OK return False
def test_user_cannot_extend_package_with_new_template_settings_in_invalid_state( self): self.new_template.service_settings.state = structure_models.ServiceSettings.States.ERRED self.new_template.service_settings.save(update_fields=['state']) response = common_utils.create_request(self.view, self.fixture.staff, self.get_valid_payload()) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data['template'], ["Template's settings must be in OK state."])
def test_package_is_replaced_on_extend(self): response = common_utils.create_request( self.view, self.fixture.staff, self.get_valid_payload() ) self.run_success_task() self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED) old_package = models.OpenStackPackage.objects.filter(uuid=self.package.uuid) self.assertFalse(old_package.exists()) new_package = models.OpenStackPackage.objects.filter(template=self.new_template) self.assertTrue(new_package.exists())
def test_create_answer_on_behalf_raises_error_when_uuid_has_invalid_format( self): response = common_utils.create_request( self.view, self.fixture.staff, post_data=[{ 'question_uuid': self.question.uuid.hex, 'value': True }], query_params={'on_behalf_user_uuid': 'INVALID'}, checklist_uuid=self.question.checklist.uuid.hex, ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_when_openstack_package_is_extended_project_total_is_updated(self): fixture = packages_fixtures.PackageFixture() package = fixture.openstack_package new_template = packages_factories.PackageTemplateFactory( service_settings=fixture.openstack_service_settings) view = packages_views.OpenStackPackageViewSet.as_view( {'post': 'change'}) response = common_utils.create_request(view, fixture.owner, { 'template': new_template.uuid.hex, 'package': package.uuid.hex, }) self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
def test_tenant_quotas_are_defined_by_template(self): response = common_utils.create_request(self.view, self.fixture.owner, self.get_valid_payload()) self.assertEqual(response.status_code, status.HTTP_201_CREATED) package = models.OpenStackPackage.objects.get( uuid=response.data['uuid']) tenant, template = package.tenant, package.template for quota_name, component_type in models.OpenStackPackage.get_quota_to_component_mapping( ).items(): self.assertEqual( tenant.quotas.get(name=quota_name).limit, template.components.get(type=component_type).amount)
def process_order_item(self, user): resource = self.get_resource() if not resource: raise serializers.ValidationError('Resource is not found.') view = self.get_view() payload = self.get_post_data() response = common_utils.create_request(view, user, payload) if response.status_code == status.HTTP_202_ACCEPTED: self.order_item.resource.set_state_updating() self.order_item.resource.save(update_fields=['state']) else: raise serializers.ValidationError(response.data)
def test_create_answer(self): response = common_utils.create_request( self.view, self.fixture.staff, post_data=[{ 'question_uuid': self.question.uuid.hex, 'value': True }], checklist_uuid=self.question.checklist.uuid.hex, ) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertTrue( models.Answer.objects.filter(question=self.question, user=self.fixture.staff).exists())
def test_create_answer_on_behalf_by_non_staff_is_ignored(self): response = common_utils.create_request( self.view, self.fixture.global_support, post_data=[{ 'question_uuid': self.question.uuid.hex, 'value': True }], query_params={'on_behalf_user_uuid': self.fixture.user.uuid.hex}, checklist_uuid=self.question.checklist.uuid.hex, ) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertFalse( models.Answer.objects.filter(question=self.question, user=self.fixture.user).exists())
def test_user_cannot_create_openstack_package_if_template_is_archived(self): spl = self.fixture.openstack_spl spl_url = 'http://testserver' + reverse( 'openstack-spl-detail', kwargs={'pk': spl.pk} ) template = factories.PackageTemplateFactory( archived=True, service_settings=spl.service.settings ) payload = { 'service_project_link': spl_url, 'name': 'test_package', 'template': template.uuid.hex, } response = common_utils.create_request(self.view, self.fixture.owner, payload) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_after_package_extension_tenant_is_updated(self): response = common_utils.create_request(self.view, self.fixture.staff, self.get_valid_payload()) self.run_success_task() self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED) self.package.tenant.refresh_from_db() self.assertDictEqual( self.package.tenant.extra_configuration, { 'package_name': self.new_template.name, 'package_uuid': self.new_template.uuid.hex, 'package_category': self.new_template.get_category_display(), 'cores': self.new_template.components.get(type='cores').amount, 'ram': self.new_template.components.get(type='ram').amount, 'storage': self.new_template.components.get(type='storage').amount, })
def create_management_security_group(self, request, uuid=None): serializer = serializers.CreateManagementSecurityGroupSerializer( data=request.data, many=True) serializer.is_valid(raise_exception=True) cluster = self.get_object() user = request.user tenant = utils.get_management_tenant(cluster) port = cluster.settings.get_option('management_tenant_access_port') rules = [] for rule in serializer.validated_data: rules.append({ 'protocol': 'tcp', 'from_port': port, 'to_port': port, 'direction': openstack_models.SecurityGroupRule.INGRESS, 'ethertype': rule['ethertype'], 'cidr': rule['cidr'], }) post_data = { 'name': cluster.name, 'description': 'Access for management of cluster %s' % cluster.name, 'rules': rules, } view = openstack_views.TenantViewSet.as_view( {'post': 'create_security_group'}) group_response = common_utils.create_request(view, user, post_data, uuid=tenant.uuid.hex) if group_response.status_code != status.HTTP_201_CREATED: return response.Response(group_response.data, status=group_response.status_code) security_group = openstack_models.SecurityGroup.objects.get( uuid=group_response.data.get('uuid')) cluster.management_security_group = security_group cluster.save() return response.Response( {'security_group_uuid': security_group.uuid.hex}, status=status.HTTP_201_CREATED, )
def test_template_data_is_saved_tenant_extra_configurations(self): response = common_utils.create_request(self.view, self.fixture.owner, self.get_valid_payload()) self.assertEqual(response.status_code, status.HTTP_201_CREATED) package = models.OpenStackPackage.objects.get( uuid=response.data['uuid']) tenant, template = package.tenant, package.template self.assertDictEqual( tenant.extra_configuration, { 'package_name': template.name, 'package_uuid': template.uuid.hex, 'package_category': template.get_category_display(), 'cores': template.components.get(type='cores').amount, 'ram': template.components.get(type='ram').amount, 'storage': template.components.get(type='storage').amount, })
def test_after_package_extension_related_service_settings_are_updated(self): response = common_utils.create_request( self.view, self.fixture.staff, self.get_valid_payload() ) self.run_success_task() self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED) quotas = self.package.service_settings.quotas components = self.new_template.components self.assertEqual( quotas.get(name='vcpu').limit, components.get(type='cores').amount ) self.assertEqual( quotas.get(name='ram').limit, components.get(type='ram').amount ) self.assertEqual( quotas.get(name='storage').limit, components.get(type='storage').amount )
def process_order_item(self, user): """We need to overwrite process order item because two cases exist: a switch of a plan and a change of limits.""" if self.is_update_limit_order_item(): try: # self.update_limits_process method can execute not is_async # because in this case an order has got only one order item. self.update_limits_process(user) except NotImplementedError: self.order_item.set_state_erred() self.order_item.save(update_fields=['state']) logger.warning( 'An update of limits has been called. ' 'But update limits process for the plugin has not been implemented. ' 'Order item ID: %s, Plugin: %s.', self.order_item.id, self.order_item.offering.type) except Exception as e: signals.limit_update_failed.send( sender=self.order_item.resource.__class__, order_item=self.order_item, error_message=str(e), ) else: signals.limit_update_succeeded.send( sender=self.order_item.resource.__class__, order_item=self.order_item, ) return resource = self.get_resource() if not resource: raise serializers.ValidationError('Resource is not found.') view = self.get_view() payload = self.get_post_data() response = common_utils.create_request(view, user, payload) if response.status_code == status.HTTP_202_ACCEPTED: self.order_item.resource.set_state_updating() self.order_item.resource.save(update_fields=['state']) else: raise serializers.ValidationError(response.data)
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.objects.create( 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_quotas() self.order_item.resource = resource self.order_item.save(update_fields=['resource'])
def schedule_resources_termination(resources): from waldur_mastermind.marketplace import views if not resources: return view = views.ResourceViewSet.as_view({'post': 'terminate'}) user = core_utils.get_system_robot() if not user: logger.error( 'Staff user with username system_robot for terminating resources ' 'of project with due date does not exist.') return for resource in resources: response = create_request(view, user, {}, uuid=resource.uuid.hex) if response.status_code != status.HTTP_200_OK: logger.error('Terminating resource %s has failed. %s' % (resource.uuid.hex, response.content))
def execute(self, instance, user_id): node = instance content_type = ContentType.objects.get_for_model( openstack_tenant_models.Instance) flavor = node.initial_data['flavor'] system_volume_size = node.initial_data['system_volume_size'] system_volume_type = node.initial_data.get('system_volume_type') data_volumes = node.initial_data.get('data_volumes', []) image = node.initial_data['image'] subnet = node.initial_data['subnet'] group = node.initial_data['group'] tenant_spl = node.initial_data['tenant_service_project_link'] user = auth.get_user_model().objects.get(pk=user_id) post_data = { 'name': node.name, 'flavor': reverse('openstacktenant-flavor-detail', kwargs={'uuid': flavor}), 'image': reverse('openstacktenant-image-detail', kwargs={'uuid': image}), 'service_project_link': reverse('openstacktenant-spl-detail', kwargs={'pk': tenant_spl}), 'system_volume_size': system_volume_size, 'system_volume_type': system_volume_type and reverse( 'openstacktenant-volume-type-detail', kwargs={'uuid': system_volume_type}, ), 'data_volumes': [{ 'size': volume['size'], 'volume_type': volume.get('volume_type') and reverse( 'openstacktenant-volume-type-detail', kwargs={'uuid': volume.get('volume_type')}, ), } for volume in data_volumes], 'security_groups': [{ 'url': reverse('openstacktenant-sgp-detail', kwargs={'uuid': group}) }], 'internal_ips_set': [{ 'subnet': reverse('openstacktenant-subnet-detail', kwargs={'uuid': subnet}) }], 'user_data': utils.format_node_cloud_config(node), } view = InstanceViewSet.as_view({'post': 'create'}) response = common_utils.create_request(view, user, post_data) if response.status_code != status.HTTP_201_CREATED: raise exceptions.RancherException(response.data) instance_uuid = response.data['uuid'] instance = openstack_tenant_models.Instance.objects.get( uuid=instance_uuid) node.content_type = content_type node.object_id = instance.id node.state = models.Node.States.CREATING node.save() resource_imported.send( sender=instance.__class__, instance=instance, )
def test_admin_can_create_openstack_package_with_permission_from_settings(self): response = common_utils.create_request( self.view, self.fixture.admin, self.get_valid_payload() ) self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_user_cannot_create_openstack_package(self, user): response = common_utils.create_request( self.view, getattr(self.fixture, user), self.get_valid_payload() ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_if_only_staff_manages_services_he_can_create_openstack_package(self, user): response = common_utils.create_request( self.view, getattr(self.fixture, user), self.get_valid_payload() ) self.assertEqual(response.status_code, status.HTTP_201_CREATED)