terraform-provider-gitea/gitea/data_source_gitea_org.go

86 lines
1.6 KiB
Go
Raw Permalink 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/helper/schema"
)
func dataSourceGiteaOrg() *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,
},
"name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"full_name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"avatar_url": {
Type: schema.TypeString,
Computed: true,
},
"website": {
Type: schema.TypeString,
Computed: true,
},
"location": {
Type: schema.TypeString,
Computed: true,
},
"visibility": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
2022-04-01 00:15:25 +00:00
func dataSourceGiteaOrgRead(d *schema.ResourceData, meta interface{}) error {
2022-03-31 21:24:00 +00:00
client := meta.(*gitea.Client)
var org *gitea.Organization
var err error
log.Printf("[INFO] Reading Gitea Org")
nameData, nameOk := d.GetOk("name")
if !nameOk {
return fmt.Errorf("name of org must be passed")
}
name := strings.ToLower(nameData.(string))
2022-03-31 23:19:14 +00:00
org, _, err = client.GetOrg(name)
2022-03-31 21:24:00 +00:00
if err != nil {
return err
}
d.Set("id", org.ID)
d.Set("name", org.UserName)
d.Set("full_name", org.FullName)
d.Set("avatar_url", org.AvatarURL)
d.Set("location", org.Location)
d.Set("website", org.Website)
d.Set("description", org.Description)
d.Set("visibility", org.Visibility)
d.SetId(fmt.Sprintf("%d", org.ID))
return nil
}