Skip to content

Commit 8cfc15e

Browse files
authored
Merge pull request #3 from stategraph/fix/create-json-flag
fix: use RunMut for create/update commands that don't support --json
2 parents c8a364b + a26ca15 commit 8cfc15e

26 files changed

+330
-88
lines changed

internal/resources/extension_resource.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,24 @@ func (r *extensionResource) Create(ctx context.Context, req resource.CreateReque
157157
return
158158
}
159159

160-
var result flyctlExtension
161-
err := r.flyctl.RunJSONMut(ctx, &result, args...)
160+
_, err := r.flyctl.RunMut(ctx, args...)
162161
if err != nil {
163162
resp.Diagnostics.AddError(fmt.Sprintf("Error creating %s extension", r.config.TypeName), err.Error())
164163
return
165164
}
166165

166+
if r.flyctl.DryRun {
167+
r.writeStateFromAPI(ctx, &resp.State, &req.Plan, nil, &flyctlExtension{Name: name}, &resp.Diagnostics)
168+
return
169+
}
170+
171+
var result flyctlExtension
172+
err = r.flyctl.RunJSON(ctx, &result, "ext", r.config.TypeName, "status", name)
173+
if err != nil {
174+
resp.Diagnostics.AddError(fmt.Sprintf("Error reading %s extension after creation", r.config.TypeName), err.Error())
175+
return
176+
}
177+
167178
r.writeStateFromAPI(ctx, &resp.State, &req.Plan, nil, &result, &resp.Diagnostics)
168179
}
169180

internal/resources/extension_resource_mock_test.go

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

99
func TestExtMySQLResource_lifecycle(t *testing.T) {
1010
flyctlPath := createMockFlyctl(t, map[string]flyctlMockResponse{
11-
"ext mysql create --name test-mysql --org personal --region iad --json": {
12-
Stdout: `{"id":"ext-1","name":"test-mysql","status":"running"}`,
11+
"ext mysql create --name test-mysql --org personal --region iad": {
12+
Stdout: "Created ext mysql test-mysql\n",
1313
},
1414
"ext mysql status test-mysql --json": {
1515
Stdout: `{"id":"ext-1","name":"test-mysql","status":"running"}`,
@@ -50,8 +50,8 @@ resource "fly_ext_mysql" "test" {
5050

5151
func TestExtSentryResource_lifecycle(t *testing.T) {
5252
flyctlPath := createMockFlyctl(t, map[string]flyctlMockResponse{
53-
"ext sentry create --name test-sentry --app my-app --json": {
54-
Stdout: `{"id":"ext-2","name":"test-sentry","status":"running"}`,
53+
"ext sentry create --name test-sentry --app my-app": {
54+
Stdout: "Created ext sentry test-sentry\n",
5555
},
5656
"ext sentry status test-sentry --json": {
5757
Stdout: `{"id":"ext-2","name":"test-sentry","status":"running"}`,

internal/resources/litefs_cluster_resource.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,37 @@ func (r *liteFSClusterResource) Create(ctx context.Context, req resource.CreateR
9494
"--region", plan.Region.ValueString(),
9595
}
9696

97-
var result flyctlLiteFSCluster
98-
err := r.flyctl.RunJSONMut(ctx, &result, args...)
97+
_, err := r.flyctl.RunMut(ctx, args...)
9998
if err != nil {
10099
resp.Diagnostics.AddError("Error creating LiteFS cluster", err.Error())
101100
return
102101
}
103102

104-
r.setModelFromAPI(&plan, &result)
103+
if r.flyctl.DryRun {
104+
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
105+
return
106+
}
107+
108+
var results []flyctlLiteFSCluster
109+
err = r.flyctl.RunJSON(ctx, &results, "litefs-cloud", "clusters", "list")
110+
if err != nil {
111+
resp.Diagnostics.AddError("Error reading LiteFS clusters after creation", err.Error())
112+
return
113+
}
114+
115+
var found *flyctlLiteFSCluster
116+
for i := range results {
117+
if results[i].Name == plan.Name.ValueString() {
118+
found = &results[i]
119+
break
120+
}
121+
}
122+
if found == nil {
123+
resp.Diagnostics.AddError("Error finding LiteFS cluster after creation", "Cluster was created but not found in the list")
124+
return
125+
}
126+
127+
r.setModelFromAPI(&plan, found)
105128
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
106129
}
107130

internal/resources/litefs_cluster_resource_mock_test.go

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

99
func TestLiteFSClusterResource_lifecycle(t *testing.T) {
1010
flyctlPath := createMockFlyctl(t, map[string]flyctlMockResponse{
11-
"litefs-cloud clusters create --name test-litefs --org personal --region iad --json": {
12-
Stdout: `{"id":"lfs-123","name":"test-litefs","status":"running","region":"iad","org":"personal"}`,
11+
"litefs-cloud clusters create --name test-litefs --org personal --region iad": {
12+
Stdout: "Created LiteFS cluster test-litefs\n",
1313
},
1414
"litefs-cloud clusters list --json": {
1515
Stdout: `[{"id":"lfs-123","name":"test-litefs","status":"running","region":"iad","org":"personal"}]`,

internal/resources/mpg_attachment_resource.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,23 @@ func (r *mpgAttachmentResource) Create(ctx context.Context, req resource.CreateR
116116
args = append(args, "--variable-name", v)
117117
}
118118

119-
var result flyctlMPGAttachment
120-
err := r.flyctl.RunJSONMut(ctx, &result, args...)
119+
_, err := r.flyctl.RunMut(ctx, args...)
121120
if err != nil {
122121
resp.Diagnostics.AddError("Error attaching MPG cluster", err.Error())
123122
return
124123
}
125124

126-
r.setModelFromAPI(&plan, &result)
125+
// Attachment has no read/status command. Set state from plan values.
126+
plan.ID = types.StringValue(plan.ClusterID.ValueString() + "/" + plan.App.ValueString())
127+
if plan.Database.IsUnknown() {
128+
plan.Database = types.StringValue("")
129+
}
130+
if plan.VariableName.IsUnknown() {
131+
plan.VariableName = types.StringValue("DATABASE_URL")
132+
}
133+
if plan.ConnectionURI.IsUnknown() {
134+
plan.ConnectionURI = types.StringValue("")
135+
}
127136
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
128137
}
129138

@@ -175,15 +184,3 @@ func (r *mpgAttachmentResource) ImportState(ctx context.Context, req resource.Im
175184
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("app"), parts[1])...)
176185
}
177186

178-
func (r *mpgAttachmentResource) setModelFromAPI(model *models.MPGAttachmentResourceModel, api *flyctlMPGAttachment) {
179-
model.ID = types.StringValue(model.ClusterID.ValueString() + "/" + model.App.ValueString())
180-
model.ConnectionURI = types.StringValue(api.ConnectionURI)
181-
model.VariableName = types.StringValue(api.VariableName)
182-
model.Database = types.StringValue(api.Database)
183-
}
184-
185-
type flyctlMPGAttachment struct {
186-
ConnectionURI string `json:"connection_uri"`
187-
VariableName string `json:"variable_name"`
188-
Database string `json:"database"`
189-
}

internal/resources/mpg_attachment_resource_mock_test.go

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

99
func TestMPGAttachmentResource_lifecycle(t *testing.T) {
1010
flyctlPath := createMockFlyctl(t, map[string]flyctlMockResponse{
11-
"mpg attach --cluster-id mpg-123 --app myapp --json": {
12-
Stdout: `{"connection_uri":"postgres://user:pass@host:5432/db","variable_name":"DATABASE_URL"}`,
11+
"mpg attach --cluster-id mpg-123 --app myapp": {
12+
Stdout: "Attached mpg-123 to myapp\n",
1313
},
1414
"mpg detach --cluster-id mpg-123 --app myapp --yes": {
1515
Stdout: "Detached mpg-123 from myapp\n",
@@ -33,7 +33,7 @@ resource "fly_mpg_attachment" "test" {
3333
resource.TestCheckResourceAttr("fly_mpg_attachment.test", "cluster_id", "mpg-123"),
3434
resource.TestCheckResourceAttr("fly_mpg_attachment.test", "app", "myapp"),
3535
resource.TestCheckResourceAttr("fly_mpg_attachment.test", "variable_name", "DATABASE_URL"),
36-
resource.TestCheckResourceAttr("fly_mpg_attachment.test", "connection_uri", "postgres://user:pass@host:5432/db"),
36+
resource.TestCheckResourceAttr("fly_mpg_attachment.test", "connection_uri", ""),
3737
),
3838
},
3939
{

internal/resources/mpg_cluster_resource.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,24 @@ func (r *mpgClusterResource) Create(ctx context.Context, req resource.CreateRequ
149149
args = append(args, "--enable-postgis")
150150
}
151151

152-
var result flyctlMPGCluster
153-
err := r.flyctl.RunJSONMut(ctx, &result, args...)
152+
_, err := r.flyctl.RunMut(ctx, args...)
154153
if err != nil {
155154
resp.Diagnostics.AddError("Error creating MPG cluster", err.Error())
156155
return
157156
}
158157

158+
if r.flyctl.DryRun {
159+
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
160+
return
161+
}
162+
163+
var result flyctlMPGCluster
164+
err = r.flyctl.RunJSON(ctx, &result, "mpg", "status", plan.Name.ValueString())
165+
if err != nil {
166+
resp.Diagnostics.AddError("Error reading MPG cluster after creation", err.Error())
167+
return
168+
}
169+
159170
r.setModelFromAPI(&plan, &result)
160171
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
161172
}

internal/resources/mpg_cluster_resource_mock_test.go

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

99
func TestMPGClusterResource_lifecycle(t *testing.T) {
1010
flyctlPath := createMockFlyctl(t, map[string]flyctlMockResponse{
11-
"mpg create --name test-pg --org personal --region iad --json": {
12-
Stdout: `{"id":"mpg-123","name":"test-pg","status":"running","region":"iad","plan":"starter","volume_size":10,"pg_major_version":16,"enable_postgis":false}`,
11+
"mpg create --name test-pg --org personal --region iad": {
12+
Stdout: "Created MPG cluster test-pg\n",
1313
},
1414
"mpg status test-pg --json": {
1515
Stdout: `{"id":"mpg-123","name":"test-pg","status":"running","primary_region":"iad","region":"iad","plan":"starter","volume_size":10,"pg_major_version":16,"enable_postgis":false}`,

internal/resources/mpg_database_resource.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,37 @@ func (r *mpgDatabaseResource) Create(ctx context.Context, req resource.CreateReq
8585
"--name", plan.Name.ValueString(),
8686
}
8787

88-
var result flyctlMPGDatabase
89-
err := r.flyctl.RunJSONMut(ctx, &result, args...)
88+
_, err := r.flyctl.RunMut(ctx, args...)
9089
if err != nil {
9190
resp.Diagnostics.AddError("Error creating MPG database", err.Error())
9291
return
9392
}
9493

95-
r.setModelFromAPI(&plan, &result)
94+
if r.flyctl.DryRun {
95+
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
96+
return
97+
}
98+
99+
var results []flyctlMPGDatabase
100+
err = r.flyctl.RunJSON(ctx, &results, "mpg", "databases", "list", "--cluster-id", plan.ClusterID.ValueString())
101+
if err != nil {
102+
resp.Diagnostics.AddError("Error reading MPG databases after creation", err.Error())
103+
return
104+
}
105+
106+
var found *flyctlMPGDatabase
107+
for _, db := range results {
108+
if db.Name == plan.Name.ValueString() {
109+
found = &db
110+
break
111+
}
112+
}
113+
if found == nil {
114+
resp.Diagnostics.AddError("Error finding MPG database after creation", "Database was created but not found in the list")
115+
return
116+
}
117+
118+
r.setModelFromAPI(&plan, found)
96119
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
97120
}
98121

internal/resources/mpg_database_resource_mock_test.go

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

99
func TestMPGDatabaseResource_lifecycle(t *testing.T) {
1010
flyctlPath := createMockFlyctl(t, map[string]flyctlMockResponse{
11-
"mpg databases create --cluster-id mpg-123 --name mydb --json": {
12-
Stdout: `{"name":"mydb"}`,
11+
"mpg databases create --cluster-id mpg-123 --name mydb": {
12+
Stdout: "Created database mydb\n",
1313
},
1414
"mpg databases list --cluster-id mpg-123 --json": {
1515
Stdout: `[{"name":"mydb"}]`,

0 commit comments

Comments
 (0)