Skip to content

Commit

Permalink
Merge pull request #11 from wundergraph/suvij/eng-5845-contract-inclu…
Browse files Browse the repository at this point in the history
…de-looks-to-have-broken-the-contract-api

feat: improve terraform provider
  • Loading branch information
JivusAyrus authored Dec 9, 2024
2 parents 58390b9 + 7246b38 commit 244548c
Show file tree
Hide file tree
Showing 29 changed files with 1,013 additions and 225 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ on:
paths-ignore:
- "README.md"
push:
branches:
- main
paths-ignore:
- "README.md"

Expand Down
3 changes: 3 additions & 0 deletions docs/data-sources/contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ data "cosmo_subgraph" "test" {

- `admission_webhook_secret` (String, Sensitive) The secret token used to authenticate the admission webhook requests.
- `admission_webhook_url` (String) The URL for the admission webhook that will be triggered during graph operations.
- `exclude_tags` (List of String)
- `id` (String) The unique identifier of the federated graph resource, automatically generated by the system.
- `include_tags` (List of String)
- `label_matchers` (Map of String) A list of label matchers used to select the services that will form the federated graph.
- `readme` (String) Readme content for the federated graph.
- `routing_url` (String) The URL for the federated graph.
- `supports_federation` (Boolean) SupportFederation defines if this contract's source graph is a federated graph or a monograph..
2 changes: 2 additions & 0 deletions docs/resources/contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ resource "cosmo_contract" "test" {
- `admission_webhook_secret` (String)
- `admission_webhook_url` (String)
- `exclude_tags` (List of String)
- `include_tags` (List of String)
- `readme` (String)
- `supports_federation` (Boolean)

### Read-Only

Expand Down
2 changes: 2 additions & 0 deletions docs/resources/monograph.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ resource "cosmo_monograph" "example" {
namespace = var.monograph_namespace
graph_url = var.monograph_graph_url
routing_url = var.monograph_routing_url
schema = var.monograph_schema
}
```

Expand All @@ -39,6 +40,7 @@ resource "cosmo_monograph" "example" {
- `admission_webhook_url` (String) The admission webhook URL for the monograph.
- `namespace` (String) The namespace in which the monograph is located.
- `readme` (String) The readme for the subgraph.
- `schema` (String) The schema for the subgraph.
- `subscription_protocol` (String) The subscription protocol for the subgraph.
- `subscription_url` (String) The subscription URL for the subgraph.
- `websocket_subprotocol` (String) The websocket subprotocol for the subgraph.
Expand Down
1 change: 1 addition & 0 deletions examples/guides/cosmo-monograph-contract/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module "cosmo_monograph" {
monograph_namespace = module.cosmo_namespace.name
monograph_graph_url = var.monograph_graph_url
monograph_routing_url = var.monograph_routing_url
monograph_schema = var.monograph_schema
}

module "cosmo_contract" {
Expand Down
4 changes: 4 additions & 0 deletions examples/guides/cosmo-monograph-contract/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ variable "monograph_routing_url" {
default = "http://example.com/routing"
}

variable "monograph_schema" {
default = "type Query{ a: String }"
}

variable "contract_name" {
type = string
default = "test"
Expand Down
1 change: 1 addition & 0 deletions examples/resources/cosmo_monograph/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ resource "cosmo_monograph" "example" {
namespace = var.monograph_namespace
graph_url = var.monograph_graph_url
routing_url = var.monograph_routing_url
schema = var.monograph_schema
}
4 changes: 4 additions & 0 deletions examples/resources/cosmo_monograph/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ variable "monograph_graph_url" {

variable "monograph_routing_url" {
default = "http://example.com/routing"
}

variable "monograph_schema" {
default = "type Query{ a: String }"
}
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-20241203152720-979e5a780c8e
)

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-20241203152720-979e5a780c8e h1:XFEGOGnBWR6cfW3gV+24An3lNoesWOxTH71D09Tuph4=
github.com/wundergraph/cosmo/connect-go v0.0.0-20241203152720-979e5a780c8e/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
4 changes: 2 additions & 2 deletions internal/api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const (
GraphQLWebsocketSubprotocolGraphQLTransportWS = "graphql-transport-ws"
)

func resolveWebsocketSubprotocol(protocol string) *common.GraphQLWebsocketSubprotocol {
func ResolveWebsocketSubprotocol(protocol string) *common.GraphQLWebsocketSubprotocol {
switch protocol {
case GraphQLWebsocketSubprotocolGraphQLWS:
return common.GraphQLWebsocketSubprotocol_GRAPHQL_WEBSOCKET_SUBPROTOCOL_WS.Enum()
Expand All @@ -28,7 +28,7 @@ const (
GraphQLSubscriptionProtocolSSEPost = "sse_post"
)

func resolveSubscriptionProtocol(protocol string) *common.GraphQLSubscriptionProtocol {
func ResolveSubscriptionProtocol(protocol string) *common.GraphQLSubscriptionProtocol {
switch protocol {
case GraphQLSubscriptionProtocolSSE:
return common.GraphQLSubscriptionProtocol_GRAPHQL_SUBSCRIPTION_PROTOCOL_SSE.Enum()
Expand Down
29 changes: 10 additions & 19 deletions internal/api/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,8 @@ 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) {
request := connect.NewRequest(&platformv1.CreateContractRequest{
Name: name,
Namespace: namespace,
SourceGraphName: sourceGraphName,
RoutingUrl: routingUrl,
AdmissionWebhookUrl: admissionWebhookUrl,
ExcludeTags: excludeTags,
Readme: &readme,
AdmissionWebhookSecret: &admissionWebhookSecret,
})
func (p *PlatformClient) CreateContract(ctx context.Context, data *platformv1.CreateContractRequest) (*platformv1.CreateContractResponse, *ApiError) {
request := connect.NewRequest(data)

response, err := p.Client.CreateContract(ctx, request)
if err != nil {
Expand All @@ -37,12 +28,8 @@ 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) {
request := connect.NewRequest(&platformv1.UpdateContractRequest{
Name: name,
Namespace: namespace,
ExcludeTags: excludeTags,
})
func (p *PlatformClient) UpdateContract(ctx context.Context, data *platformv1.UpdateContractRequest) (*platformv1.UpdateContractResponse, *ApiError) {
request := connect.NewRequest(data)

response, err := p.Client.UpdateContract(ctx, request)
if err != nil {
Expand All @@ -61,8 +48,12 @@ func (p *PlatformClient) UpdateContract(ctx context.Context, name, namespace str
return response.Msg, nil
}

func (p *PlatformClient) DeleteContract(ctx context.Context, name, namespace string) *ApiError {
return p.DeleteFederatedGraph(ctx, name, namespace)
func (p *PlatformClient) DeleteContract(ctx context.Context, name, namespace string, supportsFederation bool) *ApiError {
if supportsFederation {
return p.DeleteFederatedGraph(ctx, name, namespace)
} else {
return p.DeleteMonograph(ctx, name, namespace)
}
}

func (p *PlatformClient) GetContract(ctx context.Context, name, namespace string) (*platformv1.GetFederatedGraphByNameResponse, *ApiError) {
Expand Down
31 changes: 27 additions & 4 deletions internal/api/monograph.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func (p PlatformClient) CreateMonograph(ctx context.Context, name string, namesp
GraphUrl: graphUrl,
SubscriptionUrl: subscriptionUrl,
Readme: readme,
WebsocketSubprotocol: resolveWebsocketSubprotocol(websocketSubprotocol),
SubscriptionProtocol: resolveSubscriptionProtocol(subscriptionProtocol),
WebsocketSubprotocol: ResolveWebsocketSubprotocol(websocketSubprotocol),
SubscriptionProtocol: ResolveSubscriptionProtocol(subscriptionProtocol),
AdmissionWebhookURL: admissionWebhookUrl,
AdmissionWebhookSecret: &admissionWebhookSecret,
})
Expand Down Expand Up @@ -46,8 +46,8 @@ func (p PlatformClient) UpdateMonograph(ctx context.Context, name string, namesp
GraphUrl: graphUrl,
SubscriptionUrl: subscriptionUrl,
Readme: readme,
WebsocketSubprotocol: resolveWebsocketSubprotocol(websocketSubprotocol),
SubscriptionProtocol: resolveSubscriptionProtocol(subscriptionProtocol),
WebsocketSubprotocol: ResolveWebsocketSubprotocol(websocketSubprotocol),
SubscriptionProtocol: ResolveSubscriptionProtocol(subscriptionProtocol),
AdmissionWebhookURL: &admissionWebhookUrl,
AdmissionWebhookSecret: &admissionWebhookSecret,
})
Expand Down Expand Up @@ -111,3 +111,26 @@ func (p PlatformClient) GetMonograph(ctx context.Context, name string, namespace

return response.Msg.Graph, nil
}

func (p PlatformClient) PublishMonograph(ctx context.Context, name string, namespace string, schema string) *ApiError {
request := connect.NewRequest(&platformv1.PublishMonographRequest{
Name: name,
Namespace: namespace,
Schema: schema,
})
response, err := p.Client.PublishMonograph(ctx, request)
if err != nil {
return &ApiError{Err: err, Reason: "PublishMonograph", Status: common.EnumStatusCode_ERR}
}

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

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

return nil
}
31 changes: 4 additions & 27 deletions internal/api/subgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,8 @@ import (
platformv1 "github.com/wundergraph/cosmo/connect-go/gen/proto/wg/cosmo/platform/v1"
)

func (p PlatformClient) CreateSubgraph(ctx context.Context, name string, namespace string, routingUrl string, baseSubgraphName *string, labels []*platformv1.Label, subscriptionUrl *string, readme *string, isEventDrivenGraph *bool, isFeatureSubgraph *bool, subscriptionProtocol string, websocketSubprotocol string) *ApiError {
request := connect.NewRequest(&platformv1.CreateFederatedSubgraphRequest{
Name: name,
Namespace: namespace,
RoutingUrl: &routingUrl,
Labels: labels,
SubscriptionUrl: subscriptionUrl,
Readme: readme,
WebsocketSubprotocol: resolveWebsocketSubprotocol(websocketSubprotocol),
SubscriptionProtocol: resolveSubscriptionProtocol(subscriptionProtocol),
IsEventDrivenGraph: isEventDrivenGraph,
BaseSubgraphName: baseSubgraphName,
IsFeatureSubgraph: isFeatureSubgraph,
})
func (p PlatformClient) CreateSubgraph(ctx context.Context, data *platformv1.CreateFederatedSubgraphRequest) *ApiError {
request := connect.NewRequest(data)
response, err := p.Client.CreateFederatedSubgraph(ctx, request)
if err != nil {
return &ApiError{Err: err, Reason: "CreateSubgraph", Status: common.EnumStatusCode_ERR}
Expand All @@ -39,19 +27,8 @@ func (p PlatformClient) CreateSubgraph(ctx context.Context, name string, namespa
return nil
}

func (p PlatformClient) UpdateSubgraph(ctx context.Context, name, namespace, routingUrl string, labels []*platformv1.Label, headers []string, subscriptionUrl, readme *string, unsetLabels *bool, websocketSubprotocol string, subscriptionProtocol string) *ApiError {
request := connect.NewRequest(&platformv1.UpdateSubgraphRequest{
Name: name,
RoutingUrl: &routingUrl,
Labels: labels,
Headers: headers,
SubscriptionUrl: subscriptionUrl,
Readme: readme,
Namespace: namespace,
UnsetLabels: unsetLabels,
WebsocketSubprotocol: resolveWebsocketSubprotocol(websocketSubprotocol),
SubscriptionProtocol: resolveSubscriptionProtocol(subscriptionProtocol),
})
func (p PlatformClient) UpdateSubgraph(ctx context.Context, data *platformv1.UpdateSubgraphRequest) *ApiError {
request := connect.NewRequest(data)

response, err := p.Client.UpdateSubgraph(ctx, request)
if err != nil {
Expand Down
39 changes: 34 additions & 5 deletions internal/service/contract/data_source_cosmo_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type contractDataSourceModel struct {
AdmissionWebhookUrl types.String `tfsdk:"admission_webhook_url"`
AdmissionWebhookSecret types.String `tfsdk:"admission_webhook_secret"`
LabelMatchers types.Map `tfsdk:"label_matchers"`
ExcludeTags types.List `tfsdk:"exclude_tags"`
IncludeTags types.List `tfsdk:"include_tags"`
SupportsFederation types.Bool `tfsdk:"supports_federation"`
}

func (d *contractDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down Expand Up @@ -72,6 +75,18 @@ func (d *contractDataSource) Schema(ctx context.Context, req datasource.SchemaRe
MarkdownDescription: "The URL for the federated graph.",
Computed: true,
},
"exclude_tags": schema.ListAttribute{
Computed: true,
ElementType: types.StringType,
},
"include_tags": schema.ListAttribute{
Computed: true,
ElementType: types.StringType,
},
"supports_federation": schema.BoolAttribute{
MarkdownDescription: "SupportFederation defines if this contract's source graph is a federated graph or a monograph..",
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -124,22 +139,36 @@ func (d *contractDataSource) Read(ctx context.Context, req datasource.ReadReques
}

graph := apiResponse.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())
data.SupportsFederation = types.BoolValue(graph.GetSupportsFederation())

if graph.Contract != nil && len(graph.Contract.GetExcludeTags()) > 0 {
var responseExcludeTags []attr.Value
for _, tag := range graph.Contract.GetExcludeTags() {
responseExcludeTags = append(responseExcludeTags, types.StringValue(tag))
}
data.ExcludeTags = types.ListValueMust(types.StringType, responseExcludeTags)
}

labelMatchers := make(map[string]attr.Value)
for _, labelMatcher := range graph.GetLabelMatchers() {
labelMatchers[labelMatcher] = types.StringValue(labelMatcher)
if graph.Contract != nil && len(graph.Contract.GetIncludeTags()) > 0 {
var responseIncludeTags []attr.Value
for _, tag := range graph.Contract.IncludeTags {
responseIncludeTags = append(responseIncludeTags, types.StringValue(tag))
}
data.IncludeTags = types.ListValueMust(types.StringType, responseIncludeTags)
}
data.LabelMatchers = types.MapValueMust(types.StringType, labelMatchers)

if graph.Readme != nil {
data.Readme = types.StringValue(*graph.Readme)
}

if graph.GetAdmissionWebhookUrl() != "" {
data.AdmissionWebhookUrl = types.StringValue(*graph.AdmissionWebhookUrl)
}

tflog.Trace(ctx, "Read contract data source", map[string]interface{}{
"id": data.Id.ValueString(),
})
Expand Down
Loading

0 comments on commit 244548c

Please sign in to comment.