Пример #1
0
    def test_create_report_success(self):
        test_mail_address = mail_address.format(str(3))
        test_user_id = user_id.format(str(3))

        # handler
        path_parameters = {
            "organization_id": organization_id,
            "project_id": project_id
        }
        event_mock = event_create.get_event_object(
            email=test_mail_address,
            path_parameters=path_parameters,
            trace_id=test_user_id,
            body=json.dumps(report_create))
        response = reports.create_report_handler(event_mock, {})

        # Get data response
        status_code = response['statusCode']
        response_body = json.loads(response['body'])
        id = response_body["id"]
        name = response_body['name']
        generateUser = response_body['generateUser']
        awsAccounts = response_body['awsAccounts']
        status = response_body['status']
        jsonOutputTime = response_body['jsonOutputTime']
        excelOutputStatus = response_body['excelOutputStatus']
        excelOutputTime = response_body['excelOutputTime']
        schemaVersion = response_body['schemaVersion']
        projectId = response_body['projectId']
        organizationId = response_body['organizationId']
        createdAt = response_body['createdAt']
        updatedAt = response_body['updatedAt']

        # Get data in database
        report_database = pm_reports.query_report(trace_id,
                                                  id,
                                                  convert_response=True)

        # Check data
        self.assertEqual(id, report_database['id'])
        self.assertEqual(name, report_database['name'])
        self.assertEqual(generateUser, report_database['generateUser'])
        self.assertEqual(awsAccounts, report_database['awsAccounts'])
        self.assertEqual(status, report_database['status'])
        self.assertEqual(jsonOutputTime, report_database['jsonOutputTime'])
        self.assertEqual(excelOutputStatus,
                         report_database['excelOutputStatus'])
        self.assertEqual(excelOutputTime, report_database['excelOutputTime'])
        self.assertEqual(schemaVersion, report_database['schemaVersion'])
        self.assertEqual(projectId, report_database['projectId'])
        self.assertEqual(organizationId, report_database['organizationId'])
        self.assertEqual(createdAt, report_database['createdAt'])
        self.assertEqual(updatedAt, report_database['updatedAt'])
        self.assertEqual(status_code, HTTPStatus.CREATED.value)
        pm_reports.delete_reports(trace_id, id)
Пример #2
0
 def tearDown(self):
     num = 1
     while num < 7:
         pm_organizationTasks.delete(trace_id, task_id.format(str(num)))
         pm_reports.delete_reports(trace_id, report_id.format(str(num)))
         num += 1
     pm_organizationTasks.delete(trace_id, task_id.format(str(7)))
     pm_organizationTasks.delete(trace_id, task_id.format(str(8)))
     pm_awsAccountCoops.delete_awscoops(trace_id, coop_id.format(str(1)))
     pm_projects.delete_projects(trace_id, project_id)
     pm_projects.delete_projects(trace_id, project_id_error.format(str(1)))
     pm_organizations.delete_organization(trace_id, organization_id)
     pm_organizations.delete_organization(
         trace_id, organization_id_error.format(str(1)))
Пример #3
0
def delete_report(trace_id, email, report_id, organization_id, project_id):
    pm_logger = common_utils.begin_logger(trace_id, __name__,
                                          inspect.currentframe())
    try:
        # get report
        report_item = pm_reports.query_report_filter_organization_project(
            trace_id, report_id, project_id, organization_id)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_402,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    if (not report_item):
        return common_utils.error_common(MsgConst.ERR_301,
                                         HTTPStatus.NOT_FOUND, pm_logger)

    try:
        # delete report
        pm_reports.delete_reports(trace_id, report_id)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_DB_405,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    task_id = common_utils.get_uuid4()
    try:
        # create task informaiton request delete in Organization Tasks
        pm_organizationTasks.create_organizationTask(trace_id, task_id,
                                                     task_code, report_id,
                                                     trace_id, email, 0, 0, 3)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_DB_403,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # Send message to organization topic task
    aws_common.sns_organization_topic(trace_id, task_id, task_code)

    # response if delete success
    response = common_utils.get_response_by_response_body(
        HTTPStatus.NO_CONTENT, None)
    return common_utils.response(response, pm_logger)
Пример #4
0
 def tearDown(self):
     num = 1
     while num < 4:
         pm_affiliation.delete_affiliation(user_id.format(str(num)),
                                           organization_id)
         pm_reports.delete_reports(trace_id, report_id.format(str(num)))
         num += 1
     pm_projects.delete_projects(trace_id, project_id)
     pm_organizations.delete_organization(trace_id, organization_id)
     # Delete ReportJobDefs
     pm_batchJobDefs.delete_report_job_def(trace_id, code_aws)
     pm_batchJobDefs.delete_report_job_def(trace_id, code_json)
     pm_batchJobDefs.delete_report_job_def(trace_id, code_excel)
     pm_reports.delete_reports(trace_id, report_id.format(str(4)))
     pm_reports.delete_reports(trace_id, report_id.format(str(5)))
     pm_reports.delete_reports(trace_id, report_id.format(str(6)))
Пример #5
0
 def tearDown(self):
     num = 1
     while num < 7:
         pm_organizationTasks.delete(trace_id, task_id.format(str(num)))
         pm_reports.delete_reports(trace_id, report_id.format(str(num)))
         num += 1
Пример #6
0
def create_report(trace_id, email, organization_id, project_id, data_body):
    pm_logger = common_utils.begin_logger(trace_id, __name__,
                                          inspect.currentframe())
    try:
        project = pm_projects.get_projects_by_organization_id(
            trace_id, project_id, organization_id)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_402,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # 組織情報を取得します。
    if (not project):
        return common_utils.error_common(MsgConst.ERR_AWS_401,
                                         HTTPStatus.UNPROCESSABLE_ENTITY,
                                         pm_logger)

    # Parse JSON
    try:
        body_object = json.loads(data_body)
        report_name = body_object["name"]
        aws_accounts = body_object["awsAccounts"]
        output_file_type = body_object["outputFileType"]
    except Exception as e:
        return common_utils.error_exception(MsgConst.ERR_REQUEST_202,
                                            HTTPStatus.BAD_REQUEST, e,
                                            pm_logger, True)

    # Validate
    list_error = validate_report(trace_id, report_name, aws_accounts,
                                 output_file_type)
    if list_error:
        return common_utils.error_validate(MsgConst.ERR_REQUEST_201,
                                           HTTPStatus.UNPROCESSABLE_ENTITY,
                                           list_error, pm_logger)

    # Create report
    report_id = common_utils.get_uuid4()
    status = Status.Waiting.value
    html_output_status = Status.Waiting.value
    excel_output_status = Status.Waiting.value
    schema_version = CommonConst.SCHEMA_VERSION
    try:
        pm_reports.create_report(trace_id, report_id, report_name, email,
                                 aws_accounts, status, None, None, None,
                                 html_output_status, None, None,
                                 excel_output_status, None, None,
                                 schema_version, organization_id, project_id)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_DB_403,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # AWS利用状況情報収集ジョブの設定
    # レポート中間ファイル作成ジョブの設定
    # レポート出力ジョブの設定
    codes = [
        'COLLECT_AWS_RESOURCE_INFO', 'OUTPUT_REPORT_JSON',
        'OUTPUT_REPORT_EXCEL'
    ]
    job_id = []
    for code in codes:
        response, job_id = job_report(trace_id, email, report_id, code, job_id)
        if response:
            # Delete report
            pm_reports.delete_reports(trace_id, report_id)
            return response

    try:
        report = pm_reports.query_report(trace_id, report_id, True)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_402,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)
    # return data response
    response = common_utils.get_response_by_response_body(
        HTTPStatus.CREATED, report)
    return common_utils.response(response, pm_logger)
def delete_project(task_id, project_id):
    pm_logger = common_utils.begin_logger(task_id, __name__,
                                          inspect.currentframe())
    # get list awscoops with key project_id
    try:
        list_awscoops = pm_awsAccountCoops.query_awscoop_project_index(
            task_id, project_id)
    except PmError as e:
        pm_logger.error("AWSアカウント連携情報取得に失敗しました。: ProjectID=%s", project_id)
        pm_logger.error(e)
        return False

    for awscoop_item in list_awscoops:
        coop_id = awscoop_item["CoopID"]
        try:
            pm_awsAccountCoops.delete_awscoops(task_id, coop_id)
        except PmError as e:
            pm_logger.error("AWSアカウント連携情報削除に失敗しました。: CoopID=%s", coop_id)
            pm_logger.error(e)
            return False

    # get list security check webhook with key project_id
    try:
        list_security_check_webhooks = pm_securityCheckWebhook.query_project_index(
            task_id, project_id)
    except PmError as e:
        pm_logger.error("チェック実行Webhook情報の取得に失敗しました。: ProjectID=%s", project_id)
        pm_logger.error(e)
        return False

    for security_check_webhook in list_security_check_webhooks:
        security_check_webhook_id = security_check_webhook[
            "SecurityCheckWebhookID"]

        # get list security check webhook call history with key security_check_webhook_id
        security_check_webhook_call_historys = pm_securityCheckWebhookCallHistory.query(
            task_id, security_check_webhook_id)

        # delete record pm_securityCheckWebhookCallHistory with security_check_webhook_id, create_at
        for security_check_webhook_call_history in security_check_webhook_call_historys:
            create_at = security_check_webhook_call_history["CreatedAt"]
            try:
                pm_securityCheckWebhookCallHistory.delete(
                    task_id, security_check_webhook_id, create_at)
            except PmError as e:
                pm_logger.error("チェック実行Webhook実行履歴情報削除に失敗しました。: WebhookID=%s",
                                security_check_webhook_id)
                pm_logger.error(e)
                return False

        # delete record pm_securityCheckWebhook with security_check_webhook_id
        try:
            pm_securityCheckWebhook.delete(task_id, security_check_webhook_id)
        except PmError as e:
            pm_logger.error("チェック実行Webhook情報削除に失敗しました。: WebhookID=%s",
                            security_check_webhook_id)
            pm_logger.error(e)
            return False

    # get list reports with key project_id
    try:
        list_reports = pm_reports.query_list_reports_project_index(
            task_id, project_id)
    except PmError as e:
        pm_logger.error("レポート情報取得に失敗しました。: ProjectID=%s", project_id)
        pm_logger.error(e)
        return False

    for report_item in list_reports:
        report_id = report_item["ReportID"]
        try:
            pm_reports.delete_reports(task_id, report_id)
        except PmError as e:
            pm_logger.error("レポート情報削除に失敗しました。: ProjectID=%s", project_id)
            pm_logger.error(e)
            return False
        status_delete_report = reportsBatch_logic.delete_report(
            task_id, report_id)
        if status_delete_report is False:
            return False

    return True