Initial commit
This commit is contained in:
73
gitea/config.go
Normal file
73
gitea/config.go
Normal file
@ -0,0 +1,73 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/helper/logging"
|
||||
)
|
||||
|
||||
// Config is per-provider, specifies where to connect to gitea
|
||||
type Config struct {
|
||||
Token string
|
||||
Username string
|
||||
Password string
|
||||
BaseURL string
|
||||
Insecure bool
|
||||
CACertFile string
|
||||
}
|
||||
|
||||
// Client returns a *gitea.Client to interact with the configured gitea instance
|
||||
func (c *Config) Client() (interface{}, error) {
|
||||
|
||||
if c.Token == "" && c.Username == "" {
|
||||
return nil, fmt.Errorf("either a token or a username needs to be used")
|
||||
}
|
||||
// Configure TLS/SSL
|
||||
tlsConfig := &tls.Config{}
|
||||
|
||||
// If a CACertFile has been specified, use that for cert validation
|
||||
if c.CACertFile != "" {
|
||||
caCert, err := ioutil.ReadFile(c.CACertFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
caCertPool := x509.NewCertPool()
|
||||
caCertPool.AppendCertsFromPEM(caCert)
|
||||
tlsConfig.RootCAs = caCertPool
|
||||
}
|
||||
|
||||
// If configured as insecure, turn off SSL verification
|
||||
if c.Insecure {
|
||||
tlsConfig.InsecureSkipVerify = true
|
||||
}
|
||||
|
||||
t := http.DefaultTransport.(*http.Transport).Clone()
|
||||
t.TLSClientConfig = tlsConfig
|
||||
t.MaxIdleConnsPerHost = 100
|
||||
|
||||
httpClient := &http.Client{
|
||||
Transport: logging.NewTransport("Gitea", t),
|
||||
}
|
||||
|
||||
if c.BaseURL == "" {
|
||||
c.BaseURL = "https://gitea.com"
|
||||
}
|
||||
|
||||
client := gitea.NewClient(c.BaseURL, c.Token)
|
||||
client.SetHTTPClient(httpClient)
|
||||
|
||||
if c.Username != "" {
|
||||
client.SetBasicAuth(c.Username, c.Password)
|
||||
}
|
||||
|
||||
// Test the credentials by checking we can get information about the authenticated user.
|
||||
_, err := client.GetMyUserInfo()
|
||||
|
||||
return client, err
|
||||
}
|
85
gitea/data_source_gitea_org.go
Normal file
85
gitea/data_source_gitea_org.go
Normal file
@ -0,0 +1,85 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
|
||||
)
|
||||
|
||||
func dataSourceGiteaOrg() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Read: dataSourceGitlabUserRead,
|
||||
Schema: map[string]*schema.Schema{
|
||||
"id": {
|
||||
Type: schema.TypeInt,
|
||||
Computed: true,
|
||||
},
|
||||
"name": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Optional: true,
|
||||
},
|
||||
"full_name": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"description": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"avatar_url": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"website": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"location": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"visibility": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func dataSourceGitlabOrgRead(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*gitea.Client)
|
||||
|
||||
var org *gitea.Organization
|
||||
var err error
|
||||
|
||||
log.Printf("[INFO] Reading Gitea Org")
|
||||
|
||||
nameData, nameOk := d.GetOk("name")
|
||||
|
||||
if !nameOk {
|
||||
return fmt.Errorf("name of org must be passed")
|
||||
}
|
||||
name := strings.ToLower(nameData.(string))
|
||||
|
||||
org, err = client.GetOrg(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.Set("id", org.ID)
|
||||
d.Set("name", org.UserName)
|
||||
d.Set("full_name", org.FullName)
|
||||
d.Set("avatar_url", org.AvatarURL)
|
||||
d.Set("location", org.Location)
|
||||
d.Set("website", org.Website)
|
||||
d.Set("description", org.Description)
|
||||
d.Set("visibility", org.Visibility)
|
||||
|
||||
d.SetId(fmt.Sprintf("%d", org.ID))
|
||||
|
||||
return nil
|
||||
}
|
1
gitea/data_source_gitea_repo.go
Normal file
1
gitea/data_source_gitea_repo.go
Normal file
@ -0,0 +1 @@
|
||||
package gitea
|
1
gitea/data_source_gitea_repos.go
Normal file
1
gitea/data_source_gitea_repos.go
Normal file
@ -0,0 +1 @@
|
||||
package gitea
|
1
gitea/data_source_gitea_team.go
Normal file
1
gitea/data_source_gitea_team.go
Normal file
@ -0,0 +1 @@
|
||||
package gitea
|
1
gitea/data_source_gitea_teams.go
Normal file
1
gitea/data_source_gitea_teams.go
Normal file
@ -0,0 +1 @@
|
||||
package gitea
|
91
gitea/data_source_gitea_user.go
Normal file
91
gitea/data_source_gitea_user.go
Normal file
@ -0,0 +1,91 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
|
||||
)
|
||||
|
||||
func dataSourceGiteaUser() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Read: dataSourceGitlabUserRead,
|
||||
Schema: map[string]*schema.Schema{
|
||||
"id": {
|
||||
Type: schema.TypeInt,
|
||||
Computed: true,
|
||||
},
|
||||
"username": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
Optional: true,
|
||||
},
|
||||
"email": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"full_name": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"is_admin": {
|
||||
Type: schema.TypeBool,
|
||||
Computed: true,
|
||||
},
|
||||
"avatar_url": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"language": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"last_login": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"created": {
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func dataSourceGitlabUserRead(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*gitea.Client)
|
||||
|
||||
var user *gitea.User
|
||||
var err error
|
||||
|
||||
log.Printf("[INFO] Reading Gitea user")
|
||||
|
||||
usernameData, usernameOk := d.GetOk("username")
|
||||
|
||||
if !usernameOk {
|
||||
user, err = client.GetMyUserInfo()
|
||||
} else {
|
||||
username := strings.ToLower(usernameData.(string))
|
||||
|
||||
user, err = client.GetUserInfo(username)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
d.Set("id", user.ID)
|
||||
d.Set("username", user.UserName)
|
||||
d.Set("email", user.Email)
|
||||
d.Set("full_name", user.FullName)
|
||||
d.Set("is_admin", user.IsAdmin)
|
||||
d.Set("created", user.Created)
|
||||
d.Set("avatar_url", user.AvatarURL)
|
||||
d.Set("last_login", user.LastLogin)
|
||||
d.Set("language", user.Language)
|
||||
|
||||
d.SetId(fmt.Sprintf("%d", user.ID))
|
||||
|
||||
return nil
|
||||
}
|
62
gitea/data_source_gitea_user_test.go
Normal file
62
gitea/data_source_gitea_user_test.go
Normal file
@ -0,0 +1,62 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/terraform"
|
||||
)
|
||||
|
||||
func TestAccDataSourceGiteaUser_basic(t *testing.T) {
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
Steps: []resource.TestStep{
|
||||
// Get user using its username
|
||||
{
|
||||
Config: testAccDataGiteaUserConfigUsername(),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccDataSourceGiteaUser("data.gitea_user.foo"),
|
||||
),
|
||||
},
|
||||
{
|
||||
Config: testAccDataGiteaUserConfigUsername(),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccDataSourceGiteaUser("data.gitea_user.self"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccDataSourceGiteaUser(src string) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
|
||||
user := s.RootModule().Resources[src]
|
||||
userResource := user.Primary.Attributes
|
||||
|
||||
testAttributes := []string{
|
||||
"username",
|
||||
}
|
||||
|
||||
for _, attribute := range testAttributes {
|
||||
if userResource[attribute] != "test01" {
|
||||
return fmt.Errorf("Expected user's parameter `%s` to be: %s, but got: `%s`", attribute, userResource[attribute], "test01")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func testAccDataGiteaUserConfigUsername() string {
|
||||
return fmt.Sprintf(`
|
||||
data "gitea_user" "foo" {
|
||||
username = "test01"
|
||||
}
|
||||
data "gitea_user" "self" {
|
||||
}
|
||||
`)
|
||||
}
|
1
gitea/data_source_team_members.go
Normal file
1
gitea/data_source_team_members.go
Normal file
@ -0,0 +1 @@
|
||||
package gitea
|
118
gitea/provider.go
Normal file
118
gitea/provider.go
Normal file
@ -0,0 +1,118 @@
|
||||
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(),
|
||||
},
|
||||
|
||||
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
|
||||
}
|
35
gitea/provider_test.go
Normal file
35
gitea/provider_test.go
Normal file
@ -0,0 +1,35 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/terraform"
|
||||
)
|
||||
|
||||
var testAccProviders map[string]terraform.ResourceProvider
|
||||
var testAccProvider *schema.Provider
|
||||
|
||||
func init() {
|
||||
testAccProvider = Provider().(*schema.Provider)
|
||||
testAccProviders = map[string]terraform.ResourceProvider{
|
||||
"gitea": testAccProvider,
|
||||
}
|
||||
}
|
||||
|
||||
func TestProvider(t *testing.T) {
|
||||
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProvider_impl(t *testing.T) {
|
||||
var _ terraform.ResourceProvider = Provider()
|
||||
}
|
||||
|
||||
func testAccPreCheck(t *testing.T) {
|
||||
if v := os.Getenv("GITEA_TOKEN"); v == "" {
|
||||
t.Fatal("GITEA_TOKEN must be set for acceptance tests")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user