-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathssh_keys.py
More file actions
69 lines (53 loc) · 2.42 KB
/
ssh_keys.py
File metadata and controls
69 lines (53 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from gcore import Gcore
from gcore.pagination import SyncOffsetPage
from gcore.types.cloud import SSHKey, SSHKeyCreated
def main() -> None:
# TODO set API key before running
# api_key = os.environ["GCORE_API_KEY"]
# TODO set cloud project ID before running
# cloud_project_id = os.environ["GCORE_CLOUD_PROJECT_ID"]
# TODO set cloud region ID before running
# cloud_region_id = os.environ["GCORE_CLOUD_REGION_ID"]
gcore = Gcore(
# No need to explicitly pass to Gcore constructor if using environment variables
# api_key=api_key,
# cloud_project_id=cloud_project_id,
# cloud_region_id=cloud_region_id,
)
ssh_key = create_ssh_key(client=gcore)
list_ssh_keys(client=gcore)
get_ssh_key(client=gcore, ssh_key_id=ssh_key.id)
update_ssh_key(client=gcore, ssh_key_id=ssh_key.id)
delete_ssh_key(client=gcore, ssh_key_id=ssh_key.id)
def create_ssh_key(*, client: Gcore) -> SSHKeyCreated:
print("\n=== CREATE SSH KEY ===")
ssh_key = client.cloud.ssh_keys.create(name="gcore-go-example")
print(f"Created SSH key: ID={ssh_key.id}, name={ssh_key.name}")
print("========================")
return ssh_key
def list_ssh_keys(*, client: Gcore) -> SyncOffsetPage[SSHKey]:
print("\n=== LIST SSH KEYS ===")
ssh_keys = client.cloud.ssh_keys.list()
for count, ssh_key in enumerate(ssh_keys, 1):
print(f" {count}. SSH key: ID={ssh_key.id}, name={ssh_key.name}")
print("========================")
return ssh_keys
def get_ssh_key(*, client: Gcore, ssh_key_id: str) -> SSHKey:
print("\n=== GET SSH KEY ===")
ssh_key = client.cloud.ssh_keys.get(ssh_key_id=ssh_key_id)
print(f"SSH key: ID={ssh_key.id}, name={ssh_key.name}, fingerprint={ssh_key.fingerprint}")
print("========================")
return ssh_key
def update_ssh_key(*, client: Gcore, ssh_key_id: str) -> SSHKey:
print("\n=== UPDATE SSH KEY ===")
updated_ssh_key = client.cloud.ssh_keys.update(ssh_key_id=ssh_key_id, shared_in_project=True)
print(f"Updated SSH key: ID={updated_ssh_key.id}, name={updated_ssh_key.name}")
print("========================")
return updated_ssh_key
def delete_ssh_key(*, client: Gcore, ssh_key_id: str) -> None:
print("\n=== DELETE SSH KEY ===")
client.cloud.ssh_keys.delete(ssh_key_id=ssh_key_id)
print(f"Deleted SSH key: ID={ssh_key_id}")
print("========================")
if __name__ == "__main__":
main()