|
| 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 | +} |
0 commit comments