Skip to content

Commit b348aec

Browse files
authored
fix: more metald cleanup (#3919)
1 parent f4183e2 commit b348aec

File tree

17 files changed

+587
-1543
lines changed

17 files changed

+587
-1543
lines changed

go/deploy/metald/client/client.go

Lines changed: 68 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88

99
"connectrpc.com/connect"
1010
"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"
1313
)
1414

1515
// This client provides a high-level interface for metald VM operations with proper authentication
@@ -22,14 +22,7 @@ type Config struct {
2222
// UserID is the user identifier for authentication
2323
UserID string
2424

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
3326

3427
// TLS configuration
3528
TLSMode string // "disabled", "file", or "spiffe"
@@ -46,7 +39,7 @@ type Config struct {
4639

4740
// Client provides a high-level interface to metald services
4841
type Client struct {
49-
vmService vmprovisionerv1connect.VmServiceClient
42+
vmService metaldv1connect.VmServiceClient
5043
tlsProvider tls.Provider
5144
tenantID string
5245
projectID string
@@ -92,25 +85,19 @@ func New(ctx context.Context, config Config) (*Client, error) {
9285

9386
// Add authentication and tenant isolation transport
9487
httpClient.Transport = &tenantTransport{
95-
Base: httpClient.Transport,
96-
ProjectID: config.ProjectID,
97-
TenantID: config.TenantID,
98-
EnvironmentID: config.EnvironmentID,
88+
Base: httpClient.Transport,
9989
}
10090

10191
// Create ConnectRPC client
102-
vmService := vmprovisionerv1connect.NewVmServiceClient(
92+
vmService := metaldv1connect.NewVmServiceClient(
10393
httpClient,
10494
config.ServerAddress,
10595
)
10696

10797
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,
114101
}, nil
115102
}
116103

@@ -123,167 +110,85 @@ func (c *Client) Close() error {
123110
}
124111

125112
// 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))
135115
if err != nil {
136116
return nil, fmt.Errorf("failed to create VM: %w", err)
137117
}
138-
139-
return &CreateVMResponse{
140-
VMID: resp.Msg.VmId,
141-
State: resp.Msg.State,
142-
}, nil
118+
return resp.Msg, nil
143119
}
144120

145121
// 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) {
150123

151124
resp, err := c.vmService.BootVm(ctx, connect.NewRequest(req))
152125
if err != nil {
153126
return nil, fmt.Errorf("failed to boot VM: %w", err)
154127
}
155-
156-
return &BootVMResponse{
157-
Success: resp.Msg.Success,
158-
State: resp.Msg.State,
159-
}, nil
128+
return resp.Msg, nil
160129
}
161130

162131
// 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))
171134
if err != nil {
172135
return nil, fmt.Errorf("failed to shutdown VM: %w", err)
173136
}
174-
175-
return &ShutdownVMResponse{
176-
Success: resp.Msg.Success,
177-
State: resp.Msg.State,
178-
}, nil
137+
return resp.Msg, nil
179138
}
180139

181140
// 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))
189143
if err != nil {
190144
return nil, fmt.Errorf("failed to delete VM: %w", err)
191145
}
192-
193-
return &DeleteVMResponse{
194-
Success: resp.Msg.Success,
195-
}, nil
146+
return resp.Msg, nil
196147
}
197148

198149
// 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) {
204151
resp, err := c.vmService.GetVmInfo(ctx, connect.NewRequest(req))
205152
if err != nil {
206153
return nil, fmt.Errorf("failed to get VM info: %w", err)
207154
}
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
216156
}
217157

218158
// 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))
226161
if err != nil {
227162
return nil, fmt.Errorf("failed to list VMs: %w", err)
228163
}
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
235165
}
236166

237167
// 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) {
243169
resp, err := c.vmService.PauseVm(ctx, connect.NewRequest(req))
244170
if err != nil {
245171
return nil, fmt.Errorf("failed to pause VM: %w", err)
246172
}
247-
248-
return &PauseVMResponse{
249-
Success: resp.Msg.Success,
250-
State: resp.Msg.State,
251-
}, nil
173+
return resp.Msg, nil
252174
}
253175

254176
// 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) {
260178
resp, err := c.vmService.ResumeVm(ctx, connect.NewRequest(req))
261179
if err != nil {
262180
return nil, fmt.Errorf("failed to resume VM: %w", err)
263181
}
264-
265-
return &ResumeVMResponse{
266-
Success: resp.Msg.Success,
267-
State: resp.Msg.State,
268-
}, nil
182+
return resp.Msg, nil
269183
}
270184

271185
// 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))
279188
if err != nil {
280189
return nil, fmt.Errorf("failed to reboot VM: %w", err)
281190
}
282-
283-
return &RebootVMResponse{
284-
Success: resp.Msg.Success,
285-
State: resp.Msg.State,
286-
}, nil
191+
return resp.Msg, nil
287192
}
288193

289194
// GetTenantID returns the tenant ID associated with this client
@@ -296,6 +201,42 @@ func (c *Client) GetServerAddress() string {
296201
return c.serverAddr
297202
}
298203

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+
299240
// tenantTransport adds authentication and tenant isolation headers to all requests
300241
type tenantTransport struct {
301242
Base http.RoundTripper

0 commit comments

Comments
 (0)