terraform-provider-gitea/gitea/provider.go
lerentis 7ba385d44a propose features upstream (#2)
Hi @techknowlogick 👋

as discussed on twitter the changes i made on my fork 😃

not sure if you are aware of this but currently hashicorp only allows publishing via github, so if you want to publish this provider to the terraform registry as well, feel free to also take a look at my goreleaser config and drone/github actions usage her: https://git.uploadfilter24.eu/lerentis/terraform-provider-gitea

Co-authored-by: Tobias Trabelsi <lerentis@uploadfilter24.eu>
Reviewed-on: https://gitea.com/gitea/terraform-provider-gitea/pulls/2
Co-authored-by: lerentis <lerentis@noreply.gitea.io>
Co-committed-by: lerentis <lerentis@noreply.gitea.io>
2022-08-22 23:54:13 +08:00

123 lines
3.5 KiB
Go

package gitea
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)
// Provider returns a terraform.ResourceProvider.
func Provider() terraform.ResourceProvider {
// 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_public_key": resourceGiteaPublicKey(),
"gitea_team": resourceGiteaTeam(),
},
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))
}
return
}