Skip to content

Commit

Permalink
Add placeholder tests and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
fridgepoet committed Sep 26, 2024
1 parent 2e0d20b commit 8b0fc58
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 0 deletions.
35 changes: 35 additions & 0 deletions docs/data-sources/connections_metrics_endpoint_scrape_job.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "grafana_connections_metrics_endpoint_scrape_job Data Source - terraform-provider-grafana"
subcategory: "Connections"
description: |-
---

# grafana_connections_metrics_endpoint_scrape_job (Data Source)





<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `authentication_method` (String) Method to pass authentication credentials: basic or bearer.
- `name` (String) The name of the Metrics Endpoint Scrape Job. Part of the Terraform Resource ID.
- `stack_id` (String) The Stack ID of the Grafana Cloud instance. Part of the Terraform Resource ID.
- `url` (String) The url to scrape metrics.

### Optional

- `authentication_basic_password` (String, Sensitive) Password for basic authentication.
- `authentication_basic_username` (String) Username for basic authentication.
- `authentication_bearer_token` (String, Sensitive) Token for authentication bearer.

### Read-Only

- `enabled` (Boolean) Whether the Metrics Endpoint Scrape Job is enabled or not.
- `id` (String) The Terraform Resource ID. This has the format "{{ stack_id }}:{{ name }}".
- `scrape_interval_seconds` (Number) Frequency for scraping the metrics endpoint: 30, 60, or 120 seconds.
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ resource "grafana_oncall_escalation" "example_notify_step" {
- `ca_cert` (String) Certificate CA bundle (file path or literal value) to use to verify the Grafana server's certificate. May alternatively be set via the `GRAFANA_CA_CERT` environment variable.
- `cloud_access_policy_token` (String, Sensitive) Access Policy Token for Grafana Cloud. May alternatively be set via the `GRAFANA_CLOUD_ACCESS_POLICY_TOKEN` environment variable.
- `cloud_api_url` (String) Grafana Cloud's API URL. May alternatively be set via the `GRAFANA_CLOUD_API_URL` environment variable.
- `connections_access_token` (String, Sensitive) A Grafana Connections API access token. May alternatively be set via the `GRAFANA_CONNECTIONS_ACCESS_TOKEN` environment variable.
- `connections_url` (String) A Grafana Connections API backend address. May alternatively be set via the `GRAFANA_CONNECTIONS_URL` environment variable.
- `http_headers` (Map of String, Sensitive) Optional. HTTP headers mapping keys to values used for accessing the Grafana and Grafana Cloud APIs. May alternatively be set via the `GRAFANA_HTTP_HEADERS` environment variable in JSON format.
- `insecure_skip_verify` (Boolean) Skip TLS certificate verification. May alternatively be set via the `GRAFANA_INSECURE_SKIP_VERIFY` environment variable.
- `oncall_access_token` (String, Sensitive) A Grafana OnCall access token. May alternatively be set via the `GRAFANA_ONCALL_ACCESS_TOKEN` environment variable.
Expand Down
54 changes: 54 additions & 0 deletions docs/resources/connections_metrics_endpoint_scrape_job.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "grafana_connections_metrics_endpoint_scrape_job Resource - terraform-provider-grafana"
subcategory: "Connections"
description: |-
---

# grafana_connections_metrics_endpoint_scrape_job (Resource)



## Example Usage

```terraform
resource "grafana_connections_metrics_endpoint_scrape_job" "test" {
stack_id = "test-stack-id"
name = "my-scrape-job"
authentication_method = "basic"
authentication_basic_username = "my_username"
authentication_basic_password = "my_password"
url = "https://dev.my-metrics-endpoint-url.com:9000/metrics"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `authentication_method` (String) Method to pass authentication credentials: basic or bearer.
- `name` (String) The name of the Metrics Endpoint Scrape Job. Part of the Terraform Resource ID.
- `stack_id` (String) The Stack ID of the Grafana Cloud instance. Part of the Terraform Resource ID.
- `url` (String) The url to scrape metrics; a valid HTTPs URL is required.

### Optional

- `authentication_basic_password` (String, Sensitive) Password for basic authentication.
- `authentication_basic_username` (String) Username for basic authentication.
- `authentication_bearer_token` (String, Sensitive) Token for authentication bearer.
- `enabled` (Boolean) Whether the Metrics Endpoint Scrape Job is enabled or not.
- `scrape_interval_seconds` (Number) Frequency for scraping the metrics endpoint: 30, 60, or 120 seconds.

### Read-Only

- `id` (String) The Terraform Resource ID. This has the format "{{ stack_id }}:{{ name }}".

## Import

Import is supported using the following syntax:

```shell
terraform import grafana_connections_metrics_endpoint_scrape_job.name "{{ stack_id }}:{{ name }}"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data "grafana_connections_metrics_endpoint_scrape_job" "test" {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import grafana_connections_metrics_endpoint_scrape_job.name "{{ stack_id }}:{{ name }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "grafana_connections_metrics_endpoint_scrape_job" "test" {
stack_id = "test-stack-id"
name = "my-scrape-job"
authentication_method = "basic"
authentication_basic_username = "my_username"
authentication_basic_password = "my_password"
url = "https://dev.my-metrics-endpoint-url.com:9000/metrics"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "grafana_connections_metrics_endpoint_scrape_job" "test" {
stack_id = "test-stack-id"
name = "modified-scrape-job"
enabled = "false"
authentication_method = "bearer"
authentication_bearer_token = "test-token"
url = "https://www.modified-url.com:9000/metrics"
scrape_interval_seconds = "120"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package connections_test

import (
"testing"

"github.com/grafana/terraform-provider-grafana/v3/internal/testutils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAcc_DataSourceMetricsEndpointScrapeJob(t *testing.T) {
// Run this test by removing t.Skip and set env variables TF_ACC=1;GRAFANA_CONNECTIONS_ACCESS_TOKEN=whatever
// in order to test the resource code "scaffolding".
// Expected result: test fails and the error message should be "failed to create metrics endpoint scrape job: failed to do request"
// because the Connections API is not yet available.

// t.Skip("will be enabled after Connections API is available in prod")

// testutils.CheckCloudInstanceTestsEnabled(t) // TODO: enable after Connections API is available
resource.ParallelTest(t, resource.TestCase{
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
// Create this resource
Config: testutils.TestAccExample(t, "resources/grafana_connections_metrics_endpoint_scrape_job/resource.tf"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "stack_id", "test-stack-id"),
resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "name", "my-scrape-job"),
resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "enabled", "true"),
resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "authentication_method", "basic"),
resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "authentication_basic_username", "my_username"),
resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "authentication_basic_password", "my_password"),
resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "url", "https://dev.my-metrics-endpoint-url.com:9000/metrics"),
resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "scrape_interval_seconds", "60"),
testutils.CheckLister("grafana_connections_metrics_endpoint_scrape_job.test"),
),
},
{
// Verifies that the created SLO Resource is read by the Datasource Read Method
// TODO: work on after other Test Step passes
// Config: testutils.TestAccExample(t, "data-sources/grafana_connections_metrics_endpoint_scrape_job/resource.tf"),
// RefreshState: true,
// Check: resource.ComposeTestCheckFunc(
// resource.TestCheckResourceAttrSet("data.grafana_connections_metrics_endpoint_scrape_job.test", "test.0.name"),
// ),
},
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,84 @@ package connections_test

import (
"context"
"errors"
"fmt"
"testing"

"github.com/grafana/terraform-provider-grafana/v3/internal/common"
"github.com/grafana/terraform-provider-grafana/v3/internal/common/connectionsapi"
"github.com/grafana/terraform-provider-grafana/v3/internal/resources/connections"
"github.com/grafana/terraform-provider-grafana/v3/internal/testutils"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/stretchr/testify/assert"
)

func TestAcc_MetricsEndpointScrapeJob(t *testing.T) {
// Run this test by removing t.Skip and set env variables TF_ACC=1;GRAFANA_CONNECTIONS_ACCESS_TOKEN=whatever
// in order to test the resource code "scaffolding".
// Expected result: test fails and the error message should be "failed to create metrics endpoint scrape job: failed to do request"
// because the Connections API is not yet available.

// t.Skip("will be enabled after Connections API is available in prod")

// testutils.CheckCloudInstanceTestsEnabled(t) // TODO: enable after Connections API is available
resource.ParallelTest(t, resource.TestCase{
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
CheckDestroy: testAccMetricsEndpointCheckDestroy("test-stack-id", "my-scrape-job"),
Steps: []resource.TestStep{
{
// Test Create
Config: testutils.TestAccExample(t, "resources/grafana_connections_metrics_endpoint_scrape_job/resource.tf"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "stack_id", "test-stack-id"),
resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "name", "my-scrape-job"),
resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "enabled", "true"),
resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "authentication_method", "basic"),
resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "authentication_basic_username", "my_username"),
resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "authentication_basic_password", "my_password"),
resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "url", "https://dev.my-metrics-endpoint-url.com:9000/metrics"),
resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "scrape_interval_seconds", "60"),
testutils.CheckLister("grafana_connections_metrics_endpoint_scrape_job.test"),
),
},
// TODO: work on this after "Test Create" and "CheckDestroy" pass
// {
// // Tests Update
// Config: testutils.TestAccExample(t, "resources/grafana_connections_metrics_endpoint_scrape_job/resource_update.tf"),
// Check: resource.ComposeTestCheckFunc(
// resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "stack_id", "test-stack-id"),
// resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "name", "modified-scrape-job"),
// resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "enabled", "false"),
// resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "authentication_method", "bearer"),
// resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "authentication_bearer_token", "test-token"),
// resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "url", "https://www.modified-url.com:9000/metrics"),
// resource.TestCheckResourceAttr("grafana_connections_metrics_endpoint_scrape_job.test", "scrape_interval_seconds", "120"),
// ),
// },
},
})
}

func testAccMetricsEndpointCheckDestroy(stackID string, jobName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testutils.Provider.Meta().(*common.Client).ConnectionsAPIClient
_, err := client.GetMetricsEndpointScrapeJob(context.Background(), stackID, jobName)
if err != nil {
if errors.Is(err, connectionsapi.ErrNotFound) {
return nil
}
return fmt.Errorf("metrics endpoint job should return ErrNotFound but returned error %s", err.Error())
}

return fmt.Errorf("metrics endpoint job should return ErrNotFound but returned no error")
}
}

func Test_httpsURLValidator(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
Expand Down

0 comments on commit 8b0fc58

Please sign in to comment.