forked from GoogleCloudPlatform/professional-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcai.py
73 lines (63 loc) · 2.73 KB
/
cai.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
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from .base import Processor, NotConfiguredException
from googleapiclient import discovery
class CaiProcessor(Processor):
def process(self, config_key=None):
if config_key is None:
config_key = 'cai'
if config_key not in self.config:
raise NotConfiguredException('No settings configured!')
cai_config = self.config[config_key]
if 'parent' not in cai_config:
raise NotConfiguredException('No parent configured!')
cai_service = discovery.build('cloudasset',
'v1',
http=self._get_branded_http())
request_parameters = {}
request_parameters['parent'] = self._jinja_expand_string(
cai_config['parent'])
for k in ['readTime', 'pageSize', 'contentType']:
if k in cai_config:
request_parameters[k] = self._jinja_expand_string(cai_config[k])
if 'assetTypes' in cai_config:
request_parameters['assetTypes'] = self._jinja_var_to_list(
cai_config['assetTypes'])
indexing = 'asset_type'
if 'indexing' in cai_config:
if cai_config['indexing'] == 'list':
indexing = 'list'
if indexing == 'asset_type':
assets = {}
else:
assets = []
page_token = None
while True:
if page_token is not None:
request_parameters['pageToken'] = page_token
request = cai_service.assets().list(**request_parameters)
response = request.execute()
if 'assets' in response:
for asset in response['assets']:
if indexing == 'asset_type':
if asset['assetType'] not in assets:
assets[asset['assetType']] = []
assets[asset['assetType']].append(asset)
elif indexing == 'list':
assets.append(asset)
if 'nextPageToken' in response:
page_token = response['nextPageToken']
else:
break
return {'assets': assets}