terraform-provider-gitea/gitea/data_source_gitea_user.go

95 lines
1.8 KiB
Go
Raw Normal View History

2022-03-31 21:24:00 +00:00
package gitea
import (
"fmt"
"log"
"strings"
"code.gitea.io/sdk/gitea"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2022-03-31 21:24:00 +00:00
)
func dataSourceGiteaUser() *schema.Resource {
return &schema.Resource{
2022-04-01 00:15:25 +00:00
Read: dataSourceGiteaUserRead,
2022-03-31 21:24:00 +00:00
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"username": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"email": {
Type: schema.TypeString,
Computed: true,
},
"full_name": {
Type: schema.TypeString,
Computed: true,
},
"is_admin": {
Type: schema.TypeBool,
Computed: true,
},
"avatar_url": {
Type: schema.TypeString,
Computed: true,
},
"language": {
Type: schema.TypeString,
Computed: true,
},
"last_login": {
Type: schema.TypeString,
Computed: true,
},
"created": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
2022-04-01 00:15:25 +00:00
func dataSourceGiteaUserRead(d *schema.ResourceData, meta interface{}) error {
2022-03-31 21:24:00 +00:00
client := meta.(*gitea.Client)
var user *gitea.User
var err error
log.Printf("[INFO] Reading Gitea user")
usernameData, usernameOk := d.GetOk("username")
if !usernameOk {
2022-03-31 23:19:14 +00:00
user, _, err = client.GetMyUserInfo()
2022-04-01 04:23:50 +00:00
if err != nil {
return err
}
2022-03-31 21:24:00 +00:00
} else {
username := strings.ToLower(usernameData.(string))
2022-03-31 23:19:14 +00:00
user, _, err = client.GetUserInfo(username)
2022-03-31 21:24:00 +00:00
if err != nil {
return err
}
}
d.Set("id", user.ID)
d.Set("username", user.UserName)
d.Set("email", user.Email)
d.Set("full_name", user.FullName)
d.Set("is_admin", user.IsAdmin)
d.Set("created", user.Created)
d.Set("avatar_url", user.AvatarURL)
d.Set("last_login", user.LastLogin)
d.Set("language", user.Language)
d.SetId(fmt.Sprintf("%d", user.ID))
return nil
}