Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/emr/config.py
1567 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
from awscli.customizations.emr import configutils
16
from awscli.customizations.emr import exceptions
17
18
LOG = logging.getLogger(__name__)
19
20
SUPPORTED_CONFIG_LIST = [
21
{'name': 'service_role'},
22
{'name': 'log_uri'},
23
{'name': 'instance_profile', 'arg_name': 'ec2_attributes',
24
'arg_value_key': 'InstanceProfile'},
25
{'name': 'key_name', 'arg_name': 'ec2_attributes',
26
'arg_value_key': 'KeyName'},
27
{'name': 'enable_debugging', 'type': 'boolean'},
28
{'name': 'key_pair_file'}
29
]
30
31
TYPES = ['string', 'boolean']
32
33
34
def get_applicable_configurations(command):
35
supported_configurations = _create_supported_configurations()
36
return [x for x in supported_configurations if x.is_applicable(command)]
37
38
39
def _create_supported_configuration(config):
40
config_type = config['type'] if 'type' in config else 'string'
41
42
if (config_type == 'string'):
43
config_arg_name = config['arg_name'] \
44
if 'arg_name' in config else config['name']
45
config_arg_value_key = config['arg_value_key'] \
46
if 'arg_value_key' in config else None
47
configuration = StringConfiguration(config['name'],
48
config_arg_name,
49
config_arg_value_key)
50
elif (config_type == 'boolean'):
51
configuration = BooleanConfiguration(config['name'])
52
53
return configuration
54
55
56
def _create_supported_configurations():
57
return [_create_supported_configuration(config)
58
for config in SUPPORTED_CONFIG_LIST]
59
60
61
class Configuration(object):
62
63
def __init__(self, name, arg_name):
64
self.name = name
65
self.arg_name = arg_name
66
67
def is_applicable(self, command):
68
raise NotImplementedError("is_applicable")
69
70
def is_present(self, parsed_args):
71
raise NotImplementedError("is_present")
72
73
def add(self, command, parsed_args, value):
74
raise NotImplementedError("add")
75
76
def _check_arg(self, parsed_args, arg_name):
77
return getattr(parsed_args, arg_name, None)
78
79
80
class StringConfiguration(Configuration):
81
82
def __init__(self, name, arg_name, arg_value_key=None):
83
super(StringConfiguration, self).__init__(name, arg_name)
84
self.arg_value_key = arg_value_key
85
86
def is_applicable(self, command):
87
return command.supports_arg(self.arg_name.replace('_', '-'))
88
89
def is_present(self, parsed_args):
90
if (not self.arg_value_key):
91
return self._check_arg(parsed_args, self.arg_name)
92
else:
93
return self._check_arg(parsed_args, self.arg_name) \
94
and self.arg_value_key in getattr(parsed_args, self.arg_name)
95
96
def add(self, command, parsed_args, value):
97
if (not self.arg_value_key):
98
setattr(parsed_args, self.arg_name, value)
99
else:
100
if (not self._check_arg(parsed_args, self.arg_name)):
101
setattr(parsed_args, self.arg_name, {})
102
getattr(parsed_args, self.arg_name)[self.arg_value_key] = value
103
104
105
class BooleanConfiguration(Configuration):
106
107
def __init__(self, name):
108
super(BooleanConfiguration, self).__init__(name, name)
109
self.no_version_arg_name = "no_" + name
110
111
def is_applicable(self, command):
112
return command.supports_arg(self.arg_name.replace('_', '-')) and \
113
command.supports_arg(self.no_version_arg_name.replace('_', '-'))
114
115
def is_present(self, parsed_args):
116
return self._check_arg(parsed_args, self.arg_name) \
117
or self._check_arg(parsed_args, self.no_version_arg_name)
118
119
def add(self, command, parsed_args, value):
120
if (value.lower() == 'true'):
121
setattr(parsed_args, self.arg_name, True)
122
setattr(parsed_args, self.no_version_arg_name, False)
123
elif (value.lower() == 'false'):
124
setattr(parsed_args, self.arg_name, False)
125
setattr(parsed_args, self.no_version_arg_name, True)
126
else:
127
raise exceptions.InvalidBooleanConfigError(
128
config_value=value,
129
config_key=self.arg_name,
130
profile_var_name=configutils.get_current_profile_var_name(
131
command._session))
132
133