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).