Skip to content

Commit

Permalink
feat: add support for include_tags for contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
JivusAyrus committed Nov 8, 2024
1 parent 58390b9 commit 2e29513
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 23 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/hashicorp/terraform-plugin-go v0.23.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-testing v1.10.0
github.com/wundergraph/cosmo/connect-go v0.0.0-20240916094337-a4c4cae55557
github.com/wundergraph/cosmo/connect-go v0.0.0-20241104193239-b78c5917d64b
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IU
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/wundergraph/cosmo/connect-go v0.0.0-20240916094337-a4c4cae55557 h1:hzZKQsFVJZ6JqOlTWT73P7GNY2vkDV3J+3oYX0SN/iY=
github.com/wundergraph/cosmo/connect-go v0.0.0-20240916094337-a4c4cae55557/go.mod h1:RLepGeaXdENMlePq4geGzaDV895QuJZ+mUFLzG5ra9E=
github.com/wundergraph/cosmo/connect-go v0.0.0-20241104193239-b78c5917d64b h1:N6CQBlWN7B8cOIVPNxZxkmgOUrSeaZebdhmdGxmg3/8=
github.com/wundergraph/cosmo/connect-go v0.0.0-20241104193239-b78c5917d64b/go.mod h1:RLepGeaXdENMlePq4geGzaDV895QuJZ+mUFLzG5ra9E=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down
6 changes: 4 additions & 2 deletions internal/api/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
platformv1 "github.com/wundergraph/cosmo/connect-go/gen/proto/wg/cosmo/platform/v1"
)

func (p *PlatformClient) CreateContract(ctx context.Context, name, namespace, sourceGraphName, routingUrl, admissionWebhookUrl, admissionWebhookSecret string, excludeTags []string, readme string) (*platformv1.CreateContractResponse, *ApiError) {
func (p *PlatformClient) CreateContract(ctx context.Context, name, namespace, sourceGraphName, routingUrl, admissionWebhookUrl, admissionWebhookSecret string, excludeTags []string, includeTags []string, readme string) (*platformv1.CreateContractResponse, *ApiError) {
request := connect.NewRequest(&platformv1.CreateContractRequest{
Name: name,
Namespace: namespace,
Expand All @@ -18,6 +18,7 @@ func (p *PlatformClient) CreateContract(ctx context.Context, name, namespace, so
ExcludeTags: excludeTags,
Readme: &readme,
AdmissionWebhookSecret: &admissionWebhookSecret,
IncludeTags: includeTags,
})

response, err := p.Client.CreateContract(ctx, request)
Expand All @@ -37,11 +38,12 @@ func (p *PlatformClient) CreateContract(ctx context.Context, name, namespace, so
return response.Msg, nil
}

func (p *PlatformClient) UpdateContract(ctx context.Context, name, namespace string, excludeTags []string) (*platformv1.UpdateContractResponse, *ApiError) {
func (p *PlatformClient) UpdateContract(ctx context.Context, name, namespace string, excludeTags []string, includeTags []string) (*platformv1.UpdateContractResponse, *ApiError) {
request := connect.NewRequest(&platformv1.UpdateContractRequest{
Name: name,
Namespace: namespace,
ExcludeTags: excludeTags,
IncludeTags: includeTags,
})

response, err := p.Client.UpdateContract(ctx, request)
Expand Down
4 changes: 4 additions & 0 deletions internal/service/contract/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ const (
ErrUnexpectedDataSourceType = "Unexpected Data Source Configure Type"
ErrUnexpectedResourceType = "Unexpected Resource Configure Type"
)

const (
DebugCreate = "create-contract"
)
100 changes: 82 additions & 18 deletions internal/service/contract/resource_cosmo_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package contract
import (
"context"
"fmt"
"github.com/wundergraph/cosmo/connect-go/gen/proto/wg/cosmo/common"
platformv1 "github.com/wundergraph/cosmo/connect-go/gen/proto/wg/cosmo/platform/v1"
"strings"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand All @@ -28,6 +31,7 @@ type contractResourceModel struct {
SourceGraphName types.String `tfsdk:"source"`
Namespace types.String `tfsdk:"namespace"`
ExcludeTags types.List `tfsdk:"exclude_tags"`
IncludeTags types.List `tfsdk:"include_tags"`
Readme types.String `tfsdk:"readme"`
AdmissionWebhookUrl types.String `tfsdk:"admission_webhook_url"`
AdmissionWebhookSecret types.String `tfsdk:"admission_webhook_secret"`
Expand Down Expand Up @@ -68,6 +72,10 @@ For more information, refer to the Cosmo Documentation at https://cosmo-docs.wun
Optional: true,
ElementType: types.StringType,
},
"include_tags": schema.ListAttribute{
Optional: true,
ElementType: types.StringType,
},
"readme": schema.StringAttribute{
Optional: true,
},
Expand Down Expand Up @@ -109,31 +117,24 @@ func (r *contractResource) Create(ctx context.Context, req resource.CreateReques
return
}

excludeTags, err := utils.ConvertLabelMatchers(data.ExcludeTags)
if err != nil {
utils.AddDiagnosticError(resp,
ErrCreatingContract,
"Could not create contract: "+err.Error(),
)
return
}
_, apiError := r.client.CreateContract(ctx, data.Name.ValueString(), data.Namespace.ValueString(), data.SourceGraphName.ValueString(), data.RoutingURL.ValueString(), data.AdmissionWebhookUrl.ValueString(), data.AdmissionWebhookSecret.ValueString(), excludeTags, data.Readme.ValueString())
response, apiError := r.createAndFetchContract(ctx, data, resp)
if apiError != nil {
if api.IsContractCompositionFailedError(apiError) || api.IsSubgraphCompositionFailedError(apiError) {
utils.AddDiagnosticWarning(resp,
ErrCreatingContract,
"Contract composition failed: "+apiError.Error(),
)
} else {
if !api.IsSubgraphCompositionFailedError(apiError) {
utils.AddDiagnosticError(resp,
ErrCreatingContract,
"Could not create contract: "+apiError.Error(),
apiError.Error(),
)
return
}
}

data.Id = data.Name
graph := response.Graph
data.Id = types.StringValue(graph.GetId())
data.Name = types.StringValue(graph.GetName())
data.Namespace = types.StringValue(graph.GetNamespace())
data.RoutingURL = types.StringValue(graph.GetRoutingURL())

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

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Expand Down Expand Up @@ -192,7 +193,16 @@ func (r *contractResource) Update(ctx context.Context, req resource.UpdateReques
return
}

_, apiError := r.client.UpdateContract(ctx, data.Name.ValueString(), data.Namespace.ValueString(), excludeTags)
includeTags, err := utils.ConvertLabelMatchers(data.IncludeTags)
if err != nil {
utils.AddDiagnosticError(resp,
ErrCreatingContract,
"Could not create contract: "+err.Error(),
)
return
}

_, apiError := r.client.UpdateContract(ctx, data.Name.ValueString(), data.Namespace.ValueString(), excludeTags, includeTags)
if apiError != nil {
if api.IsContractCompositionFailedError(apiError) || api.IsSubgraphCompositionFailedError(apiError) {
utils.AddDiagnosticWarning(resp,
Expand Down Expand Up @@ -245,3 +255,57 @@ func (r *contractResource) Delete(ctx context.Context, req resource.DeleteReques
func (r *contractResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

func (r *contractResource) createAndFetchContract(ctx context.Context, data contractResourceModel, resp *resource.CreateResponse) (*platformv1.GetFederatedGraphByNameResponse, *api.ApiError) {
excludeTags, err := utils.ConvertLabelMatchers(data.ExcludeTags)
if err != nil {
utils.AddDiagnosticError(resp,
ErrCreatingContract,
"Could not create contract: "+err.Error(),
)
return nil, &api.ApiError{Err: err, Reason: "CreateContract", Status: common.EnumStatusCode_ERR}
}

includeTags, err := utils.ConvertLabelMatchers(data.IncludeTags)
if err != nil {
utils.AddDiagnosticError(resp,
ErrCreatingContract,
"Could not create contract: "+err.Error(),
)
return nil, &api.ApiError{Err: err, Reason: "CreateContract", Status: common.EnumStatusCode_ERR}
}

utils.DebugAction(ctx, DebugCreate, data.Name.ValueString(), data.Namespace.ValueString(), map[string]interface{}{
"routing_url": data.RoutingURL.ValueString(),
"excludeTags": strings.Join(excludeTags, ","),
"includeTags": strings.Join(includeTags, ","),
})

_, apiError := r.client.CreateContract(ctx, data.Name.ValueString(), data.Namespace.ValueString(), data.SourceGraphName.ValueString(), data.RoutingURL.ValueString(), data.AdmissionWebhookUrl.ValueString(), data.AdmissionWebhookSecret.ValueString(), excludeTags, includeTags, data.Readme.ValueString())
if apiError != nil {
if api.IsContractCompositionFailedError(apiError) || api.IsSubgraphCompositionFailedError(apiError) {
utils.AddDiagnosticWarning(resp,
ErrCreatingContract,
"Contract composition failed: "+apiError.Error(),
)
} else {
utils.AddDiagnosticError(resp,
ErrCreatingContract,
"Could not create contract: "+apiError.Error(),
)
return nil, apiError
}
}

response, apiError := r.client.GetFederatedGraph(ctx, data.Name.ValueString(), data.Namespace.ValueString())
if apiError != nil {
return nil, apiError
}

utils.DebugAction(ctx, DebugCreate, data.Name.ValueString(), data.Namespace.ValueString(), map[string]interface{}{
"id": response.Graph.GetId(),
"graph": response.Graph,
})

return response, nil
}

0 comments on commit 2e29513

Please sign in to comment.