|
| 1 | +package codefresh |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + cfClient "github.com/codefresh-io/terraform-provider-codefresh/client" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 8 | +) |
| 9 | + |
| 10 | +func dataSourceUser() *schema.Resource { |
| 11 | + return &schema.Resource{ |
| 12 | + Read: dataSourceUserRead, |
| 13 | + Schema: *UserSchema(), |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +func dataSourceUserRead(d *schema.ResourceData, meta interface{}) error { |
| 18 | + |
| 19 | + client := meta.(*cfClient.Client) |
| 20 | + |
| 21 | + users, err := client.ListUsers() |
| 22 | + if err != nil { |
| 23 | + return err |
| 24 | + } |
| 25 | + |
| 26 | + email := d.Get("email").(string) |
| 27 | + |
| 28 | + for _, user := range *users { |
| 29 | + if user.Email == email { |
| 30 | + err = mapDataUserToResource(user, d) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + if d.Id() == "" { |
| 38 | + return errors.New(fmt.Sprintf("[EROOR] User %s wasn't found", email)) |
| 39 | + } |
| 40 | + |
| 41 | + return nil |
| 42 | +} |
| 43 | + |
| 44 | +func mapDataUserToResource(user cfClient.User, d *schema.ResourceData) error { |
| 45 | + |
| 46 | + d.SetId(user.ID) |
| 47 | + d.Set("user_id", user.ID) |
| 48 | + d.Set("user_name", user.UserName) |
| 49 | + d.Set("email", user.Email) |
| 50 | + d.Set("status", user.Status) |
| 51 | + if user.Personal != nil { |
| 52 | + d.Set("personal", flattenPersonal(user.Personal)) |
| 53 | + } |
| 54 | + d.Set("short_profile", |
| 55 | + []map[string]interface{}{ |
| 56 | + {"user_name": user.ShortProfile.UserName}, |
| 57 | + }) |
| 58 | + d.Set("roles", user.Roles) |
| 59 | + d.Set("logins", flattenLogins(&user.Logins)) |
| 60 | + |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func flattenPersonal(personal *cfClient.Personal) []map[string]interface{} { |
| 65 | + return []map[string]interface{}{ |
| 66 | + { |
| 67 | + "first_name": personal.FirstName, |
| 68 | + "last_name": personal.LastName, |
| 69 | + "company_name": personal.CompanyName, |
| 70 | + "phone_number": personal.PhoneNumber, |
| 71 | + "country": personal.Country, |
| 72 | + }, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func flattenLogins(logins *[]cfClient.Login) []map[string]interface{} { |
| 77 | + |
| 78 | + var res = make([]map[string]interface{}, len(*logins)) |
| 79 | + for i, login := range *logins { |
| 80 | + m := make(map[string]interface{}) |
| 81 | + m["credentials"] = []map[string]interface{}{ |
| 82 | + {"permissions": login.Credentials.Permissions}} |
| 83 | + |
| 84 | + m["idp"] = []map[string]interface{}{ |
| 85 | + { |
| 86 | + "id": login.IDP.ID, |
| 87 | + "client_type": login.IDP.ClientType, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + res[i] = m |
| 92 | + } |
| 93 | + |
| 94 | + return res |
| 95 | +} |
| 96 | + |
| 97 | +func UserSchema() *map[string]*schema.Schema { |
| 98 | + return &map[string]*schema.Schema{ |
| 99 | + "user_name": { |
| 100 | + Type: schema.TypeString, |
| 101 | + Computed: true, |
| 102 | + }, |
| 103 | + "email": { |
| 104 | + Type: schema.TypeString, |
| 105 | + Required: true, |
| 106 | + }, |
| 107 | + "user_id": { |
| 108 | + Type: schema.TypeString, |
| 109 | + Computed: true, |
| 110 | + }, |
| 111 | + "personal": { |
| 112 | + Type: schema.TypeList, |
| 113 | + Computed: true, |
| 114 | + Elem: &schema.Resource{ |
| 115 | + Schema: map[string]*schema.Schema{ |
| 116 | + "first_name": { |
| 117 | + Type: schema.TypeString, |
| 118 | + Optional: true, |
| 119 | + }, |
| 120 | + "last_name": { |
| 121 | + Type: schema.TypeString, |
| 122 | + Optional: true, |
| 123 | + }, |
| 124 | + "company_name": { |
| 125 | + Type: schema.TypeString, |
| 126 | + Optional: true, |
| 127 | + }, |
| 128 | + "phone_number": { |
| 129 | + Type: schema.TypeString, |
| 130 | + Optional: true, |
| 131 | + }, |
| 132 | + "country": { |
| 133 | + Type: schema.TypeString, |
| 134 | + Optional: true, |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + "short_profile": { |
| 140 | + Type: schema.TypeList, |
| 141 | + Computed: true, |
| 142 | + Elem: &schema.Resource{ |
| 143 | + Schema: map[string]*schema.Schema{ |
| 144 | + "user_name": { |
| 145 | + Type: schema.TypeString, |
| 146 | + Optional: true, |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + "roles": { |
| 152 | + Type: schema.TypeSet, |
| 153 | + Computed: true, |
| 154 | + Elem: &schema.Schema{ |
| 155 | + Type: schema.TypeString, |
| 156 | + }, |
| 157 | + }, |
| 158 | + "status": { |
| 159 | + Type: schema.TypeString, |
| 160 | + Computed: true, |
| 161 | + }, |
| 162 | + "logins": { |
| 163 | + Type: schema.TypeList, |
| 164 | + Computed: true, |
| 165 | + Elem: &schema.Resource{ |
| 166 | + Schema: map[string]*schema.Schema{ |
| 167 | + "credentials": { |
| 168 | + Type: schema.TypeList, |
| 169 | + Optional: true, |
| 170 | + Elem: &schema.Resource{ |
| 171 | + Schema: map[string]*schema.Schema{ |
| 172 | + "permissions": { |
| 173 | + Type: schema.TypeSet, |
| 174 | + Optional: true, |
| 175 | + Elem: &schema.Schema{ |
| 176 | + Type: schema.TypeString, |
| 177 | + }, |
| 178 | + }, |
| 179 | + }, |
| 180 | + }, |
| 181 | + }, |
| 182 | + "idp": { |
| 183 | + Type: schema.TypeList, |
| 184 | + Optional: true, |
| 185 | + Elem: &schema.Resource{ |
| 186 | + Schema: map[string]*schema.Schema{ |
| 187 | + "id": { |
| 188 | + Type: schema.TypeString, |
| 189 | + Optional: true, |
| 190 | + }, |
| 191 | + "client_type": { |
| 192 | + Type: schema.TypeString, |
| 193 | + Optional: true, |
| 194 | + }, |
| 195 | + }, |
| 196 | + }, |
| 197 | + }, |
| 198 | + }, |
| 199 | + }, |
| 200 | + }, |
| 201 | + } |
| 202 | +} |
0 commit comments