garage-admin-sdk-generator/example/python/configure_single_node.py

81 lines
2.4 KiB
Python
Raw Normal View History

2022-09-13 15:35:06 +00:00
import garage_admin_sdk
2022-11-12 21:48:50 +00:00
from garage_admin_sdk.apis import *
from garage_admin_sdk.models import *
2022-09-13 15:35:06 +00:00
configuration = garage_admin_sdk.Configuration(
2023-11-22 17:49:08 +00:00
host = "http://localhost:3903/v1",
2022-09-13 15:35:06 +00:00
access_token = "s3cr3t"
)
with garage_admin_sdk.ApiClient(configuration) as api_client:
# --- NODE INFO ---
2022-11-12 21:48:50 +00:00
nodes = NodesApi(api_client)
2022-09-13 15:35:06 +00:00
status = nodes.get_nodes()
print(f"running garage {status.garage_version}, node_id {status.node}")
# --- LAYOUT ---
2022-11-12 21:48:50 +00:00
layout = LayoutApi(api_client)
2022-09-13 15:35:06 +00:00
current = layout.get_layout()
dirty_node = len(current.roles) != 0 or \
len(current.staged_role_changes) != 0
if dirty_node:
print("this node is dirty, we don't know how to configure it, exiting...")
exit(1)
print("it seems to be a fresh node, continuing the configuration")
2023-11-22 17:49:08 +00:00
layout.add_layout([
NodeRoleChange(
id = status.node,
2022-09-13 15:35:06 +00:00
zone = "dc1",
2023-11-22 17:49:08 +00:00
capacity = 1000000000,
2022-09-13 15:35:06 +00:00
tags = [ "dev" ],
)
2023-11-22 17:49:08 +00:00
])
2022-09-13 15:35:06 +00:00
print("added myself to the layout")
current = layout.get_layout()
assert(len(current.staged_role_changes) == 1)
print(f"change has been staged successfully: {current.staged_role_changes}")
newl = LayoutVersion(version = current.version + 1)
layout.apply_layout(layout_version = newl)
print(f"new layout has been applied")
current = layout.get_layout()
assert(len(current.roles) == 1)
print(f"new layout is live: {current.roles}")
# --- CREATE KEY ---
2022-11-12 21:48:50 +00:00
keys = KeyApi(api_client)
2022-09-13 15:35:06 +00:00
kreq = AddKeyRequest(name="openapi")
kinfo = keys.add_key(kreq)
2022-09-13 15:35:06 +00:00
print(f"key {kinfo.name} added.")
# --- UPDATE KEY ---
# not required here, just to demo the API
2023-11-22 20:18:47 +00:00
kinfo = keys.get_key(search="openapi")
2022-09-13 15:35:06 +00:00
allow_create = UpdateKeyRequestAllow(create_bucket=True)
kreq = UpdateKeyRequest(allow=allow_create)
keys.update_key(kinfo.access_key_id, kreq)
2022-09-13 15:35:06 +00:00
print(f"key {kinfo.access_key_id} can now create buckets")
# update key info
2023-11-22 20:18:47 +00:00
kinfo = keys.get_key(search="openapi", show_secret_key="true")
2022-09-13 15:35:06 +00:00
print(f"key permissions: {kinfo.permissions}")
# ---- THE END ---
print(f"""
Use your key as follow now:
export AWS_ACCESS_KEY_ID={kinfo.access_key_id}
export AWS_SECRET_ACCESS_KEY={kinfo.secret_access_key}
export AWS_REGION=garage
aws --endpoint http://localhost:3900 s3 mb s3://hello-world
aws --endpoint http://localhost:3900 s3 ls
""")