Skip to content

Commit

Permalink
Make sure we propagate the labels for each subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenW committed Nov 3, 2024
1 parent e47999e commit 038bf98
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions internal/service/subgraph/resource_cosmo_subgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -209,21 +210,28 @@ func (r *SubgraphResource) Read(ctx context.Context, req resource.ReadRequest, r
break
}
}

if subgraph == nil {
utils.AddDiagnosticError(resp, ErrSubgraphNotFound, fmt.Sprintf("Subgraph with ID '%s' not found", data.Id.ValueString()))
return
}

} else {
subgraph, apiError = r.client.GetSubgraph(ctx, data.Name.ValueString(), data.Namespace.ValueString())
}
if apiError != nil {
if api.IsNotFoundError(apiError) {
utils.AddDiagnosticWarning(resp,
ErrSubgraphNotFound,
fmt.Sprintf("Subgraph '%s' not found will be recreated %s", data.Name.ValueString(), apiError.Error()),
)
resp.State.RemoveResource(ctx)
if apiError != nil {
if api.IsNotFoundError(apiError) {
utils.AddDiagnosticWarning(resp,
ErrSubgraphNotFound,
fmt.Sprintf("Subgraph '%s' not found will be recreated %s", data.Name.ValueString(), apiError.Error()),
)
resp.State.RemoveResource(ctx)
return
}
utils.AddDiagnosticError(resp, ErrRetrievingSubgraph, fmt.Sprintf("Could not fetch subgraph '%s': %s", data.Name.ValueString(), apiError.Error()))
return
}
utils.AddDiagnosticError(resp, ErrRetrievingSubgraph, fmt.Sprintf("Could not fetch subgraph '%s': %s", data.Name.ValueString(), apiError.Error()))
return
}

schema, apiError := r.client.GetSubgraphSchema(ctx, subgraph.Name, subgraph.Namespace)
if apiError != nil {
if api.IsNotFoundError(apiError) {
Expand All @@ -237,12 +245,21 @@ func (r *SubgraphResource) Read(ctx context.Context, req resource.ReadRequest, r
utils.AddDiagnosticError(resp, ErrRetrievingSubgraph, fmt.Sprintf("Could not fetch subgraph '%s': %s", data.Name.ValueString(), apiError.Error()))
return
}
labels := map[string]attr.Value{}
for _, label := range subgraph.GetLabels() {
if label != nil {
labels[label.GetKey()] = types.StringValue(label.GetValue())
}
}
mapValue, diags := types.MapValueFrom(ctx, types.StringType, labels)
resp.Diagnostics.Append(diags...)

data.Id = types.StringValue(subgraph.GetId())
data.Name = types.StringValue(subgraph.GetName())
data.Namespace = types.StringValue(subgraph.GetNamespace())
data.RoutingURL = types.StringValue(subgraph.GetRoutingURL())
data.Schema = types.StringValue(schema)
data.Labels = mapValue

utils.LogAction(ctx, "read", data.Id.ValueString(), data.Name.ValueString(), data.Namespace.ValueString())

Expand Down

0 comments on commit 038bf98

Please sign in to comment.