-
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_instance_stats data source * Fix types for queriesPerSecond and activeConnections
- Loading branch information
Showing
4 changed files
with
206 additions
and
17 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,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, | ||
}, | ||
}, | ||
} | ||
} |
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,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"), | ||
} | ||
} |
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,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. |