mirror of
https://github.com/ysoftdevs/terraform-provider-bitbucketserver.git
synced 2026-03-27 20:01:07 +01:00
Added bitbucketserver_project resource and associated tests
This commit is contained in:
@@ -29,6 +29,7 @@ func Provider() terraform.ResourceProvider {
|
||||
},
|
||||
ConfigureFunc: providerConfigure,
|
||||
ResourcesMap: map[string]*schema.Resource{
|
||||
"bitbucketserver_project": resourceProject(),
|
||||
"bitbucketserver_repository": resourceRepository(),
|
||||
},
|
||||
}
|
||||
|
||||
178
bitbucket/resource_project.go
Normal file
178
bitbucket/resource_project.go
Normal file
@@ -0,0 +1,178 @@
|
||||
package bitbucket
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
)
|
||||
|
||||
type Project struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Key string `json:"key,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Public bool `json:"public,omitempty"`
|
||||
Avatar string `json:"avatar,omitempty"`
|
||||
}
|
||||
|
||||
func resourceProject() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceProjectCreate,
|
||||
Update: resourceProjectUpdate,
|
||||
Read: resourceProjectRead,
|
||||
Exists: resourceProjectExists,
|
||||
Delete: resourceProjectDelete,
|
||||
Importer: &schema.ResourceImporter{
|
||||
State: schema.ImportStatePassthrough,
|
||||
},
|
||||
|
||||
Schema: map[string]*schema.Schema{
|
||||
"name": {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
"key": {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
"description": {
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
"public": {
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
Default: false,
|
||||
},
|
||||
"avatar": {
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newProjectFromResource(d *schema.ResourceData) *Project {
|
||||
project := &Project{
|
||||
Name: d.Get("name").(string),
|
||||
Key: d.Get("key").(string),
|
||||
Description: d.Get("description").(string),
|
||||
Public: d.Get("public").(bool),
|
||||
Avatar: d.Get("avatar").(string),
|
||||
}
|
||||
|
||||
return project
|
||||
}
|
||||
|
||||
func resourceProjectUpdate(d *schema.ResourceData, m interface{}) error {
|
||||
client := m.(*BitbucketClient)
|
||||
project := newProjectFromResource(d)
|
||||
|
||||
bytedata, err := json.Marshal(project)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = client.Put(fmt.Sprintf("/rest/api/1.0/projects/%s",
|
||||
project.Key,
|
||||
), bytes.NewBuffer(bytedata))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return resourceProjectRead(d, m)
|
||||
}
|
||||
|
||||
func resourceProjectCreate(d *schema.ResourceData, m interface{}) error {
|
||||
client := m.(*BitbucketClient)
|
||||
project := newProjectFromResource(d)
|
||||
|
||||
bytedata, err := json.Marshal(project)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = client.Post("/rest/api/1.0/projects", bytes.NewBuffer(bytedata))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.SetId(project.Key)
|
||||
|
||||
return resourceProjectRead(d, m)
|
||||
}
|
||||
|
||||
func resourceProjectRead(d *schema.ResourceData, m interface{}) error {
|
||||
id := d.Id()
|
||||
if id != "" {
|
||||
d.Set("key", id)
|
||||
}
|
||||
|
||||
project := d.Get("key").(string)
|
||||
|
||||
client := m.(*BitbucketClient)
|
||||
project_req, err := client.Get(fmt.Sprintf("/rest/api/1.0/projects/%s",
|
||||
project,
|
||||
))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if project_req.StatusCode == 200 {
|
||||
|
||||
var project Project
|
||||
|
||||
body, readerr := ioutil.ReadAll(project_req.Body)
|
||||
if readerr != nil {
|
||||
return readerr
|
||||
}
|
||||
|
||||
decodeerr := json.Unmarshal(body, &project)
|
||||
if decodeerr != nil {
|
||||
return decodeerr
|
||||
}
|
||||
|
||||
d.Set("name", project.Name)
|
||||
d.Set("key", project.Key)
|
||||
d.Set("description", project.Description)
|
||||
d.Set("public", project.Public)
|
||||
d.Set("avatar", project.Avatar)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceProjectExists(d *schema.ResourceData, m interface{}) (bool, error) {
|
||||
client := m.(*BitbucketClient)
|
||||
project := d.Get("key").(string)
|
||||
repo_req, err := client.Get(fmt.Sprintf("/rest/api/1.0/projects/%s",
|
||||
project,
|
||||
))
|
||||
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to get project %s from bitbucket: %+v", project, err)
|
||||
}
|
||||
|
||||
if repo_req.StatusCode == 200 {
|
||||
return true, nil
|
||||
} else {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
func resourceProjectDelete(d *schema.ResourceData, m interface{}) error {
|
||||
project := d.Get("key").(string)
|
||||
client := m.(*BitbucketClient)
|
||||
_, err := client.Delete(fmt.Sprintf("/rest/api/1.0/projects/%s",
|
||||
project,
|
||||
))
|
||||
|
||||
return err
|
||||
}
|
||||
65
bitbucket/resource_project_test.go
Normal file
65
bitbucket/resource_project_test.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package bitbucket
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
func TestAccBitbucketProject(t *testing.T) {
|
||||
var repo Repository
|
||||
|
||||
testAccBitbucketProjectConfig := fmt.Sprintf(`
|
||||
resource "bitbucketserver_project" "test" {
|
||||
key = "TEST%v"
|
||||
name = "test-repo-for-repository-test"
|
||||
}
|
||||
`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckBitbucketProjectDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccBitbucketProjectConfig,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckBitbucketProjectExists("bitbucketserver_project.test", &repo),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccCheckBitbucketProjectDestroy(s *terraform.State) error {
|
||||
client := testAccProvider.Meta().(*BitbucketClient)
|
||||
rs, ok := s.RootModule().Resources["bitbucketserver_project.test"]
|
||||
if !ok {
|
||||
return fmt.Errorf("not found %s", "bitbucketserver_project.test")
|
||||
}
|
||||
|
||||
response, _ := client.Get(fmt.Sprintf("/rest/api/1.0/projects/%s", rs.Primary.Attributes["key"]))
|
||||
|
||||
if response.StatusCode != 404 {
|
||||
return fmt.Errorf("project still exists")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func testAccCheckBitbucketProjectExists(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 project ID is set")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,9 @@ package bitbucket
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
@@ -11,12 +13,17 @@ import (
|
||||
func TestAccBitbucketRepository_basic(t *testing.T) {
|
||||
var repo Repository
|
||||
|
||||
testAccBitbucketRepositoryConfig := `
|
||||
resource "bitbucketserver_repository" "test_repo" {
|
||||
project = "TEST"
|
||||
testAccBitbucketRepositoryConfig := fmt.Sprintf(`
|
||||
resource "bitbucketserver_project" "test" {
|
||||
key = "TEST%v"
|
||||
name = "test-repo-for-repository-test"
|
||||
}
|
||||
`
|
||||
|
||||
resource "bitbucketserver_repository" "test_repo" {
|
||||
project = bitbucketserver_project.test.key
|
||||
name = "test-repo-for-repository-test"
|
||||
}
|
||||
`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
@@ -36,13 +43,18 @@ func TestAccBitbucketRepository_basic(t *testing.T) {
|
||||
func TestAccBitbucketRepository_namewithspaces(t *testing.T) {
|
||||
var repo Repository
|
||||
|
||||
testAccBitbucketRepositoryConfig := `
|
||||
testAccBitbucketRepositoryConfig := fmt.Sprintf(`
|
||||
resource "bitbucketserver_project" "test" {
|
||||
key = "TEST%v"
|
||||
name = "test-repo-for-repository-test"
|
||||
}
|
||||
|
||||
resource "bitbucketserver_repository" "test_repo" {
|
||||
project = "TEST"
|
||||
project = bitbucketserver_project.test.key
|
||||
name = "Test Repo For Repository Test"
|
||||
slug = "test-repo-for-repository-test"
|
||||
}
|
||||
`
|
||||
`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
|
||||
Reference in New Issue
Block a user