Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update events to emit model/session objects #1214

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion awscli/clidriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ def __init__(self, cli_name, session, service_name=None):
else:
self._service_name = service_name
self._lineage = [self]
self._service_model = None

@property
def name(self):
Expand Down Expand Up @@ -368,6 +369,11 @@ def _get_service_object(self):
self._service_object = self.session.get_service(self._service_name)
return self._service_object

def _get_service_model(self):
if self._service_model is None:
self._service_model = self.session.get_service_model(self._service_name)
return self._service_model

def __call__(self, args, parsed_globals):
# Once we know we're trying to call a service for this operation
# we can go ahead and create the parser for it. We
Expand All @@ -380,12 +386,16 @@ def __call__(self, args, parsed_globals):
def _create_command_table(self):
command_table = OrderedDict()
service_object = self._get_service_object()
service_model = self._get_service_model()
for operation_object in service_object.operations:
cli_name = xform_name(operation_object.name, '-')
command_table[cli_name] = ServiceOperation(
name=cli_name,
parent_name=self._name,
session=self.session,
operation_object=operation_object,
operation_model=service_model.operation_model(
operation_object.name),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any other way we can pull the name out (other than from operation objects)? That way it would be deleting arguments from calls and constructors when we do the final purge of operation objects.

operation_caller=CLIOperationCaller(self.session),
service_object=service_object)
self.session.emit('building-command-table.%s' % self._name,
Expand Down Expand Up @@ -433,7 +443,7 @@ class ServiceOperation(object):
DEFAULT_ARG_CLASS = CLIArgument

def __init__(self, name, parent_name, operation_object, operation_caller,
service_object):
service_object, operation_model, session):
"""

:type name: str
Expand Down Expand Up @@ -462,6 +472,8 @@ def __init__(self, name, parent_name, operation_object, operation_caller,
self._operation_caller = operation_caller
self._service_object = service_object
self._lineage = [self]
self._operation_model = operation_model
self._session = session

@property
def name(self):
Expand Down Expand Up @@ -598,6 +610,9 @@ def _create_argument_table(self):
self._emit('building-argument-table.%s.%s' % (self._parent_name,
self._name),
operation=self._operation_object,
operation_model=self._operation_model,
session=self._session,
command=self,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I am in favor of adding the command object to the event. But could we name it command_object to be consistent with building-command-table event of both the data driven and custom commands.

argument_table=argument_table)
return argument_table

Expand Down
12 changes: 5 additions & 7 deletions awscli/customizations/cliinputjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def register_cli_input_json(cli):
cli.register('building-argument-table', add_cli_input_json)


def add_cli_input_json(operation, argument_table, **kwargs):
def add_cli_input_json(session, argument_table, **kwargs):
# This argument cannot support operations with streaming output which
# is designated by the argument name `outfile`.
if 'outfile' not in argument_table:
cli_input_json_argument = CliInputJSONArgument(operation)
cli_input_json_argument = CliInputJSONArgument(session)
cli_input_json_argument.add_to_arg_table(argument_table)


Expand All @@ -45,13 +45,11 @@ class CliInputJSONArgument(OverrideRequiredArgsArgument):
'the JSON-provided values.'
}

def __init__(self, operation_object):
self._operation_object = operation_object
super(CliInputJSONArgument, self).__init__(
self._operation_object.session)
def __init__(self, session):
super(CliInputJSONArgument, self).__init__(session)

def _register_argument_action(self):
self._operation_object.session.register(
self._session.register(
'calling-command', self.add_to_call_parameters)
super(CliInputJSONArgument, self)._register_argument_action()

Expand Down
31 changes: 16 additions & 15 deletions awscli/customizations/ec2bundleinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@
OWNER_AKID_DOCS = 'The access key ID of the owner of the Amazon S3 bucket.'

# --policy
POLICY_DOCS = ("An Amazon S3 upload policy that gives "
"Amazon EC2 permission to upload items into Amazon S3 "
"on the user's behalf. If you provide this parameter, "
"you must also provide "
"your secret access key, so we can create a policy "
"signature for you (the secret access key is not passed "
"to Amazon EC2). If you do not provide this parameter, "
"we generate an upload policy for you automatically. "
"For more information about upload policies see the "
"sections about policy construction and signatures in the "
'<a href="http://docs.aws.amazon.com/AmazonS3/latest/dev/HTTPPOSTForms.html">'
'Amazon Simple Storage Service Developer Guide</a>.')
POLICY_DOCS = (
"An Amazon S3 upload policy that gives "
"Amazon EC2 permission to upload items into Amazon S3 "
"on the user's behalf. If you provide this parameter, "
"you must also provide "
"your secret access key, so we can create a policy "
"signature for you (the secret access key is not passed "
"to Amazon EC2). If you do not provide this parameter, "
"we generate an upload policy for you automatically. "
"For more information about upload policies see the "
"sections about policy construction and signatures in the "
'<a href="http://docs.aws.amazon.com/AmazonS3/latest/dev'
'/HTTPPOSTForms.html">'
'Amazon Simple Storage Service Developer Guide</a>.')

# --owner-sak
OWNER_SAK_DOCS = ('The AWS secret access key for the owner of the '
Expand All @@ -61,7 +63,7 @@
'signature can be computed for the policy.')


def _add_params(argument_table, operation, **kwargs):
def _add_params(argument_table, **kwargs):
# Add the scalar parameters and also change the complex storage
# param to not be required so the user doesn't get an error from
# argparse if they only supply scalar params.
Expand Down Expand Up @@ -152,7 +154,7 @@ def _check_params(params, **kwargs):
('building-argument-table.ec2.bundle-instance', _add_params),
('operation-args-parsed.ec2.bundle-instance', _check_args),
('before-parameter-build.ec2.BundleInstance', _check_params),
]
]


def register_bundleinstance(event_handler):
Expand All @@ -173,7 +175,6 @@ def _build_storage(self, params, value):
params['storage'] = {'S3': {}}
params['storage']['S3'][self._storage_param] = value


def add_to_params(self, parameters, value):
if value:
self._build_storage(parameters, value)
2 changes: 1 addition & 1 deletion awscli/customizations/ec2runinstances.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
'[EC2-VPC] If specified a public IP address will be assigned '
'to the new instance in a VPC.')

def _add_params(argument_table, operation, **kwargs):
def _add_params(argument_table, **kwargs):
arg = SecondaryPrivateIpAddressesArgument(
name='secondary-private-ip-addresses',
help_text=SECONDARY_PRIVATE_IP_ADDRESSES_DOCS)
Expand Down
16 changes: 10 additions & 6 deletions awscli/customizations/ec2secgroupsimplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from awscli.arguments import CustomArgument


def _add_params(argument_table, operation, **kwargs):
def _add_params(argument_table, **kwargs):
arg = ProtocolArgument('protocol',
help_text=PROTOCOL_DOCS)
argument_table['protocol'] = arg
Expand All @@ -42,7 +42,6 @@ def _add_params(argument_table, operation, **kwargs):
argument_table['cidr'] = arg
argument_table['cidr-ip']._UNDOCUMENTED = True


arg = SourceGroupArgument('source-group',
help_text=SOURCEGROUP_DOCS)
argument_table['source-group'] = arg
Expand All @@ -67,6 +66,7 @@ def _check_args(parsed_args, **kwargs):
'with the --ip-permissions option ') % key
raise ValueError(msg)


def _add_docs(help_command, **kwargs):
doc = help_command.doc
doc.style.new_paragraph()
Expand All @@ -78,19 +78,22 @@ def _add_docs(help_command, **kwargs):


EVENTS = [
('building-argument-table.ec2.authorize-security-group-ingress', _add_params),
('building-argument-table.ec2.authorize-security-group-egress', _add_params),
('building-argument-table.ec2.authorize-security-group-ingress',
_add_params),
('building-argument-table.ec2.authorize-security-group-egress',
_add_params),
('building-argument-table.ec2.revoke-security-group-ingress', _add_params),
('building-argument-table.ec2.revoke-security-group-egress', _add_params),
('operation-args-parsed.ec2.authorize-security-group-ingress', _check_args),
('operation-args-parsed.ec2.authorize-security-group-ingress',
_check_args),
('operation-args-parsed.ec2.authorize-security-group-egress', _check_args),
('operation-args-parsed.ec2.revoke-security-group-ingress', _check_args),
('operation-args-parsed.ec2.revoke-security-group-egress', _check_args),
('doc-description.ec2.authorize-security-group-ingress', _add_docs),
('doc-description.ec2.authorize-security-group-egress', _add_docs),
('doc-description.ec2.revoke-security-group-ingress', _add_docs),
('doc-description.ec2.revoke-security-groupdoc-ingress', _add_docs),
]
]
PROTOCOL_DOCS = ('<p>The IP protocol of this permission.</p>'
'<p>Valid protocol values: <code>tcp</code>, '
'<code>udp</code>, <code>icmp</code></p>')
Expand All @@ -105,6 +108,7 @@ def _add_docs(help_command, **kwargs):
'group. Cannot be used when specifying a CIDR IP '
'address.</p>')


def register_secgroup(event_handler):
for event, handler in EVENTS:
event_handler.register(event, handler)
Expand Down
6 changes: 3 additions & 3 deletions awscli/customizations/flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,14 @@ def register(self, cli):
operation),
self.flatten_args)

def flatten_args(self, operation, argument_table, **kwargs):
def flatten_args(self, command, argument_table, **kwargs):
# For each argument with a bag of parameters
for name, argument in self.configs[operation.cli_name].items():
for name, argument in self.configs[command.name].items():
argument_from_table = argument_table[name]
overwritten = False

LOG.debug('Flattening {0} argument {1} into {2}'.format(
operation, name,
command.name, name,
', '.join([v['name'] for k, v in argument['flatten'].items()])
))

Expand Down
16 changes: 8 additions & 8 deletions awscli/customizations/generatecliskeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ def register_generate_cli_skeleton(cli):
cli.register('building-argument-table', add_generate_skeleton)


def add_generate_skeleton(operation, argument_table, **kwargs):
def add_generate_skeleton(session, operation_model, argument_table, **kwargs):
# This argument cannot support operations with streaming output which
# is designated by the argument name `outfile`.
if 'outfile' not in argument_table:
generate_cli_skeleton_argument = GenerateCliSkeletonArgument(operation)
generate_cli_skeleton_argument = GenerateCliSkeletonArgument(
session, operation_model)
generate_cli_skeleton_argument.add_to_arg_table(argument_table)


Expand All @@ -49,13 +50,12 @@ class GenerateCliSkeletonArgument(OverrideRequiredArgsArgument):
'group_name': 'generate_cli_skeleton'
}

def __init__(self, operation_object):
self._operation_object = operation_object
super(GenerateCliSkeletonArgument, self).__init__(
self._operation_object.session)
def __init__(self, session, operation_model):
super(GenerateCliSkeletonArgument, self).__init__(session)
self._operation_model = operation_model

def _register_argument_action(self):
self._operation_object.session.register(
self._session.register(
'calling-command.*', self.generate_json_skeleton)
super(GenerateCliSkeletonArgument, self)._register_argument_action()

Expand All @@ -67,7 +67,7 @@ def generate_json_skeleton(self, call_parameters, parsed_args,
if getattr(parsed_args, 'generate_cli_skeleton', False):

# Obtain the model of the operation
operation_model = self._operation_object.model
operation_model = self._operation_model

# Generate the skeleton based on the ``input_shape``.
argument_generator = ArgumentGenerator()
Expand Down
4 changes: 1 addition & 3 deletions awscli/customizations/iamvirtmfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class IAMVMFAWrapper(object):

def __init__(self, event_handler):
self._event_handler = event_handler
self._operation = None
self._outfile = FileArgument(
'outfile', help_text=OUTPUT_HELP, required=True)
self._method = StatefulArgument(
Expand All @@ -77,8 +76,7 @@ def __init__(self, event_handler):
self._event_handler.register(
'after-call.iam.CreateVirtualMFADevice', self._save_file)

def _add_options(self, argument_table, operation, **kwargs):
self._operation = operation
def _add_options(self, argument_table, **kwargs):
argument_table['outfile'] = self._outfile
argument_table['bootstrap-method'] = self._method

Expand Down
2 changes: 1 addition & 1 deletion awscli/customizations/putmetricdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def register_put_metric_data(event_handler):
'dimensions', 'statistic_values']))


def _promote_args(argument_table, operation, **kwargs):
def _promote_args(argument_table, **kwargs):
# We're providing top level params for metric-data. This means
# that metric-data is now longer a required arg. We do need
# to check that either metric-data or the complex args we've added
Expand Down
4 changes: 4 additions & 0 deletions awscli/customizations/rds.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@ def _building_command_table(command_table, session, **kwargs):
parent_name='rds', name='add-option-to-option-group',
operation_object=modify_operation,
operation_caller=CLIOperationCaller(session),
session=session,
operation_model=modify_operation.model,
service_object=rds_service)
command_table['remove-option-from-option-group'] = ServiceOperation(
parent_name='rds', name='remove-option-from-option-group',
operation_object=modify_operation,
session=session,
operation_model=modify_operation.model,
operation_caller=CLIOperationCaller(session),
service_object=rds_service)
25 changes: 14 additions & 11 deletions awscli/customizations/streamingoutputarg.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
from awscli.arguments import BaseCLIArgument


def add_streaming_output_arg(argument_table, operation, **kwargs):
def add_streaming_output_arg(argument_table, operation_model,
session, **kwargs):
# Implementation detail: hooked up to 'building-argument-table'
# event.
model = operation.model
if _has_streaming_output(model):
streaming_argument_name = _get_streaming_argument_name(model)
if _has_streaming_output(operation_model):
streaming_argument_name = _get_streaming_argument_name(operation_model)
argument_table['outfile'] = StreamingOutputArgument(
response_key=streaming_argument_name, operation=operation,
name='outfile')
response_key=streaming_argument_name,
operation_model=operation_model,
session=session, name='outfile')


def _has_streaming_output(model):
Expand All @@ -39,20 +40,22 @@ class StreamingOutputArgument(BaseCLIArgument):
BUFFER_SIZE = 32768
HELP = 'Filename where the content will be saved'

def __init__(self, response_key, operation, name, buffer_size=None):
def __init__(self, response_key, operation_model, name,
session, buffer_size=None):
self._name = name
self.argument_model = Shape('StreamingOutputArgument',
{'type': 'string'})
if buffer_size is None:
buffer_size = self.BUFFER_SIZE
self._buffer_size = buffer_size
self._operation = operation
# This is the key in the response body where we can find the
# streamed contents.
self._response_key = response_key
self._output_file = None
self._name = name
self._required = True
self._operation_model = operation_model
self._session = session

@property
def cli_name(self):
Expand Down Expand Up @@ -83,9 +86,9 @@ def add_to_parser(self, parser):

def add_to_params(self, parameters, value):
self._output_file = value
service_name = self._operation.service.endpoint_prefix
operation_name = self._operation.name
self._operation.session.register('after-call.%s.%s' % (
service_name = self._operation_model.service_model.endpoint_prefix
operation_name = self._operation_model.name
self._session.register('after-call.%s.%s' % (
service_name, operation_name), self.save_file)

def save_file(self, parsed, **kwargs):
Expand Down
Loading