1
+ import os
2
+ import sys
3
+ import argparse
4
+ import logging
5
+ from dotenv import load_dotenv
6
+ from azure .identity import DefaultAzureCredential , EnvironmentCredential
7
+ from azure .containerregistry import ContainerRegistryClient
8
+ from date_time import date_time
9
+ from azure_resource_graph_query import validate_input_acr_name
10
+ from azure .core .exceptions import HttpResponseError , ClientAuthenticationError
11
+
12
+
13
+ def return_acr_endpoint (acr_name :str ):
14
+ """
15
+ Function returns acr url from its name
16
+ :param acr_name:
17
+ :return:
18
+ """
19
+ acr_endpoint = f'https://{ acr_name .lower ()} .azurecr.io'
20
+ return acr_endpoint
21
+
22
+
23
+ def list_image_tag_details (acr_name : str ):
24
+ """
25
+ list the images and total tag counts per images
26
+ :param acr_name:
27
+ :return:
28
+ """
29
+ credential = EnvironmentCredential ()
30
+
31
+ repositories = get_image_names_from_acr (acr_name = acr_name )
32
+ acr_url = return_acr_endpoint (acr_name = acr_name )
33
+ print (f'Image details will be fetched from : { acr_url } ' )
34
+ # validate acr input
35
+ valid = validate_input_acr_name (acr_name = acr_name )
36
+
37
+ # Image - tag count
38
+ image_tag_count = {}
39
+ client = ContainerRegistryClient (endpoint = acr_url , credential = credential )
40
+ for repository in repositories :
41
+ tags_list = []
42
+ # print(f'image name is: {acr_url}/{repository}')
43
+ # print(f'available tags are:')
44
+ for tag in client .list_tag_properties (repository = repository ):
45
+ tag_details = {}
46
+ # print(f'Image name : {repository}')
47
+ # print(f'Tag name : {tag.name}')
48
+ tag_details ['tag_name' ] = tag .name
49
+ created_on = tag .created_on
50
+ fmt_date = date_time (created_on )
51
+ tag_details ['tag_created_on' ] = fmt_date
52
+ # print(f'Tag created on {fmt_date}')
53
+ tag_sha = tag .digest
54
+ tag_details ['tag_sha' ] = tag_sha
55
+ tags_list .append (tag_details )
56
+ # print(f'{"*" * 50}')
57
+ image_tag_count [repository ] = tags_list
58
+
59
+ return image_tag_count # dictionary with key as image name and value as available tag and details
60
+
61
+
62
+ def get_image_names_from_acr (acr_name : str ):
63
+ """
64
+ this will be used to pull image details from acr
65
+ :param acr_name:
66
+ :return:
67
+ """
68
+ try :
69
+ acr_url = return_acr_endpoint (acr_name = acr_name )
70
+
71
+ # define credentials
72
+ credential = EnvironmentCredential ()
73
+ client = ContainerRegistryClient (endpoint = acr_url , credential = credential )
74
+
75
+ repository_list = []
76
+ # List all images in acr
77
+ repository_names = client .list_repository_names ()
78
+ # print(repository_names)
79
+ for repository in repository_names :
80
+ # print(repository)
81
+ repository_list .append (repository )
82
+
83
+ return repository_list
84
+
85
+ except ClientAuthenticationError :
86
+ logging .error ("Authentication failed. Please check your credentials." )
87
+ return {"error" : "Authentication failed. Please check your credentials." }
88
+
89
+ except HttpResponseError as e :
90
+ logging .error (f"HTTP error occurred: { e .response .status_code } { e .response .reason } " )
91
+ return {"error" : f"HTTP error occurred: { e .response .status_code } { e .response .reason } " }
92
+
93
+ except Exception as e :
94
+ logging .error (f"An unexpected error occurred: { str (e )} " )
95
+ return {"error" : f"An unexpected error occurred: { str (e )} " }
96
+
97
+
98
+ def main ():
99
+ """
100
+ to run main function
101
+ :return:
102
+ """
103
+ parser = argparse .ArgumentParser ("To fetch image details from Azure container registry" )
104
+ parser .add_argument ("--acr_name" , help = "Azure container registry name" , type = str , required = True )
105
+
106
+ args = parser .parse_args ()
107
+
108
+ acr_name = args .acr_name
109
+ load_dotenv ()
110
+ # return_acr_endpoint(acr_name=acr_name)
111
+ # get_image_names_from_acr(acr_name=acr_name)
112
+ image_tag_count = list_image_tag_details (acr_name = acr_name )
113
+ # print(image_tag_count)
114
+
115
+ if __name__ == "__main__" :
116
+ main ()
0 commit comments