terraform-provider-gitea/gitea/provider.go
Tobias Balle-Petersen 557ea2673a
All checks were successful
Setup Terraform / Terraform Versions (push) Successful in 7s
New resources for managing team membership (#36)
This PR adds two new resources, _gitea_team_membership_ & _gitea_team_members_, in an attempt to decouple _gitea_team_ resources from team memberships. This facilitates the removal of members from teams without altering/recreating an existing _team_ resource.

This PR adresses this issue: #30

The ability to set members in the _gitea_team_ resource has been removed.

The resources proposed here are inspired by similar resources in the _GitHub_ provider:
* [team_members](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/team_members)
* [team_membership](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/team_membership)

# gitea_team_members
A single resource manages all members of a team.

- This resource must be recreated when membership changes. This means, that other team members will temporarily loose their membership until the recreation of the resource is complete.
- If the recreation of the resource fails, other users will have lost their membership until the resource can be recreated.

# gitea_team_membership
A single resource holds the relationship between a single user and a single team.

-  Memberships can be deleted without affecting other users.

Reviewed-on: #36
Reviewed-by: techknowlogick <techknowlogick@noreply.gitea.com>
Co-authored-by: Tobias Balle-Petersen <tobiasbp@gmail.com>
Co-committed-by: Tobias Balle-Petersen <tobiasbp@gmail.com>
2023-11-16 00:52:16 +00:00

132 lines
4.1 KiB
Go

package gitea
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// Provider returns a terraform.ResourceProvider.
func Provider() *schema.Provider {
// The actual provider
return &schema.Provider{
Schema: map[string]*schema.Schema{
"token": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("GITEA_TOKEN", nil),
Description: descriptions["token"],
ConflictsWith: []string{
"username",
},
},
"username": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("GITEA_USERNAME", nil),
Description: descriptions["username"],
ConflictsWith: []string{
"token",
},
},
"password": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("GITEA_PASSWORD", nil),
Description: descriptions["password"],
ConflictsWith: []string{
"token",
},
},
"base_url": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("GITEA_BASE_URL", ""),
Description: descriptions["base_url"],
ValidateFunc: validateAPIURLVersion,
},
"cacert_file": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: descriptions["cacert_file"],
},
"insecure": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: descriptions["insecure"],
},
},
DataSourcesMap: map[string]*schema.Resource{
"gitea_user": dataSourceGiteaUser(),
"gitea_org": dataSourceGiteaOrg(),
// "gitea_team": dataSourceGiteaTeam(),
// "gitea_teams": dataSourceGiteaTeams(),
// "gitea_team_members": dataSourceGiteaTeamMembers(),
"gitea_repo": dataSourceGiteaRepo(),
// "gitea_repos": dataSourceGiteaRepos(),
},
ResourcesMap: map[string]*schema.Resource{
"gitea_org": resourceGiteaOrg(),
// "gitea_team": resourceGiteaTeam(),
// "gitea_repo": resourceGiteaRepo(),
"gitea_user": resourceGiteaUser(),
"gitea_oauth2_app": resourceGiteaOauthApp(),
"gitea_repository": resourceGiteaRepository(),
"gitea_fork": resourceGiteaFork(),
"gitea_public_key": resourceGiteaPublicKey(),
"gitea_team": resourceGiteaTeam(),
"gitea_team_membership": resourceGiteaTeamMembership(),
"gitea_team_members": resourceGiteaTeamMembers(),
"gitea_git_hook": resourceGiteaGitHook(),
"gitea_token": resourceGiteaToken(),
"gitea_repository_key": resourceGiteaRepositoryKey(),
"gitea_repository_webhook": resourceGiteaRepositoryWebhook(),
},
ConfigureFunc: providerConfigure,
}
}
var descriptions map[string]string
func init() {
descriptions = map[string]string{
"token": "The application token used to connect to Gitea.",
"username": "Username in case of using basic auth",
"password": "Password in case of using basic auth",
"base_url": "The Gitea Base API URL",
"cacert_file": "A file containing the ca certificate to use in case ssl certificate is not from a standard chain",
"insecure": "Disable SSL verification of API calls",
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{
Token: d.Get("token").(string),
Username: d.Get("username").(string),
Password: d.Get("password").(string),
BaseURL: d.Get("base_url").(string),
CACertFile: d.Get("cacert_file").(string),
Insecure: d.Get("insecure").(bool),
}
return config.Client()
}
func validateAPIURLVersion(value interface{}, key string) (ws []string, es []error) {
v := value.(string)
if strings.HasSuffix(v, "/api/v1") || strings.HasSuffix(v, "/api/v1/") {
es = append(es, fmt.Errorf("terraform-gitea-provider base URL format is incorrect; Please leave out API Path %s", v))
}
if strings.Contains(v, "localhost") && strings.Contains(v, ".") {
es = append(es, fmt.Errorf("terraform-gitea-provider base URL violates RFC 2606; Please do not define a subdomain for localhost!"))
}
return
}