Skip to content

Commit 1d0e88a

Browse files
Put obj plugin website functions to another file (#454)
## 📝 Description Putting the website relevant functions into another file would make the code base more readable and well organised. ## ✔️ How to Test `export LINODE_CLI_TOKEN=<YOUR_TOKEN>` `pytest tests/integration/test_obj_plugin.py`
1 parent f028a92 commit 1d0e88a

File tree

2 files changed

+128
-115
lines changed

2 files changed

+128
-115
lines changed

linodecli/plugins/obj/__init__.py

Lines changed: 5 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
get_object,
5555
upload_object,
5656
)
57+
from linodecli.plugins.obj.website import (
58+
disable_static_site,
59+
enable_static_site,
60+
static_site_info,
61+
)
5762

5863
try:
5964
import boto3
@@ -260,97 +265,6 @@ def set_acl(get_client, args):
260265
print("ACL updated")
261266

262267

263-
def enable_static_site(get_client, args):
264-
"""
265-
Turns a bucket into a static website
266-
"""
267-
parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " ws-create"))
268-
269-
parser.add_argument(
270-
"bucket",
271-
metavar="BUCKET",
272-
type=str,
273-
help="The bucket to turn into a static site",
274-
)
275-
parser.add_argument(
276-
"--ws-index",
277-
metavar="INDEX",
278-
required=True,
279-
type=str,
280-
help="The file to serve as the index of the website",
281-
)
282-
parser.add_argument(
283-
"--ws-error",
284-
metavar="ERROR",
285-
type=str,
286-
help="The file to serve as the error page of the website",
287-
)
288-
289-
parsed = parser.parse_args(args)
290-
client = get_client()
291-
bucket = parsed.bucket
292-
293-
# make the site
294-
print(f"Setting bucket {bucket} access control to be 'public-read'")
295-
296-
client.put_bucket_acl(
297-
Bucket=bucket,
298-
ACL="public-read",
299-
)
300-
301-
index_page = parsed.ws_index
302-
303-
ws_config = {"IndexDocument": {"Suffix": index_page}}
304-
if parsed.ws_error:
305-
ws_config["ErrorDocument"] = {"Key": parsed.ws_error}
306-
307-
client.put_bucket_website(
308-
Bucket=bucket,
309-
WebsiteConfiguration=ws_config,
310-
)
311-
312-
print(
313-
"Static site now available at "
314-
f"{BASE_WEBSITE_TEMPLATE.format(cluster=client.cluster, bucket=bucket)}"
315-
"\nIf you still can't access the website, please check the "
316-
"Access Control List setting of the website related objects (files) "
317-
"in your bucket."
318-
)
319-
320-
321-
def static_site_info(get_client, args):
322-
"""
323-
Returns info about a configured static site
324-
"""
325-
parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " ws-info"))
326-
327-
parser.add_argument(
328-
"bucket",
329-
metavar="BUCKET",
330-
type=str,
331-
help="The bucket to return static site information on.",
332-
)
333-
334-
parsed = parser.parse_args(args)
335-
client = get_client()
336-
337-
bucket = parsed.bucket
338-
339-
response = client.get_bucket_website(Bucket=bucket)
340-
341-
index = response.get("IndexDocument", {}).get("Suffix", "Not Configured")
342-
error = response.get("ErrorDocument", {}).get("Key", "Not Configured")
343-
344-
endpoint = BASE_WEBSITE_TEMPLATE.format(
345-
cluster=client.cluster, bucket=bucket
346-
)
347-
348-
print(f"Bucket {bucket}: Website configuration")
349-
print(f"Website endpoint: {endpoint}")
350-
print(f"Index document: {index}")
351-
print(f"Error document: {error}")
352-
353-
354268
def show_usage(get_client, args):
355269
"""
356270
Shows space used by all buckets in this cluster, and total space
@@ -437,30 +351,6 @@ def list_all_objects(get_client, args):
437351
sys.exit(0)
438352

439353

440-
def disable_static_site(get_client, args):
441-
"""
442-
Disables static site for a bucket
443-
"""
444-
parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " du"))
445-
446-
parser.add_argument(
447-
"bucket",
448-
metavar="BUCKET",
449-
type=str,
450-
nargs="?",
451-
help="The bucket to disable static site for.",
452-
)
453-
454-
parsed = parser.parse_args(args)
455-
client = get_client()
456-
457-
bucket = parsed.bucket
458-
459-
client.delete_bucket_website(Bucket=bucket)
460-
461-
print(f"Website configuration deleted for {parsed.bucket}")
462-
463-
464354
COMMAND_MAP = {
465355
"mb": create_bucket,
466356
"rb": delete_bucket,

linodecli/plugins/obj/website.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
"""
2+
The static website module of CLI Plugin for handling object storage
3+
"""
4+
5+
from argparse import ArgumentParser
6+
7+
from linodecli.plugins import inherit_plugin_args
8+
from linodecli.plugins.obj.config import BASE_WEBSITE_TEMPLATE, PLUGIN_BASE
9+
10+
11+
def enable_static_site(get_client, args):
12+
"""
13+
Turns a bucket into a static website
14+
"""
15+
parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " ws-create"))
16+
17+
parser.add_argument(
18+
"bucket",
19+
metavar="BUCKET",
20+
type=str,
21+
help="The bucket to turn into a static site",
22+
)
23+
parser.add_argument(
24+
"--ws-index",
25+
metavar="INDEX",
26+
required=True,
27+
type=str,
28+
help="The file to serve as the index of the website",
29+
)
30+
parser.add_argument(
31+
"--ws-error",
32+
metavar="ERROR",
33+
type=str,
34+
help="The file to serve as the error page of the website",
35+
)
36+
37+
parsed = parser.parse_args(args)
38+
client = get_client()
39+
bucket = parsed.bucket
40+
41+
# make the site
42+
print(f"Setting bucket {bucket} access control to be 'public-read'")
43+
44+
client.put_bucket_acl(
45+
Bucket=bucket,
46+
ACL="public-read",
47+
)
48+
49+
index_page = parsed.ws_index
50+
51+
ws_config = {"IndexDocument": {"Suffix": index_page}}
52+
if parsed.ws_error:
53+
ws_config["ErrorDocument"] = {"Key": parsed.ws_error}
54+
55+
client.put_bucket_website(
56+
Bucket=bucket,
57+
WebsiteConfiguration=ws_config,
58+
)
59+
60+
print(
61+
"Static site now available at "
62+
f"{BASE_WEBSITE_TEMPLATE.format(cluster=client.cluster, bucket=bucket)}"
63+
"\nIf you still can't access the website, please check the "
64+
"Access Control List setting of the website related objects (files) "
65+
"in your bucket."
66+
)
67+
68+
69+
def static_site_info(get_client, args):
70+
"""
71+
Returns info about a configured static site
72+
"""
73+
parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " ws-info"))
74+
75+
parser.add_argument(
76+
"bucket",
77+
metavar="BUCKET",
78+
type=str,
79+
help="The bucket to return static site information on.",
80+
)
81+
82+
parsed = parser.parse_args(args)
83+
client = get_client()
84+
85+
bucket = parsed.bucket
86+
87+
response = client.get_bucket_website(Bucket=bucket)
88+
89+
index = response.get("IndexDocument", {}).get("Suffix", "Not Configured")
90+
error = response.get("ErrorDocument", {}).get("Key", "Not Configured")
91+
92+
endpoint = BASE_WEBSITE_TEMPLATE.format(
93+
cluster=client.cluster, bucket=bucket
94+
)
95+
96+
print(f"Bucket {bucket}: Website configuration")
97+
print(f"Website endpoint: {endpoint}")
98+
print(f"Index document: {index}")
99+
print(f"Error document: {error}")
100+
101+
102+
def disable_static_site(get_client, args):
103+
"""
104+
Disables static site for a bucket
105+
"""
106+
parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " du"))
107+
108+
parser.add_argument(
109+
"bucket",
110+
metavar="BUCKET",
111+
type=str,
112+
nargs="?",
113+
help="The bucket to disable static site for.",
114+
)
115+
116+
parsed = parser.parse_args(args)
117+
client = get_client()
118+
119+
bucket = parsed.bucket
120+
121+
client.delete_bucket_website(Bucket=bucket)
122+
123+
print(f"Website configuration deleted for {parsed.bucket}")

0 commit comments

Comments
 (0)