Skip to content

Commit 16eb0eb

Browse files
jremy42protobuf-ci-cd
andauthored
fix(cockpit): handle region in data source source (#3195)
* fix(cockpit): handle region in data source source * fix lint --------- Co-authored-by: protobuf-ci-cd <protobuf-ci-cd@scaleway.com>
1 parent bc03f52 commit 16eb0eb

6 files changed

+1120
-427
lines changed

internal/services/cockpit/source_data_source.go

Lines changed: 32 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,80 +5,30 @@ import (
55

66
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
98
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
109
"github.com/scaleway/scaleway-sdk-go/scw"
10+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
1111
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
12-
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
12+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
1313
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
1414
)
1515

1616
func DataSourceCockpitSource() *schema.Resource {
17+
dsSchema := datasource.SchemaFromResourceSchema(ResourceCockpitSource().Schema)
18+
19+
dsSchema["id"] = &schema.Schema{
20+
Type: schema.TypeString,
21+
Optional: true,
22+
Computed: true,
23+
Description: "ID of the data source.",
24+
}
25+
26+
datasource.FixDatasourceSchemaFlags(dsSchema, false, "id", "name", "project_id")
27+
datasource.AddOptionalFieldsToSchema(dsSchema, "region", "type", "origin")
28+
1729
return &schema.Resource{
1830
ReadContext: dataSourceCockpitSourceRead,
19-
Schema: map[string]*schema.Schema{
20-
"id": {
21-
Type: schema.TypeString,
22-
Optional: true,
23-
Computed: true,
24-
Description: "ID of the data source.",
25-
},
26-
"region": {
27-
Type: schema.TypeString,
28-
Computed: true,
29-
Description: "The region of the data source.",
30-
},
31-
"project_id": account.ProjectIDSchema(),
32-
"name": {
33-
Type: schema.TypeString,
34-
Optional: true,
35-
Computed: true,
36-
Description: "The name of the data source.",
37-
},
38-
"type": {
39-
Type: schema.TypeString,
40-
Optional: true,
41-
Computed: true,
42-
Description: "The type of the data source (e.g., 'metrics', 'logs', 'traces').",
43-
ValidateFunc: validation.StringInSlice([]string{
44-
"metrics", "logs", "traces",
45-
}, false),
46-
},
47-
"origin": {
48-
Type: schema.TypeString,
49-
Optional: true,
50-
Computed: true,
51-
Description: "The origin of the data source (e.g., 'scaleway', 'external', 'custom').",
52-
ValidateFunc: validation.StringInSlice([]string{
53-
"scaleway", "external", "custom",
54-
}, false),
55-
},
56-
"url": {
57-
Type: schema.TypeString,
58-
Computed: true,
59-
Description: "The URL of the data source.",
60-
},
61-
"created_at": {
62-
Type: schema.TypeString,
63-
Computed: true,
64-
Description: "The creation date of the data source.",
65-
},
66-
"updated_at": {
67-
Type: schema.TypeString,
68-
Computed: true,
69-
Description: "The last update date of the data source.",
70-
},
71-
"synchronized_with_grafana": {
72-
Type: schema.TypeBool,
73-
Computed: true,
74-
Description: "Whether the data source is synchronized with Grafana.",
75-
},
76-
"retention_days": {
77-
Type: schema.TypeInt,
78-
Computed: true,
79-
Description: "The retention period of the data source in days.",
80-
},
81-
},
31+
Schema: dsSchema,
8232
}
8333
}
8434

@@ -113,12 +63,25 @@ func fetchDataSourceByID(ctx context.Context, d *schema.ResourceData, meta any)
11363
return nil
11464
}
11565

116-
func fetchDataSourceByFilters(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
117-
api, region, err := cockpitAPIWithRegion(d, meta)
118-
if err != nil {
119-
return diag.FromErr(err)
66+
func fetchDataSourceByFilters(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
67+
var region scw.Region
68+
69+
var err error
70+
71+
if v, ok := d.GetOk("region"); ok && v.(string) != "" {
72+
region, err = scw.ParseRegion(v.(string))
73+
if err != nil {
74+
return diag.FromErr(err)
75+
}
76+
} else {
77+
_, region, err = cockpitAPIWithRegion(d, m)
78+
if err != nil {
79+
return diag.FromErr(err)
80+
}
12081
}
12182

83+
api := cockpit.NewRegionalAPI(meta.ExtractScwClient(m))
84+
12285
req := &cockpit.RegionalAPIListDataSourcesRequest{
12386
Region: region,
12487
ProjectID: d.Get("project_id").(string),

internal/services/cockpit/source_data_source_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,44 @@ func TestAccCockpitSource_DataSource_Defaults(t *testing.T) {
129129
},
130130
})
131131
}
132+
133+
func TestAccCockpitSource_DataSource_ByRegion(t *testing.T) {
134+
tt := acctest.NewTestTools(t)
135+
defer tt.Cleanup()
136+
137+
resource.ParallelTest(t, resource.TestCase{
138+
PreCheck: func() { acctest.PreCheck(t) },
139+
ProviderFactories: tt.ProviderFactories,
140+
CheckDestroy: isSourceDestroyed(tt),
141+
Steps: []resource.TestStep{
142+
{
143+
Config: `
144+
resource "scaleway_account_project" "project" {
145+
name = "tf_tests_cockpit_datasource_by_region"
146+
}
147+
148+
resource "scaleway_cockpit_source" "main" {
149+
project_id = scaleway_account_project.project.id
150+
name = "source-by-region"
151+
type = "logs"
152+
retention_days = 30
153+
region = "fr-par"
154+
}
155+
156+
data "scaleway_cockpit_source" "by_region" {
157+
project_id = scaleway_account_project.project.id
158+
name = "source-by-region"
159+
region = "fr-par"
160+
depends_on = [scaleway_cockpit_source.main]
161+
}
162+
`,
163+
Check: resource.ComposeTestCheckFunc(
164+
resource.TestCheckResourceAttrPair("data.scaleway_cockpit_source.by_region", "id", "scaleway_cockpit_source.main", "id"),
165+
resource.TestCheckResourceAttr("data.scaleway_cockpit_source.by_region", "name", "source-by-region"),
166+
resource.TestCheckResourceAttr("data.scaleway_cockpit_source.by_region", "type", "logs"),
167+
resource.TestCheckResourceAttr("data.scaleway_cockpit_source.by_region", "region", "fr-par"),
168+
),
169+
},
170+
},
171+
})
172+
}

0 commit comments

Comments
 (0)