|
1 | 1 | import logging
|
2 | 2 | import logging.config
|
3 |
| -from typing import Optional |
| 3 | +from typing import Optional, Union |
4 | 4 |
|
5 | 5 | import boto3
|
6 | 6 | from boto3.dynamodb.conditions import Key
|
@@ -133,25 +133,35 @@ def delete_cluster(dynamodb_resource, cluster: Cluster) -> None:
|
133 | 133 | table.delete_item(Key={"name": cluster.name})
|
134 | 134 |
|
135 | 135 |
|
136 |
| -def _update_cluster_claim(dynamodb_resource, cluster: Cluster, new_value: int) -> None: |
| 136 | +def _update_cluster_field( |
| 137 | + dynamodb_resource, cluster: Cluster, update_field: str, new_value: Union[int, str] |
| 138 | +) -> None: |
137 | 139 | stored_cluster = get_cluster_by_name(dynamodb_resource, cluster.name)
|
138 | 140 | if not stored_cluster:
|
139 |
| - raise ClusterNotRegistered(f"Cluster {cluster} is not registered - cannot claim it") |
| 141 | + raise ClusterNotRegistered(f"Cluster {cluster} is not registered - cannot update it") |
140 | 142 |
|
141 |
| - if stored_cluster.get("provisioning_launched") == new_value: |
142 |
| - raise ClusterAlreadyInClaimState(f"Cluster {cluster} already has claim state {new_value}") |
| 143 | + if update_field == "provisioning_launched" and stored_cluster.get(update_field) == new_value: |
| 144 | + raise ClusterAlreadyInClaimState( |
| 145 | + f"Cluster {cluster} already has {update_field} set to {new_value}" |
| 146 | + ) |
143 | 147 |
|
144 | 148 | table = dynamodb_resource.Table(CLUSTER_TABLE_NAME)
|
145 | 149 | table.update_item(
|
146 | 150 | Key={"name": cluster.name},
|
147 |
| - UpdateExpression="SET provisioning_launched = :claim", |
148 |
| - ExpressionAttributeValues={":claim": new_value}, |
| 151 | + UpdateExpression=f"SET {update_field} = :new_value", |
| 152 | + ExpressionAttributeValues={":new_value": new_value}, |
149 | 153 | )
|
150 | 154 |
|
151 | 155 |
|
152 | 156 | def claim_cluster(dynamodb_resource, cluster: Cluster) -> None:
|
153 |
| - _update_cluster_claim(dynamodb_resource, cluster, 1) |
| 157 | + _update_cluster_field(dynamodb_resource, cluster, "provisioning_launched", 1) |
154 | 158 |
|
155 | 159 |
|
156 | 160 | def release_cluster(dynamodb_resource, cluster: Cluster) -> None:
|
157 |
| - _update_cluster_claim(dynamodb_resource, cluster, 0) |
| 161 | + _update_cluster_field(dynamodb_resource, cluster, "provisioning_launched", 0) |
| 162 | + |
| 163 | + |
| 164 | +def set_cluster_sim_user_ssh_key( |
| 165 | + dynamodb_resource, cluster: Cluster, sim_user_ssh_key: str |
| 166 | +) -> None: |
| 167 | + _update_cluster_field(dynamodb_resource, cluster, "sim_pubkey", sim_user_ssh_key) |
0 commit comments