Skip to content

feat(cockpit): migrate tokens to regional v1 #2549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions docs/resources/cockpit_token.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ For more information consult the [documentation](https://www.scaleway.com/en/doc
## Example Usage

```terraform
// Get the cockpit of the default project
data "scaleway_cockpit" "main" {}
resource "scaleway_account_project" "project" {
name = "my-project"
}

// Create a token for the cockpit that can write metrics and logs
resource "scaleway_cockpit_token" "main" {
project_id = data.scaleway_cockpit.main.project_id

name = "my-awesome-token"
project_id = scaleway_account_project.project.id
name = "my-awesome-token"
}
```

```terraform
// Get the cockpit of the default project
data "scaleway_cockpit" "main" {}
resource "scaleway_account_project" "project" {
name = "my-project"
}

// Create a token for the cockpit that can read metrics and logs but not write
// Create a token that can read metrics and logs but not write
resource "scaleway_cockpit_token" "main" {
project_id = data.scaleway_cockpit.main.project_id
project_id = scaleway_account_project.project.id

name = "my-awesome-token"
scopes {
Expand All @@ -55,18 +55,23 @@ resource "scaleway_cockpit_token" "main" {
- `setup_alerts` - (Defaults to `false`) Setup alerts.
- `query_traces` - (Defaults to `false`) Query traces.
- `write_traces` - (Defaults to `false`) Write traces.
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) of the cockpit token.
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the cockpit is associated with.

## Attributes Reference

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

- `id` - The ID of the cockpit token.

~> **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

- `secret_key` - The secret key of the token.

## Import

Cockpits can be imported using the token ID, e.g.
Cockpits tokens can be imported using the `{region}/{id}`, e.g.

```bash
$ terraform import scaleway_cockpit_token.main 11111111-1111-1111-1111-111111111111
$ terraform import scaleway_cockpit_token.main fr-par/11111111-1111-1111-1111-111111111111
```
27 changes: 27 additions & 0 deletions internal/services/cockpit/helpers_cockpit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"strings"
"time"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
cockpitv1beta1 "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/scaleway-sdk-go/validation"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/transport"
Expand Down Expand Up @@ -103,3 +105,28 @@ func waitForCockpit(ctx context.Context, api *cockpitv1beta1.API, projectID stri
RetryInterval: &retryInterval,
}, scw.WithContext(ctx))
}

func cockpitTokenUpgradeV1SchemaType() cty.Type {
return cty.Object(map[string]cty.Type{
"id": cty.String,
})
}

func cockpitTokenV1UpgradeFunc(_ context.Context, rawState map[string]interface{}, _ interface{}) (map[string]interface{}, error) {
defaultRegion := scw.RegionFrPar // Default to the 'fr-par' region as all tokens created with the v1beta1 API were implicitly set to this region

if _, ok := rawState["region"]; !ok {
rawState["region"] = defaultRegion.String()
}

if id, ok := rawState["id"].(string); ok && validation.IsUUID(id) {
locality, ID, _ := regional.ParseID(id)
if locality == "" {
rawState["id"] = regional.NewIDString(defaultRegion, ID)
}
} else {
return nil, fmt.Errorf("upgrade: expected 'id' to be a string, got %T", rawState["id"])
}

return rawState, nil
}
Loading