Skip to content

Commit

Permalink
feat(iam): add iam user datasource (#1356)
Browse files Browse the repository at this point in the history
Co-authored-by: Rémy Léone <rleone@scaleway.com>
  • Loading branch information
yfodil and remyleone committed Jul 4, 2022
1 parent d8be7b1 commit 4449609
Show file tree
Hide file tree
Showing 5 changed files with 571 additions and 0 deletions.
91 changes: 91 additions & 0 deletions scaleway/data_source_iam_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package scaleway

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func dataSourceScalewayIamUser() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceScalewayIamUserRead,
Schema: map[string]*schema.Schema{
"user_id": {
Type: schema.TypeString,
Optional: true,
Description: "The ID of the IAM user",
ValidateFunc: validationUUID(),
ConflictsWith: []string{"email"},
},
"email": {
Type: schema.TypeString,
Optional: true,
Description: "The email address of the IAM user",
ValidateFunc: validationEmail(),
ConflictsWith: []string{"user_id"},
},

// Default organization_id will be available on a major release. Please check #1337
"organization_id": {
Type: schema.TypeString,
Description: "The organization_id you want to attach the resource to",
Required: true,
},
},
}
}

func dataSourceScalewayIamUserRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
iamAPI := iamAPI(meta)

var email, organizationID string
userID, ok := d.GetOk("user_id")
if ok {
userID = d.Get("user_id")
res, err := iamAPI.GetUser(&iam.GetUserRequest{
UserID: userID.(string),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
email = res.Email
organizationID = res.OrganizationID
} else {
res, err := iamAPI.ListUsers(&iam.ListUsersRequest{
OrganizationID: expandStringPtr(d.Get("organization_id")),
}, scw.WithAllPages(), scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
if len(res.Users) == 0 {
return diag.FromErr(fmt.Errorf("no user found with the email address %s", d.Get("email")))
}
for _, user := range res.Users {
if user.Email == d.Get("email").(string) {
if userID != "" {
return diag.Errorf("more than 1 user found with the same email %s", d.Get("email"))
}
userID, email = user.ID, user.Email
}
}
if userID == "" {
return diag.Errorf("no user found with the email %s", d.Get("email"))
}
}

d.SetId(userID.(string))
err := d.Set("user_id", userID)
if err != nil {
return diag.FromErr(err)
}

_ = d.Set("user_id", userID)
_ = d.Set("email", email)
_ = d.Set("organization_id", organizationID)

return nil
}
65 changes: 65 additions & 0 deletions scaleway/data_source_iam_user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package scaleway

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
)

func TestAccScalewayDataSourceIamUser_Basic(t *testing.T) {
SkipBetaTest(t)
tt := NewTestTools(t)
defer tt.Cleanup()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: tt.ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
data "scaleway_iam_user" "by_id" {
user_id = "af194b1f-55a7-43f2-b61c-22a0268559e3"
organization_id = "dd5b8103-52ef-40b6-b157-35a426650401"
}
data "scaleway_iam_user" "by_email" {
email = "developer-tools-team@scaleway.com"
organization_id = "dd5b8103-52ef-40b6-b157-35a426650401"
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayIamUserExists(tt, "data.scaleway_iam_user.by_id"),
testAccCheckScalewayIamUserExists(tt, "data.scaleway_iam_user.by_email"),

resource.TestCheckResourceAttrSet("data.scaleway_iam_user.by_id", "user_id"),
resource.TestCheckResourceAttrSet("data.scaleway_iam_user.by_id", "email"),

resource.TestCheckResourceAttrSet("data.scaleway_iam_user.by_email", "user_id"),
resource.TestCheckResourceAttrSet("data.scaleway_iam_user.by_email", "email"),
),
},
},
})
}

func testAccCheckScalewayIamUserExists(tt *TestTools, name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("resource not found: %s", name)
}

iamAPI := iamAPI(tt.Meta)

_, err := iamAPI.GetUser(&iam.GetUserRequest{
UserID: rs.Primary.ID,
})
if err != nil {
return fmt.Errorf("could not find user: %w", err)
}

return nil
}
}
1 change: 1 addition & 0 deletions scaleway/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func addBetaResources(provider *schema.Provider) {
}
betaDataSources := map[string]*schema.Resource{
"scaleway_iam_application": dataSourceScalewayIamApplication(),
"scaleway_iam_user": dataSourceScalewayIamUser(),
}
for resourceName, resource := range betaResources {
provider.ResourcesMap[resourceName] = resource
Expand Down

0 comments on commit 4449609

Please sign in to comment.