#!bin/python3
import requests
import json
import pprint
from time import sleep
import logging

def image_get(api_url, access_token, prj_id):
    response = requests.get("https://{api_url}/api/v4/projects/{prj_id}/registry/repositories".format(api_url=api_url, prj_id=prj_id), headers = {'Private-Token': access_token})
    imagels_output = response.text
    return imagels_output 

def payload_dictbuidler(payloads):
    payload = {}
    for i in range(0, len(json.loads(payloads))):
        out = json.loads(payloads)[i]
        payload['Payload {}'.format(i)] = out
    return payload

def tags_deletor(api_url, access_token, prj_id, reg_id, name_regex_delete, keep_n, older_than, name_regex_keep):
    data = {'name_regex_delete': name_regex_delete,'keep_n': keep_n, 'older_than': older_than, 'name_regex_keep': name_regex_keep}
    response = requests.delete("https://{api_url}/api/v4/projects/{prj_id}/registry/repositories/{reg_id}/tags".format(api_url=api_url, prj_id=prj_id, reg_id=reg_id), data=data, headers = {'Private-Token': access_token})
    response_status = str(response)
    response_msg = json.loads(response.text)
    if response_status == "<Response [202]>":
        print("Successfully deleted")
        logging.info('Status code: 202 \t Deletion of Tags in the Container Registry was Successful')
    elif response_status == "<Response [400]>":
        logging.error('Status code: 400 \t ' + response_msg['message'])
    else:
        logging.critical('Unknown Error please troubleshoot and run the program again')

def main():
    with open('credentials.json') as json_file:
        data = json.load(json_file)
        api_url = data["API_URL"]
        access_token = data["Access-Token"]
        prj_id = data["Project ID"]
        reg_id = data["Registry ID"]
        name_regex_delete = data["name_regex_delete"]
        keep_n = data["keep_n"]
        older_than = data["older_than"]
        name_regex_keep = data["name_regex_keep"]
        dk_images = image_get(api_url, access_token, prj_id)
        images = payload_dictbuidler(dk_images)
        pp = pprint.PrettyPrinter(indent=4)
        pp.pprint(images)
        print("Procesing Image Tags......")
        tags_deletor(api_url, access_token, prj_id, reg_id, name_regex_delete, keep_n, older_than, name_regex_keep)


d9b183a24a135e56f16298fecea42260ce94712c
<mask token>


def tags_deletor(api_url, access_token, prj_id, reg_id, name_regex_delete,
    keep_n, older_than, name_regex_keep):
    data = {'name_regex_delete': name_regex_delete, 'keep_n': keep_n,
        'older_than': older_than, 'name_regex_keep': name_regex_keep}
    response = requests.delete(
        'https://{api_url}/api/v4/projects/{prj_id}/registry/repositories/{reg_id}/tags'
        .format(api_url=api_url, prj_id=prj_id, reg_id=reg_id), data=data,
        headers={'Private-Token': access_token})
    response_status = str(response)
    response_msg = json.loads(response.text)
    if response_status == '<Response [202]>':
        print('Successfully deleted')
        logging.info(
            'Status code: 202 \t Deletion of Tags in the Container Registry was Successful'
            )
    elif response_status == '<Response [400]>':
        logging.error('Status code: 400 \t ' + response_msg['message'])
    else:
        logging.critical(
            'Unknown Error please troubleshoot and run the program again')


def main():
    with open('credentials.json') as json_file:
        data = json.load(json_file)
        api_url = data['API_URL']
        access_token = data['Access-Token']
        prj_id = data['Project ID']
        reg_id = data['Registry ID']
        name_regex_delete = data['name_regex_delete']
        keep_n = data['keep_n']
        older_than = data['older_than']
        name_regex_keep = data['name_regex_keep']
        dk_images = image_get(api_url, access_token, prj_id)
        images = payload_dictbuidler(dk_images)
        pp = pprint.PrettyPrinter(indent=4)
        pp.pprint(images)
        print('Procesing Image Tags......')
        tags_deletor(api_url, access_token, prj_id, reg_id,
            name_regex_delete, keep_n, older_than, name_regex_keep)

<mask token>


def image_get(api_url, access_token, prj_id):
    response = requests.get(
        'https://{api_url}/api/v4/projects/{prj_id}/registry/repositories'.
        format(api_url=api_url, prj_id=prj_id), headers={'Private-Token':
        access_token})
    imagels_output = response.text
    return imagels_output


<mask token>


def tags_deletor(api_url, access_token, prj_id, reg_id, name_regex_delete,
    keep_n, older_than, name_regex_keep):
    data = {'name_regex_delete': name_regex_delete, 'keep_n': keep_n,
        'older_than': older_than, 'name_regex_keep': name_regex_keep}
    response = requests.delete(
        'https://{api_url}/api/v4/projects/{prj_id}/registry/repositories/{reg_id}/tags'
        .format(api_url=api_url, prj_id=prj_id, reg_id=reg_id), data=data,
        headers={'Private-Token': access_token})
    response_status = str(response)
    response_msg = json.loads(response.text)
    if response_status == '<Response [202]>':
        print('Successfully deleted')
        logging.info(
            'Status code: 202 \t Deletion of Tags in the Container Registry was Successful'
            )
    elif response_status == '<Response [400]>':
        logging.error('Status code: 400 \t ' + response_msg['message'])
    else:
        logging.critical(
            'Unknown Error please troubleshoot and run the program again')


def main():
    with open('credentials.json') as json_file:
        data = json.load(json_file)
        api_url = data['API_URL']
        access_token = data['Access-Token']
        prj_id = data['Project ID']
        reg_id = data['Registry ID']
        name_regex_delete = data['name_regex_delete']
        keep_n = data['keep_n']
        older_than = data['older_than']
        name_regex_keep = data['name_regex_keep']
        dk_images = image_get(api_url, access_token, prj_id)
        images = payload_dictbuidler(dk_images)
        pp = pprint.PrettyPrinter(indent=4)
        pp.pprint(images)
        print('Procesing Image Tags......')
        tags_deletor(api_url, access_token, prj_id, reg_id,
            name_regex_delete, keep_n, older_than, name_regex_keep)

<mask token>


def image_get(api_url, access_token, prj_id):
    response = requests.get(
        'https://{api_url}/api/v4/projects/{prj_id}/registry/repositories'.
        format(api_url=api_url, prj_id=prj_id), headers={'Private-Token':
        access_token})
    imagels_output = response.text
    return imagels_output


def payload_dictbuidler(payloads):
    payload = {}
    for i in range(0, len(json.loads(payloads))):
        out = json.loads(payloads)[i]
        payload['Payload {}'.format(i)] = out
    return payload


def tags_deletor(api_url, access_token, prj_id, reg_id, name_regex_delete,
    keep_n, older_than, name_regex_keep):
    data = {'name_regex_delete': name_regex_delete, 'keep_n': keep_n,
        'older_than': older_than, 'name_regex_keep': name_regex_keep}
    response = requests.delete(
        'https://{api_url}/api/v4/projects/{prj_id}/registry/repositories/{reg_id}/tags'
        .format(api_url=api_url, prj_id=prj_id, reg_id=reg_id), data=data,
        headers={'Private-Token': access_token})
    response_status = str(response)
    response_msg = json.loads(response.text)
    if response_status == '<Response [202]>':
        print('Successfully deleted')
        logging.info(
            'Status code: 202 \t Deletion of Tags in the Container Registry was Successful'
            )
    elif response_status == '<Response [400]>':
        logging.error('Status code: 400 \t ' + response_msg['message'])
    else:
        logging.critical(
            'Unknown Error please troubleshoot and run the program again')


def main():
    with open('credentials.json') as json_file:
        data = json.load(json_file)
        api_url = data['API_URL']
        access_token = data['Access-Token']
        prj_id = data['Project ID']
        reg_id = data['Registry ID']
        name_regex_delete = data['name_regex_delete']
        keep_n = data['keep_n']
        older_than = data['older_than']
        name_regex_keep = data['name_regex_keep']
        dk_images = image_get(api_url, access_token, prj_id)
        images = payload_dictbuidler(dk_images)
        pp = pprint.PrettyPrinter(indent=4)
        pp.pprint(images)
        print('Procesing Image Tags......')
        tags_deletor(api_url, access_token, prj_id, reg_id,
            name_regex_delete, keep_n, older_than, name_regex_keep)

import requests
import json
import pprint
from time import sleep
import logging


def image_get(api_url, access_token, prj_id):
    response = requests.get(
        'https://{api_url}/api/v4/projects/{prj_id}/registry/repositories'.
        format(api_url=api_url, prj_id=prj_id), headers={'Private-Token':
        access_token})
    imagels_output = response.text
    return imagels_output


def payload_dictbuidler(payloads):
    payload = {}
    for i in range(0, len(json.loads(payloads))):
        out = json.loads(payloads)[i]
        payload['Payload {}'.format(i)] = out
    return payload


def tags_deletor(api_url, access_token, prj_id, reg_id, name_regex_delete,
    keep_n, older_than, name_regex_keep):
    data = {'name_regex_delete': name_regex_delete, 'keep_n': keep_n,
        'older_than': older_than, 'name_regex_keep': name_regex_keep}
    response = requests.delete(
        'https://{api_url}/api/v4/projects/{prj_id}/registry/repositories/{reg_id}/tags'
        .format(api_url=api_url, prj_id=prj_id, reg_id=reg_id), data=data,
        headers={'Private-Token': access_token})
    response_status = str(response)
    response_msg = json.loads(response.text)
    if response_status == '<Response [202]>':
        print('Successfully deleted')
        logging.info(
            'Status code: 202 \t Deletion of Tags in the Container Registry was Successful'
            )
    elif response_status == '<Response [400]>':
        logging.error('Status code: 400 \t ' + response_msg['message'])
    else:
        logging.critical(
            'Unknown Error please troubleshoot and run the program again')


def main():
    with open('credentials.json') as json_file:
        data = json.load(json_file)
        api_url = data['API_URL']
        access_token = data['Access-Token']
        prj_id = data['Project ID']
        reg_id = data['Registry ID']
        name_regex_delete = data['name_regex_delete']
        keep_n = data['keep_n']
        older_than = data['older_than']
        name_regex_keep = data['name_regex_keep']
        dk_images = image_get(api_url, access_token, prj_id)
        images = payload_dictbuidler(dk_images)
        pp = pprint.PrettyPrinter(indent=4)
        pp.pprint(images)
        print('Procesing Image Tags......')
        tags_deletor(api_url, access_token, prj_id, reg_id,
            name_regex_delete, keep_n, older_than, name_regex_keep)

#!bin/python3
import requests
import json
import pprint
from time import sleep
import logging

def image_get(api_url, access_token, prj_id):
    response = requests.get("https://{api_url}/api/v4/projects/{prj_id}/registry/repositories".format(api_url=api_url, prj_id=prj_id), headers = {'Private-Token': access_token})
    imagels_output = response.text
    return imagels_output 

def payload_dictbuidler(payloads):
    payload = {}
    for i in range(0, len(json.loads(payloads))):
        out = json.loads(payloads)[i]
        payload['Payload {}'.format(i)] = out
    return payload

def tags_deletor(api_url, access_token, prj_id, reg_id, name_regex_delete, keep_n, older_than, name_regex_keep):
    data = {'name_regex_delete': name_regex_delete,'keep_n': keep_n, 'older_than': older_than, 'name_regex_keep': name_regex_keep}
    response = requests.delete("https://{api_url}/api/v4/projects/{prj_id}/registry/repositories/{reg_id}/tags".format(api_url=api_url, prj_id=prj_id, reg_id=reg_id), data=data, headers = {'Private-Token': access_token})
    response_status = str(response)
    response_msg = json.loads(response.text)
    if response_status == "<Response [202]>":
        print("Successfully deleted")
        logging.info('Status code: 202 \t Deletion of Tags in the Container Registry was Successful')
    elif response_status == "<Response [400]>":
        logging.error('Status code: 400 \t ' + response_msg['message'])
    else:
        logging.critical('Unknown Error please troubleshoot and run the program again')

def main():
    with open('credentials.json') as json_file:
        data = json.load(json_file)
        api_url = data["API_URL"]
        access_token = data["Access-Token"]
        prj_id = data["Project ID"]
        reg_id = data["Registry ID"]
        name_regex_delete = data["name_regex_delete"]
        keep_n = data["keep_n"]
        older_than = data["older_than"]
        name_regex_keep = data["name_regex_keep"]
        dk_images = image_get(api_url, access_token, prj_id)
        images = payload_dictbuidler(dk_images)
        pp = pprint.PrettyPrinter(indent=4)
        pp.pprint(images)
        print("Procesing Image Tags......")
        tags_deletor(api_url, access_token, prj_id, reg_id, name_regex_delete, keep_n, older_than, name_regex_keep)

