Skip to content

Instantly share code, notes, and snippets.

@yob
Created April 8, 2021 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yob/92a29820a01ac4bd38f47f05d6036809 to your computer and use it in GitHub Desktop.
Save yob/92a29820a01ac4bd38f47f05d6036809 to your computer and use it in GitHub Desktop.
totally untested hack at adding archiving to thebuildkite TF provider
diff --git a/buildkite/resource_pipeline.go b/buildkite/resource_pipeline.go
index 5bc6208..aeb63b5 100644
--- a/buildkite/resource_pipeline.go
+++ b/buildkite/resource_pipeline.go
@@ -62,6 +62,11 @@ func resourcePipeline() *schema.Resource {
},
Schema: map[string]*schema.Schema{
+ "archived": {
+ Computed: true,
+ Optional: true,
+ Type: schema.TypeBool,
+ },
"cancel_intermediate_builds": {
Computed: true,
Optional: true,
@@ -300,6 +305,7 @@ func CreatePipeline(ctx context.Context, d *schema.ResourceData, m interface{})
log.Printf("Successfully created pipeline with id '%s'.", mutation.PipelineCreate.Pipeline.ID)
updatePipelineResource(d, &mutation.PipelineCreate.Pipeline)
+ archiveOrUnarchivePipeline(d, &mutation.PipelineUpdate.Pipeline)
pipelineExtraInfo, err := updatePipelineExtraInfo(d, client)
if err != nil {
@@ -376,6 +382,7 @@ func UpdatePipeline(ctx context.Context, d *schema.ResourceData, m interface{})
}
updatePipelineResource(d, &mutation.PipelineUpdate.Pipeline)
+ archiveOrUnarchivePipeline(d, &mutation.PipelineUpdate.Pipeline)
pipelineExtraInfo, err := updatePipelineExtraInfo(d, client)
if err != nil {
@@ -468,6 +475,43 @@ func updatePipelineExtraInfo(d *schema.ResourceData, client *Client) (PipelineEx
return pipelineExtraInfo, nil
}
+func archiveOrUnarchivePipeline(d *schema.ResourceData, client *Client) (error) {
+ // if schema says the pipeline should be archived but it's not {
+ var mutation struct {
+ PipelineArchive struct {
+ Pipeline struct {
+ ID graphql.ID
+ }
+ } `graphql:"pipelineArchive(input: {id: $id})"`
+ }
+ params := map[string]interface{}{
+ "id": graphql.ID(pipelineID),
+ }
+ err = client.graphql.Mutate(context.Background(), &mutation, params)
+ if err != nil {
+ log.Printf("Unable to create team pipeline %s", teamPipeline.Team.Slug)
+ return err
+ }
+ // } else if schema says the pipeline should be unarchived but it's not {
+ var mutation struct {
+ PipelineUnarchive struct {
+ Pipeline struct {
+ ID graphql.ID
+ }
+ } `graphql:"pipelineUnarchive(input: {id: $id})"`
+ }
+ params := map[string]interface{}{
+ "id": graphql.ID(pipelineID),
+ }
+ err = client.graphql.Mutate(context.Background(), &mutation, params)
+ if err != nil {
+ log.Printf("Unable to create team pipeline %s", teamPipeline.Team.Slug)
+ return err
+ }
+ // }
+ return nil
+}
+
func getTeamPipelinesFromSchema(d *schema.ResourceData) []TeamPipelineNode {
teamsInput := d.Get("team").(*schema.Set).List()
teamPipelineNodes := make([]TeamPipelineNode, len(teamsInput))
diff --git a/buildkite/resource_pipeline_test.go b/buildkite/resource_pipeline_test.go
index 2657617..20cf6c7 100644
--- a/buildkite/resource_pipeline_test.go
+++ b/buildkite/resource_pipeline_test.go
@@ -137,6 +137,50 @@ func TestAccPipeline_update(t *testing.T) {
})
}
+// Confirm that we can create a new pipeline, archive it, then unarchive it
+func TestAccPipeline_archiving(t *testing.T) {
+ var resourcePipeline PipelineNode
+
+ resource.Test(t, resource.TestCase{
+ PreCheck: func() { testAccPreCheck(t) },
+ Providers: testAccProviders,
+ CheckDestroy: testAccCheckPipelineResourceDestroy,
+ Steps: []resource.TestStep{
+ {
+ Config: testAccPipelineConfigBasic("foo"),
+ Check: resource.ComposeAggregateTestCheckFunc(
+ // Confirm the pipeline exists in the buildkite API
+ testAccCheckPipelineExists("buildkite_pipeline.foobar", &resourcePipeline),
+ // Quick check to confirm the local state is correct before we update it
+ resource.TestCheckResourceAttr("buildkite_pipeline.foobar", "name", "Test Pipeline foo"),
+ ),
+ },
+ {
+ Config: testAccPipelineConfigBasicWithArchive("foo", true),
+ Check: resource.ComposeAggregateTestCheckFunc(
+ // Confirm the pipeline exists in the buildkite API
+ testAccCheckPipelineExists("buildkite_pipeline.foobar", &resourcePipeline),
+ // Confirm the pipeline has the updated values in Buildkite's system
+ testAccCheckPipelineRemoteValues(&resourcePipeline, "Test Pipeline foo"),
+ // Confirm the pipeline has the updated values in terraform state
+ resource.TestCheckResourceAttr("buildkite_pipeline.foobar", "archived", "true"),
+ ),
+ },
+ {
+ Config: testAccPipelineConfigBasicWithArchive("foo", false),
+ Check: resource.ComposeAggregateTestCheckFunc(
+ // Confirm the pipeline exists in the buildkite API
+ testAccCheckPipelineExists("buildkite_pipeline.foobar", &resourcePipeline),
+ // Confirm the pipeline has the updated values in Buildkite's system
+ testAccCheckPipelineRemoteValues(&resourcePipeline, "Test Pipeline foo"),
+ // Confirm the pipeline has the updated values in terraform state
+ resource.TestCheckResourceAttr("buildkite_pipeline.foobar", "archived", "false"),
+ ),
+ },
+ },
+ })
+}
+
func TestAccPipeline_update_withteams(t *testing.T) {
var resourcePipeline PipelineNode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment