예제 #1
0
def delete_excluded_resources_handler(event, context):
    trace_id = eventhelper.get_trace_id(event)
    user_id = trace_id
    organization_id = eventhelper.get_organization_id(event)
    project_id = eventhelper.get_project_id(event)
    check_item_code = eventhelper.get_check_item_code(event)
    coop_id = eventhelper.get_coop_id(event)
    region_name = eventhelper.get_region_name(event)
    resource_type = eventhelper.get_resource_type(event)
    resource_name = eventhelper.get_resource_name(event)

    # Get logging
    pm_logger = common_utils.begin_logger(trace_id, __name__,
                                          inspect.currentframe())

    # アクセス権限チェックを行います。
    response_authority = checkauthority.authority(trace_id, user_id,
                                                  organization_id,
                                                  Authority.Editor)
    if response_authority:
        return common_utils.response(response_authority, pm_logger)

    # リソース除外設定情報を削除します。
    response = checkitemsettings_logic.delete_excluded_resources(
        trace_id, organization_id, project_id, check_item_code, coop_id,
        region_name, resource_type, resource_name)
    return common_utils.response(response, pm_logger)
    def test_delete_excluded_resources_case_error_validate_not_param_query_string_resource_type(
            self):
        resource_type = None

        # mock function error_validate
        mock_error_validate = mock_common_utils.mock_error_validate(self)

        # call Function test
        actual_response = checkitemsettings_logic.delete_excluded_resources(
            trace_id, organization_id, project_id, check_item_code, coop_id,
            region_name, resource_type, resource_name)

        # assert output function
        # check call function common write log error
        mock_error_validate.assert_called_once()

        # check response
        actual_actual_response_body = json.loads(actual_response["body"])
        message_201 = MsgConst.ERR_REQUEST_201
        self.assertEqual(message_201["code"],
                         actual_actual_response_body["code"])
        self.assertEqual(message_201["message"],
                         actual_actual_response_body["message"])
        self.assertEqual(HTTPStatus.UNPROCESSABLE_ENTITY.value,
                         actual_response["statusCode"])

        actual_response_error = actual_actual_response_body["errors"]
        err_val_101 = MsgConst.ERR_VAL_101
        self.assertEqual(err_val_101["code"], actual_response_error[0]["code"])
        self.assertEqual("resource_type", actual_response_error[0]["field"])
        self.assertEqual(None, actual_response_error[0]["value"])
        self.assertEqual(err_val_101["message"],
                         actual_response_error[0]["message"])
    def test_delete_excluded_resources_case_get_record_pm_aws_account_coops_is_zero(
            self):
        # mock object
        patch_query_awscoop_coop_key = patch(
            "premembers.repository.pm_awsAccountCoops.query_awscoop_coop_key")

        # start mock object
        mock_query_awscoop_coop_key = patch_query_awscoop_coop_key.start()
        mock_error_common = mock_common_utils.mock_error_common(self)

        # mock data
        mock_query_awscoop_coop_key.return_value = []

        # addCleanup stop mock object
        self.addCleanup(patch_query_awscoop_coop_key.stop)

        # call Function test
        actual_response = checkitemsettings_logic.delete_excluded_resources(
            trace_id, organization_id, project_id, check_item_code, coop_id,
            region_name, resource_type, resource_name)

        # assert output function
        # check call function common write log error
        mock_error_common.assert_called_once()

        # assert output function
        actual_response_body = json.loads(actual_response["body"])
        err_aws_401 = MsgConst.ERR_AWS_401
        self.assertEqual(err_aws_401["code"], actual_response_body["code"])
        self.assertEqual(err_aws_401["message"],
                         actual_response_body["message"])
        self.assertEqual(err_aws_401["description"],
                         actual_response_body["description"])
        self.assertEqual(HTTPStatus.UNPROCESSABLE_ENTITY.value,
                         actual_response["statusCode"])
    def test_delete_excluded_resources_case_error_delete_record_pm_exclusion_resources(
            self):
        # mock object
        patch_query_awscoop_coop_key = patch(
            "premembers.repository.pm_awsAccountCoops.query_awscoop_coop_key")
        patch_query_check_item_refine_code = patch(
            "premembers.repository.pm_exclusionResources.query_check_item_refine_code"
        )
        patch_delete_excluded_resourcess = patch(
            "premembers.repository.pm_exclusionResources.delete")

        # start mock object
        mock_query_awscoop_coop_key = patch_query_awscoop_coop_key.start()
        mock_query_check_item_refine_code = patch_query_check_item_refine_code.start(
        )
        mock_delete_excluded_resourcess = patch_delete_excluded_resourcess.start(
        )
        mock_error_exception = mock_common_utils.mock_error_exception(self)

        # mock data
        mock_query_awscoop_coop_key.return_value = data_pm_aws_account_coops
        mock_query_check_item_refine_code.return_value = [
            data_pm_exclusion_resources
        ]
        mock_delete_excluded_resourcess.side_effect = PmError()

        # addCleanup stop mock object
        self.addCleanup(patch_query_awscoop_coop_key.stop)
        self.addCleanup(patch_query_check_item_refine_code.stop)
        self.addCleanup(patch_delete_excluded_resourcess.stop)

        # call Function test
        actual_response = checkitemsettings_logic.delete_excluded_resources(
            trace_id, organization_id, project_id, check_item_code, coop_id,
            region_name, resource_type, resource_name)

        # assert output function
        # check call function common write log error
        mock_error_exception.assert_called_once()

        # assert output function
        actual_response_body = json.loads(actual_response["body"])
        err_405 = MsgConst.ERR_DB_405
        self.assertEqual(err_405["code"], actual_response_body["code"])
        self.assertEqual(err_405["message"], actual_response_body["message"])
        self.assertEqual(err_405["description"],
                         actual_response_body["description"])
        self.assertEqual(HTTPStatus.INTERNAL_SERVER_ERROR.value,
                         actual_response["statusCode"])