Skip to content

Commit

Permalink
ENG-12680: Add data source for sidecar instance stats API (#459)
Browse files Browse the repository at this point in the history
* Add cyral_sidecar_instance_stats data source

* Fix types for queriesPerSecond and activeConnections
  • Loading branch information
VictorGFM authored Oct 9, 2023
1 parent dff2196 commit d4b7bac
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 17 deletions.
79 changes: 79 additions & 0 deletions cyral/data_source_cyral_sidecar_instance_stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cyral

import (
"fmt"
"net/http"

"github.com/cyralinc/terraform-provider-cyral/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

const (
// Schema keys
InstanceIDKey = "instance_id"
QueriesPerSecondKey = "queries_per_second"
ActiveConnectionsKey = "active_connections"
)

type SidecarInstanceStats struct {
QueriesPerSecond float32 `json:"queriesPerSecond"`
ActiveConnections uint32 `json:"activeConnections"`
}

func (stats *SidecarInstanceStats) WriteToSchema(d *schema.ResourceData) error {
d.SetId(d.Get(InstanceIDKey).(string))
d.Set(QueriesPerSecondKey, stats.QueriesPerSecond)
d.Set(ActiveConnectionsKey, stats.ActiveConnections)

return nil
}

func dataSourceSidecarInstanceStats() *schema.Resource {
return &schema.Resource{
Description: "Retrieve sidecar instance statistics. See also data source " +
"[`cyral_sidecar_instance`](../data-sources/sidecar_instance.md).",
ReadContext: ReadResource(ResourceOperationConfig{
Name: "SidecarInstanceStatsDataSourceRead",
HttpMethod: http.MethodGet,
CreateURL: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf(
"https://%s/v2/sidecars/%s/instances/%s/stats",
c.ControlPlane,
d.Get(SidecarIDKey),
d.Get(InstanceIDKey),
)
},
NewResponseData: func(_ *schema.ResourceData) ResponseData {
return &SidecarInstanceStats{}
},
}),
Schema: map[string]*schema.Schema{
SidecarIDKey: {
Description: "Sidecar identifier.",
Type: schema.TypeString,
Required: true,
},
InstanceIDKey: {
Description: "Sidecar instance identifier. See also data source " +
"[`cyral_sidecar_instance`](../data-sources/sidecar_instance.md).",
Type: schema.TypeString,
Required: true,
},
IDKey: {
Description: fmt.Sprintf("Data source identifier. It's equal to `%s`.", InstanceIDKey),
Type: schema.TypeString,
Computed: true,
},
QueriesPerSecondKey: {
Description: "Amount of queries that the sidecar instance receives per second.",
Type: schema.TypeFloat,
Computed: true,
},
ActiveConnectionsKey: {
Description: "Number of active connections.",
Type: schema.TypeInt,
Computed: true,
},
},
}
}
83 changes: 83 additions & 0 deletions cyral/data_source_cyral_sidecar_instance_stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cyral

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccSidecarInstanceStatsDataSource(t *testing.T) {
dataSourceName := "instance_stats"
testSteps := []resource.TestStep{
accTestStepSidecarInstanceStatsDataSource_EmptySidecarID(dataSourceName),
accTestStepSidecarInstanceStatsDataSource_EmptyInstanceID(dataSourceName),
accTestStepSidecarInstanceStatsDataSource_NoSidecarFoundForGivenID(dataSourceName),
accTestStepSidecarInstanceStatsDataSource_NoInstanceFoundForGivenID(dataSourceName),
}
resource.ParallelTest(
t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: testSteps,
},
)
}

func accTestStepSidecarInstanceStatsDataSource_EmptySidecarID(dataSourceName string) resource.TestStep {
config := fmt.Sprintf(`
data "cyral_sidecar_instance_stats" "%s" {
}
`, dataSourceName)
return resource.TestStep{
Config: config,
ExpectError: regexp.MustCompile(fmt.Sprintf(`The argument "%s" is required`, SidecarIDKey)),
}
}

func accTestStepSidecarInstanceStatsDataSource_EmptyInstanceID(dataSourceName string) resource.TestStep {
config := fmt.Sprintf(`
data "cyral_sidecar_instance_stats" "%s" {
sidecar_id = %q
}
`, dataSourceName, "some-sidecar-id")
return resource.TestStep{
Config: config,
ExpectError: regexp.MustCompile(fmt.Sprintf(`The argument "%s" is required`, InstanceIDKey)),
}
}

func accTestStepSidecarInstanceStatsDataSource_NoSidecarFoundForGivenID(dataSourceName string) resource.TestStep {
nonExistentSidecarID := "some-non-existent-sidecar-id"
config := fmt.Sprintf(`
data "cyral_sidecar_instance_stats" "%s" {
sidecar_id = %q
instance_id = %q
}
`, dataSourceName, nonExistentSidecarID, "some-instance-id")
return resource.TestStep{
Config: config,
ExpectError: regexp.MustCompile(fmt.Sprintf("sidecar with id '%s' does not exist", nonExistentSidecarID)),
}
}

func accTestStepSidecarInstanceStatsDataSource_NoInstanceFoundForGivenID(dataSourceName string) resource.TestStep {
// Creates a sidecar that doesn't have any instances, since it was not
// deployed.
config := formatBasicSidecarIntoConfig(
basicSidecarResName,
accTestName("data-sidecar-instance", "sidecar"),
"cft-ec2",
"",
)
config += fmt.Sprintf(`
data "cyral_sidecar_instance_stats" "%s" {
sidecar_id = %s
instance_id = %q
}
`, dataSourceName, basicSidecarID, "some-non-existent-instance-id")
return resource.TestStep{
Config: config,
ExpectError: regexp.MustCompile("instance not found"),
}
}
35 changes: 18 additions & 17 deletions cyral/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,24 @@ func Provider() *schema.Provider {
},
},
DataSourcesMap: map[string]*schema.Resource{
"cyral_datalabel": dataSourceDatalabel(),
"cyral_integration_idp": dataSourceIntegrationIdP(),
"cyral_integration_idp_saml": dataSourceIntegrationIdPSAML(),
"cyral_integration_logging": dataSourceIntegrationLogging(),
"cyral_permission": dataSourcePermission(),
"cyral_repository": dataSourceRepository(),
"cyral_role": dataSourceRole(),
"cyral_saml_certificate": dataSourceSAMLCertificate(),
"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_instance": dataSourceSidecarInstance(),
"cyral_sidecar_listener": dataSourceSidecarListener(),
"cyral_system_info": dataSourceSystemInfo(),
"cyral_datalabel": dataSourceDatalabel(),
"cyral_integration_idp": dataSourceIntegrationIdP(),
"cyral_integration_idp_saml": dataSourceIntegrationIdPSAML(),
"cyral_integration_logging": dataSourceIntegrationLogging(),
"cyral_permission": dataSourcePermission(),
"cyral_repository": dataSourceRepository(),
"cyral_role": dataSourceRole(),
"cyral_saml_certificate": dataSourceSAMLCertificate(),
"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_instance_stats": dataSourceSidecarInstanceStats(),
"cyral_sidecar_instance": dataSourceSidecarInstance(),
"cyral_sidecar_listener": dataSourceSidecarListener(),
"cyral_system_info": dataSourceSystemInfo(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
26 changes: 26 additions & 0 deletions docs/data-sources/sidecar_instance_stats.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "cyral_sidecar_instance_stats Data Source - terraform-provider-cyral"
subcategory: ""
description: |-
Retrieve sidecar instance statistics. See also data source cyral_sidecar_instance ../data-sources/sidecar_instance.md.
---

# cyral_sidecar_instance_stats (Data Source)

Retrieve sidecar instance statistics. See also data source [`cyral_sidecar_instance`](../data-sources/sidecar_instance.md).

<!-- schema generated by tfplugindocs -->

## Schema

### Required

- `instance_id` (String) Sidecar instance identifier. See also data source [`cyral_sidecar_instance`](../data-sources/sidecar_instance.md).
- `sidecar_id` (String) Sidecar identifier.

### Read-Only

- `active_connections` (Number) Number of active connections.
- `id` (String) Data source identifier. It's equal to `instance_id`.
- `queries_per_second` (Number) Amount of queries that the sidecar instance receives per second.

0 comments on commit d4b7bac

Please sign in to comment.