-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |