Skip to content

Commit

Permalink
Merge pull request #3 from COzero/feature/pipeline-create
Browse files Browse the repository at this point in the history
r/pipeline
  • Loading branch information
mubeta06 authored Apr 18, 2018
2 parents 1f9c704 + f5afb72 commit 8df7470
Show file tree
Hide file tree
Showing 14 changed files with 783 additions and 49 deletions.
32 changes: 30 additions & 2 deletions Gopkg.lock

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

6 changes: 6 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
# unused-packages = true


[[constraint]]
# use the cozero fork of go-buildkite client while we're developing this provider
name = "github.com/buildkite/go-buildkite"
branch = "master"
source = "https://github.com/cozero/go-buildkite.git"

[[constraint]]
name = "github.com/hashicorp/terraform"
version = "0.11.7"
Expand Down
68 changes: 31 additions & 37 deletions buildkite/data_source_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package buildkite

import (
"fmt"
"time"

"github.com/buildkite/go-buildkite/buildkite"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -16,37 +17,39 @@ func dataSourcePipeline() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"url": &schema.Schema{
"slug": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"web_url": &schema.Schema{
"provider_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"name": &schema.Schema{
"repository": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"slug": &schema.Schema{
"url": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"builds_url": &schema.Schema{
"web_url": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"badge_url": &schema.Schema{
"builds_url": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"created_at": &schema.Schema{
"badge_url": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"repository": &schema.Schema{
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
Expand All @@ -55,37 +58,28 @@ func dataSourcePipeline() *schema.Resource {
}

func dataSourcePipelineRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*buildkite.Client)
name := d.Get("name").(string)
var pipeline buildkite.Pipeline
client := meta.(*buildkite.Client)

// The API is paginated; loop through until we find what we're looking for
for i, done := 0, false; !done; i++ {
input := &buildkite.PipelineListOptions{
ListOptions: buildkite.ListOptions{
Page: i,
},
}
pipelines, _, err := conn.Pipelines.List(d.Get("organization").(string), input)
if err != nil {
return err
}
p, _, err := client.Pipelines.Get(d.Get("organization").(string), d.Get("slug").(string))
if err != nil {
return fmt.Errorf("Error reading pipeline: %s", err)
}

if len(pipelines) == 0 {
return fmt.Errorf("No pipeline %s found", name)
}
d.SetId(StringValue(p.ID))

for _, p := range pipelines {
if *p.Name == name {
pipeline = p
done = true
break
}
}
}
d.Set("badge_url", StringValue(p.BadgeURL))
d.Set("builds_url", StringValue(p.BuildsURL))
d.Set("created_at", p.CreatedAt.Format(time.RFC3339))
d.Set("name", StringValue(p.Name))
d.Set("provider_id", StringValue(p.Provider.ID))
d.Set("repository", StringValue(p.Repository))
d.Set("slug", StringValue(p.Slug))
d.Set("url", StringValue(p.URL))
d.Set("web_url", StringValue(p.WebURL))
d.Set("webhook_url", StringValue(p.Provider.WebhookURL))

d.SetId(*pipeline.ID)
d.Set("web_url", pipeline.WebURL)
steps := buildStepsFromAPI(p.Steps)
d.Set("steps", steps)

return nil
}
42 changes: 32 additions & 10 deletions buildkite/data_source_pipelines_test.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,53 @@
package buildkite

import (
"fmt"
"testing"

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

// We need a pipeline resource so that this can be tested properly
const testAccDataSourcePipelineRead = `
data buildkite_pipeline "test" {
name = "Stu's Great Showcase Pipeline"
organization = "cozero"
}
`
func TestAccDataSourcePipeline(t *testing.T) {
rStr := acctest.RandString(6)

func TestAccDataSourcePipelineRead(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDataSourcePipelineRead,
Config: testAccDataSourcePipeline(rStr),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.buildkite_pipeline.test", "web_url"),
resource.TestCheckResourceAttr(
"data.buildkite_pipeline.test",
"name",
fmt.Sprintf("Acceptance test :terraform: %s", rStr),
),
),
},
},
})
return
}

func testAccDataSourcePipeline(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"
steps = [{
type = "script"
name = "Hi!"
command = "echo \"Hello world\""
}]
}
data buildkite_pipeline "test" {
organization = "cozero"
slug = "${buildkite_pipeline.test.slug}"
}
`, rStr)
}
14 changes: 14 additions & 0 deletions buildkite/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package buildkite

// String returns a pointer to the string value passed in
func String(v string) *string {
return &v
}

// StringValue deferences a pointer to a string or returns "" if the pointer is nil
func StringValue(v *string) string {
if v != nil {
return *v
}
return ""
}
4 changes: 4 additions & 0 deletions buildkite/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ func Provider() terraform.ResourceProvider {
"buildkite_pipeline": dataSourcePipeline(),
},

ResourcesMap: map[string]*schema.Resource{
"buildkite_pipeline": resourcePipeline(),
},

Schema: map[string]*schema.Schema{
"api_token": &schema.Schema{
Type: schema.TypeString,
Expand Down
Loading

0 comments on commit 8df7470

Please sign in to comment.