def create_custom_controls()

in createCustomStandard.py [0:0]


def create_custom_controls(input=None, controls=None, region_name=None):
    auditmanager_client = boto3.client('auditmanager', region_name=region_name)
    # Calls the list_controls() and returns a list of existing controls
    existing_controls = list_controls(region_name=region_name)
    control_sets = []
    # Iterating through the control sets in the JSON file
    for control_set in input:
        # Creating a dictionary of control sets and list of control IDs
        # to pass them into the assessment framework in the correct format
        control_sets_dict = {}
        control_ids = []
        control_sets_dict.setdefault("name", control_set)

        # Iterating through each control in the control set
        for control in input[control_set]:

            already_exists = False
            # Looping through all existing custom controls
            for existing in existing_controls["controlMetadataList"]:
                # Checking if the control being created already exists and
                # updating it if so
                if existing["name"] == input[control_set][control]["name"]:
                    already_exists = True
                    controlMappingSources = []

                    # Adding each data source to a
                    # list to feed into the control creation
                    for data_source in input[control_set][control]["controlMappingSources"]:
                        # Converting keywords to uppercase if not already
                        if "sourceKeyword" in data_source:
                            keyword = data_source["sourceKeyword"]["keywordValue"]
                            if keyword.isupper() is not True:
                                uppercase_keyword = keyword.upper()
                                data_source["sourceKeyword"]["keywordValue"] = uppercase_keyword
                        controlMappingSources.append(data_source)
                    # Create control
                    try:
                        response = auditmanager_client.update_control(
                            controlId=existing["id"],
                            name=input[control_set][control]["name"],
                            description=(
                                input[control_set][control]["description"]),
                            testingInformation=(
                                input[control_set]
                                [control]["testingInformation"]),
                            actionPlanTitle=(
                                input[control_set]
                                [control]["actionPlanTitle"]),
                            actionPlanInstructions=(
                                input[control_set]
                                [control]["actionPlanInstructions"]),
                            controlMappingSources=controlMappingSources
                        )
                    except botocore.exceptions.ClientError as error:
                        raise error

                    control_ids.append({"id": response["control"]["id"]})
                    break

            # Creating new control if it does not already exist
            if already_exists is False:
                controlMappingSources = []

                # Adding each data source to a
                # list to feed into the control creation
                for data_source in input[control_set][control]["controlMappingSources"]:
                    # Converting keywords to uppercase if not already
                    if "sourceKeyword" in data_source:
                        keyword = data_source["sourceKeyword"]["keywordValue"]
                        if keyword.isupper() is not True:
                            uppercase_keyword = keyword.upper()
                            data_source["sourceKeyword"]["keywordValue"] = uppercase_keyword
                    controlMappingSources.append(data_source)
                # Create control
                try:
                    response = auditmanager_client.create_control(
                        name=input[control_set][control]["name"],
                        description=input[control_set][control]["description"],
                        testingInformation=(
                            input[control_set][control]["testingInformation"]),
                        actionPlanTitle=(
                            input[control_set][control]["actionPlanTitle"]),
                        actionPlanInstructions=(
                            input[control_set]
                            [control]["actionPlanInstructions"]),
                        controlMappingSources=controlMappingSources
                    )
                except botocore.exceptions.ClientError as error:
                    raise error

                control_ids.append({"id": response["control"]["id"]})

        control_sets_dict["controls"] = control_ids
        control_sets.append(control_sets_dict)

    return control_sets