Merge branch 'cwood-cwood/bitbucket-provider'
This commit is contained in:
commit
83bffb4dea
|
@ -0,0 +1,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform/builtin/providers/bitbucket"
|
||||
"github.com/hashicorp/terraform/plugin"
|
||||
)
|
||||
|
||||
func main() {
|
||||
plugin.Serve(&plugin.ServeOpts{
|
||||
ProviderFunc: bitbucket.Provider,
|
||||
})
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package bitbucket
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type BitbucketClient struct {
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
func (c *BitbucketClient) Get(endpoint string) (*http.Response, error) {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", "https://api.bitbucket.org/"+endpoint, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.SetBasicAuth(c.Username, c.Password)
|
||||
return client.Do(req)
|
||||
|
||||
}
|
||||
|
||||
func (c *BitbucketClient) Post(endpoint string, jsonpayload *bytes.Buffer) (*http.Response, error) {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("POST", "https://api.bitbucket.org/"+endpoint, jsonpayload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.SetBasicAuth(c.Username, c.Password)
|
||||
req.Header.Add("content-type", "application/json")
|
||||
return client.Do(req)
|
||||
}
|
||||
|
||||
func (c *BitbucketClient) Put(endpoint string, jsonpayload *bytes.Buffer) (*http.Response, error) {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("PUT", "https://api.bitbucket.org/"+endpoint, jsonpayload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.SetBasicAuth(c.Username, c.Password)
|
||||
req.Header.Add("content-type", "application/json")
|
||||
return client.Do(req)
|
||||
}
|
||||
|
||||
func (c *BitbucketClient) PutOnly(endpoint string) (*http.Response, error) {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("PUT", "https://api.bitbucket.org/"+endpoint, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.SetBasicAuth(c.Username, c.Password)
|
||||
return client.Do(req)
|
||||
}
|
||||
|
||||
func (c *BitbucketClient) Delete(endpoint string) (*http.Response, error) {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("DELETE", "https://api.bitbucket.org/"+endpoint, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.SetBasicAuth(c.Username, c.Password)
|
||||
return client.Do(req)
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package bitbucket
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
func Provider() terraform.ResourceProvider {
|
||||
return &schema.Provider{
|
||||
Schema: map[string]*schema.Schema{
|
||||
"username": {
|
||||
Required: true,
|
||||
Type: schema.TypeString,
|
||||
DefaultFunc: schema.EnvDefaultFunc("BITBUCKET_USERNAME", nil),
|
||||
},
|
||||
"password": {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
DefaultFunc: schema.EnvDefaultFunc("BITBUCKET_PASSWORD", nil),
|
||||
},
|
||||
},
|
||||
ConfigureFunc: providerConfigure,
|
||||
ResourcesMap: map[string]*schema.Resource{
|
||||
"bitbucket_hook": resourceHook(),
|
||||
"bitbucket_default_reviewers": resourceDefaultReviewers(),
|
||||
"bitbucket_repository": resourceRepository(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
||||
client := &BitbucketClient{
|
||||
Username: d.Get("username").(string),
|
||||
Password: d.Get("password").(string),
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package bitbucket
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const testRepo string = "test-repo"
|
||||
|
||||
var testAccProviders map[string]terraform.ResourceProvider
|
||||
var testAccProvider *schema.Provider
|
||||
|
||||
func init() {
|
||||
testAccProvider = Provider().(*schema.Provider)
|
||||
testAccProviders = map[string]terraform.ResourceProvider{
|
||||
"bitbucket": 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("BITBUCKET_USERNAME"); v == "" {
|
||||
t.Fatal("BITBUCKET_USERNAME must be set for acceptence tests")
|
||||
}
|
||||
if v := os.Getenv("BITBUCKET_PASSWORD"); v == "" {
|
||||
t.Fatal("BITBUCKET_PASSWORD must be set for acceptence tests")
|
||||
}
|
||||
}
|
|
@ -0,0 +1,121 @@
|
|||
package bitbucket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
)
|
||||
|
||||
type Reviewer struct {
|
||||
DisplayName string `json:"display_name,omitempty"`
|
||||
UUID string `json:"uuid,omitempty"`
|
||||
Username string `json:"username,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
type PaginatedReviewers struct {
|
||||
Values []Reviewer `json:"values,omitempty"`
|
||||
}
|
||||
|
||||
func resourceDefaultReviewers() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceDefaultReviewersCreate,
|
||||
Read: resourceDefaultReviewersRead,
|
||||
Delete: resourceDefaultReviewersDelete,
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"owner": {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
"repository": {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
"reviewers": {
|
||||
Type: schema.TypeSet,
|
||||
Elem: &schema.Schema{Type: schema.TypeString},
|
||||
Required: true,
|
||||
Set: schema.HashString,
|
||||
ForceNew: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func resourceDefaultReviewersCreate(d *schema.ResourceData, m interface{}) error {
|
||||
client := m.(*BitbucketClient)
|
||||
|
||||
for _, user := range d.Get("reviewers").(*schema.Set).List() {
|
||||
reviewer_resp, err := client.PutOnly(fmt.Sprintf("2.0/repositories/%s/%s/default-reviewers/%s",
|
||||
d.Get("owner").(string),
|
||||
d.Get("repository").(string),
|
||||
user,
|
||||
))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if reviewer_resp.StatusCode != 200 {
|
||||
return fmt.Errorf("Failed to create reviewer %s got code %d", user.(string), reviewer_resp.StatusCode)
|
||||
}
|
||||
|
||||
defer reviewer_resp.Body.Close()
|
||||
}
|
||||
|
||||
d.SetId(fmt.Sprintf("%s/%s/reviewers", d.Get("owner").(string), d.Get("repository").(string)))
|
||||
return resourceDefaultReviewersRead(d, m)
|
||||
}
|
||||
func resourceDefaultReviewersRead(d *schema.ResourceData, m interface{}) error {
|
||||
client := m.(*BitbucketClient)
|
||||
|
||||
reviewers_response, err := client.Get(fmt.Sprintf("2.0/repositories/%s/%s/default-reviewers",
|
||||
d.Get("owner").(string),
|
||||
d.Get("repository").(string),
|
||||
))
|
||||
|
||||
var reviewers PaginatedReviewers
|
||||
|
||||
decoder := json.NewDecoder(reviewers_response.Body)
|
||||
err = decoder.Decode(&reviewers)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
terraform_reviewers := make([]string, 0, len(reviewers.Values))
|
||||
|
||||
for _, reviewer := range reviewers.Values {
|
||||
terraform_reviewers = append(terraform_reviewers, reviewer.Username)
|
||||
}
|
||||
|
||||
d.Set("reviewers", terraform_reviewers)
|
||||
|
||||
return nil
|
||||
}
|
||||
func resourceDefaultReviewersDelete(d *schema.ResourceData, m interface{}) error {
|
||||
client := m.(*BitbucketClient)
|
||||
|
||||
for _, user := range d.Get("reviewers").(*schema.Set).List() {
|
||||
resp, err := client.Delete(fmt.Sprintf("2.0/repositories/%s/%s/default-reviewers/%s",
|
||||
d.Get("owner").(string),
|
||||
d.Get("repository").(string),
|
||||
user.(string),
|
||||
))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if resp.StatusCode != 204 {
|
||||
return fmt.Errorf("[%d] Could not delete %s from default reviewer",
|
||||
resp.StatusCode,
|
||||
user.(string),
|
||||
)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
package bitbucket
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
func TestAccBitbucketDefaultReviewers_basic(t *testing.T) {
|
||||
|
||||
testUser := os.Getenv("BITBUCKET_USERNAME")
|
||||
testAccBitbucketDefaultReviewersConfig := fmt.Sprintf(`
|
||||
resource "bitbucket_repository" "test_repo" {
|
||||
owner = "%s"
|
||||
name = "test-repo-default-reviewers"
|
||||
}
|
||||
|
||||
resource "bitbucket_default_reviewers" "test_reviewers" {
|
||||
owner = "%s"
|
||||
repository = "${bitbucket_repository.test_repo.name}"
|
||||
reviewers = [
|
||||
"%s",
|
||||
]
|
||||
}
|
||||
`, testUser, testUser, testUser)
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckBitbucketDefaultReviewersDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccBitbucketDefaultReviewersConfig,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckBitbucketDefaultReviewersExists("bitbucket_default_reviewers.test_reviewers"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccCheckBitbucketDefaultReviewersDestroy(s *terraform.State) error {
|
||||
_, ok := s.RootModule().Resources["bitbucket_default_reviewers.test_reviewers"]
|
||||
if !ok {
|
||||
return fmt.Errorf("Not found %s", "bitbucket_default_reviewers.test_reviewers")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func testAccCheckBitbucketDefaultReviewersExists(n string) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
rs, ok := s.RootModule().Resources[n]
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("Not found %s", n)
|
||||
}
|
||||
|
||||
if rs.Primary.ID == "" {
|
||||
return fmt.Errorf("No default reviewers ID is set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
|
@ -0,0 +1,208 @@
|
|||
package bitbucket
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
)
|
||||
|
||||
type Hook struct {
|
||||
Uuid string `json:"uuid,omitempty"`
|
||||
Url string `json:"url,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Active bool `json:"active,omitempty"`
|
||||
Events []string `json:"events,omitempty"`
|
||||
}
|
||||
|
||||
func resourceHook() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceHookCreate,
|
||||
Read: resourceHookRead,
|
||||
Update: resourceHookUpdate,
|
||||
Delete: resourceHookDelete,
|
||||
Exists: resourceHookExists,
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"owner": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
"repository": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
"active": &schema.Schema{
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
Default: true,
|
||||
},
|
||||
"url": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
"uuid": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"description": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
"events": &schema.Schema{
|
||||
Type: schema.TypeSet,
|
||||
Required: true,
|
||||
Elem: &schema.Schema{Type: schema.TypeString},
|
||||
Set: schema.HashString,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func createHook(d *schema.ResourceData) *Hook {
|
||||
|
||||
events := make([]string, 0, len(d.Get("events").(*schema.Set).List()))
|
||||
|
||||
for _, item := range d.Get("events").(*schema.Set).List() {
|
||||
events = append(events, item.(string))
|
||||
}
|
||||
|
||||
return &Hook{
|
||||
Url: d.Get("url").(string),
|
||||
Description: d.Get("description").(string),
|
||||
Active: d.Get("active").(bool),
|
||||
Events: events,
|
||||
}
|
||||
}
|
||||
|
||||
func resourceHookCreate(d *schema.ResourceData, m interface{}) error {
|
||||
client := m.(*BitbucketClient)
|
||||
hook := createHook(d)
|
||||
|
||||
var jsonbuffer []byte
|
||||
|
||||
jsonpayload := bytes.NewBuffer(jsonbuffer)
|
||||
enc := json.NewEncoder(jsonpayload)
|
||||
enc.Encode(hook)
|
||||
|
||||
hook_req, err := client.Post(fmt.Sprintf("2.0/repositories/%s/%s/hooks",
|
||||
d.Get("owner").(string),
|
||||
d.Get("repository").(string),
|
||||
), jsonpayload)
|
||||
|
||||
decoder := json.NewDecoder(hook_req.Body)
|
||||
err = decoder.Decode(&hook)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.SetId(hook.Uuid)
|
||||
d.Set("uuid", hook.Uuid)
|
||||
|
||||
return resourceHookRead(d, m)
|
||||
}
|
||||
func resourceHookRead(d *schema.ResourceData, m interface{}) error {
|
||||
client := m.(*BitbucketClient)
|
||||
hook_req, err := client.Get(fmt.Sprintf("2.0/repositories/%s/%s/hooks/%s",
|
||||
d.Get("owner").(string),
|
||||
d.Get("repository").(string),
|
||||
d.Get("uuid").(string),
|
||||
))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var hook Hook
|
||||
|
||||
decoder := json.NewDecoder(hook_req.Body)
|
||||
err = decoder.Decode(&hook)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.Set("uuid", string(hook.Uuid))
|
||||
d.Set("description", string(hook.Description))
|
||||
d.Set("active", bool(hook.Active))
|
||||
d.Set("url", string(hook.Url))
|
||||
|
||||
eventsList := make([]string, 0, len(hook.Events))
|
||||
|
||||
for _, event := range hook.Events {
|
||||
eventsList = append(eventsList, string(event))
|
||||
}
|
||||
|
||||
d.Set("events", eventsList)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceHookUpdate(d *schema.ResourceData, m interface{}) error {
|
||||
client := m.(*BitbucketClient)
|
||||
hook := createHook(d)
|
||||
|
||||
var jsonbuffer []byte
|
||||
|
||||
jsonpayload := bytes.NewBuffer(jsonbuffer)
|
||||
enc := json.NewEncoder(jsonpayload)
|
||||
enc.Encode(hook)
|
||||
|
||||
hook_req, err := client.Put(fmt.Sprintf("2.0/repositories/%s/%s/hooks/%s",
|
||||
d.Get("owner").(string),
|
||||
d.Get("repository").(string),
|
||||
d.Get("uuid").(string),
|
||||
), jsonpayload)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(hook_req.Body)
|
||||
err = decoder.Decode(&hook)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return resourceHookRead(d, m)
|
||||
}
|
||||
|
||||
func resourceHookExists(d *schema.ResourceData, m interface{}) (bool, error) {
|
||||
client := m.(*BitbucketClient)
|
||||
if _, okay := d.GetOk("uuid"); okay {
|
||||
hook_req, err := client.Get(fmt.Sprintf("2.0/repositories/%s/%s/hooks/%s",
|
||||
d.Get("owner").(string),
|
||||
d.Get("repository").(string),
|
||||
d.Get("uuid").(string),
|
||||
))
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if hook_req.StatusCode != 200 {
|
||||
d.SetId("")
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return true, nil
|
||||
} else {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func resourceHookDelete(d *schema.ResourceData, m interface{}) error {
|
||||
client := m.(*BitbucketClient)
|
||||
_, err := client.Delete(fmt.Sprintf("2.0/repositories/%s/%s/hooks/%s",
|
||||
d.Get("owner").(string),
|
||||
d.Get("repository").(string),
|
||||
d.Get("uuid").(string),
|
||||
))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
package bitbucket
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
func TestAccBitbucketHook_basic(t *testing.T) {
|
||||
var hook Hook
|
||||
|
||||
testUser := os.Getenv("BITBUCKET_USERNAME")
|
||||
testAccBitbucketHookConfig := fmt.Sprintf(`
|
||||
resource "bitbucket_repository" "test_repo" {
|
||||
owner = "%s"
|
||||
name = "test-repo"
|
||||
}
|
||||
resource "bitbucket_hook" "test_repo_hook" {
|
||||
owner = "%s"
|
||||
repository = "${bitbucket_repository.test_repo.name}"
|
||||
description = "Test hook for terraform"
|
||||
url = "https://httpbin.org"
|
||||
events = [
|
||||
"repo:push",
|
||||
]
|
||||
}
|
||||
`, testUser, testUser)
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckBitbucketHookDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccBitbucketHookConfig,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckBitbucketHookExists("bitbucket_hook.test_repo_hook", &hook),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccCheckBitbucketHookDestroy(s *terraform.State) error {
|
||||
client := testAccProvider.Meta().(*BitbucketClient)
|
||||
rs, ok := s.RootModule().Resources["bitbucket_hook.test_repo_hook"]
|
||||
if !ok {
|
||||
return fmt.Errorf("Not found %s", "bitbucket_hook.test_repo_hook")
|
||||
}
|
||||
|
||||
response, err := client.Get(fmt.Sprintf("2.0/repositories/%s/%s/hooks/%s", rs.Primary.Attributes["owner"], rs.Primary.Attributes["repository"], rs.Primary.Attributes["uuid"]))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if response.StatusCode != 404 {
|
||||
return fmt.Errorf("Hook still exists")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func testAccCheckBitbucketHookExists(n string, hook *Hook) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
rs, ok := s.RootModule().Resources[n]
|
||||
if !ok {
|
||||
return fmt.Errorf("Not found %s", n)
|
||||
}
|
||||
if rs.Primary.ID == "" {
|
||||
return fmt.Errorf("No Hook ID is set")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
|
@ -0,0 +1,247 @@
|
|||
package bitbucket
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"log"
|
||||
)
|
||||
|
||||
type CloneUrl struct {
|
||||
Href string `json:"href,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
type Repository struct {
|
||||
SCM string `json:"scm,omitempty"`
|
||||
HasWiki bool `json:"has_wiki,omitempty"`
|
||||
HasIssues bool `json:"has_issues,omitempty"`
|
||||
Website string `json:"website,omitempty"`
|
||||
IsPrivate bool `json:"is_private,omitempty"`
|
||||
ForkPolicy string `json:"fork_policy,omitempty"`
|
||||
Language string `json:"language,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
UUID string `json:"uuid,omitempty"`
|
||||
Project struct {
|
||||
Key string `json:"key,omitempty"`
|
||||
} `json:"project,omitempty"`
|
||||
Links struct {
|
||||
Clone []CloneUrl `json:"clone,omitempty"`
|
||||
} `json:"links,omitempty"`
|
||||
}
|
||||
|
||||
func resourceRepository() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceRepositoryCreate,
|
||||
Update: resourceRepositoryUpdate,
|
||||
Read: resourceRepositoryRead,
|
||||
Delete: resourceRepositoryDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"scm": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Default: "git",
|
||||
},
|
||||
"has_wiki": &schema.Schema{
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
Default: false,
|
||||
},
|
||||
"has_issues": &schema.Schema{
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
Default: false,
|
||||
},
|
||||
"website": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
"clone_ssh": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"clone_https": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
"project_key": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
"is_private": &schema.Schema{
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
Default: true,
|
||||
},
|
||||
"fork_policy": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Default: "allow_forks",
|
||||
},
|
||||
"language": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
"description": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
"owner": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
"name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newRepositoryFromResource(d *schema.ResourceData) *Repository {
|
||||
repo := &Repository{
|
||||
Name: d.Get("name").(string),
|
||||
Language: d.Get("language").(string),
|
||||
IsPrivate: d.Get("is_private").(bool),
|
||||
Description: d.Get("description").(string),
|
||||
ForkPolicy: d.Get("fork_policy").(string),
|
||||
HasWiki: d.Get("has_wiki").(bool),
|
||||
HasIssues: d.Get("has_issues").(bool),
|
||||
SCM: d.Get("scm").(string),
|
||||
Website: d.Get("website").(string),
|
||||
}
|
||||
|
||||
repo.Project.Key = d.Get("project_key").(string)
|
||||
return repo
|
||||
}
|
||||
|
||||
func resourceRepositoryUpdate(d *schema.ResourceData, m interface{}) error {
|
||||
client := m.(*BitbucketClient)
|
||||
repository := newRepositoryFromResource(d)
|
||||
|
||||
var jsonbuffer []byte
|
||||
|
||||
jsonpayload := bytes.NewBuffer(jsonbuffer)
|
||||
enc := json.NewEncoder(jsonpayload)
|
||||
enc.Encode(repository)
|
||||
|
||||
repository_response, err := client.Put(fmt.Sprintf("2.0/repositories/%s/%s",
|
||||
d.Get("owner").(string),
|
||||
d.Get("name").(string),
|
||||
), jsonpayload)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if repository_response.StatusCode == 200 {
|
||||
decoder := json.NewDecoder(repository_response.Body)
|
||||
err = decoder.Decode(&repository)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("Failed to put: %d", repository_response.StatusCode)
|
||||
}
|
||||
|
||||
return resourceRepositoryRead(d, m)
|
||||
}
|
||||
|
||||
func resourceRepositoryCreate(d *schema.ResourceData, m interface{}) error {
|
||||
client := m.(*BitbucketClient)
|
||||
repo := newRepositoryFromResource(d)
|
||||
|
||||
var jsonbuffer []byte
|
||||
|
||||
jsonpayload := bytes.NewBuffer(jsonbuffer)
|
||||
enc := json.NewEncoder(jsonpayload)
|
||||
enc.Encode(repo)
|
||||
|
||||
log.Printf("Sending %s \n", jsonpayload)
|
||||
|
||||
repo_req, err := client.Post(fmt.Sprintf("2.0/repositories/%s/%s",
|
||||
d.Get("owner").(string),
|
||||
d.Get("name").(string),
|
||||
), jsonpayload)
|
||||
|
||||
decoder := json.NewDecoder(repo_req.Body)
|
||||
err = decoder.Decode(&repo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("Received %s \n", repo_req.Body)
|
||||
|
||||
if repo_req.StatusCode != 200 {
|
||||
return fmt.Errorf("Failed to create repository got status code %d", repo_req.StatusCode)
|
||||
}
|
||||
|
||||
d.SetId(string(fmt.Sprintf("%s/%s", d.Get("owner").(string), d.Get("name").(string))))
|
||||
|
||||
return resourceRepositoryRead(d, m)
|
||||
}
|
||||
func resourceRepositoryRead(d *schema.ResourceData, m interface{}) error {
|
||||
|
||||
client := m.(*BitbucketClient)
|
||||
repo_req, err := client.Get(fmt.Sprintf("2.0/repositories/%s/%s",
|
||||
d.Get("owner").(string),
|
||||
d.Get("name").(string),
|
||||
))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var repo Repository
|
||||
|
||||
decoder := json.NewDecoder(repo_req.Body)
|
||||
err = decoder.Decode(&repo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.Set("scm", string(repo.SCM))
|
||||
d.Set("is_private", bool(repo.IsPrivate))
|
||||
d.Set("has_wiki", bool(repo.HasWiki))
|
||||
d.Set("has_issues", bool(repo.HasIssues))
|
||||
d.Set("name", string(repo.Name))
|
||||
d.Set("language", string(repo.Language))
|
||||
d.Set("fork_policy", string(repo.ForkPolicy))
|
||||
d.Set("website", string(repo.Website))
|
||||
d.Set("description", string(repo.Description))
|
||||
d.Set("project_key", string(repo.Project.Key))
|
||||
|
||||
for _, clone_url := range repo.Links.Clone {
|
||||
if clone_url.Name == "https" {
|
||||
d.Set("clone_https", string(clone_url.Href))
|
||||
} else {
|
||||
d.Set("clone_ssh", string(clone_url.Href))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceRepositoryDelete(d *schema.ResourceData, m interface{}) error {
|
||||
client := m.(*BitbucketClient)
|
||||
delete_response, err := client.Delete(fmt.Sprintf("2.0/repositories/%s/%s",
|
||||
d.Get("owner").(string),
|
||||
d.Get("name").(string),
|
||||
))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if delete_response.StatusCode != 204 {
|
||||
return fmt.Errorf("Failed to delete the repository got status code %d", delete_response.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
package bitbucket
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
func TestAccBitbucketRepository_basic(t *testing.T) {
|
||||
var repo Repository
|
||||
|
||||
testUser := os.Getenv("BITBUCKET_USERNAME")
|
||||
testAccBitbucketRepositoryConfig := fmt.Sprintf(`
|
||||
resource "bitbucket_repository" "test_repo" {
|
||||
owner = "%s"
|
||||
name = "%s"
|
||||
}
|
||||
`, testUser, testRepo)
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckBitbucketRepositoryDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccBitbucketRepositoryConfig,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckBitbucketRepositoryExists("bitbucket_repository.test_repo", &repo),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccCheckBitbucketRepositoryDestroy(s *terraform.State) error {
|
||||
client := testAccProvider.Meta().(*BitbucketClient)
|
||||
rs, ok := s.RootModule().Resources["bitbucket_repository.test_repo"]
|
||||
if !ok {
|
||||
return fmt.Errorf("Not found %s", "bitbucket_repository.test_repo")
|
||||
}
|
||||
|
||||
response, err := client.Get(fmt.Sprintf("2.0/repositories/%s/%s", rs.Primary.Attributes["owner"], rs.Primary.Attributes["name"]))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if response.StatusCode != 404 {
|
||||
return fmt.Errorf("Repository still exists")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func testAccCheckBitbucketRepositoryExists(n string, repository *Repository) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
rs, ok := s.RootModule().Resources[n]
|
||||
if !ok {
|
||||
return fmt.Errorf("Not found %s", n)
|
||||
}
|
||||
if rs.Primary.ID == "" {
|
||||
return fmt.Errorf("No repository ID is set")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
|
@ -11,6 +11,7 @@ import (
|
|||
awsprovider "github.com/hashicorp/terraform/builtin/providers/aws"
|
||||
azureprovider "github.com/hashicorp/terraform/builtin/providers/azure"
|
||||
azurermprovider "github.com/hashicorp/terraform/builtin/providers/azurerm"
|
||||
bitbucketprovider "github.com/hashicorp/terraform/builtin/providers/bitbucket"
|
||||
chefprovider "github.com/hashicorp/terraform/builtin/providers/chef"
|
||||
clcprovider "github.com/hashicorp/terraform/builtin/providers/clc"
|
||||
cloudflareprovider "github.com/hashicorp/terraform/builtin/providers/cloudflare"
|
||||
|
@ -67,6 +68,7 @@ var InternalProviders = map[string]plugin.ProviderFunc{
|
|||
"aws": awsprovider.Provider,
|
||||
"azure": azureprovider.Provider,
|
||||
"azurerm": azurermprovider.Provider,
|
||||
"bitbucket": bitbucketprovider.Provider,
|
||||
"chef": chefprovider.Provider,
|
||||
"clc": clcprovider.Provider,
|
||||
"cloudflare": cloudflareprovider.Provider,
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
---
|
||||
layout: "bitbucket"
|
||||
page_title: "Provider: Bitbucket"
|
||||
sidebar_current: "docs-bitbucket-index"
|
||||
description: |-
|
||||
The Bitbucket provider to interact with repositories, projects, etc..
|
||||
---
|
||||
|
||||
# Bitbucket Provider
|
||||
|
||||
The Bitbucket provider allows you to manage resources including repositories,
|
||||
webhooks, and default reviewers.
|
||||
|
||||
Use the navigation to the left to read about the available resources.
|
||||
|
||||
## Example Usage
|
||||
|
||||
```
|
||||
# Configure the Bitbucket Provider
|
||||
provider "bitbucket" {
|
||||
username = "GobBluthe"
|
||||
password = "idoillusions" # you can also use app passwords
|
||||
}
|
||||
|
||||
resource "bitbucket_repsitory" "illusions" {
|
||||
owner = "theleagueofmagicians"
|
||||
name = "illussions"
|
||||
scm = "hg"
|
||||
is_private = true
|
||||
}
|
||||
```
|
||||
|
||||
## Argument Reference
|
||||
|
||||
The following arguments are supported in the `provider` block:
|
||||
|
||||
* `username` - (Required) Your username used to connect to bitbucket. You can
|
||||
also set this via the environment variable. `BITBUCKET_USERNAME`
|
||||
|
||||
* `password` - (Required) Your password used to connect to bitbucket. You can
|
||||
also set this via the environment variable. `BITBUCKET_PASSWORD`
|
|
@ -0,0 +1,35 @@
|
|||
---
|
||||
layout: "bitbucket"
|
||||
page_title: "Bitbucket: bitbucket_default_reviewers"
|
||||
sidebar_current: "docs-bitbucket-resource-default-reviewers"
|
||||
description: |-
|
||||
Provides support for setting up default reviews for bitbucket.
|
||||
---
|
||||
|
||||
# bitbucket\_default_reviewers
|
||||
|
||||
Provides support for setting up default reviewers for your repository.
|
||||
|
||||
## Example Usage
|
||||
|
||||
```
|
||||
# Manage your respository
|
||||
resource "bitbucket_default_reviewers" "infrastructure" {
|
||||
owner = "myteam"
|
||||
repository = "terraform-code"
|
||||
reviewers = [
|
||||
"gob",
|
||||
"michael",
|
||||
"michalejr"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## Argument Reference
|
||||
|
||||
The following arguments are supported:
|
||||
|
||||
* `owner` - (Required) The owner of this repository. Can be you or any team you
|
||||
have write access to.
|
||||
* `repository` - (Required) The name of the repository.
|
||||
* `reviewers` - (Required) A list of reviewers to use.
|
|
@ -0,0 +1,39 @@
|
|||
---
|
||||
layout: "bitbucket"
|
||||
page_title: "Bitbucket: bitbucket_hoook"
|
||||
sidebar_current: "docs-bitbucket-resource-hook"
|
||||
description: |-
|
||||
Provides a Bitbucket Webhook
|
||||
---
|
||||
|
||||
# bitbucket\_hook
|
||||
|
||||
Provides a Bitbucket hook resource.
|
||||
|
||||
This allows you to manage your webhooks on a repository.
|
||||
|
||||
## Example Usage
|
||||
|
||||
```
|
||||
# Manage your respositories hooks
|
||||
resource "bitbucket_hook" "deploy_on_push" {
|
||||
owner = "myteam"
|
||||
repository = "terraform-code"
|
||||
url = "https://mywebhookservice.mycompany.com/deploy-on-push"
|
||||
description = "Deploy the code via my webhook"
|
||||
events = [
|
||||
"repo:push"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## Argument Reference
|
||||
|
||||
The following arguments are supported:
|
||||
|
||||
* `owner` - (Required) The owner of this repository. Can be you or any team you
|
||||
have write access to.
|
||||
* `repository` - (Required) The name of the repository.
|
||||
* `url` - (Required) Where to POST to.
|
||||
* `description` - (Required) The name / description to show in the UI.
|
||||
* `events` - (Required) The event you want to react on.
|
|
@ -0,0 +1,48 @@
|
|||
---
|
||||
layout: "bitbucket"
|
||||
page_title: "Bitbucket: bitbucket_repository"
|
||||
sidebar_current: "docs-bitbucket-resource-repository"
|
||||
description: |-
|
||||
Provides a Bitbucket Repository
|
||||
---
|
||||
|
||||
# bitbucket\_repository
|
||||
|
||||
Provides a Bitbucket repository resource.
|
||||
|
||||
This resource allows you manage your repositories such as scm type, if it is
|
||||
private, how to fork the repository and other options.
|
||||
|
||||
## Example Usage
|
||||
|
||||
```
|
||||
# Manage your respository
|
||||
resource "bitbucket_repository" "infrastructure" {
|
||||
owner = "myteam"
|
||||
name = "terraform-code"
|
||||
}
|
||||
```
|
||||
|
||||
## Argument Reference
|
||||
|
||||
The following arguments are supported:
|
||||
|
||||
* `owner` - (Required) The owner of this repository. Can be you or any team you
|
||||
have write access to.
|
||||
* `name` - (Optional) The name of the repository.
|
||||
* `scm` - (Optional) What SCM you want to use. Valid options are hg or git.
|
||||
Defaults to git.
|
||||
* `is_private` - (Optional) If this should be private or not. Defaults to `true`.
|
||||
* `website` - (Optional) URL of website associated with this repository.
|
||||
* `has_issues` - (Optional) If this should have issues turned on or not.
|
||||
* `has_wiki` - (Optional) If this should have wiki turned on or not.
|
||||
* `project_key` - (Optional) If you want to have this repo associated with a
|
||||
project.
|
||||
* `fork_policy` - (Optional) What the fork policy should be. Defaults to
|
||||
allow_forks.
|
||||
* `description` - (Optional) What the description of the repo is.
|
||||
|
||||
## Computed Arguments
|
||||
|
||||
The following arguments are computed. You can access both `clone_ssh` and
|
||||
`clone_https` for getting a clone URL.
|
Loading…
Reference in New Issue