Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
b23c36a25f
|
|||
87d8cfd2e5 | |||
85b869d28b | |||
04f842cdfe | |||
c72fcb3a49
|
|||
9c819ad4c2
|
|||
6d5439a00a
|
|||
6d0bd5906a
|
|||
f3c0793a88 | |||
e99be27300 | |||
480994bfa9 |
4
Makefile
4
Makefile
@ -3,7 +3,7 @@ GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
|
|||||||
|
|
||||||
GOFMT ?= gofmt -s
|
GOFMT ?= gofmt -s
|
||||||
|
|
||||||
VERSION = 0.8.0
|
VERSION = 0.11.0
|
||||||
|
|
||||||
test: fmt-check
|
test: fmt-check
|
||||||
go test -i $(TEST) || exit 1
|
go test -i $(TEST) || exit 1
|
||||||
@ -38,4 +38,4 @@ install: build
|
|||||||
@mkdir -p ~/.terraform.d/plugins/terraform.local/lerentis/gitea/${VERSION}/linux_amd64
|
@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}
|
@mv terraform-provider-gitea_${VERSION} ~/.terraform.d/plugins/terraform.local/lerentis/gitea/${VERSION}/linux_amd64/terraform-provider-gitea_${VERSION}
|
||||||
doc:
|
doc:
|
||||||
tfplugindocs
|
tfplugindocs
|
||||||
|
@ -17,7 +17,7 @@ terraform {
|
|||||||
required_providers {
|
required_providers {
|
||||||
gitea = {
|
gitea = {
|
||||||
source = "Lerentis/gitea"
|
source = "Lerentis/gitea"
|
||||||
version = "0.8.0"
|
version = "0.11.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ terraform {
|
|||||||
required_providers {
|
required_providers {
|
||||||
gitea = {
|
gitea = {
|
||||||
source = "Lerentis/gitea"
|
source = "Lerentis/gitea"
|
||||||
version = "0.8.0"
|
version = "0.11.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
61
docs/resources/fork.md
Normal file
61
docs/resources/fork.md
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
---
|
||||||
|
# generated by https://github.com/hashicorp/terraform-plugin-docs
|
||||||
|
page_title: "gitea_fork Resource - terraform-provider-gitea"
|
||||||
|
subcategory: ""
|
||||||
|
description: |-
|
||||||
|
gitea_fork manages repository fork to the current user or an organisation
|
||||||
|
Forking a repository to a dedicated user is currently unsupported
|
||||||
|
Creating a fork using this resource without an organisation will create the fork in the executors name
|
||||||
|
---
|
||||||
|
|
||||||
|
# gitea_fork (Resource)
|
||||||
|
|
||||||
|
`gitea_fork` manages repository fork to the current user or an organisation
|
||||||
|
Forking a repository to a dedicated user is currently unsupported
|
||||||
|
Creating a fork using this resource without an organisation will create the fork in the executors name
|
||||||
|
|
||||||
|
## Example Usage
|
||||||
|
|
||||||
|
```terraform
|
||||||
|
resource "gitea_org" "org1" {
|
||||||
|
name = "org1"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "gitea_org" "org2" {
|
||||||
|
name = "org2"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "gitea_repository" "repo1_in_org1" {
|
||||||
|
username = gitea_org.org1.name
|
||||||
|
name = "repo1-in-org1"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "gitea_fork" "user_fork_of_repo1_in_org1" {
|
||||||
|
owner = gitea_org.org1.name
|
||||||
|
repo = gitea_repository.repo1_in_org1.name
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "gitea_fork" "org2_fork_of_repo1_in_org1" {
|
||||||
|
owner = gitea_org.org1.name
|
||||||
|
repo = gitea_repository.repo1_in_org1.name
|
||||||
|
organization = gitea_org.org2.name
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
<!-- schema generated by tfplugindocs -->
|
||||||
|
## Schema
|
||||||
|
|
||||||
|
### Required
|
||||||
|
|
||||||
|
- `owner` (String) The owner or owning organization of the repository to fork
|
||||||
|
- `repo` (String) The name of the repository to fork
|
||||||
|
|
||||||
|
### Optional
|
||||||
|
|
||||||
|
- `organization` (String) The organization that owns the forked repo
|
||||||
|
|
||||||
|
### Read-Only
|
||||||
|
|
||||||
|
- `id` (String) The ID of this resource.
|
||||||
|
|
||||||
|
|
@ -93,7 +93,10 @@ Need to exist in the gitea instance
|
|||||||
### Read-Only
|
### Read-Only
|
||||||
|
|
||||||
- `created` (String)
|
- `created` (String)
|
||||||
|
- `clone_url` (String)
|
||||||
|
- `html_url` (String)
|
||||||
- `id` (String) The ID of this resource.
|
- `id` (String) The ID of this resource.
|
||||||
|
- `ssh_url` (String)
|
||||||
- `permission_admin` (Boolean)
|
- `permission_admin` (Boolean)
|
||||||
- `permission_pull` (Boolean)
|
- `permission_pull` (Boolean)
|
||||||
- `permission_push` (Boolean)
|
- `permission_push` (Boolean)
|
||||||
|
67
docs/resources/token.md
Normal file
67
docs/resources/token.md
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
---
|
||||||
|
# generated by https://github.com/hashicorp/terraform-plugin-docs
|
||||||
|
page_title: "gitea_token Resource - terraform-provider-gitea"
|
||||||
|
subcategory: ""
|
||||||
|
description: |-
|
||||||
|
gitea_token manages gitea Access Tokens.
|
||||||
|
Due to upstream limitations (see https://gitea.com/gitea/go-sdk/issues/610) this resource
|
||||||
|
can only be used with username/password provider configuration.
|
||||||
|
WARNING:
|
||||||
|
Tokens will be stored in the terraform state!
|
||||||
|
---
|
||||||
|
|
||||||
|
# gitea_token (Resource)
|
||||||
|
|
||||||
|
`gitea_token` manages gitea Access Tokens.
|
||||||
|
|
||||||
|
Due to upstream limitations (see https://gitea.com/gitea/go-sdk/issues/610) this resource
|
||||||
|
can only be used with username/password provider configuration.
|
||||||
|
|
||||||
|
WARNING:
|
||||||
|
Tokens will be stored in the terraform state!
|
||||||
|
|
||||||
|
## Example Usage
|
||||||
|
|
||||||
|
```terraform
|
||||||
|
provider "gitea" {
|
||||||
|
base_url = var.gitea_url
|
||||||
|
# Token Auth can not be used with this resource
|
||||||
|
username = var.gitea_username
|
||||||
|
password = var.gitea_password
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "gitea_user" "test" {
|
||||||
|
username = "test"
|
||||||
|
login_name = "test"
|
||||||
|
password = "Geheim1!"
|
||||||
|
email = "test@user.dev"
|
||||||
|
must_change_password = false
|
||||||
|
admin = true
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "gitea_token" "test_token" {
|
||||||
|
username = resource.gitea_user.test.username
|
||||||
|
name = "test-token"
|
||||||
|
}
|
||||||
|
|
||||||
|
output "token" {
|
||||||
|
value = resource.gitea_token.test_token.token
|
||||||
|
sensitive = true
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
<!-- schema generated by tfplugindocs -->
|
||||||
|
## Schema
|
||||||
|
|
||||||
|
### Required
|
||||||
|
|
||||||
|
- `name` (String) The name of the Access Token
|
||||||
|
- `username` (String) The owner of the Access Token
|
||||||
|
|
||||||
|
### Read-Only
|
||||||
|
|
||||||
|
- `id` (String) The ID of this resource.
|
||||||
|
- `last_eight` (String)
|
||||||
|
- `token` (String, Sensitive) The actual Access Token
|
||||||
|
|
||||||
|
|
@ -71,3 +71,37 @@ resource "gitea_git_hook" "org_repo_pre_receive" {
|
|||||||
repo = gitea_repository.org_repo.name
|
repo = gitea_repository.org_repo.name
|
||||||
content = file("${path.module}/pre-receive.sh")
|
content = file("${path.module}/pre-receive.sh")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resource "gitea_org" "org1" {
|
||||||
|
name = "org1"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "gitea_org" "org2" {
|
||||||
|
name = "org2"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "gitea_repository" "repo1_in_org1" {
|
||||||
|
username = gitea_org.org1.name
|
||||||
|
name = "repo1-in-org1"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "gitea_fork" "user_fork_of_repo1_in_org1" {
|
||||||
|
owner = gitea_org.org1.name
|
||||||
|
repo = gitea_repository.repo1_in_org1.name
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "gitea_fork" "org2_fork_of_repo1_in_org1" {
|
||||||
|
owner = gitea_org.org1.name
|
||||||
|
repo = gitea_repository.repo1_in_org1.name
|
||||||
|
organization = gitea_org.org2.name
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "gitea_token" "test_token" {
|
||||||
|
username = data.gitea_user.me.username
|
||||||
|
name = "test-token"
|
||||||
|
}
|
||||||
|
|
||||||
|
output "token" {
|
||||||
|
value = resource.gitea_token.test_token.token
|
||||||
|
sensitive = true
|
||||||
|
}
|
@ -2,12 +2,14 @@ terraform {
|
|||||||
required_providers {
|
required_providers {
|
||||||
gitea = {
|
gitea = {
|
||||||
source = "terraform.local/lerentis/gitea"
|
source = "terraform.local/lerentis/gitea"
|
||||||
version = "0.8.0"
|
version = "0.11.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
provider "gitea" {
|
provider "gitea" {
|
||||||
base_url = var.gitea_url
|
base_url = var.gitea_url
|
||||||
token = var.gitea_token
|
username = "lerentis"
|
||||||
|
password = var.gitea_password
|
||||||
|
#token = var.gitea_token
|
||||||
}
|
}
|
@ -2,7 +2,7 @@ terraform {
|
|||||||
required_providers {
|
required_providers {
|
||||||
gitea = {
|
gitea = {
|
||||||
source = "Lerentis/gitea"
|
source = "Lerentis/gitea"
|
||||||
version = "0.8.0"
|
version = "0.11.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
23
examples/resources/gitea_fork/resource.tf
Normal file
23
examples/resources/gitea_fork/resource.tf
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
resource "gitea_org" "org1" {
|
||||||
|
name = "org1"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "gitea_org" "org2" {
|
||||||
|
name = "org2"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "gitea_repository" "repo1_in_org1" {
|
||||||
|
username = gitea_org.org1.name
|
||||||
|
name = "repo1-in-org1"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "gitea_fork" "user_fork_of_repo1_in_org1" {
|
||||||
|
owner = gitea_org.org1.name
|
||||||
|
repo = gitea_repository.repo1_in_org1.name
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "gitea_fork" "org2_fork_of_repo1_in_org1" {
|
||||||
|
owner = gitea_org.org1.name
|
||||||
|
repo = gitea_repository.repo1_in_org1.name
|
||||||
|
organization = gitea_org.org2.name
|
||||||
|
}
|
25
examples/resources/gitea_token/resource.tf
Normal file
25
examples/resources/gitea_token/resource.tf
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
provider "gitea" {
|
||||||
|
base_url = var.gitea_url
|
||||||
|
# Token Auth can not be used with this resource
|
||||||
|
username = var.gitea_username
|
||||||
|
password = var.gitea_password
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "gitea_user" "test" {
|
||||||
|
username = "test"
|
||||||
|
login_name = "test"
|
||||||
|
password = "Geheim1!"
|
||||||
|
email = "test@user.dev"
|
||||||
|
must_change_password = false
|
||||||
|
admin = true
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "gitea_token" "test_token" {
|
||||||
|
username = resource.gitea_user.test.username
|
||||||
|
name = "test-token"
|
||||||
|
}
|
||||||
|
|
||||||
|
output "token" {
|
||||||
|
value = resource.gitea_token.test_token.token
|
||||||
|
sensitive = true
|
||||||
|
}
|
@ -8,4 +8,8 @@ variable "gitea_token" {
|
|||||||
|
|
||||||
variable "gitea_mirror_token" {
|
variable "gitea_mirror_token" {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "gitea_password" {
|
||||||
|
|
||||||
}
|
}
|
@ -73,14 +73,16 @@ func Provider() *schema.Provider {
|
|||||||
|
|
||||||
ResourcesMap: map[string]*schema.Resource{
|
ResourcesMap: map[string]*schema.Resource{
|
||||||
"gitea_org": resourceGiteaOrg(),
|
"gitea_org": resourceGiteaOrg(),
|
||||||
// "gitea_team": resourceGiteaTeam(),
|
// "gitea_team": resourceGiteaTeam(),
|
||||||
// "gitea_repo": resourceGiteaRepo(),
|
// "gitea_repo": resourceGiteaRepo(),
|
||||||
"gitea_user": resourceGiteaUser(),
|
"gitea_user": resourceGiteaUser(),
|
||||||
"gitea_oauth2_app": resourceGiteaOauthApp(),
|
"gitea_oauth2_app": resourceGiteaOauthApp(),
|
||||||
"gitea_repository": resourceGiteaRepository(),
|
"gitea_repository": resourceGiteaRepository(),
|
||||||
|
"gitea_fork": resourceGiteaFork(),
|
||||||
"gitea_public_key": resourceGiteaPublicKey(),
|
"gitea_public_key": resourceGiteaPublicKey(),
|
||||||
"gitea_team": resourceGiteaTeam(),
|
"gitea_team": resourceGiteaTeam(),
|
||||||
"gitea_git_hook": resourceGiteaGitHook(),
|
"gitea_git_hook": resourceGiteaGitHook(),
|
||||||
|
"gitea_token": resourceGiteaToken(),
|
||||||
},
|
},
|
||||||
|
|
||||||
ConfigureFunc: providerConfigure,
|
ConfigureFunc: providerConfigure,
|
||||||
|
127
gitea/resource_gitea_fork.go
Normal file
127
gitea/resource_gitea_fork.go
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
package gitea
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
forkOwner string = "owner"
|
||||||
|
forkRepo string = "repo"
|
||||||
|
forkOrganization string = "organization"
|
||||||
|
)
|
||||||
|
|
||||||
|
func resourceForkCreate(d *schema.ResourceData, meta interface{}) (err error) {
|
||||||
|
client := meta.(*gitea.Client)
|
||||||
|
|
||||||
|
var opts gitea.CreateForkOption
|
||||||
|
var org string
|
||||||
|
org = d.Get(forkOrganization).(string)
|
||||||
|
if org != "" {
|
||||||
|
opts.Organization = &org
|
||||||
|
}
|
||||||
|
|
||||||
|
repo, _, err := client.CreateFork(d.Get(forkOwner).(string),
|
||||||
|
d.Get(forkRepo).(string),
|
||||||
|
opts)
|
||||||
|
if err == nil {
|
||||||
|
err = setForkResourceData(repo, d)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceForkRead(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, resp, err := client.GetRepoByID(id)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if resp.StatusCode == 404 {
|
||||||
|
d.SetId("")
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = setForkResourceData(repo, d)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceForkDelete(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)
|
||||||
|
var resp *gitea.Response
|
||||||
|
|
||||||
|
resp, err = client.DeleteRepo(repo.Owner.UserName, repo.Name)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if resp.StatusCode == 404 {
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func setForkResourceData(repo *gitea.Repository, d *schema.ResourceData) (err error) {
|
||||||
|
|
||||||
|
d.SetId(fmt.Sprintf("%d", repo.ID))
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceGiteaFork() *schema.Resource {
|
||||||
|
return &schema.Resource{
|
||||||
|
Read: resourceForkRead,
|
||||||
|
Create: resourceForkCreate,
|
||||||
|
Delete: resourceForkDelete,
|
||||||
|
Importer: &schema.ResourceImporter{
|
||||||
|
StateContext: schema.ImportStatePassthroughContext,
|
||||||
|
},
|
||||||
|
Schema: map[string]*schema.Schema{
|
||||||
|
"owner": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
ForceNew: true,
|
||||||
|
Description: "The owner or owning organization of the repository to fork",
|
||||||
|
},
|
||||||
|
"repo": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
ForceNew: true,
|
||||||
|
Description: "The name of the repository to fork",
|
||||||
|
},
|
||||||
|
"organization": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: false,
|
||||||
|
Optional: true,
|
||||||
|
ForceNew: true,
|
||||||
|
Description: "The organization that owns the forked repo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Description: "`gitea_fork` manages repository fork to the current user or an organisation\n" +
|
||||||
|
"Forking a repository to a dedicated user is currently unsupported\n" +
|
||||||
|
"Creating a fork using this resource without an organisation will create the fork in the executors name",
|
||||||
|
}
|
||||||
|
}
|
@ -530,6 +530,18 @@ func resourceGiteaRepository() *schema.Resource {
|
|||||||
Optional: true,
|
Optional: true,
|
||||||
Default: "",
|
Default: "",
|
||||||
},
|
},
|
||||||
|
"clone_url": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
|
"html_url": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
|
"ssh_url": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Description: "`gitea_repository` manages a gitea repository.\n\n" +
|
Description: "`gitea_repository` manages a gitea repository.\n\n" +
|
||||||
"Per default this repository will be initializiled with the provided configuration (gitignore, License etc.).\n" +
|
"Per default this repository will be initializiled with the provided configuration (gitignore, License etc.).\n" +
|
||||||
|
151
gitea/resource_gitea_token.go
Normal file
151
gitea/resource_gitea_token.go
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
package gitea
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
TokenUsername string = "username"
|
||||||
|
TokenName string = "name"
|
||||||
|
TokenHash string = "token"
|
||||||
|
TokenLastEight string = "last_eight"
|
||||||
|
)
|
||||||
|
|
||||||
|
func searchTokenById(c *gitea.Client, id int64) (res *gitea.AccessToken, err error) {
|
||||||
|
page := 1
|
||||||
|
|
||||||
|
for {
|
||||||
|
tokens, _, err := c.ListAccessTokens(gitea.ListAccessTokensOptions{
|
||||||
|
ListOptions: gitea.ListOptions{
|
||||||
|
Page: page,
|
||||||
|
PageSize: 50,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(tokens) == 0 {
|
||||||
|
return nil, fmt.Errorf("Token with ID %d could not be found", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, token := range tokens {
|
||||||
|
if token.ID == id {
|
||||||
|
return token, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
page += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceTokenCreate(d *schema.ResourceData, meta interface{}) (err error) {
|
||||||
|
|
||||||
|
client := meta.(*gitea.Client)
|
||||||
|
|
||||||
|
var opt gitea.CreateAccessTokenOption
|
||||||
|
opt.Name = d.Get(TokenName).(string)
|
||||||
|
|
||||||
|
token, _, err := client.CreateAccessToken(opt)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = setTokenResourceData(token, d)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceTokenRead(d *schema.ResourceData, meta interface{}) (err error) {
|
||||||
|
|
||||||
|
client := meta.(*gitea.Client)
|
||||||
|
|
||||||
|
var token *gitea.AccessToken
|
||||||
|
|
||||||
|
id, err := strconv.ParseInt(d.Id(), 10, 64)
|
||||||
|
|
||||||
|
token, err = searchTokenById(client, id)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = setTokenResourceData(token, d)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceTokenDelete(d *schema.ResourceData, meta interface{}) (err error) {
|
||||||
|
|
||||||
|
client := meta.(*gitea.Client)
|
||||||
|
var resp *gitea.Response
|
||||||
|
|
||||||
|
resp, err = client.DeleteAccessToken(d.Get(TokenName).(string))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if resp.StatusCode == 404 {
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func setTokenResourceData(token *gitea.AccessToken, d *schema.ResourceData) (err error) {
|
||||||
|
|
||||||
|
d.SetId(fmt.Sprintf("%d", token.ID))
|
||||||
|
d.Set(TokenName, token.Name)
|
||||||
|
if token.Token != "" {
|
||||||
|
d.Set(TokenHash, token.Token)
|
||||||
|
}
|
||||||
|
d.Set(TokenLastEight, token.TokenLastEight)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceGiteaToken() *schema.Resource {
|
||||||
|
return &schema.Resource{
|
||||||
|
Read: resourceTokenRead,
|
||||||
|
Create: resourceTokenCreate,
|
||||||
|
Delete: resourceTokenDelete,
|
||||||
|
Importer: &schema.ResourceImporter{
|
||||||
|
StateContext: schema.ImportStatePassthroughContext,
|
||||||
|
},
|
||||||
|
Schema: map[string]*schema.Schema{
|
||||||
|
"username": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
ForceNew: true,
|
||||||
|
Description: "The owner of the Access Token",
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
ForceNew: true,
|
||||||
|
Description: "The name of the Access Token",
|
||||||
|
},
|
||||||
|
"token": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Computed: true,
|
||||||
|
Sensitive: true,
|
||||||
|
Description: "The actual Access Token",
|
||||||
|
},
|
||||||
|
"last_eight": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Description: "`gitea_token` manages gitea Access Tokens.\n\n" +
|
||||||
|
"Due to upstream limitations (see https://gitea.com/gitea/go-sdk/issues/610) this resource\n" +
|
||||||
|
"can only be used with username/password provider configuration.\n\n" +
|
||||||
|
"WARNING:\n" +
|
||||||
|
"Tokens will be stored in the terraform state!",
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user