Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/c7n/lookup.py: 61%
31 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
« 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
4from c7n.utils import jmespath_search
6import copy
9class Lookup:
10 RESOURCE_SOURCE = 'resource'
12 schema = {
13 'type': 'object',
14 'oneOf': [
15 {
16 'properties': {
17 'type': {'type': 'string', 'enum': [RESOURCE_SOURCE]},
18 'key': {'type': 'string'}
19 },
20 'additionalProperties': False,
21 'required': ['type', 'key']
22 }
23 ]
24 }
26 @staticmethod
27 def lookup_type(schema):
28 lookup_schema = copy.deepcopy(Lookup.schema)
29 for lookup in lookup_schema['oneOf']:
30 lookup['properties']['default-value'] = schema
32 return {
33 'oneOf': [
34 lookup_schema,
35 schema
36 ]
37 }
39 @staticmethod
40 def extract(source, data=None):
41 if Lookup.is_lookup(source):
42 return Lookup.get_value(source, data)
43 else:
44 return source
46 @staticmethod
47 def is_lookup(source):
48 return isinstance(source, dict)
50 @staticmethod
51 def get_value(source, data=None):
52 if source['type'] == Lookup.RESOURCE_SOURCE:
53 return Lookup.get_value_from_resource(source, data)
55 @staticmethod
56 def get_value_from_resource(source, resource):
57 value = jmespath_search(source['key'], resource)
59 if value is not None:
60 return value
61 if 'default-value' not in source:
62 raise Exception('Lookup for key, {}, returned None'.format(source['key']))
63 else:
64 return source['default-value']