Skip to content

Commit

Permalink
Merge pull request #10 from SvenW/fix/import-subgraph
Browse files Browse the repository at this point in the history
Import by id now works by finding the relevant subgraph in the provided namespace
  • Loading branch information
Noroth authored Nov 4, 2024
2 parents 65fe8e3 + 0c249d9 commit 58390b9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/resources/subgraph.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ description: |-
# cosmo_subgraph (Resource)

This resource handles subgraphs. Each subgraph is responsible for defining its specific segment of the schema and managing the related queries.

For more information on subgraphs, please refer to the [Cosmo Documentation](https://cosmo-docs.wundergraph.com/cli/subgraph).

## Example Usage
Expand Down
45 changes: 45 additions & 0 deletions internal/api/subgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,51 @@ func (p PlatformClient) GetSubgraph(ctx context.Context, name, namespace string)
return response.Msg.GetGraph(), nil
}

func (p PlatformClient) GetSubgraphs(ctx context.Context, namespace string) ([]*platformv1.Subgraph, *ApiError) {
request := connect.NewRequest(&platformv1.GetSubgraphsRequest{
Namespace: namespace,
})

response, err := p.Client.GetSubgraphs(ctx, request)
if err != nil {
return nil, &ApiError{Err: err, Reason: "GetSubgraph", Status: common.EnumStatusCode_ERR}
}

if response.Msg == nil {
return nil, &ApiError{Err: ErrEmptyMsg, Reason: "GetSubgraph", Status: common.EnumStatusCode_ERR}
}

apiError := handleErrorCodes(response.Msg.GetResponse().Code, response.Msg.String())
if apiError != nil {
return nil, apiError
}

return response.Msg.GetGraphs(), nil
}

func (p PlatformClient) GetSubgraphSchema(ctx context.Context, name, namespace string) (string, *ApiError) {
request := connect.NewRequest(&platformv1.GetLatestSubgraphSDLRequest{
Name: name,
Namespace: namespace,
})

response, err := p.Client.GetLatestSubgraphSDL(ctx, request)
if err != nil {
return "", &ApiError{Err: err, Reason: "GetSubgraph", Status: common.EnumStatusCode_ERR}
}

if response.Msg == nil {
return "", &ApiError{Err: ErrEmptyMsg, Reason: "GetSubgraph", Status: common.EnumStatusCode_ERR}
}

apiError := handleErrorCodes(response.Msg.GetResponse().Code, response.Msg.String())
if apiError != nil {
return "", apiError
}

return response.Msg.GetSdl(), nil
}

func (p PlatformClient) PublishSubgraph(ctx context.Context, name, namespace, schema string) (*platformv1.PublishFederatedSubgraphResponse, *ApiError) {
request := connect.NewRequest(&platformv1.PublishFederatedSubgraphRequest{
Name: name,
Expand Down
1 change: 1 addition & 0 deletions internal/service/subgraph/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const (
ErrInvalidSubgraphName = "Invalid Subgraph Name"
ErrCreatingSubgraph = "Error Creating Subgraph"
ErrRetrievingSubgraph = "Error Retrieving Subgraph"
ErrRetrievingSubgraphs = "Error Retrieving Subgraphs"
ErrUpdatingSubgraph = "Error Updating Subgraph"
ErrDeletingSubgraph = "Error Deleting Subgraph"
ErrPublishingSubgraph = "Error Publishing Subgraph"
Expand Down
55 changes: 53 additions & 2 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 @@ -71,7 +72,7 @@ func (r *SubgraphResource) Schema(ctx context.Context, req resource.SchemaReques
resp.Schema = schema.Schema{
MarkdownDescription: `
This resource handles subgraphs. Each subgraph is responsible for defining its specific segment of the schema and managing the related queries.
For more information on subgraphs, please refer to the [Cosmo Documentation](https://cosmo-docs.wundergraph.com/cli/subgraph).
`,
Attributes: map[string]schema.Attribute{
Expand Down Expand Up @@ -193,7 +194,45 @@ func (r *SubgraphResource) Read(ctx context.Context, req resource.ReadRequest, r
return
}

subgraph, apiError := r.client.GetSubgraph(ctx, data.Name.ValueString(), data.Namespace.ValueString())
var apiError *api.ApiError
var subgraph *platformv1.Subgraph
// We're doing an import if the name isn't provided and therefore we need
// to fetch the subgraph by ID and namespace.
if data.Name.ValueString() == "" {
subgraphs, apiError := r.client.GetSubgraphs(ctx, data.Namespace.ValueString())
if apiError != nil {
utils.AddDiagnosticError(resp, ErrRetrievingSubgraphs, fmt.Sprintf("Could not fetch subgraphs: %s", apiError.Error()))
return
}
for _, sg := range subgraphs {
if sg.Id == data.Id.ValueString() {
subgraph = sg
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)
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) {
utils.AddDiagnosticWarning(resp,
Expand All @@ -206,11 +245,23 @@ 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())
if len(schema) > 0 {
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 58390b9

Please sign in to comment.