From a79db16011c6750661132e6f01b7031cce4bf5dc Mon Sep 17 00:00:00 2001 From: Tobias Trabelsi Date: Sat, 4 Jun 2022 17:17:08 +0200 Subject: [PATCH] added repository resource and moved repo --- .drone.yml | 32 +++ .gitignore | 1 + Makefile | 9 + README.md | 9 +- docs/data-sources/repo.md | 5 +- docs/resources/oauth2_app.md | 4 +- docs/resources/repository.md | 57 +++++ examples/.gitignore | 4 + examples/main.tf | 8 + examples/provider.tf | 13 ++ examples/variables.tf | 7 + gitea/provider.go | 1 + gitea/resource_gitea_repository.go | 354 +++++++++++++++++++++++++++++ go.mod | 4 +- go.sum | 1 - main.go | 4 +- scripts/docker-compose.yaml | 22 ++ 17 files changed, 519 insertions(+), 16 deletions(-) create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 docs/resources/repository.md create mode 100644 examples/.gitignore create mode 100644 examples/main.tf create mode 100644 examples/provider.tf create mode 100644 examples/variables.tf create mode 100644 gitea/resource_gitea_repository.go create mode 100644 scripts/docker-compose.yaml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..a0e533a --- /dev/null +++ b/.drone.yml @@ -0,0 +1,32 @@ +--- +kind: pipeline +type: kubernetes +name: terraform-provider-gitea +platform: + os: linux + arch: amd64 +steps: + - name: init + image: golang:1.18.3-alpine3.16 + commands: + - "go mod download" + when: + event: + - push + - pull_request + - name: build + image: golang:1.18.3-alpine3.16 + commands: + - "make build" + when: + event: + - push + - pull_request + - name: test + image: golang:1.18.3-alpine3.16 + commands: + - "make test" + when: + event: + - push + - pull_request \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..600d2d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode \ No newline at end of file diff --git a/Makefile b/Makefile index 8a5c11f..2ee577f 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,8 @@ GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor) GOFMT ?= gofmt -s +VERSION = 0.1.0 + test: fmt-check go test -i $(TEST) || exit 1 echo $(TEST) | \ @@ -28,3 +30,10 @@ fmt-check: echo "$${diff}"; \ exit 1; \ fi; +build: + go build -ldflags="-X 'main.Version=${VERSION}'" -o terraform-provider-gitea_${VERSION} +install: build + @echo installing to + @echo ~/.terraform.d/plugins/terraform.local/lerentis/gitea/${VERSION}/linux_amd64/terraform-provider-gitea_${VERSION} + @mkdir -p ~/.terraform.d/plugins/terraform.local/lerentis/gitea/${VERSION}/linux_amd64 + @mv terraform-provider-gitea_${VERSION} ~/.terraform.d/plugins/terraform.local/lerentis/gitea/${VERSION}/linux_amd64/terraform-provider-gitea_${VERSION} diff --git a/README.md b/README.md index 2efbdaa..ccebeef 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,7 @@ Terraform Gitea Provider -# Contributing +## History + +This is a fork of https://gitea.com/gitea/terraform-provider-gitea. Many thanks for the foundation of this provider -This is a fork of https://gitea.com/gitea/terraform-provider-gitea -Currently implemented build/release automations and available on -terraform registry for convenience. PRs raised here will eventually -be contributed back to the main repo if and when the CI automation -there is ready. diff --git a/docs/data-sources/repo.md b/docs/data-sources/repo.md index c033cab..0bbe6c6 100644 --- a/docs/data-sources/repo.md +++ b/docs/data-sources/repo.md @@ -20,10 +20,6 @@ description: |- - `name` (String) - `username` (String) -### Optional - -- `id` (String) The ID of this resource. - ### Read-Only - `clone_url` (String) @@ -34,6 +30,7 @@ description: |- - `forks` (Number) - `full_name` (String) - `html_url` (String) +- `id` (String) The ID of this resource. - `mirror` (Boolean) - `open_issue_count` (Number) - `permission_admin` (Boolean) diff --git a/docs/resources/oauth2_app.md b/docs/resources/oauth2_app.md index ae200f1..9dd1740 100644 --- a/docs/resources/oauth2_app.md +++ b/docs/resources/oauth2_app.md @@ -22,8 +22,8 @@ Handling [gitea oauth application](https://docs.gitea.io/en-us/oauth2-provider/) ### Read-Only -- `client_id` (String) OAuth2 client id -- `client_secret` (String, Sensitive) Oauth2 client secret +- `client_id` (String) OAuth2 Application client id +- `client_secret` (String, Sensitive) Oauth2 Application client secret - `id` (String) The ID of this resource. diff --git a/docs/resources/repository.md b/docs/resources/repository.md new file mode 100644 index 0000000..fd2f7d4 --- /dev/null +++ b/docs/resources/repository.md @@ -0,0 +1,57 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "gitea_repository Resource - terraform-provider-gitea" +subcategory: "" +description: |- + Handling Repository resources +--- + +# gitea_repository (Resource) + +Handling Repository resources + + + + +## Schema + +### Required + +- `name` (String) +- `username` (String) + +### Optional + +- `allow_manual_merge` (Boolean) +- `allow_merge_commits` (Boolean) +- `allow_rebase` (Boolean) +- `allow_rebase_explicit` (Boolean) +- `allow_squash_merge` (Boolean) +- `archived` (Boolean) +- `auto_init` (Boolean) +- `autodetect_manual_merge` (Boolean) +- `default_branch` (String) +- `description` (String) +- `gitignores` (String) +- `has_issues` (Boolean) +- `has_projects` (Boolean) +- `has_pull_requests` (Boolean) +- `has_wiki` (Boolean) +- `ignore_whitespace_conflicts` (Boolean) +- `issue_labels` (String) +- `license` (String) +- `private` (Boolean) +- `readme` (String) +- `repo_template` (Boolean) +- `website` (String) + +### Read-Only + +- `created` (String) +- `id` (String) The ID of this resource. +- `permission_admin` (Boolean) +- `permission_pull` (Boolean) +- `permission_push` (Boolean) +- `updated` (String) + + diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000..da6ebff --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1,4 @@ +.terraform +.terraform.lock.hcl +terraform.tfstate +terraform.tfstate.backup \ No newline at end of file diff --git a/examples/main.tf b/examples/main.tf new file mode 100644 index 0000000..8dfe28f --- /dev/null +++ b/examples/main.tf @@ -0,0 +1,8 @@ +resource "gitea_repository" "test" { + username = "lerentis" + name = "test" + private = true + issue_labels = "Default" + license = "MIT" + gitignores = "Go" +} diff --git a/examples/provider.tf b/examples/provider.tf new file mode 100644 index 0000000..0c937c4 --- /dev/null +++ b/examples/provider.tf @@ -0,0 +1,13 @@ +terraform { + required_providers { + gitea = { + source = "terraform.local/lerentis/gitea" + version = "0.1.0" + } + } +} + +provider "gitea" { + base_url = var.gitea_url + token = var.gitea_token +} \ No newline at end of file diff --git a/examples/variables.tf b/examples/variables.tf new file mode 100644 index 0000000..a88326f --- /dev/null +++ b/examples/variables.tf @@ -0,0 +1,7 @@ +variable "gitea_url" { + default = "http://localhost:3000/" +} + +variable "gitea_token" { + +} \ No newline at end of file diff --git a/gitea/provider.go b/gitea/provider.go index 79a8ca9..3269f26 100644 --- a/gitea/provider.go +++ b/gitea/provider.go @@ -78,6 +78,7 @@ func Provider() terraform.ResourceProvider { // "gitea_repo": resourceGiteaRepo(), // "gitea_user": resourceGiteaUser(), "gitea_oauth2_app": resourceGiteaOauthApp(), + "gitea_repository": resourceGiteaRepository(), }, ConfigureFunc: providerConfigure, diff --git a/gitea/resource_gitea_repository.go b/gitea/resource_gitea_repository.go new file mode 100644 index 0000000..78aa6de --- /dev/null +++ b/gitea/resource_gitea_repository.go @@ -0,0 +1,354 @@ +package gitea + +import ( + "fmt" + "strconv" + + "code.gitea.io/sdk/gitea" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +const ( + repoOwner string = "username" + repoName string = "name" + repoDescription string = "description" + repoPrivateFlag string = "private" + repoIssueLabels string = "issue_labels" + repoAutoInit string = "auto_init" + repoTemplate string = "repo_template" + repoGitignores string = "gitignores" + repoLicense string = "license" + repoReadme string = "readme" + repoDefaultBranch string = "default_branch" + repoWebsite string = "website" + repoIssues string = "has_issues" + repoWiki string = "has_wiki" + repoPrs string = "has_pull_requests" + repoProjects string = "has_projects" + repoIgnoreWhitespace string = "ignore_whitespace_conflicts" + repoAllowMerge string = "allow_merge_commits" + repoAllowRebase string = "allow_rebase" + repoAllowRebaseMerge string = "allow_rebase_explicit" + repoAllowSquash string = "allow_squash_merge" + repoAchived string = "archived" + repoAllowManualMerge string = "allow_manual_merge" + repoAutodetectManualMerge string = "autodetect_manual_merge" +) + +func resourceRepoRead(d *schema.ResourceData, meta interface{}) (err error) { + client := meta.(*gitea.Client) + + id, err := strconv.ParseInt(d.Id(), 10, 64) + + if err != nil { + return err + } + + repo, _, err := client.GetRepoByID(id) + + if err != nil { + return err + } + + err = setRepoResourceData(repo, d) + + return +} + +func resourceRepoCreate(d *schema.ResourceData, meta interface{}) (err error) { + client := meta.(*gitea.Client) + + var repo *gitea.Repository + + opts := gitea.CreateRepoOption{ + Name: d.Get(repoName).(string), + Description: d.Get(repoDescription).(string), + Private: d.Get(repoPrivateFlag).(bool), + IssueLabels: d.Get(repoIssueLabels).(string), + AutoInit: d.Get(repoAutoInit).(bool), + Template: d.Get(repoTemplate).(bool), + Gitignores: d.Get(repoGitignores).(string), + License: d.Get(repoLicense).(string), + Readme: d.Get(repoReadme).(string), + DefaultBranch: d.Get(repoDefaultBranch).(string), + TrustModel: "default", + } + repo, _, err = client.CreateRepo(opts) + + if err != nil { + return + } + + err = setRepoResourceData(repo, d) + + return +} + +func resourceRepoUpdate(d *schema.ResourceData, meta interface{}) (err error) { + client := meta.(*gitea.Client) + + var repo *gitea.Repository + + var name string = d.Get(repoName).(string) + var description string = d.Get(repoDescription).(string) + var website string = d.Get(repoDescription).(string) + var private bool = d.Get(repoPrivateFlag).(bool) + var template bool = d.Get(repoTemplate).(bool) + var hasIssues bool = d.Get(repoIssues).(bool) + var hasWiki bool = d.Get(repoWiki).(bool) + var defaultBranch string = d.Get(repoDefaultBranch).(string) + var hasPRs bool = d.Get(repoPrs).(bool) + var hasProjects bool = d.Get(repoProjects).(bool) + var ignoreWhitespaceConflicts bool = d.Get(repoIgnoreWhitespace).(bool) + var allowMerge bool = d.Get(repoAllowMerge).(bool) + var allowRebase bool = d.Get(repoAllowRebase).(bool) + var allowRebaseMerge bool = d.Get(repoAllowRebaseMerge).(bool) + var allowSquash bool = d.Get(repoAllowSquash).(bool) + var archived bool = d.Get(repoAchived).(bool) + var allowManualMerge bool = d.Get(repoAllowManualMerge).(bool) + var autodetectManualMerge bool = d.Get(repoAutodetectManualMerge).(bool) + + opts := gitea.EditRepoOption{ + Name: &name, + Description: &description, + Website: &website, + Private: &private, + Template: &template, + HasIssues: &hasIssues, + HasWiki: &hasWiki, + DefaultBranch: &defaultBranch, + HasPullRequests: &hasPRs, + HasProjects: &hasProjects, + IgnoreWhitespaceConflicts: &ignoreWhitespaceConflicts, + AllowMerge: &allowMerge, + AllowRebase: &allowRebase, + AllowRebaseMerge: &allowRebaseMerge, + AllowSquash: &allowSquash, + Archived: &archived, + AllowManualMerge: &allowManualMerge, + AutodetectManualMerge: &autodetectManualMerge, + } + + repo, _, err = client.EditRepo(d.Get(repoOwner).(string), d.Get(repoName).(string), opts) + + if err != nil { + return err + } + err = setRepoResourceData(repo, d) + + return + +} + +func respurceRepoDelete(d *schema.ResourceData, meta interface{}) (err error) { + client := meta.(*gitea.Client) + + client.DeleteRepo(d.Get(repoOwner).(string), d.Get(repoName).(string)) + + return +} + +func setRepoResourceData(repo *gitea.Repository, d *schema.ResourceData) (err error) { + d.SetId(fmt.Sprintf("%d", repo.ID)) + d.Set("name", repo.Name) + d.Set("description", repo.Description) + d.Set("full_name", repo.FullName) + d.Set("description", repo.Description) + d.Set("private", repo.Private) + d.Set("fork", repo.Fork) + d.Set("mirror", repo.Mirror) + d.Set("size", repo.Size) + d.Set("html_url", repo.HTMLURL) + d.Set("ssh_url", repo.SSHURL) + d.Set("clone_url", repo.CloneURL) + d.Set("website", repo.Website) + d.Set("stars", repo.Stars) + d.Set("forks", repo.Forks) + d.Set("watchers", repo.Watchers) + d.Set("open_issue_count", repo.OpenIssues) + d.Set("default_branch", repo.DefaultBranch) + d.Set("created", repo.Created) + d.Set("updated", repo.Updated) + d.Set("permission_admin", repo.Permissions.Admin) + d.Set("permission_push", repo.Permissions.Push) + d.Set("permission_pull", repo.Permissions.Pull) + + return +} + +func resourceGiteaRepository() *schema.Resource { + return &schema.Resource{ + Read: resourceRepoRead, + Create: resourceRepoCreate, + Update: resourceRepoUpdate, + Delete: respurceRepoDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "username": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "auto_init": { + Type: schema.TypeBool, + Required: false, + Optional: true, + Default: true, + }, + "repo_template": { + Type: schema.TypeBool, + Required: false, + Optional: true, + Default: false, + }, + "issue_labels": { + Type: schema.TypeString, + Required: false, + Optional: true, + Default: "Default", + }, + "gitignores": { + Type: schema.TypeString, + Required: false, + Optional: true, + Default: "", + }, + "license": { + Type: schema.TypeString, + Required: false, + Optional: true, + Default: "", + }, + "readme": { + Type: schema.TypeString, + Required: false, + Optional: true, + Default: "", + }, + "description": { + Type: schema.TypeString, + Required: false, + Optional: true, + Default: "", + }, + "private": { + Type: schema.TypeBool, + Required: false, + Optional: true, + Default: true, + }, + "default_branch": { + Type: schema.TypeString, + Required: false, + Optional: true, + Default: "main", + }, + "created": { + Type: schema.TypeString, + Computed: true, + }, + "updated": { + Type: schema.TypeString, + Computed: true, + }, + "permission_admin": { + Type: schema.TypeBool, + Computed: true, + }, + "permission_push": { + Type: schema.TypeBool, + Computed: true, + }, + "permission_pull": { + Type: schema.TypeBool, + Computed: true, + }, + "website": { + Type: schema.TypeString, + Required: false, + Optional: true, + Default: "", + }, + "has_issues": { + Type: schema.TypeBool, + Required: false, + Optional: true, + Default: true, + }, + "has_wiki": { + Type: schema.TypeBool, + Required: false, + Optional: true, + Default: true, + }, + "has_pull_requests": { + Type: schema.TypeBool, + Required: false, + Optional: true, + Default: true, + }, + "has_projects": { + Type: schema.TypeBool, + Required: false, + Optional: true, + Default: true, + }, + "ignore_whitespace_conflicts": { + Type: schema.TypeBool, + Required: false, + Optional: true, + Default: true, + }, + "allow_merge_commits": { + Type: schema.TypeBool, + Required: false, + Optional: true, + Default: true, + }, + "allow_rebase": { + Type: schema.TypeBool, + Required: false, + Optional: true, + Default: true, + }, + "allow_rebase_explicit": { + Type: schema.TypeBool, + Required: false, + Optional: true, + Default: true, + }, + "allow_squash_merge": { + Type: schema.TypeBool, + Required: false, + Optional: true, + Default: true, + }, + "archived": { + Type: schema.TypeBool, + Required: false, + Optional: true, + Default: false, + }, + "allow_manual_merge": { + Type: schema.TypeBool, + Required: false, + Optional: true, + Default: true, + }, + "autodetect_manual_merge": { + Type: schema.TypeBool, + Required: false, + Optional: true, + Default: true, + }, + }, + Description: "Handling Repository resources", + } +} diff --git a/go.mod b/go.mod index 086b340..8a78eed 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ -module code.gitea.io/terraform-provider-gitea +module git.uploadfilter24.eu/terraform-provider-gitea -go 1.17 +go 1.18 require ( code.gitea.io/sdk/gitea v0.15.1 diff --git a/go.sum b/go.sum index eb12ded..89e30f6 100644 --- a/go.sum +++ b/go.sum @@ -261,7 +261,6 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/ulikunitz/xz v0.5.5 h1:pFrO0lVpTBXLpYw+pnLj6TbvHuyjXMfjGeCwSqCVwok= github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8= github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= -github.com/vmihailenco/msgpack v4.0.1+incompatible h1:RMF1enSPeKTlXrXdOcqjFUElywVZjjC6pqse21bKbEU= github.com/vmihailenco/msgpack v4.0.1+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= github.com/vmihailenco/msgpack/v4 v4.3.12 h1:07s4sz9IReOgdikxLTKNbBdqDMLsjPKXwvCazn8G65U= github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4= diff --git a/main.go b/main.go index 730ab2a..d754096 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,12 @@ package main // import "src.techknowlogick.com/terraform-provider-gitea" import ( - "code.gitea.io/terraform-provider-gitea/gitea" + "git.uploadfilter24.eu/terraform-provider-gitea/gitea" "github.com/hashicorp/terraform-plugin-sdk/plugin" ) +var Version = "development" + func main() { plugin.Serve(&plugin.ServeOpts{ ProviderFunc: gitea.Provider}) diff --git a/scripts/docker-compose.yaml b/scripts/docker-compose.yaml new file mode 100644 index 0000000..aef83e2 --- /dev/null +++ b/scripts/docker-compose.yaml @@ -0,0 +1,22 @@ +version: "3" + +networks: + gitea: + external: false + +services: + server: + image: gitea/gitea:1.16.8 + container_name: gitea + environment: + - USER_UID=1000 + - USER_GID=1000 + restart: always + networks: + - gitea + volumes: + - /etc/timezone:/etc/timezone:ro + - /etc/localtime:/etc/localtime:ro + ports: + - "3000:3000" + - "222:22" \ No newline at end of file