def retrieve(self, request, tenant_pk=None, aws_env_pk=None, region_pk=None, service_pk=None, pk=None): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: retrieve") try: aws_environment = AwsEnvironmentModel.objects.get( id=aws_env_pk, tenant_id=tenant_pk) resource = ControlResourceUseCase(log).describe_resource( request.user, aws_environment, Resource.get_service_resource(region_pk, service_pk, pk)) except (TypeError, ValueError, KeyError, NarukoException) as e: # リクエストデータが不正 logger.exception(e) return Response(status=status.HTTP_400_BAD_REQUEST) except ObjectDoesNotExist as e: # AWS環境が存在しない logger.exception(e) return Response(status=status.HTTP_404_NOT_FOUND) except Exception as e: logger.exception(e) raise else: logger.info("END: retrieve") return Response(data=resource.serialize(aws_environment), status=status.HTTP_200_OK)
def list(self, request, tenant_pk=None, aws_env_pk=None, region_pk=None, service_pk=None, resource_pk=None): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: list") try: aws_environment = AwsEnvironmentModel.objects.get( id=aws_env_pk, tenant_id=tenant_pk) resource = Resource.get_service_resource(region_pk, service_pk, resource_pk) backups = ControlResourceUseCase(log).fetch_backups( request.user, aws_environment, resource) except (TypeError, ValueError, KeyError, ClientError, NarukoException) as e: # リクエストデータが不正 logger.exception(e) return Response(status=status.HTTP_400_BAD_REQUEST) except ObjectDoesNotExist as e: # AWS環境が存在しない logger.exception(e) return Response(status=status.HTTP_404_NOT_FOUND) except Exception as e: logger.exception(e) raise else: logger.info("END: list") return Response(data=[backup.serialize() for backup in backups], status=status.HTTP_200_OK)
def create(self, request, tenant_pk=None, aws_env_pk=None, region_pk=None, service_pk=None, resource_pk=None): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: create") try: aws_environment = AwsEnvironmentModel.objects.get( id=aws_env_pk, tenant_id=tenant_pk) resource = Resource.get_service_resource(region_pk, service_pk, resource_pk) no_reboot = request.data["no_reboot"] if isinstance(resource, Ec2) else None backup_id = ControlResourceUseCase(log).create_backup( request.user, aws_environment, resource, no_reboot) except (TypeError, ValueError, KeyError, ClientError, NarukoException) as e: # リクエストデータが不正 logger.exception(e) return Response(status=status.HTTP_400_BAD_REQUEST) except ObjectDoesNotExist as e: # AWS環境が存在しない logger.exception(e) return Response(status=status.HTTP_404_NOT_FOUND) except Exception as e: logger.exception(e) raise else: logger.info("END: create") return Response(data={"backup_id": backup_id}, status=status.HTTP_201_CREATED)
def test_fetch_resources(self, mock_tag: mock.Mock, mock_cloudwatch: mock.Mock): mock_user = mock.Mock(spec=UserModel) mock_aws = mock.Mock(spec=AwsEnvironmentModel) region = "region" # mockの準備 cloudwatch_return_value = mock_cloudwatch.return_value cloudwatch_return_value.get_resources_status.return_value = { "EC2": { "ec2": "OK" }, "RDS": { "rds": "DANGER" }, "ELB": { "elb": "CAUTION" } } tag_return_value = mock_tag.return_value mock_resource1 = mock.Mock() mock_resource1.get_service_name.return_value = "EC2" mock_resource1.resource_id = "ec2" mock_resource2 = mock.Mock() mock_resource2.get_service_name.return_value = "RDS" mock_resource2.resource_id = "rds" mock_resource3 = mock.Mock() mock_resource3.get_service_name.return_value = "ELB" mock_resource3.resource_id = "elb" mock_resource4 = mock.Mock() mock_resource4.get_service_name.return_value = "EC2" tag_return_value.get_resources.return_value = [[ mock_resource1, mock_resource2, mock_resource3, mock_resource4 ], []] # 検証対象の実行 resp = ControlResourceUseCase(mock.Mock()).fetch_resources( mock_user, mock_aws, region) # 戻り値の検証 self.assertEqual( resp, [mock_resource1, mock_resource2, mock_resource3, mock_resource4]) self.assertEqual(["OK", "DANGER", "CAUTION", "UNSET"], [ mock_resource1.status, mock_resource2.status, mock_resource3.status, mock_resource4.status ]) # 呼び出し検証 mock_user.has_aws_env.assert_called_with(mock_aws) mock_cloudwatch.assert_called_with(aws_environment=mock_aws, region=region) mock_tag.assert_called_with(aws_environment=mock_aws, region=region) cloudwatch_return_value.get_resources_status.assert_called() tag_return_value.get_resources.assert_called()
def stop(self, request, tenant_pk=None, aws_env_pk=None, region_pk=None, service_pk=None, pk=None): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: stop") aws_environment = AwsEnvironmentModel.objects.get(id=aws_env_pk, tenant_id=tenant_pk) resource = Resource.get_service_resource(region_pk, service_pk, pk) ControlResourceUseCase(log).stop_resource(request.user, aws_environment, resource) logger.info("END: stop") return Response(status=status.HTTP_200_OK)
def list(self, request, tenant_pk=None, aws_env_pk=None, region_pk=None): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: list") aws_environment = AwsEnvironmentModel.objects.get(id=aws_env_pk, tenant_id=tenant_pk) documents = ControlResourceUseCase(log).fetch_documents( request.user, aws_environment, region_pk) logger.info("END: list") return Response(data=[doc.serialize() for doc in documents], status=status.HTTP_200_OK)
def test_run_command(self): mock_user = mock.Mock(spec=UserModel) mock_aws = mock.Mock(spec=AwsEnvironmentModel) mock_command = mock.Mock() # 検証対象の実行 res = ControlResourceUseCase(mock.Mock()).run_command( mock_user, mock_aws, mock_command) mock_user.is_belong_to_tenant.assert_called_once_with(mock_aws.tenant) mock_user.has_aws_env.assert_called_once_with(mock_aws) mock_command.run.assert_called_once_with(mock_aws) self.assertEqual(res, mock_command)
def test_create_backup(self): mock_user = mock.Mock(spec=UserModel) mock_aws = mock.Mock(spec=AwsEnvironmentModel) mock_resource = mock.Mock() # 検証対象の実行 res = ControlResourceUseCase(mock.Mock()).create_backup( mock_user, mock_aws, mock_resource, True) # 呼び出し検証 mock_user.is_belong_to_tenant.assert_called_once() mock_user.has_aws_env.assert_called_once() self.assertEqual(res, mock_resource.create_backup.return_value)
def retrieve(self, request, tenant_pk=None, aws_env_pk=None, region_pk=None, service_pk=None, pk=None): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: retrieve") aws_environment = AwsEnvironmentModel.objects.get(id=aws_env_pk, tenant_id=tenant_pk) resource = ControlResourceUseCase(log).describe_resource( request.user, aws_environment, Resource.get_service_resource(region_pk, service_pk, pk)) logger.info("END: retrieve") return Response(data=resource.serialize(aws_environment), status=status.HTTP_200_OK)
def test_create_backup_not_have_aws(self): mock_user = mock.Mock(spec=UserModel) mock_user.has_aws_env.return_value = False mock_aws = mock.Mock(spec=AwsEnvironmentModel) mock_resource = mock.Mock() # 検証対象の実行 with self.assertRaises(PermissionDenied): ControlResourceUseCase(mock.Mock()).create_backup( mock_user, mock_aws, mock_resource, True) # 呼び出し検証 mock_user.is_belong_to_tenant.assert_called_once() mock_user.has_aws_env.assert_called_once()
def test_stop_resource(self): mock_user = mock.Mock(spec=UserModel) mock_aws = mock.Mock(spec=AwsEnvironmentModel) mock_resource = mock.Mock() # 検証対象の実行 ControlResourceUseCase(mock.Mock()).stop_resource( mock_user, mock_aws, mock_resource) # 呼び出し検証 mock_user.is_belong_to_tenant.assert_called_once() mock_user.has_aws_env.assert_called_once() mock_resource.stop.assert_called_once()
def list(self, request, tenant_pk=None, aws_env_pk=None): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: list") region = request.GET.get("region") aws_environment = AwsEnvironmentModel.objects.get(id=aws_env_pk, tenant_id=tenant_pk) resources = ControlResourceUseCase(log).fetch_resources( request.user, aws_environment, region ) logger.info("END: list") return Response(data=[resource.serialize(aws_environment) for resource in resources], status=status.HTTP_200_OK)
def test_fetch_documents(self, mock_ssm): mock_user = mock.Mock(spec=UserModel) mock_aws = mock.Mock(spec=AwsEnvironmentModel) list_documents = mock_ssm.return_value.list_documents list_documents.return_value = [[1, 2, 3], [4, 5, 6]] # 検証対象の実行 res = ControlResourceUseCase(mock.Mock()).fetch_documents( mock_user, mock_aws, "region") mock_user.is_belong_to_tenant.assert_called_once_with(mock_aws.tenant) mock_user.has_aws_env.assert_called_once_with(mock_aws) list_documents.assert_called() self.assertEqual(res, [1, 2, 3, 4, 5, 6])
def run_command(self, request, tenant_pk=None, aws_env_pk=None, region_pk=None, service_pk=None, pk=None): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: run_command") aws_environment = AwsEnvironmentModel.objects.get(id=aws_env_pk, tenant_id=tenant_pk) resource = Resource.get_service_resource(region_pk, service_pk, pk) command = Command( Document( request.data["name"], [Parameter(**param) for param in request.data["parameters"]]), resource) command = ControlResourceUseCase(log).run_command(request.user, aws_environment, command) logger.info("END: run_command") return Response(status=status.HTTP_200_OK, data=command.serialize())
def test_fetch_documents_no_aws(self, mock_ssm): mock_user = mock.Mock(spec=UserModel) mock_user.has_aws_env.return_value = False mock_aws = mock.Mock(spec=AwsEnvironmentModel) list_documents = mock_ssm.return_value.list_documents list_documents.return_value = [[1, 2, 3], [4, 5, 6]] # 検証対象の実行 with self.assertRaises(PermissionDenied): ControlResourceUseCase(mock.Mock()).fetch_documents( mock_user, mock_aws, "region") mock_user.is_belong_to_tenant.assert_called_once_with(mock_aws.tenant) mock_user.has_aws_env.assert_called_once_with(mock_aws) list_documents.assert_not_called()
def test_describe_resource_not_belong_to_tenant(self): mock_user = mock.Mock(spec=UserModel) mock_user.is_belong_to_tenant.return_value = False mock_aws = mock.Mock(spec=AwsEnvironmentModel) mock_resource = mock.Mock() # 検証対象の実行 with self.assertRaises(PermissionDenied): ControlResourceUseCase(mock.Mock()).describe_resource( mock_user, mock_aws, mock_resource) # 呼び出し検証 mock_user.is_belong_to_tenant.assert_called_once() mock_user.has_aws_env.assert_not_called() mock_resource.describe.assert_not_called()
def test_describe_document(self, mock_ssm): mock_user = mock.Mock(spec=UserModel) mock_aws = mock.Mock(spec=AwsEnvironmentModel) mock_doc = mock.Mock() describe_document = mock_ssm.return_value.describe_document describe_document.return_value = mock_doc # 検証対象の実行 res = ControlResourceUseCase(mock.Mock()).describe_document( mock_user, mock_aws, "region", "name") mock_user.is_belong_to_tenant.assert_called_once_with(mock_aws.tenant) mock_user.has_aws_env.assert_called_once_with(mock_aws) describe_document.assert_called_once_with("name") self.assertEqual(res, mock_doc)
def test_describe_document_no_aws(self, mock_ssm): mock_user = mock.Mock(spec=UserModel) mock_user.has_aws_env.return_value = False mock_aws = mock.Mock(spec=AwsEnvironmentModel) mock_doc = mock.Mock() describe_document = mock_ssm.return_value.describe_document describe_document.return_value = mock_doc # 検証対象の実行 with self.assertRaises(PermissionDenied): ControlResourceUseCase(mock.Mock()).describe_document( mock_user, mock_aws, "region", "name") mock_user.is_belong_to_tenant.assert_called_once_with(mock_aws.tenant) mock_user.has_aws_env.assert_called_once_with(mock_aws) describe_document.assert_not_called()
def list(self, request, tenant_pk=None, aws_env_pk=None, region_pk=None, service_pk=None, resource_pk=None): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: list") aws_environment = AwsEnvironmentModel.objects.get(id=aws_env_pk, tenant_id=tenant_pk) resource = Resource.get_service_resource(region_pk, service_pk, resource_pk) backups = ControlResourceUseCase(log).fetch_backups( request.user, aws_environment, resource) logger.info("END: list") return Response(data=[backup.serialize() for backup in backups], status=status.HTTP_200_OK)
def create(self, request, tenant_pk=None, aws_env_pk=None, region_pk=None, service_pk=None, resource_pk=None): log = NarukoLogging(request) logger = log.get_logger(__name__) logger.info("START: create") aws_environment = AwsEnvironmentModel.objects.get(id=aws_env_pk, tenant_id=tenant_pk) resource = Resource.get_service_resource(region_pk, service_pk, resource_pk) no_reboot = request.data["no_reboot"] if isinstance(resource, Ec2) else None backup_id = ControlResourceUseCase(log).create_backup( request.user, aws_environment, resource, no_reboot) logger.info("END: create") return Response(data={"backup_id": backup_id}, status=status.HTTP_201_CREATED)
def test_not_belong_to_tenant(self, mock_tag: mock.Mock, mock_cloudwatch: mock.Mock): mock_user = mock.Mock(spec=UserModel) mock_user.is_belong_to_tenant.return_value = False mock_aws = mock.Mock(spec=AwsEnvironmentModel) region = "region" # 検証対象の実行 with self.assertRaises(PermissionDenied): ControlResourceUseCase(mock.Mock()).fetch_resources( mock_user, mock_aws, region) # 呼び出し検証 mock_user.has_aws_env.assert_not_called() mock_cloudwatch.assert_not_called() mock_tag.assert_not_called() cloudwatch_return_value = mock_cloudwatch.return_value tag_return_value = mock_tag.return_value cloudwatch_return_value.get_resources_status.assert_not_called() tag_return_value.get_resources.assert_not_called()