Skip to content

Commit

Permalink
feat:(cockpit): add push urls to cockpit datasources (#2700)
Browse files Browse the repository at this point in the history
* t add push url in ressource source

* handle error in createPushURL

* fix test
  • Loading branch information
jremy42 authored Aug 13, 2024
1 parent a7e77ec commit c02687d
Show file tree
Hide file tree
Showing 24 changed files with 2,944 additions and 1,891 deletions.
1 change: 1 addition & 0 deletions docs/resources/cockpit_source.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ In addition to all arguments above, the following attributes are exported:
~> **Important:** cockpit data sources' IDs are [regional](../guides/regions_and_zones.md#resource-ids), which means they are of the form `{region}/{id}`, e.g. `fr-par/11111111-1111-1111-1111-111111111111

- `url` - The URL of the cockpit data source.
- `push_url` - The URL endpoint used for pushing data to the cockpit data source.
- `origin` - The origin of the cockpit data source.
- `synchronized_with_grafana` - Indicates whether the data source is synchronized with Grafana.
- `created_at` - Date and time of the cockpit data source's creation (RFC 3339 format).
Expand Down
2 changes: 1 addition & 1 deletion internal/services/cockpit/cockpit.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interfac
endpoints := flattenCockpitEndpoints(dataSourcesRes.DataSources, grafana.GrafanaURL, alertManagerURL)

_ = d.Set("endpoints", endpoints)
_ = d.Set("push_url", createCockpitPushURL(endpoints))
_ = d.Set("push_url", createCockpitPushURLList(endpoints))

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/services/cockpit/cockpit_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func dataSourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interf
endpoints := flattenCockpitEndpoints(dataSourcesRes.DataSources, grafana.GrafanaURL, alertManagerURL)

_ = d.Set("endpoints", endpoints)
_ = d.Set("push_url", createCockpitPushURL(endpoints))
_ = d.Set("push_url", createCockpitPushURLList(endpoints))
d.SetId(projectID)
return nil
}
2 changes: 1 addition & 1 deletion internal/services/cockpit/cockpit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestAccCockpit_WithSourceEndpoints(t *testing.T) {
resource "scaleway_cockpit_grafana_user" "main" {
project_id = scaleway_account_project.project.id
login = "cockpit_test"
login = "cockpit_test_endpoint"
role = "editor"
}
Expand Down
1 change: 1 addition & 0 deletions internal/services/cockpit/helpers_cockpit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
DefaultCockpitTimeout = 5 * time.Minute
pathMetricsURL = "/api/v1/push"
pathLogsURL = "/loki/api/v1/push"
pathTracesURL = "/otlp/v1/traces"
)

// NewGlobalAPI returns a new global cockpit API.
Expand Down
11 changes: 11 additions & 0 deletions internal/services/cockpit/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func ResourceCockpitSource() *schema.Resource {
Computed: true,
Description: "The date and time of the last update of the cockpit datasource",
},
"push_url": {
Type: schema.TypeString,
Computed: true,
Description: "The URL endpoint used for pushing data to the cockpit data source.",
},
"project_id": account.ProjectIDSchema(),
"region": regional.Schema(),
},
Expand Down Expand Up @@ -112,6 +117,11 @@ func ResourceCockpitSourceRead(ctx context.Context, d *schema.ResourceData, meta
return diag.FromErr(err)
}

pushURL, err := createCockpitPushURL(res.Type, res.URL)
if err != nil {
return diag.FromErr(err)
}

_ = d.Set("name", res.Name)
_ = d.Set("type", res.Type.String())
_ = d.Set("url", res.URL)
Expand All @@ -121,6 +131,7 @@ func ResourceCockpitSourceRead(ctx context.Context, d *schema.ResourceData, meta
_ = d.Set("created_at", types.FlattenTime(res.CreatedAt))
_ = d.Set("updated_at", types.FlattenTime(res.UpdatedAt))
_ = d.Set("project_id", res.ProjectID)
_ = d.Set("push_url", pushURL)

return nil
}
Expand Down
42 changes: 41 additions & 1 deletion internal/services/cockpit/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/cockpit"
)

func TestAccCockpitSource_Basic(t *testing.T) {
func TestAccCockpitSource_Basic_metrics(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()

Expand All @@ -39,6 +39,46 @@ func TestAccCockpitSource_Basic(t *testing.T) {
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "type", "metrics"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "region", "fr-par"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "url"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "push_url"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "origin"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "created_at"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "updated_at"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "synchronized_with_grafana"),
resource.TestCheckResourceAttrPair("scaleway_cockpit_source.main", "project_id", "scaleway_account_project.project", "id"),
),
},
},
})
}

func TestAccCockpitSource_Basic_logs(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: isSourceDestroyed(tt),
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_account_project" "project" {
name = "tf_tests_cockpit_datasource_basic"
}
resource "scaleway_cockpit_source" "main" {
project_id = scaleway_account_project.project.id
name = "my-source"
type = "logs"
}
`,
Check: resource.ComposeTestCheckFunc(
isSourcePresent(tt, "scaleway_cockpit_source.main"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "name", "my-source"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "type", "logs"),
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "region", "fr-par"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "url"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "push_url"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "origin"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "created_at"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "updated_at"),
Expand Down
Loading

0 comments on commit c02687d

Please sign in to comment.