Skip to content

Commit 3b6fbbd

Browse files
yfodilCodelax
andauthored
feat(cockpit): migrate tokens to regional v1 (#2549)
* feat(cockpit): migrate tokens to regional v1 * Update docs/resources/cockpit_token.md Co-authored-by: Jules Castéran <[email protected]> * Update docs/resources/cockpit_token.md Co-authored-by: Jules Castéran <[email protected]> * add comment --------- Co-authored-by: Jules Castéran <[email protected]>
1 parent 93aab97 commit 3b6fbbd

File tree

8 files changed

+2020
-2158
lines changed

8 files changed

+2020
-2158
lines changed

docs/resources/cockpit_token.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ For more information consult the [documentation](https://www.scaleway.com/en/doc
1212
## Example Usage
1313

1414
```terraform
15-
// Get the cockpit of the default project
16-
data "scaleway_cockpit" "main" {}
15+
resource "scaleway_account_project" "project" {
16+
name = "my-project"
17+
}
1718
18-
// Create a token for the cockpit that can write metrics and logs
1919
resource "scaleway_cockpit_token" "main" {
20-
project_id = data.scaleway_cockpit.main.project_id
21-
22-
name = "my-awesome-token"
20+
project_id = scaleway_account_project.project.id
21+
name = "my-awesome-token"
2322
}
2423
```
2524

2625
```terraform
27-
// Get the cockpit of the default project
28-
data "scaleway_cockpit" "main" {}
26+
resource "scaleway_account_project" "project" {
27+
name = "my-project"
28+
}
2929
30-
// Create a token for the cockpit that can read metrics and logs but not write
30+
// Create a token that can read metrics and logs but not write
3131
resource "scaleway_cockpit_token" "main" {
32-
project_id = data.scaleway_cockpit.main.project_id
32+
project_id = scaleway_account_project.project.id
3333
3434
name = "my-awesome-token"
3535
scopes {
@@ -55,18 +55,23 @@ resource "scaleway_cockpit_token" "main" {
5555
- `setup_alerts` - (Defaults to `false`) Setup alerts.
5656
- `query_traces` - (Defaults to `false`) Query traces.
5757
- `write_traces` - (Defaults to `false`) Write traces.
58+
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) of the cockpit token.
5859
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the cockpit is associated with.
5960

6061
## Attributes Reference
6162

6263
In addition to all arguments above, the following attributes are exported:
6364

65+
- `id` - The ID of the cockpit token.
66+
67+
~> **Important:** cockpit tokens' 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
68+
6469
- `secret_key` - The secret key of the token.
6570

6671
## Import
6772

68-
Cockpits can be imported using the token ID, e.g.
73+
Cockpits tokens can be imported using the `{region}/{id}`, e.g.
6974

7075
```bash
71-
$ terraform import scaleway_cockpit_token.main 11111111-1111-1111-1111-111111111111
76+
$ terraform import scaleway_cockpit_token.main fr-par/11111111-1111-1111-1111-111111111111
7277
```

internal/services/cockpit/helpers_cockpit.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
"strings"
88
"time"
99

10+
"github.com/hashicorp/go-cty/cty"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1112
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
1213
cockpitv1beta1 "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1beta1"
1314
"github.com/scaleway/scaleway-sdk-go/scw"
15+
"github.com/scaleway/scaleway-sdk-go/validation"
1416
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1517
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
1618
"github.com/scaleway/terraform-provider-scaleway/v2/internal/transport"
@@ -103,3 +105,28 @@ func waitForCockpit(ctx context.Context, api *cockpitv1beta1.API, projectID stri
103105
RetryInterval: &retryInterval,
104106
}, scw.WithContext(ctx))
105107
}
108+
109+
func cockpitTokenUpgradeV1SchemaType() cty.Type {
110+
return cty.Object(map[string]cty.Type{
111+
"id": cty.String,
112+
})
113+
}
114+
115+
func cockpitTokenV1UpgradeFunc(_ context.Context, rawState map[string]interface{}, _ interface{}) (map[string]interface{}, error) {
116+
defaultRegion := scw.RegionFrPar // Default to the 'fr-par' region as all tokens created with the v1beta1 API were implicitly set to this region
117+
118+
if _, ok := rawState["region"]; !ok {
119+
rawState["region"] = defaultRegion.String()
120+
}
121+
122+
if id, ok := rawState["id"].(string); ok && validation.IsUUID(id) {
123+
locality, ID, _ := regional.ParseID(id)
124+
if locality == "" {
125+
rawState["id"] = regional.NewIDString(defaultRegion, ID)
126+
}
127+
} else {
128+
return nil, fmt.Errorf("upgrade: expected 'id' to be a string, got %T", rawState["id"])
129+
}
130+
131+
return rawState, nil
132+
}

0 commit comments

Comments
 (0)