From e304cb9ef127cd7cf2297a24922705474b5eeb06 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 13 Sep 2022 17:35:06 +0200 Subject: [PATCH] Add an example on how to use the API --- example/configure_single_node.py | 83 ++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 example/configure_single_node.py diff --git a/example/configure_single_node.py b/example/configure_single_node.py new file mode 100644 index 0000000..b19159f --- /dev/null +++ b/example/configure_single_node.py @@ -0,0 +1,83 @@ +import garage_admin_sdk +from garage_admin_sdk.api import nodes_api, layout_api, key_api +from garage_admin_sdk.model.node_cluster_info import NodeClusterInfo +from garage_admin_sdk.model.layout_version import LayoutVersion +from garage_admin_sdk.model.add_key_request import AddKeyRequest +from garage_admin_sdk.model.update_key_request import UpdateKeyRequest +from garage_admin_sdk.model.update_key_request_allow import UpdateKeyRequestAllow + +configuration = garage_admin_sdk.Configuration( + host = "http://localhost:3903/v0", + access_token = "s3cr3t" +) + +with garage_admin_sdk.ApiClient(configuration) as api_client: + # --- NODE INFO --- + nodes = nodes_api.NodesApi(api_client) + status = nodes.get_nodes() + print(f"running garage {status.garage_version}, node_id {status.node}") + + # --- LAYOUT --- + layout = layout_api.LayoutApi(api_client) + + 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") + + layout.add_layout(request_body={ + f"{status.node}": NodeClusterInfo( + zone = "dc1", + capacity = 1, + tags = [ "dev" ], + ) + }) + 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 --- + keys = key_api.KeyApi(api_client) + kreq = AddKeyRequest(name="openapi") + kinfo = keys.add_key(add_key_request=kreq) + print(f"key {kinfo.name} added.") + + # --- UPDATE KEY --- + # not required here, just to demo the API + kinfo = keys.search_key("openapi") + + allow_create = UpdateKeyRequestAllow(create_bucket=True) + kreq = UpdateKeyRequest(allow=allow_create) + keys.update_key(kinfo.access_key_id, update_key_request=kreq) + print(f"key {kinfo.access_key_id} can now create buckets") + + # update key info + kinfo = keys.search_key("openapi") + 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 +""")