Skip to content

Commit f5f6bde

Browse files
authored
[v2] Add tagging support for the s3 mb command (aws#10114)
1 parent 2ac8e45 commit f5f6bde

4 files changed

Lines changed: 101 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "enhancement",
3+
"category": "``s3``",
4+
"description": "Add support for specifying tags on buckets during bucket creation using the ``aws s3 mb`` command via a new ``--tags`` flag."
5+
}

awscli/customizations/s3/subcommands.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,18 @@
673673
),
674674
}
675675

676+
TAGS = {
677+
'name': 'tags',
678+
'synopsis': '--tags <key> <value>',
679+
'action': 'append',
680+
'nargs': 2,
681+
'help_text': (
682+
'This flag specifies tags to be added to the bucket in the format of '
683+
'``--tags key value``. You can specify this flag multiple times, '
684+
'once for each tag.'
685+
),
686+
}
687+
676688

677689
CASE_CONFLICT = {
678690
'name': 'case-conflict',
@@ -1206,7 +1218,16 @@ class MbCommand(S3Command):
12061218
NAME = 'mb'
12071219
DESCRIPTION = "Creates an S3 bucket."
12081220
USAGE = "<S3Uri>"
1209-
ARG_TABLE = [{'name': 'path', 'positional_arg': True, 'synopsis': USAGE}]
1221+
ARG_TABLE = (
1222+
[
1223+
{
1224+
'name': 'path',
1225+
'positional_arg': True,
1226+
'synopsis': USAGE,
1227+
}
1228+
]
1229+
+ [TAGS]
1230+
)
12101231

12111232
def _run_main(self, parsed_args, parsed_globals):
12121233
super(MbCommand, self)._run_main(parsed_args, parsed_globals)
@@ -1222,9 +1243,17 @@ def _run_main(self, parsed_args, parsed_globals):
12221243
"Cannot use mb command with a directory bucket."
12231244
)
12241245

1225-
bucket_config = {'LocationConstraint': self.client.meta.region_name}
12261246
params = {'Bucket': bucket}
1247+
bucket_config = {}
1248+
bucket_tags = self._create_bucket_tags(parsed_args)
1249+
1250+
# Only set LocationConstraint when the region name is not us-east-1.
1251+
# Sending LocationConstraint with value us-east-1 results in an error.
12271252
if self.client.meta.region_name != 'us-east-1':
1253+
bucket_config['LocationConstraint'] = self.client.meta.region_name
1254+
if bucket_tags:
1255+
bucket_config['Tags'] = bucket_tags
1256+
if bucket_config:
12281257
params['CreateBucketConfiguration'] = bucket_config
12291258

12301259
# TODO: Consolidate how we handle return codes and errors
@@ -1239,6 +1268,11 @@ def _run_main(self, parsed_args, parsed_globals):
12391268
)
12401269
return 1
12411270

1271+
def _create_bucket_tags(self, parsed_args):
1272+
if parsed_args.tags is not None:
1273+
return [{'Key': tag[0], 'Value': tag[1]} for tag in parsed_args.tags]
1274+
return []
1275+
12421276

12431277
class RbCommand(S3Command):
12441278
NAME = 'rb'

awscli/examples/s3/mb.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,16 @@ user makes the bucket ``amzn-s3-demo-bucket`` in the region ``us-west-1``::
2020
Output::
2121

2222
make_bucket: s3://amzn-s3-demo-bucket
23+
24+
**Example 3: Create a bucket with specified tags**
25+
26+
The following ``mb`` command creates a bucket with specified tags using the ``--tags`` parameter. In this example, the
27+
user makes the bucket ``amzn-s3-demo-bucket`` with two tags with keys ``Key1`` and ``Key2``, respectively. ::
28+
29+
aws s3 mb s3://amzn-s3-demo-bucket \
30+
--tags Key1 Value1 \
31+
--tags Key2 Value2
32+
33+
Output::
34+
35+
make_bucket: s3://amzn-s3-demo-bucket

tests/functional/s3/test_mb_command.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,50 @@ def test_incompatible_with_express_directory_bucket(self):
4545
command = self.prefix + 's3://bucket--usw2-az1--x-s3/'
4646
stderr = self.run_cmd(command, expected_rc=252)[1]
4747
self.assertIn('Cannot use mb command with a directory bucket.', stderr)
48+
49+
def test_make_bucket_with_single_tag(self):
50+
command = self.prefix + 's3://bucket --tags Key1 Value1 --region us-west-2'
51+
expected_params = {
52+
'Bucket': 'bucket',
53+
'CreateBucketConfiguration': {
54+
'LocationConstraint': 'us-west-2',
55+
'Tags': [
56+
{'Key': 'Key1', 'Value': 'Value1'}
57+
]
58+
}
59+
}
60+
self.assert_params_for_cmd(command, expected_params)
61+
62+
def test_make_bucket_with_single_tag_us_east_1(self):
63+
command = self.prefix + 's3://bucket --tags Key1 Value1 --region us-east-1'
64+
expected_params = {
65+
'Bucket': 'bucket',
66+
'CreateBucketConfiguration': {
67+
'Tags': [
68+
{'Key': 'Key1', 'Value': 'Value1'}
69+
]
70+
}
71+
}
72+
self.assert_params_for_cmd(command, expected_params)
73+
74+
def test_make_bucket_with_multiple_tags(self):
75+
command = self.prefix + 's3://bucket --tags Key1 Value1 --tags Key2 Value2 --region us-west-2'
76+
expected_params = {
77+
'Bucket': 'bucket',
78+
'CreateBucketConfiguration': {
79+
'LocationConstraint': 'us-west-2',
80+
'Tags': [
81+
{'Key': 'Key1', 'Value': 'Value1'},
82+
{'Key': 'Key2', 'Value': 'Value2'}
83+
]
84+
}
85+
}
86+
self.assert_params_for_cmd(command, expected_params)
87+
88+
def test_tags_with_three_arguments_fails(self):
89+
command = self.prefix + 's3://bucket --tags Key1 Value1 ExtraArg'
90+
self.assert_params_for_cmd(
91+
command,
92+
expected_rc=252,
93+
stderr_contains='ParamValidation'
94+
)

0 commit comments

Comments
 (0)