forked from marcin-kolda/gcp-iam-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_service.py
123 lines (90 loc) · 4.15 KB
/
cache_service.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
import os.path
import json
class JsonCacheService(object):
def __init__(self, service, use_cache):
self.service = service
self.use_cache = use_cache
def get(self, **kwargs):
cached_file = self._get_filename(**kwargs)
if self.use_cache:
if os.path.isfile(cached_file):
with open(cached_file) as data_file:
return json.load(data_file)
response = self._get_data(**kwargs)
if self.use_cache:
if not os.path.exists(os.path.dirname(cached_file)):
os.makedirs(os.path.dirname(cached_file))
with open(cached_file, 'w') as data_file:
json.dump(response, data_file)
return response
def _get_filename(self, **kwargs):
raise NotImplementedError()
def _get_data(self, **kwargs):
raise NotImplementedError()
class CRMProjects(JsonCacheService):
def _get_filename(self, **kwargs):
return "cache/projects/projects_{0}.json".format(
kwargs.get('nextPageToken', ''))
def _get_data(self, **kwargs):
return self.service.projects().list(
pageSize=400,
pageToken=kwargs.get('nextPageToken', None)).execute()
class CRMProjectIam(JsonCacheService):
def _get_filename(self, **kwargs):
return "cache/%s/iam.json" % (kwargs['project_id'])
def _get_data(self, **kwargs):
return self.service.projects().getIamPolicy(
resource=kwargs['project_id'], body={}).execute()
class ServiceAccountService(JsonCacheService):
def _get_filename(self, **kwargs):
return "cache/serviceAccounts/{0}_{1}.json" \
.format(kwargs['project_id'], kwargs.get('nextPageToken', ''))
def _get_data(self, **kwargs):
return self.service.projects().serviceAccounts().list(
name='projects/' + kwargs['project_id'],
pageToken=kwargs.get('nextPageToken', None)).execute()
class ServiceAccountKeyService(JsonCacheService):
def _get_filename(self, **kwargs):
return "cache/serviceAccountsKeys/{0}.json".format(kwargs['email'])
def _get_data(self, **kwargs):
return self.service.projects().serviceAccounts().keys().list(
name='projects/-/serviceAccounts/' + kwargs['email'],
keyTypes='USER_MANAGED').execute()
class GCSBuckets(JsonCacheService):
def _get_filename(self, **kwargs):
return "cache/{0}/buckets_{1}.json"\
.format(kwargs['project_id'], kwargs.get('pageToken', ''))
def _get_data(self, **kwargs):
return self.service.buckets().list(
project=kwargs['project_id'],
pageToken=kwargs.get('pageToken', '')).execute()
class GCSBucketACL(JsonCacheService):
def _get_filename(self, **kwargs):
return "cache/buckets/gcs_acl_{0}.json".format(kwargs['bucket_id'])
def _get_data(self, **kwargs):
return self.service.bucketAccessControls().list(
bucket=kwargs['bucket_id']).execute()
class BQDataset(JsonCacheService):
def _get_filename(self, **kwargs):
return "cache/{0}/datasets/{1}.json".format(
kwargs['project_id'], kwargs['dataset_id'])
def _get_data(self, **kwargs):
return self.service.datasets().get(projectId=kwargs['project_id'],
datasetId=kwargs[
'dataset_id']).execute()
class BQDatasets(JsonCacheService):
def _get_filename(self, **kwargs):
return "cache/{0}/datasets_{1}.json" \
.format(kwargs['project_id'], kwargs.get('pageToken', ''))
def _get_data(self, **kwargs):
return self.service.datasets().list(
projectId=kwargs['project_id'],
pageToken=kwargs.get('pageToken', '')).execute()
class ServiceManagement(JsonCacheService):
def _get_filename(self, **kwargs):
return "cache/{0}/services_{1}.json" \
.format(kwargs['project_id'], kwargs.get('pageToken', ''))
def _get_data(self, **kwargs):
return self.service.services().list(
consumerId='project:' + kwargs['project_id'],
pageToken=kwargs.get('pageToken', '')).execute()