|
10 | 10 |
|
11 | 11 | from jumpstarter.client import LeaseRequest
|
12 | 12 | from jumpstarter.common import MetadataFilter
|
| 13 | +from jumpstarter.v1 import jumpstarter_pb2, jumpstarter_pb2_grpc |
13 | 14 |
|
14 | 15 | from .common import CONFIG_PATH
|
15 | 16 | from .env import JMP_DRIVERS_ALLOW, JMP_ENDPOINT, JMP_TOKEN
|
@@ -47,21 +48,40 @@ class ClientConfigV1Alpha1(BaseModel):
|
47 | 48 | kind: Literal["ClientConfig"] = Field(default="ClientConfig")
|
48 | 49 | client: ClientConfigV1Alpha1Client = Field(default_factory=ClientConfigV1Alpha1Client)
|
49 | 50 |
|
| 51 | + async def channel(self): |
| 52 | + credentials = grpc.composite_channel_credentials( |
| 53 | + grpc.ssl_channel_credentials(), |
| 54 | + grpc.access_token_call_credentials(self.client.token), |
| 55 | + ) |
| 56 | + |
| 57 | + return grpc.aio.secure_channel(self.client.endpoint, credentials) |
| 58 | + |
50 | 59 | @contextmanager
|
51 | 60 | def lease(self, metadata_filter: MetadataFilter):
|
52 | 61 | with start_blocking_portal() as portal:
|
53 | 62 | with portal.wrap_async_context_manager(self.lease_async(metadata_filter, portal)) as lease:
|
54 | 63 | yield lease
|
55 | 64 |
|
| 65 | + def list_leases(self): |
| 66 | + with start_blocking_portal() as portal: |
| 67 | + return portal.call(self.list_leases_async) |
| 68 | + |
| 69 | + def release_lease(self, name): |
| 70 | + with start_blocking_portal() as portal: |
| 71 | + portal.call(self.release_lease_async, name) |
| 72 | + |
| 73 | + async def list_leases_async(self): |
| 74 | + stub = jumpstarter_pb2_grpc.ControllerServiceStub(await self.channel()) |
| 75 | + return (await stub.ListLeases(jumpstarter_pb2.ListLeasesRequest())).names |
| 76 | + |
| 77 | + async def release_lease_async(self, name): |
| 78 | + stub = jumpstarter_pb2_grpc.ControllerServiceStub(await self.channel()) |
| 79 | + await stub.ReleaseLease(jumpstarter_pb2.ReleaseLeaseRequest(name=name)) |
| 80 | + |
56 | 81 | @asynccontextmanager
|
57 | 82 | async def lease_async(self, metadata_filter: MetadataFilter, portal: BlockingPortal):
|
58 |
| - credentials = grpc.composite_channel_credentials( |
59 |
| - grpc.ssl_channel_credentials(), |
60 |
| - grpc.access_token_call_credentials(self.client.token), |
61 |
| - ) |
62 |
| - |
63 | 83 | async with LeaseRequest(
|
64 |
| - channel=grpc.aio.secure_channel(self.client.endpoint, credentials), |
| 84 | + channel=await self.channel(), |
65 | 85 | metadata_filter=metadata_filter,
|
66 | 86 | portal=portal,
|
67 | 87 | ) as lease:
|
|
0 commit comments