Skip to content

Commit 4449609

Browse files
yfodilremyleone
andauthored
feat(iam): add iam user datasource (#1356)
Co-authored-by: Rémy Léone <[email protected]>
1 parent d8be7b1 commit 4449609

5 files changed

+571
-0
lines changed

scaleway/data_source_iam_user.go

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package scaleway
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
10+
"github.com/scaleway/scaleway-sdk-go/scw"
11+
)
12+
13+
func dataSourceScalewayIamUser() *schema.Resource {
14+
return &schema.Resource{
15+
ReadContext: dataSourceScalewayIamUserRead,
16+
Schema: map[string]*schema.Schema{
17+
"user_id": {
18+
Type: schema.TypeString,
19+
Optional: true,
20+
Description: "The ID of the IAM user",
21+
ValidateFunc: validationUUID(),
22+
ConflictsWith: []string{"email"},
23+
},
24+
"email": {
25+
Type: schema.TypeString,
26+
Optional: true,
27+
Description: "The email address of the IAM user",
28+
ValidateFunc: validationEmail(),
29+
ConflictsWith: []string{"user_id"},
30+
},
31+
32+
// Default organization_id will be available on a major release. Please check #1337
33+
"organization_id": {
34+
Type: schema.TypeString,
35+
Description: "The organization_id you want to attach the resource to",
36+
Required: true,
37+
},
38+
},
39+
}
40+
}
41+
42+
func dataSourceScalewayIamUserRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
43+
iamAPI := iamAPI(meta)
44+
45+
var email, organizationID string
46+
userID, ok := d.GetOk("user_id")
47+
if ok {
48+
userID = d.Get("user_id")
49+
res, err := iamAPI.GetUser(&iam.GetUserRequest{
50+
UserID: userID.(string),
51+
}, scw.WithContext(ctx))
52+
if err != nil {
53+
return diag.FromErr(err)
54+
}
55+
email = res.Email
56+
organizationID = res.OrganizationID
57+
} else {
58+
res, err := iamAPI.ListUsers(&iam.ListUsersRequest{
59+
OrganizationID: expandStringPtr(d.Get("organization_id")),
60+
}, scw.WithAllPages(), scw.WithContext(ctx))
61+
if err != nil {
62+
return diag.FromErr(err)
63+
}
64+
if len(res.Users) == 0 {
65+
return diag.FromErr(fmt.Errorf("no user found with the email address %s", d.Get("email")))
66+
}
67+
for _, user := range res.Users {
68+
if user.Email == d.Get("email").(string) {
69+
if userID != "" {
70+
return diag.Errorf("more than 1 user found with the same email %s", d.Get("email"))
71+
}
72+
userID, email = user.ID, user.Email
73+
}
74+
}
75+
if userID == "" {
76+
return diag.Errorf("no user found with the email %s", d.Get("email"))
77+
}
78+
}
79+
80+
d.SetId(userID.(string))
81+
err := d.Set("user_id", userID)
82+
if err != nil {
83+
return diag.FromErr(err)
84+
}
85+
86+
_ = d.Set("user_id", userID)
87+
_ = d.Set("email", email)
88+
_ = d.Set("organization_id", organizationID)
89+
90+
return nil
91+
}

scaleway/data_source_iam_user_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package scaleway
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
9+
iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
10+
)
11+
12+
func TestAccScalewayDataSourceIamUser_Basic(t *testing.T) {
13+
SkipBetaTest(t)
14+
tt := NewTestTools(t)
15+
defer tt.Cleanup()
16+
resource.ParallelTest(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
ProviderFactories: tt.ProviderFactories,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: `
22+
data "scaleway_iam_user" "by_id" {
23+
user_id = "af194b1f-55a7-43f2-b61c-22a0268559e3"
24+
organization_id = "dd5b8103-52ef-40b6-b157-35a426650401"
25+
}
26+
27+
data "scaleway_iam_user" "by_email" {
28+
29+
organization_id = "dd5b8103-52ef-40b6-b157-35a426650401"
30+
}
31+
`,
32+
Check: resource.ComposeTestCheckFunc(
33+
testAccCheckScalewayIamUserExists(tt, "data.scaleway_iam_user.by_id"),
34+
testAccCheckScalewayIamUserExists(tt, "data.scaleway_iam_user.by_email"),
35+
36+
resource.TestCheckResourceAttrSet("data.scaleway_iam_user.by_id", "user_id"),
37+
resource.TestCheckResourceAttrSet("data.scaleway_iam_user.by_id", "email"),
38+
39+
resource.TestCheckResourceAttrSet("data.scaleway_iam_user.by_email", "user_id"),
40+
resource.TestCheckResourceAttrSet("data.scaleway_iam_user.by_email", "email"),
41+
),
42+
},
43+
},
44+
})
45+
}
46+
47+
func testAccCheckScalewayIamUserExists(tt *TestTools, name string) resource.TestCheckFunc {
48+
return func(s *terraform.State) error {
49+
rs, ok := s.RootModule().Resources[name]
50+
if !ok {
51+
return fmt.Errorf("resource not found: %s", name)
52+
}
53+
54+
iamAPI := iamAPI(tt.Meta)
55+
56+
_, err := iamAPI.GetUser(&iam.GetUserRequest{
57+
UserID: rs.Primary.ID,
58+
})
59+
if err != nil {
60+
return fmt.Errorf("could not find user: %w", err)
61+
}
62+
63+
return nil
64+
}
65+
}

scaleway/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func addBetaResources(provider *schema.Provider) {
3636
}
3737
betaDataSources := map[string]*schema.Resource{
3838
"scaleway_iam_application": dataSourceScalewayIamApplication(),
39+
"scaleway_iam_user": dataSourceScalewayIamUser(),
3940
}
4041
for resourceName, resource := range betaResources {
4142
provider.ResourcesMap[resourceName] = resource

0 commit comments

Comments
 (0)