-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(iam): add iam user datasource (#1356)
Co-authored-by: Rémy Léone <rleone@scaleway.com>
- Loading branch information
Showing
5 changed files
with
571 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.