diff --git a/internal/api/subgraph.go b/internal/api/subgraph.go index b6becde..cff698d 100644 --- a/internal/api/subgraph.go +++ b/internal/api/subgraph.go @@ -109,3 +109,21 @@ func (p PlatformClient) GetSubgraph(ctx context.Context, name, namespace string) return subgraph, nil } + +func (p PlatformClient) PublishSubgraph(ctx context.Context, name, namespace, schema string) (*platformv1.PublishFederatedSubgraphResponse, error) { + request := connect.NewRequest(&platformv1.PublishFederatedSubgraphRequest{ + Name: name, + Namespace: namespace, + Schema: schema, + }) + response, err := p.Client.PublishFederatedSubgraph(ctx, request) + if err != nil { + return nil, err + } + + if response.Msg == nil { + return nil, fmt.Errorf("failed to publish subgraph: %s, the server response is nil", name) + } + + return response.Msg, nil +} diff --git a/internal/service/subgraph/data_source_cosmo_subgraph.go b/internal/service/subgraph/data_source_cosmo_subgraph.go index 11b1f85..5c9ee93 100644 --- a/internal/service/subgraph/data_source_cosmo_subgraph.go +++ b/internal/service/subgraph/data_source_cosmo_subgraph.go @@ -37,6 +37,7 @@ type SubgraphDataSourceModel struct { IsEventDrivenGraph types.Bool `tfsdk:"is_event_driven_graph"` IsFeatureSubgraph types.Bool `tfsdk:"is_feature_subgraph"` Headers types.List `tfsdk:"headers"` + Schema types.String `tfsdk:"schema"` } func (d *SubgraphDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { @@ -101,6 +102,10 @@ func (d *SubgraphDataSource) Schema(ctx context.Context, req datasource.SchemaRe MarkdownDescription: "Labels for the subgraph.", ElementType: types.StringType, }, + "schema": schema.StringAttribute{ + Computed: true, + MarkdownDescription: "The schema for the subgraph.", + }, }, } } diff --git a/internal/service/subgraph/errors.go b/internal/service/subgraph/errors.go index f63635f..986665e 100644 --- a/internal/service/subgraph/errors.go +++ b/internal/service/subgraph/errors.go @@ -6,6 +6,7 @@ const ( ErrRetrievingSubgraph = "Error Retrieving Subgraph" ErrUpdatingSubgraph = "Error Updating Subgraph" ErrDeletingSubgraph = "Error Deleting Subgraph" + ErrPublishingSubgraph = "Error Publishing Subgraph" ErrUnexpectedDataSourceType = "Unexpected Data Source Configure Type" ErrInvalidNamespace = "Invalid Namespace" ) diff --git a/internal/service/subgraph/resource_cosmo_subgraph.go b/internal/service/subgraph/resource_cosmo_subgraph.go index c4d0cd9..dcf2152 100644 --- a/internal/service/subgraph/resource_cosmo_subgraph.go +++ b/internal/service/subgraph/resource_cosmo_subgraph.go @@ -35,6 +35,7 @@ type SubgraphResourceModel struct { UnsetLabels types.Bool `tfsdk:"unset_labels"` Headers types.List `tfsdk:"headers"` Labels types.Map `tfsdk:"labels"` + Schema types.String `tfsdk:"schema"` } func NewSubgraphResource() resource.Resource { @@ -123,6 +124,10 @@ func (r *SubgraphResource) Schema(ctx context.Context, req resource.SchemaReques MarkdownDescription: "Labels for the subgraph.", ElementType: types.StringType, }, + "schema": schema.StringAttribute{ + Optional: true, + MarkdownDescription: "The schema for the subgraph.", + }, // TODO: re-enable this once Graph Feature Flags are implementd // "base_subgraph_name": schema.StringAttribute{ // Optional: true, @@ -164,6 +169,14 @@ func (r *SubgraphResource) Create(ctx context.Context, req resource.CreateReques return } + if data.Schema.ValueString() != "" { + _, err := r.client.PublishSubgraph(ctx, data.Name.ValueString(), data.Namespace.ValueString(), data.Schema.ValueString()) + if err != nil { + utils.AddDiagnosticError(resp, ErrPublishingSubgraph, fmt.Sprintf("Could not publish subgraph '%s': %s", data.Name.ValueString(), err)) + return + } + } + data.Id = types.StringValue(subgraph.GetId()) data.Name = types.StringValue(subgraph.GetName()) data.Namespace = types.StringValue(subgraph.GetNamespace()) @@ -216,6 +229,17 @@ func (r *SubgraphResource) Update(ctx context.Context, req resource.UpdateReques } } + if data.Schema.ValueString() != "" { + apiResponse, err := r.client.PublishSubgraph(ctx, data.Name.ValueString(), data.Namespace.ValueString(), data.Schema.ValueString()) + if err != nil { + utils.AddDiagnosticError(resp, ErrPublishingSubgraph, fmt.Sprintf("Could not publish subgraph '%s': %s", data.Name.ValueString(), err)) + return + } + if apiResponse.HasChanged != nil && *apiResponse.HasChanged { + resp.Diagnostics.AddWarning("Subgraph schema has changed", fmt.Sprintf("The schema for subgraph '%s' has changed and was published.", data.Name.ValueString())) + } + } + var unsetLabels *bool if data.UnsetLabels.ValueBool() { unsetLabels = &[]bool{true}[0]