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

Commit

Permalink
Make org field in app take a slug instead of an id. Closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
DAlperin committed May 27, 2022
1 parent acdbc14 commit 38c01d1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 18 deletions.
52 changes: 52 additions & 0 deletions graphql/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions graphql/genqlient.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,10 @@ mutation RemoveWireguardPeer(
name
}
}
}

query Organization($slug: String) {
organization(slug: $slug) {
id
}
}
27 changes: 9 additions & 18 deletions internal/provider/app_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type flyAppResourceType struct{}

type flyAppResourceData struct {
Name types.String `tfsdk:"name"`
Id types.String `tfsdk:"id"`
Org types.String `tfsdk:"org"`
}

Expand All @@ -38,12 +37,6 @@ func (ar flyAppResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diag
Required: true,
Type: types.StringType,
},
"id": {
MarkdownDescription: "Name of application",
Optional: true,
Computed: true,
Type: types.StringType,
},
"org": {
Computed: true,
Optional: true,
Expand Down Expand Up @@ -83,6 +76,13 @@ func (r flyAppResource) Create(ctx context.Context, req tfsdk.CreateResourceRequ
return
}
data.Org.Value = defaultOrg.Id
} else {
org, err := graphql.Organization(context.Background(), *r.provider.client, data.Org.Value)
if err != nil {
resp.Diagnostics.AddError("Could not resolve organization", err.Error())
return
}
data.Org.Value = org.Organization.Id
}

mresp, err := graphql.CreateAppMutation(context.Background(), *r.provider.client, data.Name.Value, data.Org.Value)
Expand All @@ -92,7 +92,6 @@ func (r flyAppResource) Create(ctx context.Context, req tfsdk.CreateResourceRequ
}

data = flyAppResourceData{
Id: types.String{Value: mresp.CreateApp.App.Name},
Org: types.String{Value: mresp.CreateApp.App.Organization.Id},
Name: types.String{Value: mresp.CreateApp.App.Name},
}
Expand All @@ -114,14 +113,7 @@ func (r flyAppResource) Read(ctx context.Context, req tfsdk.ReadResourceRequest,
return
}

var appKey string
if !data.Name.Unknown {
appKey = data.Id.Value
} else {
appKey = data.Name.Value
}

query, err := graphql.GetFullApp(context.Background(), *r.provider.client, appKey)
query, err := graphql.GetFullApp(context.Background(), *r.provider.client, data.Name.Value)
var errList gqlerror.List
if errors.As(err, &errList) {
for _, err := range errList {
Expand All @@ -136,7 +128,6 @@ func (r flyAppResource) Read(ctx context.Context, req tfsdk.ReadResourceRequest,

data = flyAppResourceData{
Name: types.String{Value: query.App.Name},
Id: types.String{Value: query.App.Name},
Org: types.String{Value: query.App.Organization.Id},
}

Expand Down Expand Up @@ -201,5 +192,5 @@ func (r flyAppResource) Delete(ctx context.Context, req tfsdk.DeleteResourceRequ
}

func (r flyAppResource) ImportState(ctx context.Context, req tfsdk.ImportResourceStateRequest, resp *tfsdk.ImportResourceStateResponse) {
tfsdk.ResourceImportStatePassthroughID(ctx, tftypes.NewAttributePath().WithAttributeName("id"), req, resp)
tfsdk.ResourceImportStatePassthroughID(ctx, tftypes.NewAttributePath().WithAttributeName("name"), req, resp)
}

0 comments on commit 38c01d1

Please sign in to comment.