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

Initial version of website configuration command for s3. #481

Closed
wants to merge 1 commit into from
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
11 changes: 11 additions & 0 deletions awscli/customizations/s3/description.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def add_command_descriptions(cmd_dict):
cmd_dict['rb']['description'] = "Deletes an S3 bucket."
cmd_dict['rb']['usage'] = "<S3Path>"

cmd_dict['website']['description'] = "Configures an S3 bucket " \
" as a website."
cmd_dict['website']['usage'] = "<S3Path>"


def add_param_descriptions(params_dict):
"""
Expand Down Expand Up @@ -121,3 +125,10 @@ def add_param_descriptions(params_dict):
"``permission=grantee`` where permission is one of: "
"``read``, ``readacl``, ``writeacp``, ``full``")

params_dict['index-document']['documents'] = (
"The name of the object in the bucket that will act as "
"the index document of the website.")

params_dict['error-document']['documents'] = (
"The name of the object in the bucket that will act as "
"the generic error document of the website.")
25 changes: 24 additions & 1 deletion awscli/customizations/s3/fileinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,26 @@ class TaskInfo(object):
Note that a local file will always have its absolute path, and a s3 file
will have its path in the form of bucket/key
"""
def __init__(self, src, src_type, operation_name, service, endpoint):
def __init__(self, src, src_type, operation_name, service, endpoint,
parameters=None):
self.src = src
self.src_type = src_type
self.operation_name = operation_name
self.service = service
self.endpoint = endpoint
self.parameters = parameters

def _handle_config_params(self, params):
website_config = {}
if self.parameters['index_document']:
website_config['IndexDocument'] = {
'Suffix': self.parameters['index_document'][0]}
if self.parameters['error_document']:
website_config['ErrorDocument'] = {
'Key': self.parameters['error_document'][0]}
if website_config:
params['website_configuration'] = website_config


def make_bucket(self):
"""
Expand All @@ -106,6 +120,15 @@ def remove_bucket(self):
params = {'endpoint': self.endpoint, 'bucket': bucket}
response_data, http = operate(self.service, 'DeleteBucket', params)

def website_config(self):
"""
This operation configures the website properties of a bucket
"""
bucket, key = find_bucket_key(self.src)
params = {'endpoint': self.endpoint, 'bucket': bucket}
self._handle_config_params(params)
response_data, http = operate(self.service, 'PutBucketWebsite', params)


class FileInfo(TaskInfo):
"""
Expand Down
21 changes: 15 additions & 6 deletions awscli/customizations/s3/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def create_instructions(self):
instruction list because it sends the request to S3 and does not
yield anything.
"""
if self.cmd not in ['mb', 'rb']:
if self.cmd not in ['mb', 'rb', 'website']:
self.instructions.append('file_generator')
if self.parameters.get('filters'):
self.instructions.append('filters')
Expand Down Expand Up @@ -489,7 +489,8 @@ def run(self):
cmd_translation['s3'] = {
'rm': 'delete',
'mb': 'make_bucket',
'rb': 'remove_bucket'
'rb': 'remove_bucket',
'website': 'website_config'
}
operation_name = cmd_translation[paths_type][self.cmd]

Expand Down Expand Up @@ -535,6 +536,10 @@ def run(self):
command_dict['rb'] = {'setup': [taskinfo],
's3_handler': [s3handler]}

command_dict['website'] = {'setup': [taskinfo],
's3_handler': [s3handler],
'params': self.parameters}

files = command_dict[self.cmd]['setup']

while self.instructions:
Expand Down Expand Up @@ -576,7 +581,7 @@ def __init__(self, session, cmd, parameters):
self.parameters = parameters
if 'dir_op' not in parameters:
self.parameters['dir_op'] = False
if self.cmd in ['sync', 'mb', 'rb']:
if self.cmd in ['sync', 'mb', 'rb', 'website']:
self.parameters['dir_op'] = True

def add_paths(self, paths):
Expand Down Expand Up @@ -624,7 +629,7 @@ def check_path_type(self, paths):
template_type = {'s3s3': ['cp', 'sync', 'mv'],
's3local': ['cp', 'sync', 'mv'],
'locals3': ['cp', 'sync', 'mv'],
's3': ['mb', 'rb', 'rm'],
's3': ['mb', 'rb', 'rm', 'website'],
'local': [], 'locallocal': []}
paths_type = ''
usage = "usage: aws s3 %s %s" % (self.cmd,
Expand Down Expand Up @@ -657,7 +662,7 @@ def check_src_path(self, paths):
src_path = paths[0]
dir_op = self.parameters['dir_op']
if src_path.startswith('s3://'):
if self.cmd in ['mb', 'rb']:
if self.cmd in ['mb', 'rb', 'website']:
return
session = self.session
service = session.get_service('s3')
Expand Down Expand Up @@ -777,7 +782,9 @@ def check_region(self, parsed_globals):
'params': [], 'default': 's3://',
'command_class': ListCommand},
'mb': {'options': {'nargs': 1}, 'params': []},
'rb': {'options': {'nargs': 1}, 'params': ['force']}
'rb': {'options': {'nargs': 1}, 'params': ['force']},
'website': {'options': {'nargs': 1},
'params': ['index-document', 'error-document']}
}

add_command_descriptions(CMD_DICT)
Expand Down Expand Up @@ -816,6 +823,8 @@ def check_region(self, parsed_globals):
'content-encoding': {'options': {'nargs': 1}},
'content-language': {'options': {'nargs': 1}},
'expires': {'options': {'nargs': 1}},
'index-document': {'options': {'nargs': 1}},
'error-document': {'options': {'nargs': 1}},
}

add_param_descriptions(PARAMS_DICT)
Expand Down
3 changes: 2 additions & 1 deletion awscli/customizations/s3/s3handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def __init__(self, session, params, multi_threshold=MULTI_THRESHOLD,
'content_type': None, 'cache_control': None,
'content_disposition': None, 'content_encoding': None,
'content_language': None, 'expires': None,
'grants': None}
'grants': None, 'index_document': None,
'error_document': None}
self.params['region'] = params['region']
for key in self.params.keys():
if key in params:
Expand Down
2 changes: 1 addition & 1 deletion awscli/customizations/s3/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def print_operation(filename, failed, dryrun=False):
print_str = print_str + "s3://" + filename.src
else:
print_str += relative_path(filename.src)
if filename.operation_name not in ["delete", "make_bucket", "remove_bucket"]:
if filename.operation_name not in ["delete", "make_bucket", "remove_bucket", "website_config"]:
if filename.dest_type == "s3":
print_str += " to s3://" + filename.dest
else:
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/customizations/s3/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_initialize(self):
reference.append("building-command-table.main")
reference.append("building-operation-table.s3")
reference.append("doc-examples.S3.*")
cmds = ['mv', 'rm', 'ls', 'rb', 'mb', 'cp', 'sync']
cmds = ['mv', 'rm', 'ls', 'rb', 'mb', 'cp', 'sync', 'website']
for cmd in cmds:
reference.append("building-parameter-table.s3." + cmd)
for arg in self.cli.register.call_args_list:
Expand Down Expand Up @@ -347,7 +347,7 @@ def test_check_path_type_pass(self):
cmds = {'cp': ['locals3', 's3s3', 's3local'],
'mv': ['locals3', 's3s3', 's3local'],
'rm': ['s3'], 'mb': ['s3'], 'rb': ['s3'],
'sync': ['locals3', 's3s3', 's3local']}
'website': ['s3'], 'sync': ['locals3', 's3s3', 's3local']}
s3_file = 's3://' + self.bucket + '/' + 'text1.txt'
local_file = self.loc_files[0]

Expand Down Expand Up @@ -375,7 +375,8 @@ def test_check_path_type_fail(self):
'ls': ['local', 'locallocal', 's3s3', 'locals3', 's3local'],
'sync': ['local', 'locallocal', 's3'],
'mb': ['local', 'locallocal', 's3s3', 'locals3', 's3local'],
'rb': ['local', 'locallocal', 's3s3', 'locals3', 's3local']}
'rb': ['local', 'locallocal', 's3s3', 'locals3', 's3local'],
'website': ['local', 'locallocal', 's3s3', 'locals3', 's3local']}
s3_file = 's3://' + self.bucket + '/' + 'text1.txt'
local_file = self.loc_files[0]

Expand Down