Skip to content

Commit

Permalink
ENG-12678: Add data source for sidecar health API (#456)
Browse files Browse the repository at this point in the history
* Add cyral_sidecar_health data source

* Add tests

* Add docs

* Update status field description
  • Loading branch information
VictorGFM authored Sep 27, 2023
1 parent 2583fe2 commit 0b9a271
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
65 changes: 65 additions & 0 deletions cyral/data_source_cyral_sidecar_health.go
Original file line number Diff line number Diff line change
@@ -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,
},
},
}
}
71 changes: 71 additions & 0 deletions cyral/data_source_cyral_sidecar_health_test.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
1 change: 1 addition & 0 deletions cyral/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
24 changes: 24 additions & 0 deletions docs/data-sources/sidecar_health.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->

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

0 comments on commit 0b9a271

Please sign in to comment.