Skip to content

Commit ed3c182

Browse files
tbellaviaremyleone
andauthored
feat(iam_api_key): add data source (#2647)
* feat(iam): add iam api key datasource * fix: golangci-lint errors * docs(iam): add documentation for IAM api key datasource --------- Co-authored-by: Rémy Léone <[email protected]>
1 parent 855ec15 commit ed3c182

File tree

5 files changed

+847
-0
lines changed

5 files changed

+847
-0
lines changed

docs/data-sources/iam_api_key.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
subcategory: "IAM"
3+
page_title: "Scaleway: scaleway_iam_api_key"
4+
---
5+
6+
# scaleway_iam_api_key
7+
8+
Gets information about an existing IAM API key. For more information, refer to the [IAM API documentation](https://www.scaleway.com/en/developers/api/iam/#api-keys-3665ae).
9+
10+
## Example Usage
11+
12+
```hcl
13+
# Get api key infos by id (access_key)
14+
data "scaleway_iam_api_key" "main" {
15+
access_key = "SCWABCDEFGHIJKLMNOPQ"
16+
}
17+
```
18+
19+
## Argument Reference
20+
21+
- `access_key` - The access key of the IAM API key which is also the ID of the API key.
22+
23+
## Attribute Reference
24+
25+
Exported attributes are the ones from `iam_api_key` [resource](../resources/iam_api_key.md) except the `secret_key` field

internal/provider/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ func Provider(config *Config) plugin.ProviderFunc {
255255
"scaleway_iam_group": iam.DataSourceGroup(),
256256
"scaleway_iam_ssh_key": iam.DataSourceSSHKey(),
257257
"scaleway_iam_user": iam.DataSourceUser(),
258+
"scaleway_iam_api_key": iam.DataSourceAPIKey(),
258259
"scaleway_instance_image": instance.DataSourceImage(),
259260
"scaleway_instance_ip": instance.DataSourceIP(),
260261
"scaleway_instance_placement_group": instance.DataSourcePlacementGroup(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package iam
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
9+
)
10+
11+
func DataSourceAPIKey() *schema.Resource {
12+
dsSchema := datasource.SchemaFromResourceSchema(ResourceAPIKey().Schema)
13+
14+
dsSchema["access_key"].Required = true
15+
dsSchema["access_key"].Computed = false
16+
delete(dsSchema, "secret_key")
17+
return &schema.Resource{
18+
ReadContext: DataSourceIamAPIKeyRead,
19+
Schema: dsSchema,
20+
}
21+
}
22+
23+
func DataSourceIamAPIKeyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
24+
accessKey := d.Get("access_key").(string)
25+
26+
d.SetId(accessKey)
27+
28+
diags := resourceIamAPIKeyRead(ctx, d, m)
29+
if diags != nil {
30+
return append(diags, diag.Errorf("failed to read iam api key state")...)
31+
}
32+
33+
if d.Id() == "" {
34+
return diag.Errorf("iam api key (%s) not found", accessKey)
35+
}
36+
37+
return nil
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package iam_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
8+
)
9+
10+
func TestAccDataSourceApiKey_Basic(t *testing.T) {
11+
tt := acctest.NewTestTools(t)
12+
defer tt.Cleanup()
13+
14+
resource.ParallelTest(t, resource.TestCase{
15+
PreCheck: func() { acctest.PreCheck(t) },
16+
ProviderFactories: tt.ProviderFactories,
17+
CheckDestroy: resource.ComposeTestCheckFunc(
18+
testAccCheckIamAPIKeyDestroy(tt),
19+
),
20+
Steps: []resource.TestStep{
21+
{
22+
Config: `
23+
resource "scaleway_iam_application" "main" {
24+
name = "tf_tests_app_key_basic"
25+
}
26+
27+
resource "scaleway_iam_api_key" "main" {
28+
application_id = scaleway_iam_application.main.id
29+
description = "tf_tests_with_application"
30+
}
31+
32+
data "scaleway_iam_api_key" "main" {
33+
access_key = scaleway_iam_api_key.main.id
34+
}
35+
`,
36+
Check: resource.ComposeTestCheckFunc(
37+
testAccCheckIamAPIKeyExists(tt, "scaleway_iam_api_key.main"),
38+
resource.TestCheckResourceAttrPair("data.scaleway_iam_api_key.main", "access_key", "scaleway_iam_api_key.main", "id"),
39+
resource.TestCheckResourceAttrPair("data.scaleway_iam_api_key.main", "id", "scaleway_iam_api_key.main", "id"),
40+
resource.TestCheckResourceAttrPair("data.scaleway_iam_api_key.main", "application_id", "scaleway_iam_application.main", "id"),
41+
),
42+
},
43+
},
44+
})
45+
}

0 commit comments

Comments
 (0)