|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/hashicorp/terraform/helper/schema" |
| 5 | + gitlab "github.com/xanzy/go-gitlab" |
| 6 | +) |
| 7 | + |
| 8 | +func dataSourceGitlabUsers() *schema.Resource { |
| 9 | + return &schema.Resource{ |
| 10 | + Read: dataSourceGitlabUsersRead, |
| 11 | + |
| 12 | + Schema: map[string]*schema.Schema{ |
| 13 | + "name": { |
| 14 | + Type: schema.TypeString, |
| 15 | + Optional: true, |
| 16 | + Default: "all_users", |
| 17 | + }, |
| 18 | + "users": { |
| 19 | + Type: schema.TypeList, |
| 20 | + Computed: true, |
| 21 | + Elem: &schema.Resource{ |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "username": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Computed: true, |
| 26 | + }, |
| 27 | + "email": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Computed: true, |
| 30 | + }, |
| 31 | + "name": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Computed: true, |
| 34 | + }, |
| 35 | + "is_admin": { |
| 36 | + Type: schema.TypeBool, |
| 37 | + Computed: true, |
| 38 | + }, |
| 39 | + "can_create_group": { |
| 40 | + Type: schema.TypeBool, |
| 41 | + Computed: true, |
| 42 | + }, |
| 43 | + "can_create_project": { |
| 44 | + Type: schema.TypeBool, |
| 45 | + Computed: true, |
| 46 | + }, |
| 47 | + "projects_limit": { |
| 48 | + Type: schema.TypeInt, |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + "created_at": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + }, |
| 55 | + "state": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func dataSourceGitlabUsersRead(d *schema.ResourceData, meta interface{}) error { |
| 67 | + client := meta.(*gitlab.Client) |
| 68 | + |
| 69 | + users, _, err := client.Users.ListUsers(nil) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + d.Set("users", flattenGitlabUsers(users)) |
| 75 | + d.SetId(d.Get("name").(string)) |
| 76 | + |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func flattenGitlabUsers(users []*gitlab.User) []interface{} { |
| 81 | + usersList := []interface{}{} |
| 82 | + |
| 83 | + for _, user := range users { |
| 84 | + values := map[string]interface{}{ |
| 85 | + "username": user.Username, |
| 86 | + "email": user.Email, |
| 87 | + "name": user.Name, |
| 88 | + "is_admin": user.IsAdmin, |
| 89 | + "can_create_group": user.CanCreateGroup, |
| 90 | + "can_create_project": user.CanCreateProject, |
| 91 | + "projects_limit": user.ProjectsLimit, |
| 92 | + "state": user.State, |
| 93 | + } |
| 94 | + |
| 95 | + if user.CreatedAt != nil { |
| 96 | + values["created_at"] = user.CreatedAt.String() |
| 97 | + } |
| 98 | + |
| 99 | + usersList = append(usersList, values) |
| 100 | + } |
| 101 | + |
| 102 | + return usersList |
| 103 | +} |
0 commit comments