2022-06-12 13:34:17 +00:00
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-13 20:37:46 +00:00
|
|
|
"strconv"
|
2022-06-12 13:34:17 +00:00
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
2022-08-06 14:21:18 +00:00
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
2022-06-12 13:34:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
orgName string = "name"
|
|
|
|
orgFullName string = "full_name"
|
|
|
|
orgDescription string = "description"
|
|
|
|
orgWebsite string = "website"
|
|
|
|
orgLocation string = "location"
|
|
|
|
orgVisibility string = "visibility"
|
|
|
|
RepoAdminChangeTeamAccess string = "repo_admin_change_team_access"
|
2023-07-19 18:52:36 +00:00
|
|
|
orgRepos string = "org_repos"
|
2022-06-12 13:34:17 +00:00
|
|
|
)
|
|
|
|
|
2022-08-13 20:15:19 +00:00
|
|
|
// might come in handy if we want to stick to numeric IDs
|
2022-08-13 20:37:46 +00:00
|
|
|
func searchOrgByClientId(c *gitea.Client, id int64) (res *gitea.Organization, err error) {
|
2022-08-13 20:15:19 +00:00
|
|
|
|
|
|
|
page := 1
|
|
|
|
|
|
|
|
for {
|
|
|
|
orgs, _, err := c.AdminListOrgs(gitea.AdminListOrgsOptions{
|
|
|
|
ListOptions: gitea.ListOptions{
|
|
|
|
Page: page,
|
|
|
|
PageSize: 50,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(orgs) == 0 {
|
|
|
|
return nil, fmt.Errorf("Organisation with ID %d could not be found", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, org := range orgs {
|
|
|
|
if org.ID == id {
|
|
|
|
return org, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
page += 1
|
|
|
|
}
|
2022-08-13 20:37:46 +00:00
|
|
|
}
|
2022-08-13 20:15:19 +00:00
|
|
|
|
2023-07-19 18:52:36 +00:00
|
|
|
func getAllOrgRepos(c *gitea.Client, orgName string) (repos []string, err error) {
|
|
|
|
page := 1
|
|
|
|
|
|
|
|
for {
|
|
|
|
repoBuffer, _, err := c.ListOrgRepos(orgName, gitea.ListOrgReposOptions{
|
|
|
|
ListOptions: gitea.ListOptions{
|
|
|
|
Page: page,
|
|
|
|
PageSize: 50,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(repoBuffer) == 0 {
|
|
|
|
return repos, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, repo := range repoBuffer {
|
|
|
|
repos = append(repos, repo.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
page += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-12 13:34:17 +00:00
|
|
|
func resourceOrgRead(d *schema.ResourceData, meta interface{}) (err error) {
|
|
|
|
client := meta.(*gitea.Client)
|
|
|
|
|
|
|
|
var org *gitea.Organization
|
|
|
|
|
2022-08-13 20:37:46 +00:00
|
|
|
id, err := strconv.ParseInt(d.Id(), 10, 64)
|
2022-08-13 20:15:19 +00:00
|
|
|
|
2022-08-13 20:37:46 +00:00
|
|
|
org, err = searchOrgByClientId(client, id)
|
2022-06-12 13:34:17 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2023-01-28 21:49:54 +00:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
2022-06-12 13:34:17 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 18:52:36 +00:00
|
|
|
repos, _ := getAllOrgRepos(client, org.UserName)
|
|
|
|
err = setOrgResourceData(org, d, &repos)
|
2022-06-12 13:34:17 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceOrgCreate(d *schema.ResourceData, meta interface{}) (err error) {
|
|
|
|
client := meta.(*gitea.Client)
|
|
|
|
|
|
|
|
opts := gitea.CreateOrgOption{
|
|
|
|
Name: d.Get(orgName).(string),
|
|
|
|
FullName: d.Get(orgFullName).(string),
|
|
|
|
Description: d.Get(orgDescription).(string),
|
|
|
|
Website: d.Get(orgWebsite).(string),
|
|
|
|
Location: d.Get(orgLocation).(string),
|
|
|
|
Visibility: gitea.VisibleType(d.Get(orgVisibility).(string)),
|
|
|
|
RepoAdminChangeTeamAccess: d.Get(RepoAdminChangeTeamAccess).(bool),
|
|
|
|
}
|
|
|
|
|
|
|
|
org, _, err := client.CreateOrg(opts)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-19 18:52:36 +00:00
|
|
|
repos, _ := getAllOrgRepos(client, org.UserName)
|
|
|
|
err = setOrgResourceData(org, d, &repos)
|
2022-06-12 13:34:17 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceOrgUpdate(d *schema.ResourceData, meta interface{}) (err error) {
|
|
|
|
client := meta.(*gitea.Client)
|
|
|
|
|
|
|
|
var org *gitea.Organization
|
|
|
|
var resp *gitea.Response
|
|
|
|
|
|
|
|
org, resp, err = client.GetOrg(d.Get(orgName).(string))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if resp.StatusCode == 404 {
|
|
|
|
resourceOrgCreate(d, meta)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := gitea.EditOrgOption{
|
|
|
|
FullName: d.Get(orgFullName).(string),
|
|
|
|
Description: d.Get(orgDescription).(string),
|
|
|
|
Website: d.Get(orgWebsite).(string),
|
|
|
|
Location: d.Get(orgLocation).(string),
|
|
|
|
Visibility: gitea.VisibleType(d.Get(orgVisibility).(string)),
|
|
|
|
}
|
|
|
|
|
|
|
|
client.EditOrg(d.Get(orgName).(string), opts)
|
|
|
|
|
|
|
|
org, resp, err = client.GetOrg(d.Get(orgName).(string))
|
|
|
|
|
2023-07-19 18:52:36 +00:00
|
|
|
repos, _ := getAllOrgRepos(client, org.UserName)
|
|
|
|
err = setOrgResourceData(org, d, &repos)
|
2022-06-12 13:34:17 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-14 21:24:42 +00:00
|
|
|
func resourceOrgDelete(d *schema.ResourceData, meta interface{}) (err error) {
|
2022-06-12 13:34:17 +00:00
|
|
|
client := meta.(*gitea.Client)
|
|
|
|
|
|
|
|
var resp *gitea.Response
|
|
|
|
|
|
|
|
resp, err = client.DeleteOrg(d.Get(orgName).(string))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if resp.StatusCode == 404 {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-19 18:52:36 +00:00
|
|
|
func setOrgResourceData(org *gitea.Organization, d *schema.ResourceData, repos *[]string) (err error) {
|
2022-08-13 20:37:46 +00:00
|
|
|
d.SetId(fmt.Sprintf("%d", org.ID))
|
2022-06-12 13:34:17 +00:00
|
|
|
d.Set("name", org.UserName)
|
|
|
|
d.Set("full_name", org.FullName)
|
|
|
|
d.Set("avatar_url", org.AvatarURL)
|
|
|
|
d.Set("description", org.Description)
|
|
|
|
d.Set("website", org.Website)
|
|
|
|
d.Set("location", org.Location)
|
|
|
|
d.Set("visibility", org.Visibility)
|
2023-07-19 18:52:36 +00:00
|
|
|
d.Set("repos", repos)
|
2022-06-12 13:34:17 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceGiteaOrg() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Read: resourceOrgRead,
|
|
|
|
Create: resourceOrgCreate,
|
|
|
|
Update: resourceOrgUpdate,
|
2022-06-14 21:24:42 +00:00
|
|
|
Delete: resourceOrgDelete,
|
2022-06-12 13:34:17 +00:00
|
|
|
Importer: &schema.ResourceImporter{
|
2022-08-13 20:15:19 +00:00
|
|
|
StateContext: schema.ImportStatePassthroughContext,
|
2022-06-12 13:34:17 +00:00
|
|
|
},
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": {
|
2022-06-12 15:49:33 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Description: "The name of the organisation without spaces.",
|
2022-06-12 13:34:17 +00:00
|
|
|
},
|
|
|
|
"full_name": {
|
2022-06-12 15:49:33 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: false,
|
|
|
|
Optional: true,
|
|
|
|
Description: "The display name of the organisation. Defaults to the value of `name`.",
|
2022-06-12 13:34:17 +00:00
|
|
|
},
|
|
|
|
"description": {
|
2022-06-12 15:49:33 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: false,
|
|
|
|
Optional: true,
|
|
|
|
Description: "A description of this organisation.",
|
2022-06-12 13:34:17 +00:00
|
|
|
},
|
|
|
|
"website": {
|
2022-06-12 15:49:33 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: false,
|
|
|
|
Optional: true,
|
|
|
|
Description: "A link to a website with more information about this organisation.",
|
2022-06-12 13:34:17 +00:00
|
|
|
},
|
|
|
|
"location": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: false,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"repo_admin_change_team_access": {
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Required: false,
|
|
|
|
Optional: true,
|
|
|
|
Default: true,
|
|
|
|
},
|
|
|
|
"avatar_url": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: false,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"visibility": {
|
2022-06-12 15:49:33 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: false,
|
|
|
|
Optional: true,
|
|
|
|
Default: "public",
|
|
|
|
Description: "Flag is this organisation should be publicly visible or not.",
|
2022-06-12 13:34:17 +00:00
|
|
|
},
|
2023-07-19 18:52:36 +00:00
|
|
|
"repos": {
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Required: false,
|
|
|
|
Computed: true,
|
|
|
|
Description: "List of all Repositories that are part of this organisation",
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
2022-06-12 13:34:17 +00:00
|
|
|
},
|
2022-06-12 15:49:33 +00:00
|
|
|
Description: "`gitea_org` manages a gitea organisation.\n\n" +
|
|
|
|
"Organisations are a way to group repositories and abstract permission management in a gitea instance.",
|
2022-06-12 13:34:17 +00:00
|
|
|
}
|
|
|
|
}
|