-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3bucketlist.py
152 lines (128 loc) · 5.18 KB
/
s3bucketlist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
import boto3
import botocore
import datetime
import sys
class bucket4terraform:
def __init__(self, name, profile="default", tagKey="Managed_by", tagValue="terraform", extraDetails=False):
self.profile = profile
try:
bucket_head = self._getSession('c').head_bucket(Bucket=name)
except botocore.exceptions.ClientError as error:
print (error)
sys.exit(3)
except botocore.exceptions.NoCredentialsError as error:
print (error)
sys.exit(33)
self.extraDetails = extraDetails
self.name = name
self.tagKey = tagKey
self.tagValue = tagValue
self.timeCreation = self._getSession('r').Bucket(name).creation_date
self._setFlags()
if extraDetails:
addDetails()
else:
self.objectsNumber = "Unknown"
self.sizetotal = "Unknown"
self.timeLastUpdate = "Unknown"
def addDetails(self):
objects = 0
size = 0
update = self.timeCreation
session = self._getSession("c")
paginator = session.get_paginator("list_objects_v2")
response = paginator.paginate(Bucket=self.name)
for page in response:
files = page.get("Contents")
if files:
for file in files:
objects += 1
size += file["Size"]
if file["LastModified"] > update:
update = file["LastModified"]
else:
objects = 0
size = 0
update = self.timeCreation
self.objectsNumber = objects
self.sizeTotal = size
self.timeLastUpdate = update
self.extraDetails = True
def _getSession(self, kind):
try:
session = boto3.session.Session(profile_name=self.profile, region_name='us-east-1')
except botocore.exceptions.ProfileNotFound as Error:
print (f"Profile '{self.profile}' not found in the config")
sys.exit(2)
if kind == 'c':
return session.client(service_name="s3")
elif kind == 'r':
return session.resource("s3")
def _setFlags(self):
self.isAccessible = True
self.isEmpty = False
self.isTagged = True
self.isTerraformed = False
session = self._getSession("r")
bucket = session.Bucket(self.name)
try:
tagSet = bucket.Tagging().tag_set
except botocore.exceptions.ClientError as error:
if error.response['Error']['Code'] == "NoSuchTagSet":
self.isTagged = False
elif error.response['Error']['Code'] == "AccessDenied":
self.isAccessible = False
else:
print (error)
print ("=" *80)
print ("Not handled error accessing tags for bucket %s" % self.name)
sys.exit(5)
else:
for tags in tagSet:
if tags == {"Key": self.tagKey, "Value": self.tagValue}:
self.isTerraformed = True
break
if sum([1 for _ in bucket.objects.limit(1)]) == 0:
self.isEmpty = True
class bucketlists4terraform:
def __init__(self, profile="default", tagKey="Managed_by", tagValue="terraform"):
self.profile = profile
self.tagKey = tagKey
self.tagValue = tagValue
session = self._getSession("c")
self.lists = [
"isAccessible",
"isEmpty",
"isTagged",
"isTerraformed",
]
self.all = [b['Name'] for b in session.list_buckets()["Buckets"]]
tempLists = {}
for l in self.lists:
tempLists[l] = []
for b in self.all:
bucket = bucket4terraform(b, profile=self.profile, tagKey=self.tagKey, tagValue=self.tagValue)
for l in self.lists:
if getattr(bucket,l):
tempLists[l].append(bucket.name)
self.empty = tempLists['isEmpty']
self.notAccessible = [b for b in self.all if b not in tempLists['isAccessible']]
self.tagged = [b for b in self.all if b in tempLists['isTagged']]
self.taggedEmpty = [b for b in self.tagged if b in self.empty]
self.notTagged = [b for b in self.all if b not in tempLists['isTagged']]
self.notTaggedEmpty = [b for b in self.notTagged if b in self.empty]
self.terraformed = [b for b in self.tagged if b in tempLists['isTerraformed']]
self.terraformEmpty = [b for b in self.terraformed if b in self.empty]
self.notTerraformed = [b for b in self.tagged if b not in tempLists['isTerraformed']]
self.notTerraformed.extend(self.notTagged)
def _getSession(self, kind):
try:
session = boto3.session.Session(profile_name=self.profile, region_name='us-east-1')
except botocore.exceptions.ProfileNotFound as Error:
print (f"Profile '{self.profile}' not found in the config")
sys.exit(2)
if kind == 'c':
return session.client(service_name="s3")
elif kind == 'r':
return session.resource("s3")