|
| 1 | +package gitea |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "code.gitea.io/sdk/gitea" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +func dataSourceGiteaUser() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Read: dataSourceGitlabUserRead, |
| 15 | + Schema: map[string]*schema.Schema{ |
| 16 | + "id": { |
| 17 | + Type: schema.TypeInt, |
| 18 | + Computed: true, |
| 19 | + }, |
| 20 | + "username": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Computed: true, |
| 23 | + Optional: true, |
| 24 | + }, |
| 25 | + "email": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Computed: true, |
| 28 | + }, |
| 29 | + "full_name": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Computed: true, |
| 32 | + }, |
| 33 | + "is_admin": { |
| 34 | + Type: schema.TypeBool, |
| 35 | + Computed: true, |
| 36 | + }, |
| 37 | + "avatar_url": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Computed: true, |
| 40 | + }, |
| 41 | + "language": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + "last_login": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + }, |
| 49 | + "created": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Computed: true, |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func dataSourceGitlabUserRead(d *schema.ResourceData, meta interface{}) error { |
| 58 | + client := meta.(*gitea.Client) |
| 59 | + |
| 60 | + var user *gitea.User |
| 61 | + var err error |
| 62 | + |
| 63 | + log.Printf("[INFO] Reading Gitea user") |
| 64 | + |
| 65 | + usernameData, usernameOk := d.GetOk("username") |
| 66 | + |
| 67 | + if !usernameOk { |
| 68 | + user, err = client.GetMyUserInfo() |
| 69 | + } else { |
| 70 | + username := strings.ToLower(usernameData.(string)) |
| 71 | + |
| 72 | + user, err = client.GetUserInfo(username) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + d.Set("id", user.ID) |
| 79 | + d.Set("username", user.UserName) |
| 80 | + d.Set("email", user.Email) |
| 81 | + d.Set("full_name", user.FullName) |
| 82 | + d.Set("is_admin", user.IsAdmin) |
| 83 | + d.Set("created", user.Created) |
| 84 | + d.Set("avatar_url", user.AvatarURL) |
| 85 | + d.Set("last_login", user.LastLogin) |
| 86 | + d.Set("language", user.Language) |
| 87 | + |
| 88 | + d.SetId(fmt.Sprintf("%d", user.ID)) |
| 89 | + |
| 90 | + return nil |
| 91 | +} |
0 commit comments