Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/cloudsearch.py
1566 views
1
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
14
import logging
15
16
from awscli.customizations.flatten import FlattenArguments, SEP
17
from botocore.compat import OrderedDict
18
19
LOG = logging.getLogger(__name__)
20
21
DEFAULT_VALUE_TYPE_MAP = {
22
'Int': int,
23
'Double': float,
24
'IntArray': int,
25
'DoubleArray': float
26
}
27
28
29
def index_hydrate(params, container, cli_type, key, value):
30
"""
31
Hydrate an index-field option value to construct something like::
32
33
{
34
'index_field': {
35
'DoubleOptions': {
36
'DefaultValue': 0.0
37
}
38
}
39
}
40
"""
41
if 'IndexField' not in params:
42
params['IndexField'] = {}
43
44
if 'IndexFieldType' not in params['IndexField']:
45
raise RuntimeError('You must pass the --type option.')
46
47
# Find the type and transform it for the type options field name
48
# E.g: int-array => IntArray
49
_type = params['IndexField']['IndexFieldType']
50
_type = ''.join([i.capitalize() for i in _type.split('-')])
51
52
# ``index_field`` of type ``latlon`` is mapped to ``Latlon``.
53
# However, it is defined as ``LatLon`` in the model so it needs to
54
# be changed.
55
if _type == 'Latlon':
56
_type = 'LatLon'
57
58
# Transform string value to the correct type?
59
if key.split(SEP)[-1] == 'DefaultValue':
60
value = DEFAULT_VALUE_TYPE_MAP.get(_type, lambda x: x)(value)
61
62
# Set the proper options field
63
if _type + 'Options' not in params['IndexField']:
64
params['IndexField'][_type + 'Options'] = {}
65
66
params['IndexField'][_type + 'Options'][key.split(SEP)[-1]] = value
67
68
69
FLATTEN_CONFIG = {
70
"define-expression": {
71
"expression": {
72
"keep": False,
73
"flatten": OrderedDict([
74
# Order is crucial here! We're
75
# flattening ExpressionValue to be "expression",
76
# but this is the name ("expression") of the our parent
77
# key, the top level nested param.
78
("ExpressionName", {"name": "name"}),
79
("ExpressionValue", {"name": "expression"}),]),
80
}
81
},
82
"define-index-field": {
83
"index-field": {
84
"keep": False,
85
# We use an ordered dict because `type` needs to be parsed before
86
# any of the <X>Options values.
87
"flatten": OrderedDict([
88
("IndexFieldName", {"name": "name"}),
89
("IndexFieldType", {"name": "type"}),
90
("IntOptions.DefaultValue", {"name": "default-value",
91
"type": "string",
92
"hydrate": index_hydrate}),
93
("IntOptions.FacetEnabled", {"name": "facet-enabled",
94
"hydrate": index_hydrate }),
95
("IntOptions.SearchEnabled", {"name": "search-enabled",
96
"hydrate": index_hydrate}),
97
("IntOptions.ReturnEnabled", {"name": "return-enabled",
98
"hydrate": index_hydrate}),
99
("IntOptions.SortEnabled", {"name": "sort-enabled",
100
"hydrate": index_hydrate}),
101
("IntOptions.SourceField", {"name": "source-field",
102
"type": "string",
103
"hydrate": index_hydrate }),
104
("TextOptions.HighlightEnabled", {"name": "highlight-enabled",
105
"hydrate": index_hydrate}),
106
("TextOptions.AnalysisScheme", {"name": "analysis-scheme",
107
"hydrate": index_hydrate})
108
])
109
}
110
}
111
}
112
113
114
def initialize(cli):
115
"""
116
The entry point for CloudSearch customizations.
117
"""
118
flattened = FlattenArguments('cloudsearch', FLATTEN_CONFIG)
119
flattened.register(cli)
120
121