fixed handling of vanished resources
Some checks reported errors
continuous-integration/drone/push Build was killed

added support for organisations
This commit is contained in:
Tobias Trabelsi 2022-06-12 15:34:17 +02:00
parent 126547905a
commit 24357370bc
Signed by: lerentis
GPG Key ID: FF0C2839718CAF2E
7 changed files with 258 additions and 7 deletions

View File

@ -3,7 +3,7 @@ GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
GOFMT ?= gofmt -s
VERSION = 0.1.0
VERSION = 0.3.0
test: fmt-check
go test -i $(TEST) || exit 1

36
docs/resources/org.md Normal file
View File

@ -0,0 +1,36 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gitea_org Resource - terraform-provider-gitea"
subcategory: ""
description: |-
Handling Ogranisation resources
---
# gitea_org (Resource)
Handling Ogranisation resources
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `name` (String)
### Optional
- `description` (String)
- `full_name` (String)
- `location` (String)
- `repo_admin_change_team_access` (Boolean)
- `visibility` (String)
- `website` (String)
### Read-Only
- `avatar_url` (String)
- `id` (String) The ID of this resource.

View File

@ -16,3 +16,12 @@ resource "gitea_repository" "mirror" {
migration_service = "gitea"
migration_service_auth_token = var.gitea_mirror_token
}
resource "gitea_org" "test_org" {
name = "test-org"
}
resource "gitea_repository" "org_repo" {
username = gitea_org.test_org.name
name = "org-test-repo"
}

View File

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

View File

@ -73,7 +73,7 @@ func Provider() terraform.ResourceProvider {
},
ResourcesMap: map[string]*schema.Resource{
// "gitea_org": resourceGiteaOrg(),
"gitea_org": resourceGiteaOrg(),
// "gitea_team": resourceGiteaTeam(),
// "gitea_repo": resourceGiteaRepo(),
// "gitea_user": resourceGiteaUser(),

View File

@ -0,0 +1,186 @@
package gitea
import (
"fmt"
"code.gitea.io/sdk/gitea"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
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"
)
type VisibleType string
func resourceOrgRead(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 {
d.SetId("")
return nil
} else {
return err
}
}
err = setOrgResourceData(org, d)
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
}
err = setOrgResourceData(org, d)
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))
err = setOrgResourceData(org, d)
return
}
func respurceOrgDelete(d *schema.ResourceData, meta interface{}) (err error) {
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
}
func setOrgResourceData(org *gitea.Organization, d *schema.ResourceData) (err error) {
d.SetId(fmt.Sprintf("%d", org.ID))
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)
return
}
func resourceGiteaOrg() *schema.Resource {
return &schema.Resource{
Read: resourceOrgRead,
Create: resourceOrgCreate,
Update: resourceOrgUpdate,
Delete: respurceOrgDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"full_name": {
Type: schema.TypeString,
Required: false,
Optional: true,
},
"description": {
Type: schema.TypeString,
Required: false,
Optional: true,
},
"website": {
Type: schema.TypeString,
Required: false,
Optional: true,
},
"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": {
Type: schema.TypeString,
Required: false,
Optional: true,
Default: "public",
},
},
Description: "Handling Ogranisation resources",
}
}

View File

@ -51,16 +51,22 @@ func resourceRepoRead(d *schema.ResourceData, meta interface{}) (err error) {
client := meta.(*gitea.Client)
id, err := strconv.ParseInt(d.Id(), 10, 64)
var resp *gitea.Response
if err != nil {
return err
}
repo, _, err := client.GetRepoByID(id)
repo, resp, err := client.GetRepoByID(id)
if err != nil {
if resp.StatusCode == 404 {
d.SetId("")
return nil
} else {
return err
}
}
err = setRepoResourceData(repo, d)
@ -71,6 +77,16 @@ func resourceRepoCreate(d *schema.ResourceData, meta interface{}) (err error) {
client := meta.(*gitea.Client)
var repo *gitea.Repository
var resp *gitea.Response
var orgRepo bool
_, resp, err = client.GetOrg(d.Get(repoOwner).(string))
if resp.StatusCode == 404 {
orgRepo = false
} else {
orgRepo = true
}
if (d.Get(repoMirror)).(bool) {
opts := gitea.MigrateRepoOption{
@ -105,7 +121,6 @@ func resourceRepoCreate(d *schema.ResourceData, meta interface{}) (err error) {
repo, _, err = client.MigrateRepo(opts)
} else {
opts := gitea.CreateRepoOption{
Name: d.Get(repoName).(string),
Description: d.Get(repoDescription).(string),
@ -119,8 +134,13 @@ func resourceRepoCreate(d *schema.ResourceData, meta interface{}) (err error) {
DefaultBranch: d.Get(repoDefaultBranch).(string),
TrustModel: "default",
}
if orgRepo {
repo, _, err = client.CreateOrgRepo(d.Get(repoOwner).(string), opts)
} else {
repo, _, err = client.CreateRepo(opts)
}
}
if err != nil {
return