diff --git a/Makefile b/Makefile index 2ee577f..ed8a6b8 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/docs/resources/org.md b/docs/resources/org.md new file mode 100644 index 0000000..ed959ef --- /dev/null +++ b/docs/resources/org.md @@ -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 + +### 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. + + diff --git a/examples/main.tf b/examples/main.tf index c6753ce..194241b 100644 --- a/examples/main.tf +++ b/examples/main.tf @@ -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" +} \ No newline at end of file diff --git a/examples/provider.tf b/examples/provider.tf index 0c937c4..79b0333 100644 --- a/examples/provider.tf +++ b/examples/provider.tf @@ -2,7 +2,7 @@ terraform { required_providers { gitea = { source = "terraform.local/lerentis/gitea" - version = "0.1.0" + version = "0.3.0" } } } diff --git a/gitea/provider.go b/gitea/provider.go index 3269f26..e99b044 100644 --- a/gitea/provider.go +++ b/gitea/provider.go @@ -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(), diff --git a/gitea/resource_gitea_organisation.go b/gitea/resource_gitea_organisation.go new file mode 100644 index 0000000..384b819 --- /dev/null +++ b/gitea/resource_gitea_organisation.go @@ -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", + } +} diff --git a/gitea/resource_gitea_repository.go b/gitea/resource_gitea_repository.go index e11072e..c228065 100644 --- a/gitea/resource_gitea_repository.go +++ b/gitea/resource_gitea_repository.go @@ -51,15 +51,21 @@ 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 { - return err + 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,7 +134,12 @@ func resourceRepoCreate(d *schema.ResourceData, meta interface{}) (err error) { DefaultBranch: d.Get(repoDefaultBranch).(string), TrustModel: "default", } - repo, _, err = client.CreateRepo(opts) + + if orgRepo { + repo, _, err = client.CreateOrgRepo(d.Get(repoOwner).(string), opts) + } else { + repo, _, err = client.CreateRepo(opts) + } } if err != nil {