#12 fix terraform import for organisations

This commit is contained in:
Tobias Trabelsi 2022-08-13 22:15:19 +02:00
parent cfa43451d8
commit 8b25a1519e
No known key found for this signature in database
GPG key ID: FF0C2839718CAF2E
6 changed files with 53 additions and 12 deletions

View file

@ -17,7 +17,7 @@ terraform {
required_providers {
gitea = {
source = "Lerentis/gitea"
version = "0.7.1"
version = "0.7.2"
}
}
}

View file

@ -17,7 +17,7 @@ terraform {
required_providers {
gitea = {
source = "Lerentis/gitea"
version = "0.3.0"
version = "0.7.2"
}
}
}

View file

@ -18,7 +18,8 @@ resource "gitea_repository" "mirror" {
}
resource "gitea_org" "test_org" {
name = "test-org"
name = "test-org"
description = "test description"
}
resource "gitea_repository" "org_repo" {

View file

@ -2,7 +2,7 @@ terraform {
required_providers {
gitea = {
source = "terraform.local/lerentis/gitea"
version = "0.7.1"
version = "0.7.2"
}
}
}

View file

@ -2,7 +2,7 @@ terraform {
required_providers {
gitea = {
source = "Lerentis/gitea"
version = "0.3.0"
version = "0.7.2"
}
}
}

View file

@ -17,20 +17,60 @@ const (
RepoAdminChangeTeamAccess string = "repo_admin_change_team_access"
)
// might come in handy if we want to stick to numeric IDs
/*func searchOrgByClientId(c *gitea.Client, id int64) (res *gitea.Organization, err error) {
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
}
}*/
func resourceOrgRead(d *schema.ResourceData, meta interface{}) (err error) {
client := meta.(*gitea.Client)
var org *gitea.Organization
var resp *gitea.Response
var tmpOrgName string
org, resp, err = client.GetOrg(d.Get(orgName).(string))
if d.Get(orgName).(string) == "" {
// terraform import as only access to the ID, therfore we set the ID to the name
tmpOrgName = d.Id()
} else {
tmpOrgName = d.Get(orgName).(string)
}
org, resp, err = client.GetOrg(tmpOrgName)
if err != nil {
if resp.StatusCode == 404 {
d.SetId("")
return nil
if resp != nil {
if resp.StatusCode == 404 {
d.SetId("")
return
}
} else {
return err
return fmt.Errorf("Error response from client: %s\nOrg Name %s", err, tmpOrgName)
}
}
@ -114,7 +154,7 @@ func resourceOrgDelete(d *schema.ResourceData, meta interface{}) (err error) {
}
func setOrgResourceData(org *gitea.Organization, d *schema.ResourceData) (err error) {
d.SetId(fmt.Sprintf("%d", org.ID))
d.SetId(org.UserName)
d.Set("name", org.UserName)
d.Set("full_name", org.FullName)
d.Set("avatar_url", org.AvatarURL)
@ -133,7 +173,7 @@ func resourceGiteaOrg() *schema.Resource {
Update: resourceOrgUpdate,
Delete: resourceOrgDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {