Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/c7n_gcp/resources/bigquery.py: 72%

96 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-08 06:51 +0000

1# Copyright The Cloud Custodian Authors. 

2# SPDX-License-Identifier: Apache-2.0 

3from c7n.utils import type_schema, jmespath_search 

4from c7n_gcp.query import QueryResourceManager, TypeInfo, ChildTypeInfo, ChildResourceManager 

5from c7n_gcp.provider import resources 

6from c7n_gcp.actions import MethodAction 

7 

8 

9@resources.register('bq-dataset') 

10class DataSet(QueryResourceManager): 

11 """GCP resource: https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets 

12 """ 

13 class resource_type(TypeInfo): 

14 service = 'bigquery' 

15 version = 'v2' 

16 component = 'datasets' 

17 enum_spec = ('list', 'datasets[]', None) 

18 scope = 'project' 

19 scope_key = 'projectId' 

20 get_requires_event = True 

21 id = "id" 

22 name = "friendlyName" 

23 default_report_fields = [ 

24 id, name, "description", 

25 "creationTime", "lastModifiedTime"] 

26 asset_type = "bigquery.googleapis.com/Dataset" 

27 scc_type = "google.cloud.bigquery.Dataset" 

28 metric_key = "resource.labels.dataset_id" 

29 permissions = ('bigquery.datasets.get',) 

30 urn_component = "dataset" 

31 urn_id_path = "datasetReference.datasetId" 

32 

33 @staticmethod 

34 def get(client, event): 

35 # dataset creation doesn't include data set name in resource name. 

36 if 'protoPayload' in event: 

37 _, method = event['protoPayload']['methodName'].split('.') 

38 if method not in ('insert', 'update'): 

39 raise RuntimeError("unknown event %s" % event) 

40 expr = 'protoPayload.serviceData.dataset{}Response.resource.datasetName'.format( 

41 method.capitalize()) 

42 ref = jmespath_search(expr, event) 

43 else: 

44 ref = event 

45 return client.execute_query('get', verb_arguments=ref) 

46 

47 def augment(self, resources): 

48 client = self.get_client() 

49 results = [] 

50 for r in resources: 

51 ref = r['datasetReference'] 

52 results.append( 

53 client.execute_query( 

54 'get', verb_arguments=ref)) 

55 return results 

56 

57 

58@resources.register('bq-job') 

59class BigQueryJob(QueryResourceManager): 

60 """GCP resource: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs 

61 """ 

62 # its unclear why this is needed 

63 class resource_type(TypeInfo): 

64 service = 'bigquery' 

65 version = 'v2' 

66 component = 'jobs' 

67 enum_spec = ('list', 'jobs[]', {'allUsers': True, 'projection': 'full'}) 

68 get_requires_event = True 

69 scope = 'project' 

70 scope_key = 'projectId' 

71 name = id = 'id' 

72 default_report_fields = ["id", "user_email", "status.state"] 

73 urn_component = "job" 

74 

75 @staticmethod 

76 def get(client, event): 

77 return client.execute_query('get', { 

78 'projectId': jmespath_search('resource.labels.project_id', event), 

79 'jobId': jmespath_search( 

80 'protoPayload.metadata.tableCreation.jobName', event 

81 ).rsplit('/', 1)[-1] 

82 }) 

83 

84 @classmethod 

85 def _get_urn_id(cls, resource): 

86 jobRef = resource['jobReference'] 

87 return f"{jobRef['location']}/{jobRef['jobId']}" 

88 

89 

90@resources.register('bq-table') 

91class BigQueryTable(ChildResourceManager): 

92 """GCP resource: https://cloud.google.com/bigquery/docs/reference/rest/v2/tables 

93 """ 

94 

95 class resource_type(ChildTypeInfo): 

96 service = 'bigquery' 

97 version = 'v2' 

98 component = 'tables' 

99 enum_spec = ('list', 'tables[]', None) 

100 scope_key = 'projectId' 

101 id = 'id' 

102 name = "friendlyName" 

103 default_report_fields = [ 

104 id, name, "description", "creationTime", "lastModifiedTime", "numRows", "numBytes"] 

105 parent_spec = { 

106 'resource': 'bq-dataset', 

107 'child_enum_params': [ 

108 ('datasetReference.datasetId', 'datasetId'), 

109 ], 

110 'parent_get_params': [ 

111 ('tableReference.projectId', 'projectId'), 

112 ('tableReference.datasetId', 'datasetId'), 

113 ] 

114 } 

115 asset_type = "bigquery.googleapis.com/Table" 

116 urn_component = "table" 

117 urn_id_path = "tableReference.tableId" 

118 

119 @classmethod 

120 def _get_urn_id(cls, resource): 

121 tableRef = resource['tableReference'] 

122 return f"{tableRef['datasetId']}/{tableRef['tableId']}" 

123 

124 @staticmethod 

125 def get(client, event): 

126 return client.execute_query('get', { 

127 'projectId': event['project_id'], 

128 'datasetId': event['dataset_id'], 

129 'tableId': event['resourceName'].rsplit('/', 1)[-1] 

130 }) 

131 

132 def augment(self, resources): 

133 client = self.get_client() 

134 results = [] 

135 for r in resources: 

136 ref = r['tableReference'] 

137 results.append( 

138 client.execute_query( 

139 'get', verb_arguments=ref)) 

140 return results 

141 

142 

143@BigQueryTable.action_registry.register('delete') 

144class DeleteBQTable(MethodAction): 

145 schema = type_schema('delete') 

146 method_spec = {'op': 'delete'} 

147 permissions = ('bigquery.tables.get', 'bigquery.tables.delete') 

148 

149 def get_resource_params(self, model, r): 

150 return { 

151 'projectId': r['tableReference']['projectId'], 

152 'datasetId': r['tableReference']['datasetId'], 

153 'tableId': r['tableReference']['tableId'] 

154 }