From 0b9a27196d312ea88792e005afc8f99e4a0d67e9 Mon Sep 17 00:00:00 2001 From: Victor Moraes Date: Wed, 27 Sep 2023 13:21:01 -0300 Subject: [PATCH] ENG-12678: Add data source for sidecar health API (#456) * Add cyral_sidecar_health data source * Add tests * Add docs * Update status field description --- cyral/data_source_cyral_sidecar_health.go | 65 +++++++++++++++++ .../data_source_cyral_sidecar_health_test.go | 71 +++++++++++++++++++ cyral/provider.go | 1 + docs/data-sources/sidecar_health.md | 24 +++++++ 4 files changed, 161 insertions(+) create mode 100644 cyral/data_source_cyral_sidecar_health.go create mode 100644 cyral/data_source_cyral_sidecar_health_test.go create mode 100644 docs/data-sources/sidecar_health.md diff --git a/cyral/data_source_cyral_sidecar_health.go b/cyral/data_source_cyral_sidecar_health.go new file mode 100644 index 00000000..7012e488 --- /dev/null +++ b/cyral/data_source_cyral_sidecar_health.go @@ -0,0 +1,65 @@ +package cyral + +import ( + "fmt" + "net/http" + + "github.com/cyralinc/terraform-provider-cyral/client" + "github.com/google/uuid" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +const ( + // Schema keys + StatusKey = "status" +) + +type SidecarHealth struct { + Status string `json:"status"` +} + +func (health *SidecarHealth) WriteToSchema(d *schema.ResourceData) error { + d.SetId(uuid.New().String()) + d.Set(StatusKey, health.Status) + + return nil +} + +func dataSourceSidecarHealth() *schema.Resource { + return &schema.Resource{ + Description: "Retrieve aggregated information about the " + + "[sidecar's health](https://cyral.com/docs/sidecars/sidecar-manage/#check-sidecar-cluster-status), " + + "considering all instances of the sidecar.", + ReadContext: ReadResource(ResourceOperationConfig{ + Name: "SidecarHealthDataSourceRead", + HttpMethod: http.MethodGet, + CreateURL: func(d *schema.ResourceData, c *client.Client) string { + return fmt.Sprintf( + "https://%s/v2/sidecars/%s/health", c.ControlPlane, d.Get(SidecarIDKey), + ) + }, + NewResponseData: func(_ *schema.ResourceData) ResponseData { + return &SidecarHealth{} + }, + }), + Schema: map[string]*schema.Schema{ + SidecarIDKey: { + Description: "ID of the Sidecar that will be used to retrieve health information.", + Type: schema.TypeString, + Required: true, + }, + IDKey: { + Description: "Data source identifier.", + Type: schema.TypeString, + Computed: true, + }, + StatusKey: { + Description: "Sidecar health status. Possible values are: `HEALTHY`, `DEGRADED`, `UNHEALTHY` " + + "and `UNKNOWN`. For more information, see " + + "[Sidecar Status](https://cyral.com/docs/sidecars/sidecar-manage/#check-sidecar-cluster-status).", + Type: schema.TypeString, + Computed: true, + }, + }, + } +} diff --git a/cyral/data_source_cyral_sidecar_health_test.go b/cyral/data_source_cyral_sidecar_health_test.go new file mode 100644 index 00000000..14d980cf --- /dev/null +++ b/cyral/data_source_cyral_sidecar_health_test.go @@ -0,0 +1,71 @@ +package cyral + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +const ( + sidecarHealthDataSourceFullNameFmt = "data.cyral_sidecar_health.%s" +) + +func TestAccSidecarHealthDataSource(t *testing.T) { + dataSourceName := "sidecar_health" + testSteps := []resource.TestStep{ + accTestStepSidecarHealthDataSource_RequiredArgumentSidecarID(dataSourceName), + accTestStepSidecarHealthDataSource_ListAllSidecarHealthInfo(dataSourceName), + } + resource.ParallelTest(t, resource.TestCase{ + ProviderFactories: providerFactories, + Steps: testSteps, + }) +} + +func accTestStepSidecarHealthDataSource_RequiredArgumentSidecarID(dataSourceName string) resource.TestStep { + config := fmt.Sprintf(` + data "cyral_sidecar_health" "%s" { + } + `, dataSourceName) + return resource.TestStep{ + Config: config, + ExpectError: regexp.MustCompile( + fmt.Sprintf( + `The argument "%s" is required, but no definition was found.`, + SidecarIDKey, + ), + ), + } +} + +func accTestStepSidecarHealthDataSource_ListAllSidecarHealthInfo(dataSourceName string) resource.TestStep { + config := formatBasicSidecarIntoConfig( + basicSidecarResName, + accTestName("data-sidecar-health", "sidecar"), + "cft-ec2", + "", + ) + config += fmt.Sprintf(` + data "cyral_sidecar_health" "%s" { + sidecar_id = %s + } + `, dataSourceName, basicSidecarID) + dataSourceFullName := fmt.Sprintf(sidecarHealthDataSourceFullNameFmt, dataSourceName) + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet( + dataSourceFullName, + IDKey, + ), + resource.TestCheckResourceAttr( + dataSourceFullName, + StatusKey, + "UNKNOWN", + ), + ) + return resource.TestStep{ + Config: config, + Check: check, + } +} diff --git a/cyral/provider.go b/cyral/provider.go index b5312896..60050fc6 100644 --- a/cyral/provider.go +++ b/cyral/provider.go @@ -82,6 +82,7 @@ func Provider() *schema.Provider { "cyral_saml_configuration": dataSourceSAMLConfiguration(), "cyral_sidecar_bound_ports": dataSourceSidecarBoundPorts(), "cyral_sidecar_cft_template": dataSourceSidecarCftTemplate(), + "cyral_sidecar_health": dataSourceSidecarHealth(), "cyral_sidecar_id": dataSourceSidecarID(), "cyral_sidecar_instance_ids": dataSourceSidecarInstanceIDs(), "cyral_sidecar_listener": dataSourceSidecarListener(), diff --git a/docs/data-sources/sidecar_health.md b/docs/data-sources/sidecar_health.md new file mode 100644 index 00000000..05aa05dd --- /dev/null +++ b/docs/data-sources/sidecar_health.md @@ -0,0 +1,24 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "cyral_sidecar_health Data Source - terraform-provider-cyral" +subcategory: "" +description: |- + Retrieve aggregated information about the sidecar's health https://cyral.com/docs/sidecars/sidecar-manage/#check-sidecar-cluster-status, considering all instances of the sidecar. +--- + +# cyral_sidecar_health (Data Source) + +Retrieve aggregated information about the [sidecar's health](https://cyral.com/docs/sidecars/sidecar-manage/#check-sidecar-cluster-status), considering all instances of the sidecar. + + + +## Schema + +### Required + +- `sidecar_id` (String) ID of the Sidecar that will be used to retrieve health information. + +### Read-Only + +- `id` (String) Data source identifier. +- `status` (String) Sidecar health status. Possible values are: `HEALTHY`, `DEGRADED`, `UNHEALTHY` and `UNKNOWN`. For more information, see [Sidecar Status](https://cyral.com/docs/sidecars/sidecar-manage/#check-sidecar-cluster-status).