Skip to content

Commit

Permalink
Add customizations for cloudsearchdomain command
Browse files Browse the repository at this point in the history
* Rename --query to --search-query to avoid shadowing our --query arg
* Add validation to require --endpoint-url when using cloudsearch domain
  commands.
  • Loading branch information
jamesls committed Jun 26, 2014
1 parent cbf4ebc commit b01737c
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 10 deletions.
6 changes: 5 additions & 1 deletion awscli/clidriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,12 @@ def main(self, args=None):
parser = self._create_parser()
command_table = self._get_command_table()
parsed_args, remaining = parser.parse_known_args(args)
self._handle_top_level_args(parsed_args)
try:
# Because _handle_top_level_args emits events, it's possible
# that exceptions can be raised, which should have the same
# general exception handling logic as calling into the
# command table. This is why it's in the try/except clause.
self._handle_top_level_args(parsed_args)
return command_table[parsed_args.command](remaining, parsed_args)
except UnknownArgumentError as e:
sys.stderr.write(str(e) + '\n')
Expand Down
1 change: 1 addition & 0 deletions awscli/customizations/argrename.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
'datapipeline.*.query': 'objects-query',
'emr.*.job-flow-ids': 'cluster-ids',
'emr.*.job-flow-id': 'cluster-id',
'cloudsearchdomain.search.query': 'search-query',
}


Expand Down
29 changes: 29 additions & 0 deletions awscli/customizations/cloudsearchdomain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Customizations for the cloudsearchdomain command.
This module customizes the cloudsearchdomain command:
* Add validation that --endpoint-url is required.
"""

def register_cloudsearchdomain(cli):
cli.register('top-level-args-parsed', validate_endpoint_url)


def validate_endpoint_url(parsed_args, **kwargs):
if parsed_args.command == 'cloudsearchdomain' and \
parsed_args.endpoint_url is None:
raise ValueError(
"--endpoint-url is required for cloudsearchdomain commands")
2 changes: 2 additions & 0 deletions awscli/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from awscli.customizations.globalargs import register_parse_global_args
from awscli.customizations.cloudsearch import initialize as cloudsearch_init
from awscli.customizations.emr.emr import emr_initialize
from awscli.customizations.cloudsearchdomain import register_cloudsearchdomain


def awscli_initialize(event_handlers):
Expand Down Expand Up @@ -92,3 +93,4 @@ def awscli_initialize(event_handlers):
datapipeline.register_customizations(event_handlers)
cloudsearch_init(event_handlers)
emr_initialize(event_handlers)
register_cloudsearchdomain(event_handlers)
9 changes: 8 additions & 1 deletion awscli/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,14 @@ def run_cmd(self, cmd, expected_rc=0):
captured_stdout = six.StringIO()
with mock.patch('sys.stderr', captured_stderr):
with mock.patch('sys.stdout', captured_stdout):
rc = self.driver.main(cmdlist)
try:
rc = self.driver.main(cmdlist)
except SystemExit as e:
# We need to catch SystemExit so that we
# can get a proper rc and still present the
# stdout/stderr to the test runner so we can
# figure out what went wrong.
rc = e.code
stderr = captured_stderr.getvalue()
stdout = captured_stdout.getvalue()
self.assertEqual(
Expand Down
62 changes: 62 additions & 0 deletions tests/unit/customizations/test_cloudsearchdomain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from awscli.testutils import unittest
from awscli.testutils import BaseAWSCommandParamsTest
from awscli.customizations.cloudsearchdomain import validate_endpoint_url

import mock


class TestSearchCommand(BaseAWSCommandParamsTest):
prefix = 'cloudsearchdomain search '

def test_search_with_query(self):
cmd = self.prefix.split()
cmd += [
'--endpoint-url', 'http://example.com/',
# Note we're also verifying that --query is renamed to
# --search-query from argrename.py.
'--search-query', 'George Lucas',
'--query-options',
'{"defaultOperator":"and","fields":["directors^10"]}']

result = {
'headers': {},
'uri_params': {
'query': 'George Lucas',
'queryOptions':
'{"defaultOperator":"and","fields":["directors^10"]}'}}

self.assert_params_for_cmd(cmd, result, ignore_params=['payload'])
# We ignore'd the paylod, but we can verify that there should be
# no payload for this request.
self.assertIsNone(self.last_params['payload'].getvalue())

def test_endpoint_is_required(self):
cmd = self.prefix.split()
cmd += ['--search-query', 'foo']
stderr = self.run_cmd(cmd, expected_rc=255)[1]
self.assertIn('--endpoint-url is required', stderr)


class TestCloudsearchDomainHandler(unittest.TestCase):
def test_validate_endpoint_url_is_none(self):
parsed_args = mock.Mock()
parsed_args.endpoint_url = None
parsed_args.command = 'cloudsearchdomain'
with self.assertRaises(ValueError):
validate_endpoint_url(parsed_args)


if __name__ == "__main__":
unittest.main()
16 changes: 8 additions & 8 deletions tests/unit/test_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@

COMPLETIONS = [
('aws ', -1, set(['autoscaling', 'cloudformation', 'cloudsearch',
'cloudtrail', 'cloudwatch', 'configure', 'datapipeline',
'directconnect', 'dynamodb', 'ec2',
'elasticache', 'elasticbeanstalk', 'elastictranscoder',
'elb', 'iam', 'importexport', 'kinesis',
'opsworks', 'rds', 'redshift', 'route53', 's3', 's3api',
'ses', 'sns', 'sqs', 'storagegateway', 'sts',
'support', 'swf'])),
'cloudsearchdomain', 'cloudtrail', 'cloudwatch',
'configure', 'datapipeline', 'directconnect', 'dynamodb',
'ec2', 'elasticache', 'elasticbeanstalk',
'elastictranscoder', 'elb', 'iam', 'importexport',
'kinesis', 'opsworks', 'rds', 'redshift', 'route53',
's3', 's3api', 'ses', 'sns', 'sqs', 'storagegateway',
'sts', 'support', 'swf'])),
('aws cloud', -1, set(['cloudformation', 'cloudsearch',
'cloudtrail', 'cloudwatch'])),
'cloudsearchdomain', 'cloudtrail', 'cloudwatch'])),
('aws cloudf', -1, set(['cloudformation'])),
('aws cloudfr', -1, set([])),
('aws foobar', -1, set([])),
Expand Down

0 comments on commit b01737c

Please sign in to comment.