Skip to content

Commit f6fce49

Browse files
joshpollaraclaude
andcommitted
fix: mpg_cluster Read uses list instead of status to find by name
`flyctl mpg status <name>` doesn't work — it requires the cluster ID. Switch Read and post-create read-back to use `flyctl mpg list` and find the cluster by name, matching the pattern used by postgres_cluster. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 8cfc15e commit f6fce49

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

internal/resources/mpg_cluster_resource.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,17 @@ func (r *mpgClusterResource) Create(ctx context.Context, req resource.CreateRequ
160160
return
161161
}
162162

163-
var result flyctlMPGCluster
164-
err = r.flyctl.RunJSON(ctx, &result, "mpg", "status", plan.Name.ValueString())
163+
cluster, err := r.findClusterByName(ctx, plan.Name.ValueString())
165164
if err != nil {
166165
resp.Diagnostics.AddError("Error reading MPG cluster after creation", err.Error())
167166
return
168167
}
168+
if cluster == nil {
169+
resp.Diagnostics.AddError("Error finding MPG cluster after creation", "Cluster was created but not found in the list")
170+
return
171+
}
169172

170-
r.setModelFromAPI(&plan, &result)
173+
r.setModelFromAPI(&plan, cluster)
171174
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
172175
}
173176

@@ -178,18 +181,17 @@ func (r *mpgClusterResource) Read(ctx context.Context, req resource.ReadRequest,
178181
return
179182
}
180183

181-
var result flyctlMPGCluster
182-
err := r.flyctl.RunJSON(ctx, &result, "mpg", "status", state.Name.ValueString())
184+
cluster, err := r.findClusterByName(ctx, state.Name.ValueString())
183185
if err != nil {
184-
if flyctl.IsNotFound(err) {
185-
resp.State.RemoveResource(ctx)
186-
return
187-
}
188186
resp.Diagnostics.AddError("Error reading MPG cluster", err.Error())
189187
return
190188
}
189+
if cluster == nil {
190+
resp.State.RemoveResource(ctx)
191+
return
192+
}
191193

192-
r.setModelFromAPI(&state, &result)
194+
r.setModelFromAPI(&state, cluster)
193195
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
194196
}
195197

@@ -219,6 +221,20 @@ func (r *mpgClusterResource) ImportState(ctx context.Context, req resource.Impor
219221
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
220222
}
221223

224+
func (r *mpgClusterResource) findClusterByName(ctx context.Context, name string) (*flyctlMPGCluster, error) {
225+
var results []flyctlMPGCluster
226+
err := r.flyctl.RunJSON(ctx, &results, "mpg", "list")
227+
if err != nil {
228+
return nil, err
229+
}
230+
for i := range results {
231+
if results[i].Name == name {
232+
return &results[i], nil
233+
}
234+
}
235+
return nil, nil
236+
}
237+
222238
func (r *mpgClusterResource) setModelFromAPI(model *models.MPGClusterResourceModel, api *flyctlMPGCluster) {
223239
id := api.ID
224240
if id == "" {

internal/resources/mpg_cluster_resource_mock_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ func TestMPGClusterResource_lifecycle(t *testing.T) {
1111
"mpg create --name test-pg --org personal --region iad": {
1212
Stdout: "Created MPG cluster test-pg\n",
1313
},
14-
"mpg status test-pg --json": {
15-
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}`,
14+
"mpg list --json": {
15+
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}]`,
1616
},
1717
"mpg destroy test-pg --yes": {
1818
Stdout: "Destroyed test-pg\n",

0 commit comments

Comments
 (0)