Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/ec2/addcount.py
1567 views
1
# Copyright 2013 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
import logging
14
15
from botocore import model
16
17
from awscli.arguments import BaseCLIArgument
18
19
20
logger = logging.getLogger(__name__)
21
22
23
DEFAULT = 1
24
HELP = """
25
<p>Number of instances to launch. If a single number is provided, it
26
is assumed to be the minimum to launch (defaults to %d). If a range is
27
provided in the form <code>min:max</code> then the first number is
28
interpreted as the minimum number of instances to launch and the second
29
is interpreted as the maximum number of instances to launch.</p>""" % DEFAULT
30
31
32
def register_count_events(event_handler):
33
event_handler.register(
34
'building-argument-table.ec2.run-instances', ec2_add_count)
35
event_handler.register(
36
'before-parameter-build.ec2.RunInstances', set_default_count)
37
38
39
def ec2_add_count(argument_table, **kwargs):
40
argument_table['count'] = CountArgument('count')
41
del argument_table['min-count']
42
del argument_table['max-count']
43
44
45
def set_default_count(params, **kwargs):
46
params.setdefault('MaxCount', DEFAULT)
47
params.setdefault('MinCount', DEFAULT)
48
49
50
class CountArgument(BaseCLIArgument):
51
52
def __init__(self, name):
53
self.argument_model = model.Shape('CountArgument', {'type': 'string'})
54
self._name = name
55
self._required = False
56
57
@property
58
def cli_name(self):
59
return '--' + self._name
60
61
@property
62
def cli_type_name(self):
63
return 'string'
64
65
@property
66
def required(self):
67
return self._required
68
69
@required.setter
70
def required(self, value):
71
self._required = value
72
73
@property
74
def documentation(self):
75
return HELP
76
77
def add_to_parser(self, parser):
78
# We do NOT set default value here. It will be set later by event hook.
79
parser.add_argument(self.cli_name, metavar=self.py_name,
80
help='Number of instances to launch')
81
82
def add_to_params(self, parameters, value):
83
if value is None:
84
# NO-OP if value is not explicitly set by user
85
return
86
try:
87
if ':' in value:
88
minstr, maxstr = value.split(':')
89
else:
90
minstr, maxstr = (value, value)
91
parameters['MinCount'] = int(minstr)
92
parameters['MaxCount'] = int(maxstr)
93
except:
94
msg = ('count parameter should be of '
95
'form min[:max] (e.g. 1 or 1:10)')
96
raise ValueError(msg)
97
98