Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for run triggers #132

Merged
merged 4 commits into from Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
@@ -1,7 +1,7 @@
module github.com/terraform-providers/terraform-provider-tfe

require (
github.com/hashicorp/go-tfe v0.4.0
github.com/hashicorp/go-tfe v0.5.0
github.com/hashicorp/go-version v1.2.0
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce
github.com/hashicorp/terraform-plugin-sdk v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -99,8 +99,8 @@ github.com/hashicorp/go-safetemp v1.0.0 h1:2HR189eFNrjHQyENnQMMpCiBAsRxzbTMIgBhE
github.com/hashicorp/go-safetemp v1.0.0/go.mod h1:oaerMy3BhqiTbVye6QuFhFtIceqFoDHxNAB65b+Rj1I=
github.com/hashicorp/go-slug v0.4.1 h1:/jAo8dNuLgSImoLXaX7Od7QB4TfYCVPam+OpAt5bZqc=
github.com/hashicorp/go-slug v0.4.1/go.mod h1:I5tq5Lv0E2xcNXNkmx7BSfzi1PsJ2cNjs3cC3LwyhK8=
github.com/hashicorp/go-tfe v0.4.0 h1:p5eh7MhrJ5ysAQaq4sbWg/eVJ94qtrqFJWqlYZZhgLE=
github.com/hashicorp/go-tfe v0.4.0/go.mod h1:DVPSW2ogH+M9W1/i50ASgMht8cHP7NxxK0nrY9aFikQ=
github.com/hashicorp/go-tfe v0.5.0 h1:8fKVNGdCziwZy0VtZkKmurYL7RJRPvGL9R72glwk6F8=
github.com/hashicorp/go-tfe v0.5.0/go.mod h1:DVPSW2ogH+M9W1/i50ASgMht8cHP7NxxK0nrY9aFikQ=
github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1BE=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.1.0 h1:bPIoEKD27tNdebFGGxxYwcL4nepeY4j1QP23PFRGzg0=
Expand Down
1 change: 1 addition & 0 deletions tfe/provider.go
Expand Up @@ -76,6 +76,7 @@ func Provider() terraform.ResourceProvider {
"tfe_organization_token": resourceTFEOrganizationToken(),
"tfe_policy_set": resourceTFEPolicySet(),
"tfe_policy_set_parameter": resourceTFEPolicySetParameter(),
"tfe_run_trigger": resourceTFERunTrigger(),
"tfe_sentinel_policy": resourceTFESentinelPolicy(),
"tfe_ssh_key": resourceTFESSHKey(),
"tfe_team": resourceTFETeam(),
Expand Down
96 changes: 96 additions & 0 deletions tfe/resource_tfe_run_trigger.go
@@ -0,0 +1,96 @@
package tfe

import (
"fmt"
"log"

tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func resourceTFERunTrigger() *schema.Resource {
return &schema.Resource{
Create: resourceTFERunTriggerCreate,
Read: resourceTFERunTriggerRead,
Delete: resourceTFERunTriggerDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"workspace_external_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"sourceable_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceTFERunTriggerCreate(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)

// Get workspace
workspaceID := d.Get("workspace_external_id").(string)

// Get attributes
sourceableID := d.Get("sourceable_id").(string)

// Create a new options struct
options := tfe.RunTriggerCreateOptions{
Sourceable: &tfe.Workspace{
ID: sourceableID,
},
}

log.Printf("[DEBUG] Create run trigger on workspace %s with sourceable %s", workspaceID, sourceableID)
runTrigger, err := tfeClient.RunTriggers.Create(ctx, workspaceID, options)
if err != nil {
return fmt.Errorf("Error creating run trigger on workspace %s with sourceable %s: %v", workspaceID, sourceableID, err)
}

d.SetId(runTrigger.ID)

return resourceTFERunTriggerRead(d, meta)
}

func resourceTFERunTriggerRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)

log.Printf("[DEBUG] Read run trigger: %s", d.Id())
runTrigger, err := tfeClient.RunTriggers.Read(ctx, d.Id())
if err != nil {
if err == tfe.ErrResourceNotFound {
log.Printf("[DEBUG] run trigger %s no longer exists", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading run trigger %s: %v", d.Id(), err)
}

// Update config
d.Set("workspace_external_id", runTrigger.Workspace.ID)
d.Set("sourceable_id", runTrigger.Sourceable.ID)

return nil
}

func resourceTFERunTriggerDelete(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)

log.Printf("[DEBUG] Delete run trigger: %s", d.Id())
err := tfeClient.RunTriggers.Delete(ctx, d.Id())
if err != nil {
if err == tfe.ErrResourceNotFound {
return nil
}
return fmt.Errorf("Error deleting run trigger %s: %v", d.Id(), err)
}

return nil
}
135 changes: 135 additions & 0 deletions tfe/resource_tfe_run_trigger_test.go
@@ -0,0 +1,135 @@
package tfe

import (
"fmt"
"testing"

"github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccTFERunTrigger_basic(t *testing.T) {
runTrigger := &tfe.RunTrigger{}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFERunTriggerDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFERunTrigger_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckTFERunTriggerExists(
"tfe_run_trigger.foobar", runTrigger),
testAccCheckTFERunTriggerAttributes(runTrigger),
),
},
},
})
}

func TestAccTFERunTriggerImport(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFERunTriggerDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFERunTrigger_basic,
},

{
ResourceName: "tfe_run_trigger.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckTFERunTriggerExists(n string, runTrigger *tfe.RunTrigger) resource.TestCheckFunc {
return func(s *terraform.State) error {
tfeClient := testAccProvider.Meta().(*tfe.Client)

rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No instance ID is set")
}

rt, err := tfeClient.RunTriggers.Read(ctx, rs.Primary.ID)
if err != nil {
return err
}

*runTrigger = *rt

return nil
}
}

func testAccCheckTFERunTriggerAttributes(runTrigger *tfe.RunTrigger) resource.TestCheckFunc {
return func(s *terraform.State) error {
tfeClient := testAccProvider.Meta().(*tfe.Client)

workspaceID := runTrigger.Workspace.ID
workspace, _ := tfeClient.Workspaces.Read(ctx, "tst-terraform", "workspace-test")
if workspace.ID != workspaceID {
return fmt.Errorf("Wrong workspace: %v", workspace.ID)
}

sourceableID := runTrigger.Sourceable.ID
sourceable, _ := tfeClient.Workspaces.Read(ctx, "tst-terraform", "sourceable-test")
if sourceable.ID != sourceableID {
return fmt.Errorf("Wrong sourceable: %v", sourceable.ID)
}

return nil
}
}

func testAccCheckTFERunTriggerDestroy(s *terraform.State) error {
tfeClient := testAccProvider.Meta().(*tfe.Client)

for _, rs := range s.RootModule().Resources {
if rs.Type != "tfe_run_trigger" {
continue
}

if rs.Primary.ID == "" {
return fmt.Errorf("No instance ID is set")
}

_, err := tfeClient.RunTriggers.Read(ctx, rs.Primary.ID)
if err == nil {
return fmt.Errorf("Notification configuration %s still exists", rs.Primary.ID)
}
}

return nil
}

const testAccTFERunTrigger_basic = `
resource "tfe_organization" "foobar" {
name = "tst-terraform"
email = "admin@company.com"
}

resource "tfe_workspace" "workspace" {
name = "workspace-test"
organization = "${tfe_organization.foobar.id}"
}

resource "tfe_workspace" "sourceable" {
name = "sourceable-test"
organization = "${tfe_organization.foobar.id}"
}

resource "tfe_run_trigger" "foobar" {
workspace_external_id = "${tfe_workspace.workspace.external_id}"
sourceable_id = "${tfe_workspace.sourceable.external_id}"
}`
1 change: 1 addition & 0 deletions vendor/github.com/hashicorp/go-tfe/README.md

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