1# Copyright The Cloud Custodian Authors.
2# SPDX-License-Identifier: Apache-2.0
3
4from c7n.utils import type_schema
5
6from c7n_gcp.actions import MethodAction
7from c7n_gcp.filters import IamPolicyFilter
8from c7n_gcp.provider import resources
9from c7n_gcp.query import QueryResourceManager, TypeInfo
10
11
12@resources.register('function')
13class Function(QueryResourceManager):
14
15 class resource_type(TypeInfo):
16 service = 'cloudfunctions'
17 version = 'v1'
18 component = 'projects.locations.functions'
19 enum_spec = ('list', 'functions[]', None)
20 scope = 'project'
21 scope_key = 'parent'
22 scope_template = "projects/{}/locations/-"
23 name = id = "name"
24 metric_key = "resource.labels.function_name"
25 default_report_fields = [
26 'name', 'runtime', 'eventTrigger.eventType', 'status', 'updateTime']
27
28 events = {
29 'create': 'google.cloud.functions.v1.CloudFunctionsService.CreateFunction',
30 'delete': 'google.cloud.functions.v1.CloudFunctionsService.DeleteFunction',
31 'update': 'google.cloud.functions.v1.CloudFunctionsService.UpdateFunction'}
32 urn_component = "function"
33 asset_type = "cloudfunctions.googleapis.com/CloudFunction"
34
35 @staticmethod
36 def get(client, resource_info):
37 return client.execute_command(
38 'get', {'name': (
39 'projects/{project_id}/locations/'
40 '{location_id}/functions/{function_name}').format(
41 **resource_info)})
42
43 @classmethod
44 def _get_location(cls, resource):
45 "The region is the fourth segment of the name."
46 return resource["name"].split('/')[3]
47
48 @classmethod
49 def _get_urn_id(cls, resource):
50 "The id is the last segment of the name ."
51 return resource["name"].split('/', 6)[-1]
52
53
54@Function.filter_registry.register('iam-policy')
55class FunctionIamPolicyFilter(IamPolicyFilter):
56 """
57 Overrides the base implementation to process function resources correctly.
58 """
59 permissions = ('cloudfunctions.functions.getIamPolicy',)
60
61
62@Function.action_registry.register('delete')
63class Delete(MethodAction):
64
65 schema = type_schema('delete')
66 method_spec = {'op': 'delete'}
67
68 def get_resource_params(self, model, resource):
69 return {'name': resource['name']}