Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Futz with pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
DAlperin committed Sep 19, 2023
1 parent 00902be commit e07d13f
Showing 1 changed file with 14 additions and 107 deletions.
121 changes: 14 additions & 107 deletions pkg/apiv1/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,99 +19,6 @@ type MachineAPI struct {
endpoint string
}

//type MachineMount struct {
// Encrypted bool `json:"encrypted,omitempty"`
// Path string `json:"path"`
// SizeGb int `json:"size_gb,omitempty"`
// Volume string `json:"volume"`
//}
//
//type Port struct {
// Port int64 `json:"port"`
// Handlers []string `json:"handlers"`
//}
//
//type Service struct {
// Ports []Port `json:"ports"`
// Protocol string `json:"protocol"`
// InternalPort int64 `json:"internal_port"`
//}
//
//type InitConfig struct {
// Cmd []string `json:"cmd,omitempty"`
// Entrypoint []string `json:"entrypoint,omitempty"`
// Exec []string `json:"exec,omitempty"`
//}
//
//type MachineConfig struct {
// Image string `json:"image"`
// Env map[string]string `json:"env"`
// Init InitConfig `json:"init,omitempty"`
// Mounts []MachineMount `json:"mounts,omitempty"`
// Services []Service `json:"services"`
// Guest GuestConfig `json:"guest,omitempty"`
//}
//
//type GuestConfig struct {
// Cpus int `json:"cpus,omitempty"`
// MemoryMb int `json:"memory_mb,omitempty"`
// CpuType string `json:"cpu_kind,omitempty"`
//}
//
//type MachineCreateOrUpdateRequest struct {
// Name string `json:"name"`
// Region string `json:"region"`
// Config MachineConfig `json:"config"`
//}
//
//type MachineResponse struct {
// ID string `json:"id"`
// Name string `json:"name"`
// State string `json:"state"`
// Region string `json:"region"`
// InstanceID string `json:"instance_id"`
// PrivateIP string `json:"private_ip"`
// Config struct {
// Env map[string]string `json:"env"`
// Init struct {
// Exec []string `json:"exec"`
// Entrypoint []string `json:"entrypoint"`
// Cmd []string `json:"cmd"`
// //Tty bool `json:"tty"`
// } `json:"init"`
// Image string `json:"image"`
// Metadata interface{} `json:"metadata"`
// Restart struct {
// Policy string `json:"policy"`
// } `json:"restart"`
// Services []Service `json:"services"`
// Mounts []MachineMount `json:"mounts"`
// Guest struct {
// CPUKind string `json:"cpu_kind"`
// Cpus int `json:"cpus"`
// MemoryMb int `json:"memory_mb"`
// } `json:"guest"`
// } `json:"config"`
// ImageRef struct {
// Registry string `json:"registry"`
// Repository string `json:"repository"`
// Tag string `json:"tag"`
// Digest string `json:"digest"`
// Labels struct {
// } `json:"labels"`
// } `json:"image_ref"`
// CreatedAt time.Time `json:"created_at"`
//}
//
//type MachineLease struct {
// Status string `json:"status"`
// Data struct {
// Nonce string `json:"nonce"`
// ExpiresAt int64 `json:"expires_at"`
// Owner string `json:"owner"`
// }
//}

func NewMachineAPI(httpClient *hreq.Client, endpoint string) *MachineAPI {
return &MachineAPI{
httpClient: httpClient,
Expand All @@ -120,12 +27,12 @@ func NewMachineAPI(httpClient *hreq.Client, endpoint string) *MachineAPI {
}

func (a *MachineAPI) LockMachine(app string, id string, timeout int) (*api.MachineLease, error) {
var res api.MachineLease
_, err := a.httpClient.R().SetResult(&res).Post(fmt.Sprintf("http://%s/v1/apps/%s/machines/%s/lease/?ttl=%d", a.endpoint, app, id, timeout))
var res *api.MachineLease
_, err := a.httpClient.R().SetSuccessResult(&res).Post(fmt.Sprintf("http://%s/v1/apps/%s/machines/%s/lease/?ttl=%d", a.endpoint, app, id, timeout))
if err != nil {
return nil, err
}
return &res, nil
return res, nil
}

func (a *MachineAPI) ReleaseMachine(lease api.MachineLease, app string, id string) error {
Expand All @@ -152,7 +59,7 @@ func (a *MachineAPI) CreateMachine(req api.Machine, app string, res *api.Machine
if req.Config.Guest.MemoryMB == 0 {
req.Config.Guest.MemoryMB = 256
}
createResponse, err := a.httpClient.R().SetBody(req).SetResult(res).Post(fmt.Sprintf("http://%s/v1/apps/%s/machines", a.endpoint, app))
createResponse, err := a.httpClient.R().SetBody(req).SetSuccessResult(res).Post(fmt.Sprintf("http://%s/v1/apps/%s/machines", a.endpoint, app))

if err != nil {
return err
Expand All @@ -178,7 +85,7 @@ func (a *MachineAPI) UpdateMachine(req api.Machine, app string, id string, res *
if err != nil {
return err
}
reqRes, err := a.httpClient.R().SetBody(req).SetResult(res).SetHeader(NonceHeader, lease.Data.Nonce).Post(fmt.Sprintf("http://%s/v1/apps/%s/machines/%s", a.endpoint, app, id))
reqRes, err := a.httpClient.R().SetBody(req).SetSuccessResult(res).SetHeader(NonceHeader, lease.Data.Nonce).Post(fmt.Sprintf("http://%s/v1/apps/%s/machines/%s", a.endpoint, app, id))
if err != nil {
return err
}
Expand All @@ -193,14 +100,14 @@ func (a *MachineAPI) UpdateMachine(req api.Machine, app string, id string, res *
}

func (a *MachineAPI) ReadMachine(app string, id string, res *api.Machine) (*hreq.Response, error) {
return a.httpClient.R().SetResult(res).Get(fmt.Sprintf("http://%s/v1/apps/%s/machines/%s", a.endpoint, app, id))
return a.httpClient.R().SetSuccessResult(res).Get(fmt.Sprintf("http://%s/v1/apps/%s/machines/%s", a.endpoint, app, id))
}

func (a *MachineAPI) DeleteMachine(app string, id string, maxRetries int) error {
deleted := false
for i := 0; i < maxRetries; i++ {
var machine api.Machine
readResponse, err := a.httpClient.R().SetResult(&machine).Get(fmt.Sprintf("http://%s/v1/apps/%s/machines/%s", a.endpoint, app, id))
var machine *api.Machine
readResponse, err := a.httpClient.R().SetSuccessResult(&machine).Get(fmt.Sprintf("http://%s/v1/apps/%s/machines/%s", a.endpoint, app, id))
if err != nil {
return err
}
Expand Down Expand Up @@ -231,26 +138,26 @@ func (a *MachineAPI) DeleteMachine(app string, id string, maxRetries int) error
}

func (a *MachineAPI) CreateVolume(ctx context.Context, name, app, region string, size int) (*api.Volume, error) {
var res api.Volume
var res *api.Volume
_, err := a.httpClient.R().SetContext(ctx).SetBody(api.CreateVolumeRequest{
Name: name,
Region: region,
SizeGb: &size,
}).SetResult(&res).Post(fmt.Sprintf("http://%s/v1/apps/%s/volumes", a.endpoint, app))
}).SetSuccessResult(&res).Post(fmt.Sprintf("http://%s/v1/apps/%s/volumes", a.endpoint, app))
if err != nil {
return nil, err
}
return &res, nil
return res, nil
}

func (a *MachineAPI) GetVolume(ctx context.Context, id, app string) (*api.Volume, error) {
var res api.Volume
_, err := a.httpClient.R().SetContext(ctx).SetResult(&res).Get(fmt.Sprintf("http://%s/v1/apps/%s/volumes/%s", a.endpoint, app, id))
var res *api.Volume
_, err := a.httpClient.R().SetContext(ctx).SetSuccessResult(&res).Get(fmt.Sprintf("http://%s/v1/apps/%s/volumes/%s", a.endpoint, app, id))
if err != nil {
return nil, err
}

return &res, nil
return res, nil
}

func (a *MachineAPI) DeleteVolume(ctx context.Context, id, app string) error {
Expand Down

0 comments on commit e07d13f

Please sign in to comment.