ic_oss/
cluster.rs

1use candid::{Nat, Principal};
2use ic_agent::Agent;
3use ic_oss_types::{cluster::*, cose::Token};
4use serde_bytes::{ByteArray, ByteBuf};
5use std::{
6    collections::{BTreeMap, BTreeSet},
7    sync::Arc,
8};
9
10use crate::agent::{query_call, update_call};
11
12#[derive(Clone)]
13pub struct Client {
14    agent: Arc<Agent>,
15    cluster: Principal,
16}
17
18impl Client {
19    pub fn new(agent: Arc<Agent>, cluster: Principal) -> Client {
20        Client { agent, cluster }
21    }
22
23    /// the caller of agent should be canister controller
24    pub async fn admin_set_managers(&self, args: BTreeSet<Principal>) -> Result<(), String> {
25        update_call(&self.agent, &self.cluster, "admin_set_managers", (args,)).await?
26    }
27
28    /// the caller of agent should be canister manager
29    pub async fn admin_sign_access_token(&self, args: Token) -> Result<ByteBuf, String> {
30        update_call(
31            &self.agent,
32            &self.cluster,
33            "admin_sign_access_token",
34            (args,),
35        )
36        .await?
37    }
38
39    pub async fn admin_ed25519_access_token(&self, args: Token) -> Result<ByteBuf, String> {
40        update_call(
41            &self.agent,
42            &self.cluster,
43            "admin_ed25519_access_token",
44            (args,),
45        )
46        .await?
47    }
48
49    pub async fn admin_weak_access_token(
50        &self,
51        args: Token,
52        now_sec: u64,
53        expiration_sec: u64,
54    ) -> Result<ByteBuf, String> {
55        query_call(
56            &self.agent,
57            &self.cluster,
58            "admin_weak_access_token",
59            (args, now_sec, expiration_sec),
60        )
61        .await?
62    }
63
64    /// the caller of agent should be canister manager
65    pub async fn admin_attach_policies(&self, args: Token) -> Result<(), String> {
66        update_call(&self.agent, &self.cluster, "admin_attach_policies", (args,)).await?
67    }
68
69    /// the caller of agent should be canister manager
70    pub async fn admin_detach_policies(&self, args: Token) -> Result<(), String> {
71        update_call(&self.agent, &self.cluster, "admin_detach_policies", (args,)).await?
72    }
73
74    pub async fn access_token(&self, audience: Principal) -> Result<ByteBuf, String> {
75        update_call(&self.agent, &self.cluster, "access_token", (audience,)).await?
76    }
77
78    pub async fn ed25519_access_token(&self, audience: Principal) -> Result<ByteBuf, String> {
79        update_call(
80            &self.agent,
81            &self.cluster,
82            "ed25519_access_token",
83            (audience,),
84        )
85        .await?
86    }
87
88    pub async fn get_cluster_info(&self) -> Result<ClusterInfo, String> {
89        query_call(&self.agent, &self.cluster, "get_cluster_info", ()).await?
90    }
91
92    pub async fn get_bucket_wasm(&self, hash: ByteArray<32>) -> Result<WasmInfo, String> {
93        query_call(&self.agent, &self.cluster, "get_bucket_wasm", (hash,)).await?
94    }
95
96    pub async fn get_buckets(&self) -> Result<Vec<Principal>, String> {
97        query_call(&self.agent, &self.cluster, "get_buckets", ()).await?
98    }
99
100    pub async fn get_deployed_buckets(&self) -> Result<Vec<BucketDeploymentInfo>, String> {
101        query_call(&self.agent, &self.cluster, "get_deployed_buckets", ()).await?
102    }
103
104    pub async fn bucket_deployment_logs(
105        &self,
106        prev: Option<Nat>,
107        take: Option<Nat>,
108    ) -> Result<Vec<BucketDeploymentInfo>, String> {
109        query_call(
110            &self.agent,
111            &self.cluster,
112            "bucket_deployment_logs",
113            (prev, take),
114        )
115        .await?
116    }
117
118    pub async fn get_subject_policies(
119        &self,
120        subject: Principal,
121    ) -> Result<BTreeMap<Principal, String>, String> {
122        query_call(
123            &self.agent,
124            &self.cluster,
125            "get_subject_policies",
126            (subject,),
127        )
128        .await?
129    }
130
131    pub async fn get_subject_policies_for(
132        &self,
133        subject: Principal,
134        audience: Principal,
135    ) -> Result<String, String> {
136        query_call(
137            &self.agent,
138            &self.cluster,
139            "get_subject_policies_for",
140            (subject, audience),
141        )
142        .await?
143    }
144
145    pub async fn admin_add_wasm(
146        &self,
147        args: AddWasmInput,
148        force_prev_hash: Option<ByteArray<32>>,
149    ) -> Result<(), String> {
150        update_call(
151            &self.agent,
152            &self.cluster,
153            "admin_add_wasm",
154            (args, force_prev_hash),
155        )
156        .await?
157    }
158
159    pub async fn admin_deploy_bucket(
160        &self,
161        args: DeployWasmInput,
162        ignore_prev_hash: Option<ByteArray<32>>,
163    ) -> Result<(), String> {
164        update_call(
165            &self.agent,
166            &self.cluster,
167            "admin_deploy_bucket",
168            (args, ignore_prev_hash),
169        )
170        .await?
171    }
172
173    pub async fn admin_upgrade_all_buckets(&self, args: Option<ByteBuf>) -> Result<(), String> {
174        update_call(
175            &self.agent,
176            &self.cluster,
177            "admin_upgrade_all_buckets",
178            (args,),
179        )
180        .await?
181    }
182
183    pub async fn admin_batch_call_buckets(
184        &self,
185        buckets: BTreeSet<Principal>,
186        method: String,
187        args: Option<ByteBuf>,
188    ) -> Result<Vec<ByteBuf>, String> {
189        update_call(
190            &self.agent,
191            &self.cluster,
192            "admin_batch_call_buckets",
193            (buckets, method, args),
194        )
195        .await?
196    }
197
198    pub async fn admin_topup_all_buckets(&self) -> Result<u128, String> {
199        update_call(&self.agent, &self.cluster, "admin_topup_all_buckets", ()).await?
200    }
201}