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

cloudfront create-invalidation --paths #1662

Merged
merged 7 commits into from
Dec 8, 2015
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Next Release (TBD)
* feature:``aws configure add-model``: Added command for updating commands
in the CLI and clients in boto3.
(`issue 1664 <https://github.com/aws/aws-cli/pull/1664>`__)
* feature:``aws cloudfront create-invalidation``: Add a new --paths option.
(`issue 1662 <https://github.com/aws/aws-cli/pull/1662>`__)


1.9.11
Expand Down
51 changes: 51 additions & 0 deletions awscli/customizations/cloudfront.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2015 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.
import time
import random

from awscli.arguments import CustomArgument
from awscli.customizations.utils import validate_mutually_exclusive_handler


def register(event_handler):
"""Provides a simpler --paths for ``aws cloudfront create-invalidation``"""

event_handler.register(
'building-argument-table.cloudfront.create-invalidation', _add_paths)
event_handler.register(
'operation-args-parsed.cloudfront.create-invalidation',
validate_mutually_exclusive_handler(['invalidation_batch'], ['paths']))


def _add_paths(argument_table, **kwargs):
argument_table['invalidation-batch'].required = False
argument_table['paths'] = PathsArgument()


class PathsArgument(CustomArgument):

def __init__(self):
doc = (
'The space-separated paths to be invalidated.'
' Note: --invalidation-batch and --paths are mututally exclusive.'
)
super(PathsArgument, self).__init__('paths', nargs='+', help_text=doc)

def add_to_params(self, parameters, value):
if value is not None:
caller_reference = 'cli-%s-%s' % (
int(time.time()), random.randint(1, 1000000))
parameters['InvalidationBatch'] = {
"CallerReference": caller_reference,
"Paths": {"Quantity": len(value), "Items": value},
}
9 changes: 8 additions & 1 deletion awscli/examples/cloudfront/create-invalidation.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
The following command creates an invalidation for a CloudFront distribution with the ID ``S11A16G5KZMEQD``::

aws cloudfront create-invalidation --distribution-id S11A16G5KZMEQD \
--paths /index.html /error.html

The --paths will automatically generate a random ``CallerReference`` every time.

Or you can use the following command to do the same thing, so that you can have a chance to specify your own ``CallerReference`` here::

aws cloudfront create-invalidation --invalidation-batch file://invbatch.json --distribution-id S11A16G5KZMEQD

The distribution ID is available in the output of ``create-distribution`` and ``list-distributions``.
Expand All @@ -14,7 +21,7 @@ The file ``invbatch.json`` is a JSON document in the current folder that specifi
"CallerReference": "my-invalidation-2015-09-01"
}

Output::
Output of both commands::

{
"Invalidation": {
Expand Down
2 changes: 2 additions & 0 deletions awscli/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from awscli.customizations.rds import register_rds_modify_split
from awscli.customizations.putmetricdata import register_put_metric_data
from awscli.customizations.sessendemail import register_ses_send_email
from awscli.customizations.cloudfront import register as cloudfront_register
from awscli.customizations.iamvirtmfa import IAMVMFAWrapper
from awscli.customizations.argrename import register_arg_renames
from awscli.customizations.configure import register_configure_cmd
Expand Down Expand Up @@ -108,6 +109,7 @@ def awscli_initialize(event_handlers):
register_rds_modify_split(event_handlers)
register_put_metric_data(event_handlers)
register_ses_send_email(event_handlers)
cloudfront_register(event_handlers)
IAMVMFAWrapper(event_handlers)
register_arg_renames(event_handlers)
register_configure_cmd(event_handlers)
Expand Down
52 changes: 52 additions & 0 deletions tests/functional/cloudfront/test_create_invalidation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright 2015 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.
import mock

from awscli.testutils import BaseAWSPreviewCommandParamsTest as \
BaseAWSCommandParamsTest

Copy link
Contributor

Choose a reason for hiding this comment

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

PEP8. Use two blank lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Done.


class TestCreateInvalidation(BaseAWSCommandParamsTest):

prefix = 'cloudfront create-invalidation --distribution-id my_id '

def test_invalidation_batch_only(self):
batch = "Paths={Quantity=2,Items=[foo.txt,bar.txt]},CallerReference=ab"
cmdline = self.prefix + '--invalidation-batch ' + batch
result = {
'DistributionId': 'my_id',
'InvalidationBatch': {
'Paths': {'Items': ['foo.txt', 'bar.txt'], 'Quantity': 2},
'CallerReference': 'ab',
},
}
self.assert_params_for_cmd(cmdline, result)

def test_paths_only(self):
cmdline = self.prefix + '--paths index.html foo.txt'
result = {
'DistributionId': 'my_id',
'InvalidationBatch': {
'Paths': {'Items': ['index.html', 'foo.txt'], 'Quantity': 2},
'CallerReference': mock.ANY,
},
}
self.run_cmd(cmdline)
self.assertEqual(self.last_kwargs, result)

def test_invalidation_batch_and_paths(self):
cmdline = self.prefix + '--invalidation-batch {} --paths foo'
self.run_cmd(cmdline, expected_rc=255)

def test_neither_invalidation_batch_or_paths(self):
self.run_cmd(self.prefix, expected_rc=255)