Skip to content

Commit

Permalink
feat: improve tests and handle optional values
Browse files Browse the repository at this point in the history
  • Loading branch information
JivusAyrus committed Dec 3, 2024
1 parent 5e075cd commit 1cfbbc7
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 44 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-20241104193239-b78c5917d64b
github.com/wundergraph/cosmo/connect-go v0.0.0-20241203152720-979e5a780c8e
)

require (
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAh
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
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/wundergraph/cosmo/connect-go v0.0.0-20241127080034-0681d60a5f89 h1:JnLcb7QijyHR8LoulHw+jiBqM5Z9RNU7/7co3MTgxTY=
github.com/wundergraph/cosmo/connect-go v0.0.0-20241127080034-0681d60a5f89/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
14 changes: 9 additions & 5 deletions internal/api/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ 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, includeTags []string) (*platformv1.UpdateContractResponse, *ApiError) {
func (p *PlatformClient) UpdateContract(ctx context.Context, name, namespace string, excludeTags []string, includeTags []string, routingUrl, admissionWebhookUrl, admissionWebhookSecret, readme string) (*platformv1.UpdateContractResponse, *ApiError) {
request := connect.NewRequest(&platformv1.UpdateContractRequest{
Name: name,
Namespace: namespace,
ExcludeTags: excludeTags,
IncludeTags: includeTags,
Name: name,
Namespace: namespace,
ExcludeTags: excludeTags,
IncludeTags: includeTags,
RoutingUrl: &routingUrl,
AdmissionWebhookUrl: &admissionWebhookUrl,
AdmissionWebhookSecret: &admissionWebhookSecret,
Readme: &readme,
})

response, err := p.Client.UpdateContract(ctx, request)
Expand Down
6 changes: 3 additions & 3 deletions internal/api/subgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ 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 {
func (p PlatformClient) UpdateSubgraph(ctx context.Context, name, namespace, routingUrl string, labels []*platformv1.Label, headers []string, subscriptionUrl, readme string, unsetLabels *bool, subscriptionProtocol string, websocketSubprotocol string) *ApiError {
request := connect.NewRequest(&platformv1.UpdateSubgraphRequest{
Name: name,
RoutingUrl: &routingUrl,
Labels: labels,
Headers: headers,
SubscriptionUrl: subscriptionUrl,
Readme: readme,
SubscriptionUrl: &subscriptionUrl,
Readme: &readme,
Namespace: namespace,
UnsetLabels: unsetLabels,
WebsocketSubprotocol: resolveWebsocketSubprotocol(websocketSubprotocol),
Expand Down
17 changes: 16 additions & 1 deletion internal/service/contract/resource_cosmo_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,22 @@ func (r *contractResource) Update(ctx context.Context, req resource.UpdateReques
return
}

_, apiError := r.client.UpdateContract(ctx, data.Name.ValueString(), data.Namespace.ValueString(), excludeTags, includeTags)
admissionWebhookUrl := ""
if data.AdmissionWebhookUrl.ValueStringPointer() != nil {
admissionWebhookUrl = *data.AdmissionWebhookUrl.ValueStringPointer()
}

admissionWebhookSecret := ""
if data.AdmissionWebhookSecret.ValueStringPointer() != nil {
admissionWebhookSecret = *data.AdmissionWebhookSecret.ValueStringPointer()
}

readme := ""
if data.Readme.ValueStringPointer() != nil {
readme = *data.Readme.ValueStringPointer()
}

_, apiError := r.client.UpdateContract(ctx, data.Name.ValueString(), data.Namespace.ValueString(), excludeTags, includeTags, data.RoutingURL.ValueString(), admissionWebhookUrl, admissionWebhookSecret, readme)
if apiError != nil {
if api.IsContractCompositionFailedError(apiError) || api.IsSubgraphCompositionFailedError(apiError) {
utils.AddDiagnosticError(resp,
Expand Down
65 changes: 60 additions & 5 deletions internal/service/contract/resource_cosmo_contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,29 @@ func TestAccContractResource(t *testing.T) {
ProtoV6ProviderFactories: acceptance.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccContractResourceConfig(namespace, federatedGraphName, federatedGraphRoutingURL, subgraphName, subgraphRoutingURL, subgraphSchema, name, readme),
Config: testAccContractResourceConfig(namespace, federatedGraphName, federatedGraphRoutingURL, subgraphName, subgraphRoutingURL, subgraphSchema, name, "internal", &readme),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("cosmo_contract.test", "name", name),
resource.TestCheckResourceAttr("cosmo_contract.test", "namespace", namespace),
resource.TestCheckResourceAttr("cosmo_contract.test", "readme", readme),
resource.TestCheckResourceAttr("cosmo_contract.test", "exclude_tags.0", "internal"),
),
},
{
Config: testAccContractResourceConfig(namespace, federatedGraphName, federatedGraphRoutingURL, subgraphName, subgraphRoutingURL, subgraphSchema, name, "external", &readme),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("cosmo_contract.test", "name", name),
resource.TestCheckResourceAttr("cosmo_contract.test", "namespace", namespace),
resource.TestCheckResourceAttr("cosmo_contract.test", "readme", readme),
resource.TestCheckResourceAttr("cosmo_contract.test", "exclude_tags.0", "external"),
),
},
{
ResourceName: "cosmo_contract.test",
RefreshState: true,
},
{
Config: testAccContractResourceConfig(namespace, federatedGraphName, federatedGraphRoutingURL, subgraphName, subgraphRoutingURL, subgraphSchema, name, readme),
Config: testAccContractResourceConfig(namespace, federatedGraphName, federatedGraphRoutingURL, subgraphName, subgraphRoutingURL, subgraphSchema, name, "external", &readme),
Destroy: true,
},
{
Expand All @@ -58,7 +68,51 @@ func TestAccContractResource(t *testing.T) {
})
}

func testAccContractResourceConfig(namespace, federatedGraphName, federatedGraphRoutingURL, subgraphName, subgraphRoutingURL, subgraphSchema, contractName, contractReadme string) string {
func TestOptionalValuesOfContractResource(t *testing.T) {
name := acctest.RandomWithPrefix("test-contract")
namespace := acctest.RandomWithPrefix("test-namespace")

federatedGraphName := acctest.RandomWithPrefix("test-federated-graph")
federatedGraphRoutingURL := "https://example.com:3000"

subgraphName := acctest.RandomWithPrefix("test-subgraph")
subgraphRoutingURL := "https://subgraph-standalone-example.com"
subgraphSchema := acceptance.TestAccValidSubgraphSchema

readme := "Initial readme content"

resource.Test(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acceptance.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccContractResourceConfig(namespace, federatedGraphName, federatedGraphRoutingURL, subgraphName, subgraphRoutingURL, subgraphSchema, name, "internal", &readme),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("cosmo_contract.test", "name", name),
resource.TestCheckResourceAttr("cosmo_contract.test", "namespace", namespace),
resource.TestCheckResourceAttr("cosmo_contract.test", "exclude_tags.0", "internal"),
resource.TestCheckResourceAttr("cosmo_contract.test", "readme", readme),
),
},
{
Config: testAccContractResourceConfig(namespace, federatedGraphName, federatedGraphRoutingURL, subgraphName, subgraphRoutingURL, subgraphSchema, name, "external", nil),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("cosmo_contract.test", "name", name),
resource.TestCheckResourceAttr("cosmo_contract.test", "namespace", namespace),
resource.TestCheckResourceAttr("cosmo_contract.test", "exclude_tags.0", "external"),
resource.TestCheckNoResourceAttr("cosmo_contract.test", "readme"),
),
},
},
})
}

func testAccContractResourceConfig(namespace, federatedGraphName, federatedGraphRoutingURL, subgraphName, subgraphRoutingURL, subgraphSchema, contractName, contractExcludeTag string, contractReadme *string) string {

Check failure on line 110 in internal/service/contract/resource_cosmo_contract_test.go

View workflow job for this annotation

GitHub Actions / Build

`testAccContractResourceConfig` - `federatedGraphRoutingURL` always receives `federatedGraphRoutingURL` (`"https://example.com:3000"`) (unparam)

Check failure on line 110 in internal/service/contract/resource_cosmo_contract_test.go

View workflow job for this annotation

GitHub Actions / Build

`testAccContractResourceConfig` - `federatedGraphRoutingURL` always receives `federatedGraphRoutingURL` (`"https://example.com:3000"`) (unparam)
var readmePart string
if contractReadme != nil {
readmePart = fmt.Sprintf(`readme = "%s"`, *contractReadme)
}

return fmt.Sprintf(`
resource "cosmo_namespace" "test" {
name = "%s"
Expand Down Expand Up @@ -86,9 +140,10 @@ resource "cosmo_contract" "test" {
namespace = cosmo_namespace.test.name
source = cosmo_federated_graph.test.name
routing_url = "http://localhost:3003"
readme = "%s"
exclude_tags = ["%s"]
%s
}
`, namespace, federatedGraphName, federatedGraphRoutingURL, subgraphName, subgraphRoutingURL, subgraphSchema, contractName, contractReadme)
`, namespace, federatedGraphName, federatedGraphRoutingURL, subgraphName, subgraphRoutingURL, subgraphSchema, contractName, contractExcludeTag, readmePart)
}

func testAccContractOfMonographResourceConfig(namespace, federatedGraphName, federatedGraphRoutingURL, graphUrl, schema, contractName, contractReadme string) string {
Expand Down
26 changes: 18 additions & 8 deletions internal/service/federated-graph/resource_cosmo_federated_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,31 @@ func (r *FederatedGraphResource) Update(ctx context.Context, req resource.Update
return
}

readme := ""
if data.Readme.ValueStringPointer() != nil {
readme = *data.Readme.ValueStringPointer()
}

admissionWebhookUrl := ""
if data.AdmissionWebhookUrl.ValueStringPointer() != nil {
admissionWebhookUrl = *data.AdmissionWebhookUrl.ValueStringPointer()
}

admissionWebhookSecret := ""
if !data.AdmissionWebhookSecret.IsNull() {
admissionWebhookSecret = *data.AdmissionWebhookSecret.ValueStringPointer()
}

updatedGraph := platformv1.FederatedGraph{
Name: data.Name.ValueString(),
Namespace: data.Namespace.ValueString(),
RoutingURL: data.RoutingURL.ValueString(),
AdmissionWebhookUrl: data.AdmissionWebhookUrl.ValueStringPointer(),
AdmissionWebhookUrl: &admissionWebhookUrl,
LabelMatchers: labelMatchers,
Readme: data.Readme.ValueStringPointer(),
}

var admissionWebhookSecret *string
if !data.AdmissionWebhookSecret.IsNull() {
admissionWebhookSecret = data.AdmissionWebhookSecret.ValueStringPointer()
Readme: &readme,
}

_, apiError := r.client.UpdateFederatedGraph(ctx, admissionWebhookSecret, &updatedGraph)
_, apiError := r.client.UpdateFederatedGraph(ctx, &admissionWebhookSecret, &updatedGraph)
if apiError != nil {
if api.IsSubgraphCompositionFailedError(apiError) {
utils.AddDiagnosticError(resp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestAccFederatedGraphResource(t *testing.T) {
ProtoV6ProviderFactories: acceptance.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccFederatedGraphResourceConfig(namespace, name, routingURL, readme),
Config: testAccFederatedGraphResourceConfig(namespace, name, routingURL, &readme),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("cosmo_federated_graph.test", "name", name),
resource.TestCheckResourceAttr("cosmo_federated_graph.test", "namespace", namespace),
Expand All @@ -34,42 +34,53 @@ func TestAccFederatedGraphResource(t *testing.T) {
),
},
{
Config: testAccFederatedGraphResourceConfig(namespace, name, routingURL, newReadme),
Config: testAccFederatedGraphResourceConfig(namespace, name, routingURL, &newReadme),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("cosmo_federated_graph.test", "readme", newReadme),
),
},
{
Config: testAccFederatedGraphResourceConfig(namespace, name, updatedRoutingURL, newReadme),
Config: testAccFederatedGraphResourceConfig(namespace, name, updatedRoutingURL, nil),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("cosmo_federated_graph.test", "routing_url", updatedRoutingURL),
resource.TestCheckNoResourceAttr("cosmo_federated_graph.test", "readme"),
),
},
{
ResourceName: "cosmo_federated_graph.test",
RefreshState: true,
},
{
Config: testAccFederatedGraphResourceConfig(namespace, name, updatedRoutingURL, nil),
Destroy: true,
},
},
})
}

func TestAccFederatedGraphResourceInvalidConfig(t *testing.T) {
name := acctest.RandomWithPrefix("test-federated-graph")
namespace := acctest.RandomWithPrefix("test-namespace")
readme := "Initial readme content"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acceptance.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccFederatedGraphResourceConfig(name, namespace, "invalid-url", ""),
Config: testAccFederatedGraphResourceConfig(name, namespace, "invalid-url", &readme),
ExpectError: regexp.MustCompile(`.*Routing URL is not a valid URL*`),
},
},
})
}

func testAccFederatedGraphResourceConfig(namespace, name, routingURL, readme string) string {
func testAccFederatedGraphResourceConfig(namespace, name, routingURL string, readme *string) string {
var readmePart string
if readme != nil {
readmePart = fmt.Sprintf(`readme = "%s"`, *readme)
}

return fmt.Sprintf(`
resource "cosmo_namespace" "test" {
name = "%s"
Expand All @@ -79,7 +90,7 @@ resource "cosmo_federated_graph" "test" {
name = "%s"
namespace = cosmo_namespace.test.name
routing_url = "%s"
readme = "%s"
%s
}
`, namespace, name, routingURL, readme)
`, namespace, name, routingURL, readmePart)
}
22 changes: 21 additions & 1 deletion internal/service/subgraph/resource_cosmo_subgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,29 @@ func (r *SubgraphResource) Update(ctx context.Context, req resource.UpdateReques
unsetLabels = &[]bool{true}[0]
}

readme := ""
if data.Readme.ValueStringPointer() != nil {
readme = *data.Readme.ValueStringPointer()
}

subscriptionUrl := ""
if data.SubscriptionUrl.ValueStringPointer() != nil {
subscriptionUrl = *data.SubscriptionUrl.ValueStringPointer()
}

subscriptionProtocol := api.GraphQLSubscriptionProtocolWS
if data.SubscriptionProtocol.ValueStringPointer() != nil {
subscriptionProtocol = *data.SubscriptionProtocol.ValueStringPointer()
}

websocketSubprotocol := api.GraphQLWebsocketSubprotocolDefault
if data.WebsocketSubprotocol.ValueStringPointer() != nil {
websocketSubprotocol = *data.WebsocketSubprotocol.ValueStringPointer()
}

// TBD: This is only used in the update subgraph method and not used atm
// headers := utils.ConvertHeadersToStringList(data.Headers)
apiErr := r.client.UpdateSubgraph(ctx, data.Name.ValueString(), data.Namespace.ValueString(), data.RoutingURL.ValueString(), labels, []string{}, data.SubscriptionUrl.ValueStringPointer(), data.Readme.ValueStringPointer(), unsetLabels, data.SubscriptionProtocol.ValueString(), data.WebsocketSubprotocol.ValueString())
apiErr := r.client.UpdateSubgraph(ctx, data.Name.ValueString(), data.Namespace.ValueString(), data.RoutingURL.ValueString(), labels, []string{}, subscriptionUrl, readme, unsetLabels, subscriptionProtocol, websocketSubprotocol)
if apiErr != nil {
if api.IsSubgraphCompositionFailedError(apiErr) {
utils.AddDiagnosticWarning(resp,
Expand Down
Loading

0 comments on commit 1cfbbc7

Please sign in to comment.