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

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
DAlperin committed Sep 19, 2023
1 parent e07d13f commit 1b7c880
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 69 deletions.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ require (
github.com/onsi/ginkgo/v2 v2.11.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-19 v0.3.2 // indirect
github.com/quic-go/qtls-go1-20 v0.2.2 // indirect
github.com/quic-go/quic-go v0.36.0 // indirect
github.com/quic-go/qtls-go1-20 v0.3.3 // indirect
github.com/quic-go/quic-go v0.38.1 // indirect
github.com/refraction-networking/utls v1.3.2 // indirect
github.com/superfly/graphql v0.2.4 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo=
github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A=
github.com/quic-go/qtls-go1-19 v0.3.2 h1:tFxjCFcTQzK+oMxG6Zcvp4Dq8dx4yD3dDiIiyc86Z5U=
github.com/quic-go/qtls-go1-19 v0.3.2/go.mod h1:ySOI96ew8lnoKPtSqx2BlI5wCpUVPT05RMAlajtnyOI=
github.com/quic-go/qtls-go1-20 v0.2.2 h1:WLOPx6OY/hxtTxKV1Zrq20FtXtDEkeY00CGQm8GEa3E=
github.com/quic-go/qtls-go1-20 v0.2.2/go.mod h1:JKtK6mjbAVcUTN/9jZpvLbGxvdWIKS8uT7EiStoU1SM=
github.com/quic-go/quic-go v0.36.0 h1:JIrO7p7Ug6hssFcARjWDiqS2RAKJHCiwPxBAA989rbI=
github.com/quic-go/quic-go v0.36.0/go.mod h1:zPetvwDlILVxt15n3hr3Gf/I3mDf7LpLKPhR4Ez0AZQ=
github.com/quic-go/qtls-go1-20 v0.3.3 h1:17/glZSLI9P9fDAeyCHBFSWSqJcwx1byhLwP5eUIDCM=
github.com/quic-go/qtls-go1-20 v0.3.3/go.mod h1:X9Nh97ZL80Z+bX/gUXMbipO6OxdiDi58b/fMC9mAL+k=
github.com/quic-go/quic-go v0.38.1 h1:M36YWA5dEhEeT+slOu/SwMEucbYd0YFidxG3KlGPZaE=
github.com/quic-go/quic-go v0.38.1/go.mod h1:ijnZM7JsFIkp4cRyjxJNIzdSfCLmUMg9wdyhGmg+SN4=
github.com/refraction-networking/utls v1.3.2 h1:o+AkWB57mkcoW36ET7uJ002CpBWHu0KPxi6vzxvPnv8=
github.com/refraction-networking/utls v1.3.2/go.mod h1:fmoaOww2bxzzEpIKOebIsnBvjQpqP7L2vcm/9KUfm/E=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
Expand Down
55 changes: 30 additions & 25 deletions internal/provider/machine_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,7 @@ func (r *flyMachineResource) Create(ctx context.Context, req resource.CreateRequ

machineAPI := apiv1.NewMachineAPI(r.config.httpClient, r.config.httpEndpoint)

var newMachine api.Machine
err := machineAPI.CreateMachine(createReq, data.App.ValueString(), &newMachine)
newMachine, err := machineAPI.CreateMachine(createReq, data.App.ValueString())
if err != nil {
resp.Diagnostics.AddError("Failed to create machine", err.Error())
return
Expand All @@ -325,33 +324,43 @@ func (r *flyMachineResource) Create(ctx context.Context, req resource.CreateRequ
tflog.Info(ctx, fmt.Sprintf("%+v", newMachine))

// env := utils.KVToTfMap(newMachine.Config.Env, types.StringType)
env, diags := types.MapValueFrom(ctx, types.StringType, newMachine.Config.Env)
env, diags := types.MapValueFrom(ctx, types.StringType, map[string]string{})
if newMachine.Config != nil {
env, diags = types.MapValueFrom(ctx, types.StringType, newMachine.Config.Env)
}
resp.Diagnostics.Append(diags...)

tfservices := ServicesToTfServices(newMachine.Config.Services)
tfservices := ServicesToTfServices([]api.MachineService{})
if newMachine.Config != nil {
tfservices = ServicesToTfServices(newMachine.Config.Services)
}

if data.Services == nil && len(tfservices) == 0 {
tfservices = nil
}

data = flyMachineResourceData{
Name: types.StringValue(newMachine.Name),
Region: types.StringValue(newMachine.Region),
Id: types.StringValue(newMachine.ID),
App: data.App,
PrivateIP: types.StringValue(newMachine.PrivateIP),
Image: types.StringValue(newMachine.Config.Image),
Cpus: types.Int64Value(int64(newMachine.Config.Guest.CPUs)),
MemoryMb: types.Int64Value(int64(newMachine.Config.Guest.MemoryMB)),
CpuType: types.StringValue(newMachine.Config.Guest.CPUKind),
Cmd: newMachine.Config.Init.Cmd,
Entrypoint: newMachine.Config.Init.Entrypoint,
Exec: newMachine.Config.Init.Exec,
Env: env,
Services: tfservices,
Name: types.StringValue(newMachine.Name),
Region: types.StringValue(newMachine.Region),
Id: types.StringValue(newMachine.ID),
App: data.App,
PrivateIP: types.StringValue(newMachine.PrivateIP),
Env: env,
Services: tfservices,
}

if len(newMachine.Config.Mounts) > 0 {
if newMachine.Config != nil {
data.Image = types.StringValue(newMachine.Config.Image)
data.Cpus = types.Int64Value(int64(newMachine.Config.Guest.CPUs))
data.MemoryMb = types.Int64Value(int64(newMachine.Config.Guest.MemoryMB))
data.CpuType = types.StringValue(newMachine.Config.Guest.CPUKind)
data.Cmd = newMachine.Config.Init.Cmd
data.Entrypoint = newMachine.Config.Init.Entrypoint
data.Exec = newMachine.Config.Init.Exec

}

if newMachine.Config != nil && len(newMachine.Config.Mounts) > 0 {
var tfmounts []TfMachineMount
for _, m := range newMachine.Config.Mounts {
tfmounts = append(tfmounts, TfMachineMount{
Expand Down Expand Up @@ -385,9 +394,7 @@ func (r *flyMachineResource) Read(ctx context.Context, req resource.ReadRequest,

machineAPI := apiv1.NewMachineAPI(r.config.httpClient, r.config.httpEndpoint)

var machine api.Machine

_, err := machineAPI.ReadMachine(data.App.ValueString(), data.Id.ValueString(), &machine)
machine, err := machineAPI.ReadMachine(data.App.ValueString(), data.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError("Failed to create machine", err.Error())
return
Expand Down Expand Up @@ -517,9 +524,7 @@ func (r *flyMachineResource) Update(ctx context.Context, req resource.UpdateRequ

machineApi := apiv1.NewMachineAPI(r.config.httpClient, r.config.httpEndpoint)

var updatedMachine api.Machine

err := machineApi.UpdateMachine(updateReq, state.App.ValueString(), state.Id.ValueString(), &updatedMachine)
updatedMachine, err := machineApi.UpdateMachine(updateReq, state.App.ValueString(), state.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError("Failed to update machine", err.Error())
return
Expand Down
81 changes: 46 additions & 35 deletions pkg/apiv1/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,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
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 @@ -49,58 +49,69 @@ func (a *MachineAPI) WaitForMachine(app string, id string, instanceID string) er
}

// CreateMachine takes a MachineCreateOrUpdateRequest and creates the requested machine in the given app and then writes the response into the `res` param
func (a *MachineAPI) CreateMachine(req api.Machine, app string, res *api.Machine) error {
if req.Config.Guest.CPUKind == "" {
req.Config.Guest.CPUKind = "shared"
}
if req.Config.Guest.CPUs == 0 {
req.Config.Guest.CPUs = 1
}
if req.Config.Guest.MemoryMB == 0 {
req.Config.Guest.MemoryMB = 256
func (a *MachineAPI) CreateMachine(req api.Machine, app string) (*api.Machine, error) {
var res api.Machine
if req.Config != nil && req.Config.Guest != nil {
if req.Config.Guest.CPUKind == "" {
req.Config.Guest.CPUKind = "shared"
}
if req.Config.Guest.CPUs == 0 {
req.Config.Guest.CPUs = 1
}
if req.Config.Guest.MemoryMB == 0 {
req.Config.Guest.MemoryMB = 256
}
}
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
return nil, err
}

if createResponse.StatusCode != http.StatusCreated && createResponse.StatusCode != http.StatusOK {
return errors.New(fmt.Sprintf("Create request failed: %s, %+v", createResponse.Status, createResponse))
return nil, errors.New(fmt.Sprintf("Create request failed: %s, %+v", createResponse.Status, createResponse))
}
return nil
return &res, nil
}

func (a *MachineAPI) UpdateMachine(req api.Machine, app string, id string, res *api.Machine) error {
if req.Config.Guest.CPUKind == "" {
req.Config.Guest.CPUKind = "shared"
}
if req.Config.Guest.CPUs == 0 {
req.Config.Guest.CPUs = 1
}
if req.Config.Guest.MemoryMB == 0 {
req.Config.Guest.MemoryMB = 256
func (a *MachineAPI) UpdateMachine(req api.Machine, app string, id string) (*api.Machine, error) {
var res api.Machine
if req.Config != nil && req.Config.Guest != nil {
if req.Config.Guest.CPUKind == "" {
req.Config.Guest.CPUKind = "shared"
}
if req.Config.Guest.CPUs == 0 {
req.Config.Guest.CPUs = 1
}
if req.Config.Guest.MemoryMB == 0 {
req.Config.Guest.MemoryMB = 256
}
}
lease, err := a.LockMachine(app, id, 30)
if err != nil {
return err
return nil, err
}
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))
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
return nil, err
}
err = a.ReleaseMachine(*lease, app, id)
if err != nil {
return err
return nil, err
}
if reqRes.StatusCode != http.StatusCreated && reqRes.StatusCode != http.StatusOK {
return errors.New(fmt.Sprintf("Update request failed: %s, %+v", reqRes.Status, reqRes))
return nil, errors.New(fmt.Sprintf("Update request failed: %s, %+v", reqRes.Status, reqRes))
}
return nil
return &res, nil
}

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

func (a *MachineAPI) DeleteMachine(app string, id string, maxRetries int) error {
Expand Down Expand Up @@ -138,7 +149,7 @@ 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,
Expand All @@ -147,17 +158,17 @@ func (a *MachineAPI) CreateVolume(ctx context.Context, name, app, region string,
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
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 1b7c880

Please sign in to comment.