import json


def update_placeholder(resource_structure, iteration):
    """Update/replace the placeholder in the resource.

    Parameters
    ----------
    resource_structure : type
        CloudFormation resource definition.
    iteration : integer
        Number of iteration.

    Returns
    -------
    type
        Modified CloudFormation resource

    """
    resource_string = json.dumps(resource_structure)
    place_holder_count = resource_string.count('%s')

    # If the placeholder is found then replace it
    if place_holder_count > 0:

        print("Found {} occurrences of string placeholder in JSON, replacing with iterator value {}".format(place_holder_count, iteration + 1))

        # Generate a random string for replacement
        #replacement_values = ''.join(choice(ascii_lowercase) for i in range(10))
        # For now... replacing with the iteration count
        replacement_values = str(iteration)

        # Replace the placeholders
        resource_string = resource_string % (replacement_values)

        # Convert the string back to json and return it
        return json.loads(resource_string)

    else:

        print("No occurences of decimal placeholder found in JSON, therefore nothing will be replaced")
        return resource_structure