Skip to content
Open
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
34 changes: 32 additions & 2 deletions awscli/customizations/s3/subcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,15 @@
),
}

TAGS = {
'name': 'tags',
'action': 'append',
'nargs': 2,
'help_text': (
"This flag specifies tags to be added to the bucket."
),
}


CASE_CONFLICT = {
'name': 'case-conflict',
Expand Down Expand Up @@ -1206,7 +1215,16 @@ class MbCommand(S3Command):
NAME = 'mb'
DESCRIPTION = "Creates an S3 bucket."
USAGE = "<S3Uri>"
ARG_TABLE = [{'name': 'path', 'positional_arg': True, 'synopsis': USAGE}]
ARG_TABLE = (
[
{
'name': 'path',
'positional_arg': True,
'synopsis': USAGE,
}
]
+ [TAGS]
)

def _run_main(self, parsed_args, parsed_globals):
super(MbCommand, self)._run_main(parsed_args, parsed_globals)
Expand All @@ -1222,8 +1240,15 @@ def _run_main(self, parsed_args, parsed_globals):
"Cannot use mb command with a directory bucket."
)

bucket_config = {'LocationConstraint': self.client.meta.region_name}
bucket_config = {
'LocationConstraint': self.client.meta.region_name
}

params = {'Bucket': bucket}
bucket_tags = self._create_bucket_tags(parsed_args)

if bucket_tags:
bucket_config['Tags'] = bucket_tags
if self.client.meta.region_name != 'us-east-1':
params['CreateBucketConfiguration'] = bucket_config

Expand All @@ -1239,6 +1264,11 @@ def _run_main(self, parsed_args, parsed_globals):
)
return 1

def _create_bucket_tags(self, parsed_args):
if parsed_args.tags is not None:
return [{'Key': tag[0], 'Value': tag[1]} for tag in parsed_args.tags]
return []


class RbCommand(S3Command):
NAME = 'rb'
Expand Down
35 changes: 35 additions & 0 deletions tests/functional/s3/test_mb_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,38 @@ def test_incompatible_with_express_directory_bucket(self):
command = self.prefix + 's3://bucket--usw2-az1--x-s3/'
stderr = self.run_cmd(command, expected_rc=252)[1]
self.assertIn('Cannot use mb command with a directory bucket.', stderr)

def test_make_bucket_with_single_tag(self):
command = self.prefix + 's3://bucket --tags Key1 Value1 --region us-west-2'
expected_params = {
'Bucket': 'bucket',
'CreateBucketConfiguration': {
'LocationConstraint': 'us-west-2',
'Tags': [
{'Key': 'Key1', 'Value': 'Value1'}
]
}
}
self.assert_params_for_cmd(command, expected_params)

def test_make_bucket_with_multiple_tags(self):
command = self.prefix + 's3://bucket --tags Key1 Value1 --tags Key2 Value2 --region us-west-2'
expected_params = {
'Bucket': 'bucket',
'CreateBucketConfiguration': {
'LocationConstraint': 'us-west-2',
'Tags': [
{'Key': 'Key1', 'Value': 'Value1'},
{'Key': 'Key2', 'Value': 'Value2'}
]
}
}
self.assert_params_for_cmd(command, expected_params)

def test_tags_with_three_arguments_fails(self):
command = self.prefix + 's3://bucket --tags Key1 Value1 ExtraArg'
self.assert_params_for_cmd(
command,
expected_rc=252,
stderr_contains='ParamValidation'
)