|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform/helper/schema" |
| 10 | + "github.com/hashicorp/terraform/helper/validation" |
| 11 | + gitlab "github.com/xanzy/go-gitlab" |
| 12 | +) |
| 13 | + |
| 14 | +func dataSourceGitlabUsers() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: dataSourceGitlabUsersRead, |
| 17 | + |
| 18 | + Schema: map[string]*schema.Schema{ |
| 19 | + "order_by": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Optional: true, |
| 22 | + Default: "id", |
| 23 | + ValidateFunc: validation.StringInSlice([]string{"id", "name", |
| 24 | + "username", "created_at", "updated_at"}, true), |
| 25 | + }, |
| 26 | + "sort": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Optional: true, |
| 29 | + Default: "desc", |
| 30 | + ValidateFunc: validation.StringInSlice([]string{"desc", "asc"}, true), |
| 31 | + }, |
| 32 | + "search": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Optional: true, |
| 35 | + }, |
| 36 | + "active": { |
| 37 | + Type: schema.TypeBool, |
| 38 | + Optional: true, |
| 39 | + }, |
| 40 | + "blocked": { |
| 41 | + Type: schema.TypeBool, |
| 42 | + Optional: true, |
| 43 | + }, |
| 44 | + "extern_uid": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Optional: true, |
| 47 | + }, |
| 48 | + "extern_provider": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Optional: true, |
| 51 | + }, |
| 52 | + "created_before": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Optional: true, |
| 55 | + }, |
| 56 | + "created_after": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Optional: true, |
| 59 | + }, |
| 60 | + "users": { |
| 61 | + Type: schema.TypeList, |
| 62 | + Computed: true, |
| 63 | + Elem: &schema.Resource{ |
| 64 | + Schema: map[string]*schema.Schema{ |
| 65 | + "id": { |
| 66 | + Type: schema.TypeInt, |
| 67 | + Computed: true, |
| 68 | + }, |
| 69 | + "username": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Computed: true, |
| 72 | + }, |
| 73 | + "email": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + }, |
| 77 | + "name": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + }, |
| 81 | + "is_admin": { |
| 82 | + Type: schema.TypeBool, |
| 83 | + Computed: true, |
| 84 | + }, |
| 85 | + "can_create_group": { |
| 86 | + Type: schema.TypeBool, |
| 87 | + Computed: true, |
| 88 | + }, |
| 89 | + "can_create_project": { |
| 90 | + Type: schema.TypeBool, |
| 91 | + Computed: true, |
| 92 | + }, |
| 93 | + "projects_limit": { |
| 94 | + Type: schema.TypeInt, |
| 95 | + Computed: true, |
| 96 | + }, |
| 97 | + "created_at": { |
| 98 | + Type: schema.TypeString, |
| 99 | + Computed: true, |
| 100 | + }, |
| 101 | + "state": { |
| 102 | + Type: schema.TypeString, |
| 103 | + Computed: true, |
| 104 | + }, |
| 105 | + "external": { |
| 106 | + Type: schema.TypeBool, |
| 107 | + Computed: true, |
| 108 | + }, |
| 109 | + "extern_uid": { |
| 110 | + Type: schema.TypeString, |
| 111 | + Computed: true, |
| 112 | + }, |
| 113 | + "organization": { |
| 114 | + Type: schema.TypeString, |
| 115 | + Computed: true, |
| 116 | + }, |
| 117 | + "two_factor_enabled": { |
| 118 | + Type: schema.TypeBool, |
| 119 | + Computed: true, |
| 120 | + }, |
| 121 | + "provider": { |
| 122 | + Type: schema.TypeString, |
| 123 | + Computed: true, |
| 124 | + }, |
| 125 | + "avatar_url": { |
| 126 | + Type: schema.TypeString, |
| 127 | + Computed: true, |
| 128 | + }, |
| 129 | + "bio": { |
| 130 | + Type: schema.TypeString, |
| 131 | + Computed: true, |
| 132 | + }, |
| 133 | + "location": { |
| 134 | + Type: schema.TypeString, |
| 135 | + Computed: true, |
| 136 | + }, |
| 137 | + "skype": { |
| 138 | + Type: schema.TypeString, |
| 139 | + Computed: true, |
| 140 | + }, |
| 141 | + "linkedin": { |
| 142 | + Type: schema.TypeString, |
| 143 | + Computed: true, |
| 144 | + }, |
| 145 | + "twitter": { |
| 146 | + Type: schema.TypeString, |
| 147 | + Computed: true, |
| 148 | + }, |
| 149 | + "website_url": { |
| 150 | + Type: schema.TypeString, |
| 151 | + Computed: true, |
| 152 | + }, |
| 153 | + "theme_id": { |
| 154 | + Type: schema.TypeInt, |
| 155 | + Computed: true, |
| 156 | + }, |
| 157 | + "color_scheme_id": { |
| 158 | + Type: schema.TypeInt, |
| 159 | + Computed: true, |
| 160 | + }, |
| 161 | + "last_sign_in_at": { |
| 162 | + Type: schema.TypeString, |
| 163 | + Computed: true, |
| 164 | + }, |
| 165 | + "current_sign_in_at": { |
| 166 | + Type: schema.TypeString, |
| 167 | + Computed: true, |
| 168 | + }, |
| 169 | + }, |
| 170 | + }, |
| 171 | + }, |
| 172 | + }, |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +func dataSourceGitlabUsersRead(d *schema.ResourceData, meta interface{}) error { |
| 177 | + client := meta.(*gitlab.Client) |
| 178 | + |
| 179 | + listUsersOptions, id, err := expandGitlabUsersOptions(d) |
| 180 | + if err != nil { |
| 181 | + return err |
| 182 | + } |
| 183 | + users, _, err := client.Users.ListUsers(listUsersOptions) |
| 184 | + |
| 185 | + if err != nil { |
| 186 | + return err |
| 187 | + } |
| 188 | + |
| 189 | + d.Set("users", flattenGitlabUsers(users)) |
| 190 | + d.SetId(fmt.Sprintf("%d", id)) |
| 191 | + |
| 192 | + return nil |
| 193 | +} |
| 194 | + |
| 195 | +func flattenGitlabUsers(users []*gitlab.User) []interface{} { |
| 196 | + usersList := []interface{}{} |
| 197 | + |
| 198 | + for _, user := range users { |
| 199 | + values := map[string]interface{}{ |
| 200 | + "id": user.ID, |
| 201 | + "username": user.Username, |
| 202 | + "email": user.Email, |
| 203 | + "name": user.Name, |
| 204 | + "is_admin": user.IsAdmin, |
| 205 | + "can_create_group": user.CanCreateGroup, |
| 206 | + "can_create_project": user.CanCreateProject, |
| 207 | + "projects_limit": user.ProjectsLimit, |
| 208 | + "state": user.State, |
| 209 | + "external": user.External, |
| 210 | + "extern_uid": user.ExternUID, |
| 211 | + "provider": user.Provider, |
| 212 | + "two_factor_enabled": user.TwoFactorEnabled, |
| 213 | + "avatar_url": user.AvatarURL, |
| 214 | + "bio": user.Bio, |
| 215 | + "location": user.Location, |
| 216 | + "skype": user.Skype, |
| 217 | + "linkedin": user.Linkedin, |
| 218 | + "twitter": user.Twitter, |
| 219 | + "website_url": user.WebsiteURL, |
| 220 | + "organization": user.Organization, |
| 221 | + "theme_id": user.ThemeID, |
| 222 | + "color_scheme_id": user.ColorSchemeID, |
| 223 | + } |
| 224 | + |
| 225 | + if user.CreatedAt != nil { |
| 226 | + values["created_at"] = user.CreatedAt.String() |
| 227 | + } |
| 228 | + if user.LastSignInAt != nil { |
| 229 | + values["last_sign_in_at"] = user.LastSignInAt.String() |
| 230 | + } |
| 231 | + if user.CurrentSignInAt != nil { |
| 232 | + values["current_sign_in_at"] = user.CurrentSignInAt.String() |
| 233 | + } |
| 234 | + |
| 235 | + usersList = append(usersList, values) |
| 236 | + } |
| 237 | + |
| 238 | + return usersList |
| 239 | +} |
| 240 | + |
| 241 | +func expandGitlabUsersOptions(d *schema.ResourceData) (*gitlab.ListUsersOptions, int, error) { |
| 242 | + listUsersOptions := &gitlab.ListUsersOptions{} |
| 243 | + var optionsHash strings.Builder |
| 244 | + |
| 245 | + if data, ok := d.GetOk("order_by"); ok { |
| 246 | + orderBy := data.(string) |
| 247 | + listUsersOptions.OrderBy = &orderBy |
| 248 | + optionsHash.WriteString(orderBy) |
| 249 | + } |
| 250 | + optionsHash.WriteString(",") |
| 251 | + if data, ok := d.GetOk("sort"); ok { |
| 252 | + sort := data.(string) |
| 253 | + listUsersOptions.Sort = &sort |
| 254 | + optionsHash.WriteString(sort) |
| 255 | + } |
| 256 | + optionsHash.WriteString(",") |
| 257 | + if data, ok := d.GetOk("search"); ok { |
| 258 | + search := data.(string) |
| 259 | + listUsersOptions.Search = &search |
| 260 | + optionsHash.WriteString(search) |
| 261 | + } |
| 262 | + optionsHash.WriteString(",") |
| 263 | + if data, ok := d.GetOk("active"); ok { |
| 264 | + active := data.(bool) |
| 265 | + listUsersOptions.Active = &active |
| 266 | + optionsHash.WriteString(strconv.FormatBool(active)) |
| 267 | + } |
| 268 | + optionsHash.WriteString(",") |
| 269 | + if data, ok := d.GetOk("blocked"); ok { |
| 270 | + blocked := data.(bool) |
| 271 | + listUsersOptions.Blocked = &blocked |
| 272 | + optionsHash.WriteString(strconv.FormatBool(blocked)) |
| 273 | + } |
| 274 | + optionsHash.WriteString(",") |
| 275 | + if data, ok := d.GetOk("extern_uid"); ok { |
| 276 | + externalUID := data.(string) |
| 277 | + listUsersOptions.ExternalUID = &externalUID |
| 278 | + optionsHash.WriteString(externalUID) |
| 279 | + } |
| 280 | + optionsHash.WriteString(",") |
| 281 | + if data, ok := d.GetOk("extern_provider"); ok { |
| 282 | + provider := data.(string) |
| 283 | + listUsersOptions.Provider = &provider |
| 284 | + optionsHash.WriteString(provider) |
| 285 | + } |
| 286 | + optionsHash.WriteString(",") |
| 287 | + if data, ok := d.GetOk("created_before"); ok { |
| 288 | + createdBefore := data.(string) |
| 289 | + date, err := time.Parse("2006-01-02", createdBefore) |
| 290 | + if err != nil { |
| 291 | + return nil, 0, fmt.Errorf("created_before must be in yyyy-mm-dd format") |
| 292 | + } |
| 293 | + listUsersOptions.CreatedBefore = &date |
| 294 | + optionsHash.WriteString(createdBefore) |
| 295 | + } |
| 296 | + optionsHash.WriteString(",") |
| 297 | + if data, ok := d.GetOk("created_after"); ok { |
| 298 | + createdAfter := data.(string) |
| 299 | + date, err := time.Parse("2006-01-02", createdAfter) |
| 300 | + if err != nil { |
| 301 | + return nil, 0, fmt.Errorf("created_after must be in yyyy-mm-dd format") |
| 302 | + } |
| 303 | + listUsersOptions.CreatedAfter = &date |
| 304 | + optionsHash.WriteString(createdAfter) |
| 305 | + } |
| 306 | + |
| 307 | + id := schema.HashString(optionsHash.String()) |
| 308 | + |
| 309 | + return listUsersOptions, id, nil |
| 310 | +} |
0 commit comments