-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: workflow trigger new resources (#627)
- Loading branch information
1 parent
f4dc42d
commit 8b06996
Showing
7 changed files
with
345 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package env0 | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourceWorkflowTrigger() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceWorkflowTriggerCreate, | ||
ReadContext: resourceWorkflowTriggerRead, | ||
DeleteContext: resourceWorkflowTriggerDelete, | ||
Description: "cannot be used with env0_workflow_triggers", | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"environment_id": { | ||
Type: schema.TypeString, | ||
Description: "id of the source environment", | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"downstream_environment_id": { | ||
Type: schema.TypeString, | ||
Description: "environment to trigger", | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceWorkflowTriggerRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(client.ApiClientInterface) | ||
|
||
environmentId := d.Get("environment_id").(string) | ||
downstreamEnvironmentId := d.Get("downstream_environment_id").(string) | ||
|
||
triggers, err := apiClient.WorkflowTrigger(environmentId) | ||
|
||
if err != nil { | ||
return diag.Errorf("could not get workflow triggers: %v", err) | ||
} | ||
|
||
for _, trigger := range triggers { | ||
if trigger.Id == downstreamEnvironmentId { | ||
return nil | ||
} | ||
} | ||
|
||
log.Printf("[WARN] Drift Detected: Terraform will remove %s from state", d.Id()) | ||
d.SetId("") | ||
|
||
return nil | ||
} | ||
|
||
func resourceWorkflowTriggerCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(client.ApiClientInterface) | ||
|
||
environmentId := d.Get("environment_id").(string) | ||
downstreamEnvironmentId := d.Get("downstream_environment_id").(string) | ||
|
||
payload := client.WorkflowTriggerEnvironments{DownstreamEnvironmentIds: []string{downstreamEnvironmentId}} | ||
|
||
if err := apiClient.SubscribeWorkflowTrigger(environmentId, payload); err != nil { | ||
return diag.Errorf("failed to subscribe a workflow trigger: %v", err) | ||
} | ||
|
||
d.SetId(environmentId + "_" + downstreamEnvironmentId) | ||
|
||
return nil | ||
} | ||
|
||
func resourceWorkflowTriggerDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(client.ApiClientInterface) | ||
|
||
environmentId := d.Get("environment_id").(string) | ||
downstreamEnvironmentId := d.Get("downstream_environment_id").(string) | ||
|
||
payload := client.WorkflowTriggerEnvironments{DownstreamEnvironmentIds: []string{downstreamEnvironmentId}} | ||
|
||
if err := apiClient.UnsubscribeWorkflowTrigger(environmentId, payload); err != nil { | ||
return diag.Errorf("failed to unsubscribe a workflow trigger: %v", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
package env0 | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/golang/mock/gomock" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestUnitWorkflowTriggerResource(t *testing.T) { | ||
resourceType := "env0_workflow_trigger" | ||
resourceName := "test" | ||
accessor := resourceAccessor(resourceType, resourceName) | ||
environmentId := "environment_id" | ||
triggerId := "trigger_environment_id" | ||
otherTriggerId := "other_trigger_environment_id" | ||
|
||
t.Run("Success", func(t *testing.T) { | ||
|
||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ | ||
"environment_id": environmentId, | ||
"downstream_environment_id": triggerId, | ||
}), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(accessor, "id", environmentId+"_"+triggerId), | ||
resource.TestCheckResourceAttr(accessor, "environment_id", environmentId), | ||
resource.TestCheckResourceAttr(accessor, "downstream_environment_id", triggerId), | ||
), | ||
}, | ||
{ | ||
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ | ||
"environment_id": environmentId, | ||
"downstream_environment_id": otherTriggerId, | ||
}), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(accessor, "id", environmentId+"_"+otherTriggerId), | ||
resource.TestCheckResourceAttr(accessor, "environment_id", environmentId), | ||
resource.TestCheckResourceAttr(accessor, "downstream_environment_id", otherTriggerId), | ||
), | ||
}, | ||
}, | ||
} | ||
|
||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { | ||
gomock.InOrder( | ||
mock.EXPECT().SubscribeWorkflowTrigger(environmentId, client.WorkflowTriggerEnvironments{ | ||
DownstreamEnvironmentIds: []string{triggerId}, | ||
}).Times(1).Return(nil), | ||
mock.EXPECT().WorkflowTrigger(environmentId).Times(2).Return([]client.WorkflowTrigger{ | ||
{ | ||
Id: triggerId, | ||
}, | ||
}, nil), | ||
mock.EXPECT().UnsubscribeWorkflowTrigger(environmentId, client.WorkflowTriggerEnvironments{ | ||
DownstreamEnvironmentIds: []string{triggerId}, | ||
}).Times(1).Return(nil), | ||
mock.EXPECT().SubscribeWorkflowTrigger(environmentId, client.WorkflowTriggerEnvironments{ | ||
DownstreamEnvironmentIds: []string{otherTriggerId}, | ||
}).Times(1).Return(nil), | ||
mock.EXPECT().WorkflowTrigger(environmentId).Times(1).Return([]client.WorkflowTrigger{ | ||
{ | ||
Id: otherTriggerId, | ||
}, | ||
}, nil), | ||
mock.EXPECT().UnsubscribeWorkflowTrigger(environmentId, client.WorkflowTriggerEnvironments{ | ||
DownstreamEnvironmentIds: []string{otherTriggerId}, | ||
}).Times(1).Return(nil), | ||
) | ||
}) | ||
|
||
}) | ||
|
||
t.Run("Failure in Get Triggers", func(t *testing.T) { | ||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ | ||
"environment_id": environmentId, | ||
"downstream_environment_id": triggerId, | ||
}), | ||
ExpectError: regexp.MustCompile("could not get workflow triggers: error"), | ||
}, | ||
}, | ||
} | ||
|
||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { | ||
gomock.InOrder( | ||
mock.EXPECT().SubscribeWorkflowTrigger(environmentId, client.WorkflowTriggerEnvironments{ | ||
DownstreamEnvironmentIds: []string{triggerId}, | ||
}).Times(1).Return(nil), | ||
mock.EXPECT().WorkflowTrigger(environmentId).Times(1).Return(nil, errors.New("error")), | ||
mock.EXPECT().UnsubscribeWorkflowTrigger(environmentId, client.WorkflowTriggerEnvironments{ | ||
DownstreamEnvironmentIds: []string{triggerId}, | ||
}).Times(1).Return(nil), | ||
) | ||
}) | ||
}) | ||
|
||
t.Run("Failure in Unsubscribe", func(t *testing.T) { | ||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ | ||
"environment_id": environmentId, | ||
"downstream_environment_id": triggerId, | ||
}), | ||
}, | ||
{ | ||
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ | ||
"environment_id": environmentId, | ||
"downstream_environment_id": otherTriggerId, | ||
}), | ||
ExpectError: regexp.MustCompile("failed to unsubscribe a workflow trigger: error"), | ||
}, | ||
}, | ||
} | ||
|
||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { | ||
gomock.InOrder( | ||
mock.EXPECT().SubscribeWorkflowTrigger(environmentId, client.WorkflowTriggerEnvironments{ | ||
DownstreamEnvironmentIds: []string{triggerId}, | ||
}).Times(1).Return(nil), | ||
mock.EXPECT().WorkflowTrigger(environmentId).Times(2).Return([]client.WorkflowTrigger{ | ||
{ | ||
Id: triggerId, | ||
}, | ||
}, nil), | ||
mock.EXPECT().UnsubscribeWorkflowTrigger(environmentId, client.WorkflowTriggerEnvironments{ | ||
DownstreamEnvironmentIds: []string{triggerId}, | ||
}).Times(1).Return(errors.New("error")), | ||
mock.EXPECT().UnsubscribeWorkflowTrigger(environmentId, client.WorkflowTriggerEnvironments{ | ||
DownstreamEnvironmentIds: []string{triggerId}, | ||
}).Times(1).Return(nil), | ||
) | ||
}) | ||
}) | ||
|
||
t.Run("Failure in Subscribe", func(t *testing.T) { | ||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ | ||
"environment_id": environmentId, | ||
"downstream_environment_id": triggerId, | ||
}), | ||
ExpectError: regexp.MustCompile("failed to subscribe a workflow trigger: error"), | ||
}, | ||
}, | ||
} | ||
|
||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { | ||
gomock.InOrder( | ||
mock.EXPECT().SubscribeWorkflowTrigger(environmentId, client.WorkflowTriggerEnvironments{ | ||
DownstreamEnvironmentIds: []string{triggerId}, | ||
}).Times(1).Return(errors.New("error")), | ||
) | ||
}) | ||
}) | ||
|
||
t.Run("Drift", func(t *testing.T) { | ||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{ | ||
"environment_id": environmentId, | ||
"downstream_environment_id": triggerId, | ||
}), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(accessor, "id", environmentId+"_"+triggerId), | ||
resource.TestCheckResourceAttr(accessor, "environment_id", environmentId), | ||
resource.TestCheckResourceAttr(accessor, "downstream_environment_id", triggerId), | ||
), | ||
ExpectNonEmptyPlan: true, | ||
}, | ||
}, | ||
} | ||
|
||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { | ||
gomock.InOrder( | ||
mock.EXPECT().SubscribeWorkflowTrigger(environmentId, client.WorkflowTriggerEnvironments{ | ||
DownstreamEnvironmentIds: []string{triggerId}, | ||
}).Times(1).Return(nil), | ||
mock.EXPECT().WorkflowTrigger(environmentId).Times(2).Return([]client.WorkflowTrigger{ | ||
{ | ||
Id: otherTriggerId, | ||
}, | ||
}, nil), | ||
) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
data "env0_project" "default" { | ||
name = "Default Organization Project" | ||
} | ||
|
||
resource "env0_template" "template" { | ||
name = "Template for environment resource" | ||
type = "terraform" | ||
repository = "https://github.com/env0/templates" | ||
path = "misc/null-resource" | ||
terraform_version = "0.15.1" | ||
} | ||
|
||
resource "env0_environment" "the_trigger" { | ||
force_destroy = true | ||
name = "the_trigger" | ||
project_id = data.env0_project.default.id | ||
template_id = env0_template.template.id | ||
approve_plan_automatically = true | ||
} | ||
|
||
resource "env0_environment" "downstream_environment" { | ||
force_destroy = true | ||
name = "downstream_environment" | ||
project_id = data.env0_project.default.id | ||
template_id = env0_template.template.id | ||
approve_plan_automatically = true | ||
} | ||
|
||
resource "env0_workflow_trigger" "trigger_link" { | ||
environment_id = env0_environment.the_trigger.id | ||
downstream_environment_id = env0_environment.downstream_environment.id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters