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

Commit

Permalink
Make volumes use flaps
Browse files Browse the repository at this point in the history
  • Loading branch information
DAlperin committed Sep 19, 2023
1 parent 30d40d5 commit 00902be
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 45 deletions.
35 changes: 16 additions & 19 deletions internal/provider/volume_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package provider

import (
"context"
"github.com/fly-apps/terraform-provider-fly/pkg/apiv1"
"regexp"

basegql "github.com/Khan/genqlient/graphql"
"github.com/fly-apps/terraform-provider-fly/graphql"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand All @@ -18,7 +17,7 @@ var _ datasource.DataSource = &volumeDataSourceType{}
var _ datasource.DataSourceWithConfigure = &appDataSourceType{}

type volumeDataSourceType struct {
client *basegql.Client
config ProviderConfig
}

func NewVolumeDataSource() datasource.DataSource {
Expand All @@ -33,9 +32,7 @@ func (d *volumeDataSourceType) Configure(_ context.Context, req datasource.Confi
if req.ProviderData == nil {
return
}

config := req.ProviderData.(ProviderConfig)
d.client = config.gqclient
d.config = req.ProviderData.(ProviderConfig)
}

// Matches Schema
Expand Down Expand Up @@ -87,27 +84,27 @@ func (d *volumeDataSourceType) Read(ctx context.Context, req datasource.ReadRequ
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)

// strip leading vol_ off name
internalId := data.Id.ValueString()[4:]
id := data.Id.ValueString()
// New flaps based volumes don't have this prefix I'm pretty sure
if id[:4] == "vol_" {
// strip leading vol_ off name
id = id[4:]
}
app := data.Appid.ValueString()

query, err := graphql.VolumeQuery(ctx, *d.client, app, internalId)
machineAPI := apiv1.NewMachineAPI(d.config.httpClient, d.config.httpEndpoint)
query, err := machineAPI.GetVolume(ctx, id, app)
if err != nil {
resp.Diagnostics.AddError("Query failed", err.Error())
return
}

// this query will currently still return success if it finds nothing, so check it:
if query.App.Volume.Id == "" {
resp.Diagnostics.AddError("Query failed", "Could not find matching volume")
}

data = volumeDataSourceOutput{
Id: types.StringValue(query.App.Volume.Id),
Name: types.StringValue(query.App.Volume.Name),
Size: types.Int64Value(int64(query.App.Volume.SizeGb)),
Appid: data.Appid,
Region: types.StringValue(query.App.Volume.Region),
Id: types.StringValue(query.ID),
Name: types.StringValue(query.Name),
Size: types.Int64Value(int64(query.SizeGb)),
Appid: types.StringValue(data.Appid.ValueString()),
Region: types.StringValue(query.Region),
}

if resp.Diagnostics.HasError() {
Expand Down
50 changes: 24 additions & 26 deletions internal/provider/volume_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package provider
import (
"context"
"fmt"
"github.com/fly-apps/terraform-provider-fly/pkg/apiv1"
"regexp"
"strings"

basegql "github.com/Khan/genqlient/graphql"
"github.com/fly-apps/terraform-provider-fly/graphql"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand All @@ -22,7 +21,7 @@ var _ resource.ResourceWithConfigure = &flyVolumeResource{}
var _ resource.ResourceWithImportState = &flyVolumeResource{}

type flyVolumeResource struct {
client *basegql.Client
config ProviderConfig
}

func NewVolumeResource() resource.Resource {
Expand All @@ -37,9 +36,7 @@ func (r *flyVolumeResource) Configure(_ context.Context, req resource.ConfigureR
if req.ProviderData == nil {
return
}

config := req.ProviderData.(ProviderConfig)
r.client = config.gqclient
r.config = req.ProviderData.(ProviderConfig)
}

type flyVolumeResourceData struct {
Expand Down Expand Up @@ -91,18 +88,19 @@ func (r *flyVolumeResource) Create(ctx context.Context, req resource.CreateReque
diags := req.Plan.Get(ctx, &data)
resp.Diagnostics.Append(diags...)

q, err := graphql.CreateVolume(ctx, *r.client, data.Appid.ValueString(), data.Name.ValueString(), data.Region.ValueString(), int(data.Size.ValueInt64()))
machineAPI := apiv1.NewMachineAPI(r.config.httpClient, r.config.httpEndpoint)
q, err := machineAPI.CreateVolume(ctx, data.Appid.ValueString(), data.Name.ValueString(), data.Region.ValueString(), int(data.Size.ValueInt64()))
if err != nil {
resp.Diagnostics.AddError("Failed to create volume", err.Error())
return
}

data = flyVolumeResourceData{
Id: types.StringValue(q.CreateVolume.Volume.Id),
Name: types.StringValue(q.CreateVolume.Volume.Name),
Size: types.Int64Value(int64(q.CreateVolume.Volume.SizeGb)),
Id: types.StringValue(q.ID),
Name: types.StringValue(q.Name),
Size: types.Int64Value(int64(q.SizeGb)),
Appid: types.StringValue(data.Appid.ValueString()),
Region: types.StringValue(q.CreateVolume.Volume.Region),
Region: types.StringValue(q.Region),
}

tflog.Info(ctx, fmt.Sprintf("%+v", data))
Expand All @@ -120,28 +118,27 @@ func (r *flyVolumeResource) Read(ctx context.Context, req resource.ReadRequest,
diags := req.State.Get(ctx, &data)
resp.Diagnostics.Append(diags...)

// strip leading vol_ off name
internalId := data.Id.ValueString()[4:]
id := data.Id.ValueString()
// New flaps based volumes don't have this prefix I'm pretty sure
if id[:4] == "vol_" {
// strip leading vol_ off name
id = id[4:]
}
app := data.Appid.ValueString()

query, err := graphql.VolumeQuery(ctx, *r.client, app, internalId)
machineAPI := apiv1.NewMachineAPI(r.config.httpClient, r.config.httpEndpoint)
query, err := machineAPI.GetVolume(ctx, id, app)
if err != nil {
resp.Diagnostics.AddError("Read: query failed", err.Error())
resp.Diagnostics.AddError("Query failed", err.Error())
return
}

// this query will currently still return success if it finds nothing, so check it:
if query.App.Volume.Id == "" {
resp.Diagnostics.AddError("Query failed", "Could not find matching volume")
}

data = flyVolumeResourceData{
Id: types.StringValue(query.App.Volume.Id),
Name: types.StringValue(query.App.Volume.Name),
Size: types.Int64Value(int64(query.App.Volume.SizeGb)),
Id: types.StringValue(query.ID),
Name: types.StringValue(query.Name),
Size: types.Int64Value(int64(query.SizeGb)),
Appid: types.StringValue(data.Appid.ValueString()),
Region: types.StringValue(query.App.Volume.Region),
// Internalid: types.StringValue(query.App.Volume.InternalId),
Region: types.StringValue(query.Region),
}

diags = resp.State.Set(ctx, &data)
Expand All @@ -163,7 +160,8 @@ func (r *flyVolumeResource) Delete(ctx context.Context, req resource.DeleteReque
resp.Diagnostics.Append(diags...)

if !data.Id.IsUnknown() && !data.Id.IsNull() && data.Id.ValueString() != "" {
_, err := graphql.DeleteVolume(ctx, *r.client, data.Id.ValueString())
machineAPI := apiv1.NewMachineAPI(r.config.httpClient, r.config.httpEndpoint)
err := machineAPI.DeleteVolume(ctx, data.Appid.ValueString(), data.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError("Delete volume failed", err.Error())
return
Expand Down
32 changes: 32 additions & 0 deletions pkg/apiv1/machines.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apiv1

import (
"context"
"errors"
"fmt"
"github.com/Khan/genqlient/graphql"
Expand Down Expand Up @@ -228,3 +229,34 @@ func (a *MachineAPI) DeleteMachine(app string, id string, maxRetries int) error
}
return nil
}

func (a *MachineAPI) CreateVolume(ctx context.Context, name, app, region string, size int) (*api.Volume, error) {
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))
if err != nil {
return nil, err
}
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))
if err != nil {
return nil, err
}

return &res, nil
}

func (a *MachineAPI) DeleteVolume(ctx context.Context, id, app string) error {
_, err := a.httpClient.R().SetContext(ctx).Delete(fmt.Sprintf("http://%s/v1/apps/%s/volumes/%s", a.endpoint, app, id))
if err != nil {
return err
}
return nil
}

0 comments on commit 00902be

Please sign in to comment.