8
8
9
9
"connectrpc.com/connect"
10
10
"github.com/unkeyed/unkey/go/deploy/pkg/tls"
11
- vmprovisionerv1 "github.com/unkeyed/unkey/go/gen/proto/metal/vmprovisioner /v1"
12
- "github.com/unkeyed/unkey/go/gen/proto/metal/vmprovisioner/ v1/vmprovisionerv1connect "
11
+ metaldv1 "github.com/unkeyed/unkey/go/gen/proto/metald /v1"
12
+ "github.com/unkeyed/unkey/go/gen/proto/metald/ v1/metaldv1connect "
13
13
)
14
14
15
15
// This client provides a high-level interface for metald VM operations with proper authentication
@@ -22,14 +22,7 @@ type Config struct {
22
22
// UserID is the user identifier for authentication
23
23
UserID string
24
24
25
- // TenantID is the tenant identifier for data scoping
26
- TenantID string
27
-
28
- // ProjectID identifies the tenants project
29
- ProjectID string
30
-
31
- // EnvironmentID identifies the environment within the project
32
- EnvironmentID string
25
+ DeploymentID string
33
26
34
27
// TLS configuration
35
28
TLSMode string // "disabled", "file", or "spiffe"
@@ -46,7 +39,7 @@ type Config struct {
46
39
47
40
// Client provides a high-level interface to metald services
48
41
type Client struct {
49
- vmService vmprovisionerv1connect .VmServiceClient
42
+ vmService metaldv1connect .VmServiceClient
50
43
tlsProvider tls.Provider
51
44
tenantID string
52
45
projectID string
@@ -92,25 +85,19 @@ func New(ctx context.Context, config Config) (*Client, error) {
92
85
93
86
// Add authentication and tenant isolation transport
94
87
httpClient .Transport = & tenantTransport {
95
- Base : httpClient .Transport ,
96
- ProjectID : config .ProjectID ,
97
- TenantID : config .TenantID ,
98
- EnvironmentID : config .EnvironmentID ,
88
+ Base : httpClient .Transport ,
99
89
}
100
90
101
91
// Create ConnectRPC client
102
- vmService := vmprovisionerv1connect .NewVmServiceClient (
92
+ vmService := metaldv1connect .NewVmServiceClient (
103
93
httpClient ,
104
94
config .ServerAddress ,
105
95
)
106
96
107
97
return & Client {
108
- vmService : vmService ,
109
- tlsProvider : tlsProvider ,
110
- tenantID : config .TenantID ,
111
- projectID : config .ProjectID ,
112
- environmentID : config .EnvironmentID ,
113
- serverAddr : config .ServerAddress ,
98
+ vmService : vmService ,
99
+ tlsProvider : tlsProvider ,
100
+ serverAddr : config .ServerAddress ,
114
101
}, nil
115
102
}
116
103
@@ -123,167 +110,85 @@ func (c *Client) Close() error {
123
110
}
124
111
125
112
// CreateVM creates a new virtual machine with the specified configuration
126
- func (c * Client ) CreateVM (ctx context.Context , req * CreateVMRequest ) (* CreateVMResponse , error ) {
127
- // Convert to protobuf request
128
- pbReq := & vmprovisionerv1.CreateVmRequest {
129
- VmId : req .VMID ,
130
- Config : req .Config ,
131
- TenantId : c .tenantID ,
132
- }
133
-
134
- resp , err := c .vmService .CreateVm (ctx , connect .NewRequest (pbReq ))
113
+ func (c * Client ) CreateVM (ctx context.Context , req * metaldv1.CreateVmRequest ) (* metaldv1.CreateVmResponse , error ) {
114
+ resp , err := c .vmService .CreateVm (ctx , connect .NewRequest (req ))
135
115
if err != nil {
136
116
return nil , fmt .Errorf ("failed to create VM: %w" , err )
137
117
}
138
-
139
- return & CreateVMResponse {
140
- VMID : resp .Msg .VmId ,
141
- State : resp .Msg .State ,
142
- }, nil
118
+ return resp .Msg , nil
143
119
}
144
120
145
121
// BootVM starts a created virtual machine
146
- func (c * Client ) BootVM (ctx context.Context , vmID string ) (* BootVMResponse , error ) {
147
- req := & vmprovisionerv1.BootVmRequest {
148
- VmId : vmID ,
149
- }
122
+ func (c * Client ) BootVM (ctx context.Context , req * metaldv1.BootVmRequest ) (* metaldv1.BootVmResponse , error ) {
150
123
151
124
resp , err := c .vmService .BootVm (ctx , connect .NewRequest (req ))
152
125
if err != nil {
153
126
return nil , fmt .Errorf ("failed to boot VM: %w" , err )
154
127
}
155
-
156
- return & BootVMResponse {
157
- Success : resp .Msg .Success ,
158
- State : resp .Msg .State ,
159
- }, nil
128
+ return resp .Msg , nil
160
129
}
161
130
162
131
// ShutdownVM gracefully stops a running virtual machine
163
- func (c * Client ) ShutdownVM (ctx context.Context , req * ShutdownVMRequest ) (* ShutdownVMResponse , error ) {
164
- pbReq := & vmprovisionerv1.ShutdownVmRequest {
165
- VmId : req .VMID ,
166
- Force : req .Force ,
167
- TimeoutSeconds : int32 (req .TimeoutSeconds ),
168
- }
169
-
170
- resp , err := c .vmService .ShutdownVm (ctx , connect .NewRequest (pbReq ))
132
+ func (c * Client ) ShutdownVM (ctx context.Context , req * metaldv1.ShutdownVmRequest ) (* metaldv1.ShutdownVmResponse , error ) {
133
+ resp , err := c .vmService .ShutdownVm (ctx , connect .NewRequest (req ))
171
134
if err != nil {
172
135
return nil , fmt .Errorf ("failed to shutdown VM: %w" , err )
173
136
}
174
-
175
- return & ShutdownVMResponse {
176
- Success : resp .Msg .Success ,
177
- State : resp .Msg .State ,
178
- }, nil
137
+ return resp .Msg , nil
179
138
}
180
139
181
140
// DeleteVM removes a virtual machine
182
- func (c * Client ) DeleteVM (ctx context.Context , req * DeleteVMRequest ) (* DeleteVMResponse , error ) {
183
- pbReq := & vmprovisionerv1.DeleteVmRequest {
184
- VmId : req .VMID ,
185
- Force : req .Force ,
186
- }
187
-
188
- resp , err := c .vmService .DeleteVm (ctx , connect .NewRequest (pbReq ))
141
+ func (c * Client ) DeleteVM (ctx context.Context , req * metaldv1.DeleteVmRequest ) (* metaldv1.DeleteVmResponse , error ) {
142
+ resp , err := c .vmService .DeleteVm (ctx , connect .NewRequest (req ))
189
143
if err != nil {
190
144
return nil , fmt .Errorf ("failed to delete VM: %w" , err )
191
145
}
192
-
193
- return & DeleteVMResponse {
194
- Success : resp .Msg .Success ,
195
- }, nil
146
+ return resp .Msg , nil
196
147
}
197
148
198
149
// GetVMInfo retrieves detailed information about a virtual machine
199
- func (c * Client ) GetVMInfo (ctx context.Context , vmID string ) (* VMInfo , error ) {
200
- req := & vmprovisionerv1.GetVmInfoRequest {
201
- VmId : vmID ,
202
- }
203
-
150
+ func (c * Client ) GetVMInfo (ctx context.Context , req * metaldv1.GetVmInfoRequest ) (* metaldv1.GetVmInfoResponse , error ) {
204
151
resp , err := c .vmService .GetVmInfo (ctx , connect .NewRequest (req ))
205
152
if err != nil {
206
153
return nil , fmt .Errorf ("failed to get VM info: %w" , err )
207
154
}
208
-
209
- return & VMInfo {
210
- VMID : resp .Msg .VmId ,
211
- State : resp .Msg .State ,
212
- Config : resp .Msg .Config ,
213
- Metrics : resp .Msg .Metrics ,
214
- NetworkInfo : resp .Msg .NetworkInfo ,
215
- }, nil
155
+ return resp .Msg , nil
216
156
}
217
157
218
158
// ListVMs retrieves a list of virtual machines for the authenticated customer
219
- func (c * Client ) ListVMs (ctx context.Context , req * ListVMsRequest ) (* ListVMsResponse , error ) {
220
- pbReq := & vmprovisionerv1.ListVmsRequest {
221
- PageSize : req .PageSize ,
222
- PageToken : req .PageToken ,
223
- }
224
-
225
- resp , err := c .vmService .ListVms (ctx , connect .NewRequest (pbReq ))
159
+ func (c * Client ) ListVMs (ctx context.Context , req * metaldv1.ListVmsRequest ) (* metaldv1.ListVmsResponse , error ) {
160
+ resp , err := c .vmService .ListVms (ctx , connect .NewRequest (req ))
226
161
if err != nil {
227
162
return nil , fmt .Errorf ("failed to list VMs: %w" , err )
228
163
}
229
-
230
- return & ListVMsResponse {
231
- VMs : resp .Msg .Vms ,
232
- NextPageToken : resp .Msg .NextPageToken ,
233
- TotalCount : resp .Msg .TotalCount ,
234
- }, nil
164
+ return resp .Msg , nil
235
165
}
236
166
237
167
// PauseVM pauses a running virtual machine
238
- func (c * Client ) PauseVM (ctx context.Context , vmID string ) (* PauseVMResponse , error ) {
239
- req := & vmprovisionerv1.PauseVmRequest {
240
- VmId : vmID ,
241
- }
242
-
168
+ func (c * Client ) PauseVM (ctx context.Context , req * metaldv1.PauseVmRequest ) (* metaldv1.PauseVmResponse , error ) {
243
169
resp , err := c .vmService .PauseVm (ctx , connect .NewRequest (req ))
244
170
if err != nil {
245
171
return nil , fmt .Errorf ("failed to pause VM: %w" , err )
246
172
}
247
-
248
- return & PauseVMResponse {
249
- Success : resp .Msg .Success ,
250
- State : resp .Msg .State ,
251
- }, nil
173
+ return resp .Msg , nil
252
174
}
253
175
254
176
// ResumeVM resumes a paused virtual machine
255
- func (c * Client ) ResumeVM (ctx context.Context , vmID string ) (* ResumeVMResponse , error ) {
256
- req := & vmprovisionerv1.ResumeVmRequest {
257
- VmId : vmID ,
258
- }
259
-
177
+ func (c * Client ) ResumeVM (ctx context.Context , req * metaldv1.ResumeVmRequest ) (* metaldv1.ResumeVmResponse , error ) {
260
178
resp , err := c .vmService .ResumeVm (ctx , connect .NewRequest (req ))
261
179
if err != nil {
262
180
return nil , fmt .Errorf ("failed to resume VM: %w" , err )
263
181
}
264
-
265
- return & ResumeVMResponse {
266
- Success : resp .Msg .Success ,
267
- State : resp .Msg .State ,
268
- }, nil
182
+ return resp .Msg , nil
269
183
}
270
184
271
185
// RebootVM restarts a virtual machine
272
- func (c * Client ) RebootVM (ctx context.Context , req * RebootVMRequest ) (* RebootVMResponse , error ) {
273
- pbReq := & vmprovisionerv1.RebootVmRequest {
274
- VmId : req .VMID ,
275
- Force : req .Force ,
276
- }
277
-
278
- resp , err := c .vmService .RebootVm (ctx , connect .NewRequest (pbReq ))
186
+ func (c * Client ) RebootVM (ctx context.Context , req * metaldv1.RebootVmRequest ) (* metaldv1.RebootVmResponse , error ) {
187
+ resp , err := c .vmService .RebootVm (ctx , connect .NewRequest (req ))
279
188
if err != nil {
280
189
return nil , fmt .Errorf ("failed to reboot VM: %w" , err )
281
190
}
282
-
283
- return & RebootVMResponse {
284
- Success : resp .Msg .Success ,
285
- State : resp .Msg .State ,
286
- }, nil
191
+ return resp .Msg , nil
287
192
}
288
193
289
194
// GetTenantID returns the tenant ID associated with this client
@@ -296,6 +201,42 @@ func (c *Client) GetServerAddress() string {
296
201
return c .serverAddr
297
202
}
298
203
204
+ // CreateDeployment creates a new deployment with multiple VMs
205
+ func (c * Client ) CreateDeployment (ctx context.Context , req * metaldv1.CreateDeploymentRequest ) (* metaldv1.CreateDeploymentResponse , error ) {
206
+ resp , err := c .vmService .CreateDeployment (ctx , connect .NewRequest (req ))
207
+ if err != nil {
208
+ return nil , fmt .Errorf ("failed to create deployment: %w" , err )
209
+ }
210
+ return resp .Msg , nil
211
+ }
212
+
213
+ // UpdateDeployment updates an existing deployment
214
+ func (c * Client ) UpdateDeployment (ctx context.Context , req * metaldv1.UpdateDeploymentRequest ) (* metaldv1.UpdateDeploymentResponse , error ) {
215
+ resp , err := c .vmService .UpdateDeployment (ctx , connect .NewRequest (req ))
216
+ if err != nil {
217
+ return nil , fmt .Errorf ("failed to update deployment: %w" , err )
218
+ }
219
+ return resp .Msg , nil
220
+ }
221
+
222
+ // DeleteDeployment deletes an existing deployment
223
+ func (c * Client ) DeleteDeployment (ctx context.Context , req * metaldv1.DeleteDeploymentRequest ) (* metaldv1.DeleteDeploymentResponse , error ) {
224
+ resp , err := c .vmService .DeleteDeployment (ctx , connect .NewRequest (req ))
225
+ if err != nil {
226
+ return nil , fmt .Errorf ("failed to delete deployment: %w" , err )
227
+ }
228
+ return resp .Msg , nil
229
+ }
230
+
231
+ // GetDeployment retrieves information about a deployment
232
+ func (c * Client ) GetDeployment (ctx context.Context , req * metaldv1.GetDeploymentRequest ) (* metaldv1.GetDeploymentResponse , error ) {
233
+ resp , err := c .vmService .GetDeployment (ctx , connect .NewRequest (req ))
234
+ if err != nil {
235
+ return nil , fmt .Errorf ("failed to get deployment: %w" , err )
236
+ }
237
+ return resp .Msg , nil
238
+ }
239
+
299
240
// tenantTransport adds authentication and tenant isolation headers to all requests
300
241
type tenantTransport struct {
301
242
Base http.RoundTripper
0 commit comments