|
| 1 | +package codefresh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | +) |
| 9 | + |
| 10 | +func dataSourceCurrentAccountUser() *schema.Resource { |
| 11 | + return &schema.Resource{ |
| 12 | + Description: "Returns a user the current Codefresh account by name or email.", |
| 13 | + Read: dataSourceCurrentAccountUserRead, |
| 14 | + Schema: map[string]*schema.Schema{ |
| 15 | + "name": { |
| 16 | + Type: schema.TypeString, |
| 17 | + ExactlyOneOf: []string{"name", "email"}, |
| 18 | + Optional: true, |
| 19 | + }, |
| 20 | + "email": { |
| 21 | + Type: schema.TypeString, |
| 22 | + ExactlyOneOf: []string{"name", "email"}, |
| 23 | + Optional: true, |
| 24 | + }, |
| 25 | + }, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func dataSourceCurrentAccountUserRead(d *schema.ResourceData, meta interface{}) error { |
| 30 | + client := meta.(*cfclient.Client) |
| 31 | + var currentAccount *cfclient.CurrentAccount |
| 32 | + var err error |
| 33 | + |
| 34 | + currentAccount, err = client.GetCurrentAccount() |
| 35 | + |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + |
| 40 | + if currentAccount == nil { |
| 41 | + return fmt.Errorf("data.codefresh_current_account - failed to get current_account") |
| 42 | + } |
| 43 | + |
| 44 | + var ( |
| 45 | + userAttributeName string |
| 46 | + userAttributeValue string |
| 47 | + ) |
| 48 | + |
| 49 | + if _email, _emailOk := d.GetOk("email"); _emailOk { |
| 50 | + userAttributeName = "email" |
| 51 | + userAttributeValue = _email.(string) |
| 52 | + } else if _name, _nameOk := d.GetOk("name"); _nameOk { |
| 53 | + userAttributeName = "name" |
| 54 | + userAttributeValue = _name.(string) |
| 55 | + } else { |
| 56 | + return fmt.Errorf("data.codefresh_current_account_user - must specify name or email") |
| 57 | + } |
| 58 | + |
| 59 | + return mapDataCurrentAccountUserToResource(currentAccount, d, userAttributeName, userAttributeValue) |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +func mapDataCurrentAccountUserToResource(currentAccount *cfclient.CurrentAccount, d *schema.ResourceData, userAttributeName string, userAttributeValue string) error { |
| 64 | + |
| 65 | + if currentAccount == nil || currentAccount.ID == "" { |
| 66 | + return fmt.Errorf("data.codefresh_current_account - failed to mapDataCurrentAccountUserToResource no id for current account set") |
| 67 | + } |
| 68 | + |
| 69 | + isFound := false |
| 70 | + |
| 71 | + |
| 72 | + for _, user := range currentAccount.Users { |
| 73 | + if (userAttributeName == "name" && user.UserName == userAttributeValue) || (userAttributeName == "email" && user.Email == userAttributeValue) { |
| 74 | + isFound = true |
| 75 | + d.SetId(user.ID) |
| 76 | + d.Set("name", user.UserName) |
| 77 | + d.Set("email", user.Email) |
| 78 | + break |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if !isFound { |
| 83 | + return fmt.Errorf("data.codefresh_current_account_user - cannot find user with %s %s", userAttributeName, userAttributeValue) |
| 84 | + } |
| 85 | + |
| 86 | + return nil |
| 87 | +} |
0 commit comments