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

30 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 

3 

4from c7n_gcp.actions import MethodAction 

5from c7n_gcp.provider import resources 

6from c7n_gcp.query import QueryResourceManager, TypeInfo 

7from c7n.utils import type_schema 

8 

9 

10@resources.register('service') 

11class Service(QueryResourceManager): 

12 """GCP Service Usage Management 

13 

14 https://cloud.google.com/service-usage/docs/reference/rest 

15 https://cloud.google.com/service-infrastructure/docs/service-management/reference/rest/v1/services 

16 """ 

17 class resource_type(TypeInfo): 

18 service = 'serviceusage' 

19 version = 'v1' 

20 component = 'services' 

21 enum_spec = ('list', 'services[]', None) 

22 scope = 'project' 

23 scope_key = 'parent' 

24 scope_template = 'projects/{}' 

25 name = id = 'name' 

26 default_report_fields = [name, "state"] 

27 asset_type = 'serviceusage.googleapis.com/Service' 

28 urn_component = "service" 

29 urn_id_segments = (-1,) # Just use the last segment of the id in the URN 

30 

31 @staticmethod 

32 def get(client, resource_info): 

33 return client.execute_command('get', {'name': resource_info['resourceName']}) 

34 

35 def get_resource_query(self): 

36 # https://cloud.google.com/service-usage/docs/reference/rest/v1/services/list 

37 # default to just listing enabled services, if we add in support for enabling 

38 # services we would want to add some user specified query filtering capability 

39 # here, ala 

40 # use:: 

41 # query: 

42 # - filter: "state:DISABLED" 

43 return {'filter': 'state:ENABLED'} 

44 

45 

46@Service.action_registry.register('disable') 

47class Disable(MethodAction): 

48 """Disable a service for the current project 

49 

50 Example:: 

51 

52 policies: 

53 - name: disable-disallowed-services 

54 resource: gcp.service 

55 mode: 

56 type: gcp-audit 

57 methods: 

58 - google.api.servicemanagement.v1.ServiceManagerV1.ActivateServices 

59 filters: 

60 - config.name: translate.googleapis.com 

61 actions: 

62 - disable 

63 """ 

64 

65 schema = type_schema( 

66 'disable', 

67 dependents={'type': 'boolean', 'default': False}, 

68 usage={'enum': ['SKIP', 'CHECK']}) 

69 

70 method_spec = {'op': 'disable'} 

71 

72 def get_resource_params(self, model, resource): 

73 return {'name': resource['name'], 

74 'body': { 

75 'disableDependentServices': self.data.get('dependents', False), 

76 'checkIfServiceHasUsage': self.data.get( 

77 'usage', 'CHECK_IF_SERVICE_HAS_USAGE_UNSPECIFIED')}}