Skip to content

Commit

Permalink
add the read and update verbs
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuart Auld committed Apr 17, 2018
1 parent d9cdf9d commit 0208d39
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 21 deletions.
13 changes: 10 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 59 additions & 2 deletions buildkite/resource_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package buildkite

import (
"fmt"
"log"
"time"

"github.com/buildkite/go-buildkite/buildkite"
Expand Down Expand Up @@ -178,15 +179,71 @@ func resourcePipelineCreate(d *schema.ResourceData, meta interface{}) error {

updatePipelineFromAPI(d, pipe)

return nil
return resourcePipelineRead(d, meta)
}

func resourcePipelineRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*buildkite.Client)

pipe, resp, err := client.Pipelines.Get(d.Get("organization").(string), d.Get("slug").(string))
if err != nil {
// If pipeline not found, delete if from the state file
if resp.StatusCode == 404 {
log.Printf("[WARN] Pipeline (%s) not found, removing from state", d.Get("slug"))
d.SetId("")
return nil
}

return fmt.Errorf("Error reading pipeline: %s", err)
}

updatePipelineFromAPI(d, pipe)

return nil
}

func resourcePipelineUpdate(d *schema.ResourceData, meta interface{}) error {
return nil
client := meta.(*buildkite.Client)

pipe := &buildkite.Pipeline{
Name: String(d.Get("name").(string)),
Repository: String(d.Get("repository").(string)),
}

steps := d.Get("step").(*schema.Set).List()
pipe.Steps = make([]*buildkite.Step, len(steps))

for i, s := range steps {
step := s.(map[string]interface{})
pipe.Steps[i] = &buildkite.Step{
Type: String(step["type"].(string)),
Name: String(step["name"].(string)),
Command: String(step["command"].(string)),
ArtifactPaths: String(step["artifact_paths"].(string)),
BranchConfiguration: String(step["branch_configuration"].(string)),
Env: map[string]string{},
TimeoutInMinutes: step["timeout_in_minutes"].(int),
// Not yet supported by API client
// Concurrency: step["concurrency"].(int),
// Parallelism: step["parallelism"].(int),
}

for k, v := range step["env"].(map[string]interface{}) {
pipe.Steps[i].Env[k] = v.(string)
}

agentQueryRules := make([]string, len(step["agent_query_rules"].([]interface{})))

for j, v := range step["agent_query_rules"].([]interface{}) {
agentQueryRules[j] = v.(string)
}

pipe.Steps[i].AgentQueryRules = agentQueryRules
}

client.Pipelines.Update(d.Get("organization").(string), pipe)

return resourcePipelineRead(d, meta)
}

func resourcePipelineDelete(d *schema.ResourceData, meta interface{}) error {
Expand Down
48 changes: 32 additions & 16 deletions buildkite/resource_pipeline_test.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
package buildkite

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

const testAccResourcePipelineCreate = `
resource buildkite_pipeline "test" {
name = "Acceptance test :terraform: Pipeline"
organization = "cozero"
description = "Generated via acceptance tests - please delete if left dangling"
repository = "[email protected]:COzero/terraform-provider-buildkite.git"
step {
type = "script"
name = "Hi!"
command = "echo \"Hello world\""
}
}
`

func TestAccResourcePipelineCreate(t *testing.T) {
rStr := acctest.RandString(6)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccResourcePipelineCreate,
Config: testAccResourcePipelineCreate(rStr),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("buildkite_pipeline.test", "web_url"),
resource.TestCheckResourceAttr(
"buildkite_pipeline.test",
"name",
fmt.Sprintf("Acceptance test :terraform: %s", rStr),
),
resource.TestCheckResourceAttr(
"buildkite_pipeline.test",
"slug",
fmt.Sprintf("acceptance-test-terraform-%s", rStr),
),
),
},
},
})
return
}

func testAccResourcePipelineCreate(rStr string) string {
return fmt.Sprintf(`
resource buildkite_pipeline "test" {
name = "Acceptance test :terraform: %s"
organization = "cozero"
description = "Generated via acceptance tests - please delete if left dangling"
repository = "[email protected]:COzero/terraform-provider-buildkite.git"
step {
type = "script"
name = "Hi!"
command = "echo \"Hello world\""
}
}
`, rStr)
}

0 comments on commit 0208d39

Please sign in to comment.