-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
elbaz
committed
Jan 23, 2022
1 parent
88c1e7d
commit 4f75ddf
Showing
19 changed files
with
611 additions
and
18 deletions.
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
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,44 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "env0_workflow_triggers Data Source - terraform-provider-env0" | ||
subcategory: "" | ||
description: |- | ||
--- | ||
|
||
# env0_workflow_triggers (Data Source) | ||
|
||
|
||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "env0_workflow_triggers" "downstream_envs" { | ||
environment_id = "environment_id" | ||
} | ||
data "env0_environment" "by_name" { | ||
id = data.env0_workflow_triggers.downstream_envs.0.id | ||
} | ||
output "downstream_env_name" { | ||
value = data.env0_environment.by_name.name | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- **environment_id** (String) id of the source environment | ||
|
||
### Optional | ||
|
||
- **id** (String) The ID of this resource. | ||
|
||
### Read-Only | ||
|
||
- **downstream_environment_ids** (List of String) environments to trigger | ||
|
||
|
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,64 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "env0_workflow_triggers Resource - terraform-provider-env0" | ||
subcategory: "" | ||
description: |- | ||
--- | ||
|
||
# env0_workflow_triggers (Resource) | ||
|
||
|
||
|
||
## Example Usage | ||
|
||
```terraform | ||
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_triggers" "trigger_link" { | ||
environment_id = env0_environment.the_trigger.id | ||
downstream_environment_ids = [ | ||
env0_environment.downstream_environment.id | ||
] | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- **downstream_environment_ids** (List of String) environments to trigger | ||
- **environment_id** (String) id of the source environment | ||
|
||
### Optional | ||
|
||
- **id** (String) The ID of this resource. | ||
|
||
|
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,56 @@ | ||
package env0 | ||
|
||
import ( | ||
"context" | ||
"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 dataWorkflowTriggers() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataWorkflowTriggersRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"environment_id": { | ||
Type: schema.TypeString, | ||
Description: "id of the source environment", | ||
Required: true, | ||
}, | ||
"downstream_environment_ids": { | ||
Type: schema.TypeList, | ||
Description: "environments to trigger", | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
Description: "id of the downstream environments to trigger", | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataWorkflowTriggersRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(client.ApiClientInterface) | ||
|
||
environmentId := d.Get("environment_id").(string) | ||
|
||
triggers, err := apiClient.WorkflowTrigger(environmentId) | ||
|
||
if err != nil { | ||
return diag.Errorf("could not get workflow triggers: %v", err) | ||
} | ||
d.SetId(environmentId) | ||
var triggerIds []string | ||
for _, value := range triggers { | ||
triggerIds = append(triggerIds, value.Id) | ||
} | ||
|
||
d.Set(`downstream_environment_ids`, triggerIds) | ||
|
||
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,59 @@ | ||
package env0 | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestWorkflowTriggerDataSource(t *testing.T) { | ||
trigger := client.WorkflowTrigger{ | ||
Id: "id0", | ||
} | ||
|
||
environmentId := "environment_id" | ||
resourceType := "env0_workflow_triggers" | ||
resourceName := "test" | ||
accessor := dataSourceAccessor(resourceType, resourceName) | ||
|
||
t.Run("By environment id", func(t *testing.T) { | ||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{ | ||
"environment_id": environmentId, | ||
}), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(accessor, "id", environmentId), | ||
resource.TestCheckResourceAttr(accessor, "environment_id", environmentId), | ||
resource.TestCheckResourceAttr(accessor, "downstream_environment_ids.0", trigger.Id), | ||
), | ||
}, | ||
}, | ||
} | ||
|
||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().WorkflowTrigger(environmentId).AnyTimes().Return([]client.WorkflowTrigger{trigger}, nil) | ||
}) | ||
}) | ||
|
||
t.Run("When Error", func(t *testing.T) { | ||
testCase := resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{ | ||
"environment_id": environmentId, | ||
}), | ||
ExpectError: regexp.MustCompile("could not get workflow triggers: error"), | ||
}, | ||
}, | ||
} | ||
|
||
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().WorkflowTrigger(environmentId).AnyTimes().Return([]client.WorkflowTrigger{}, errors.New("error")) | ||
}) | ||
}) | ||
} |
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,93 @@ | ||
package env0 | ||
|
||
import ( | ||
"context" | ||
. "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 resourceWorkflowTriggers() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceWorkflowTriggersCreateOrUpdate, | ||
ReadContext: resourceWorkflowTriggersRead, | ||
UpdateContext: resourceWorkflowTriggersCreateOrUpdate, | ||
DeleteContext: resourceWorkflowTriggersDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"environment_id": { | ||
Type: schema.TypeString, | ||
Description: "id of the source environment", | ||
Required: true, | ||
}, | ||
"downstream_environment_ids": { | ||
Type: schema.TypeList, | ||
Description: "environments to trigger", | ||
Required: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
Description: "id of the downstream environments to trigger", | ||
}, | ||
MinItems: 1, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceWorkflowTriggersRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(ApiClientInterface) | ||
|
||
environmentId := d.Get("environment_id").(string) | ||
|
||
triggers, err := apiClient.WorkflowTrigger(environmentId) | ||
|
||
if err != nil { | ||
return diag.Errorf("could not get workflow triggers: %v", err) | ||
} | ||
|
||
var triggerIds []string | ||
for _, value := range triggers { | ||
triggerIds = append(triggerIds, value.Id) | ||
} | ||
|
||
d.Set(`downstream_environment_ids`, triggerIds) | ||
return nil | ||
} | ||
|
||
func resourceWorkflowTriggersCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(ApiClientInterface) | ||
environmentId := d.Get("environment_id").(string) | ||
rawDownstreamIds := d.Get("downstream_environment_ids").([]interface{}) | ||
var requestDownstreamIds []string | ||
|
||
for _, rawId := range rawDownstreamIds { | ||
requestDownstreamIds = append(requestDownstreamIds, rawId.(string)) | ||
} | ||
request := WorkflowTriggerUpsertPayload{ | ||
requestDownstreamIds, | ||
} | ||
triggers, err := apiClient.WorkflowTriggerUpsert(environmentId, request) | ||
if err != nil { | ||
return diag.Errorf("could not Create or Update workflow triggers: %v", err) | ||
} | ||
|
||
var downstreamIds []string | ||
for _, trigger := range triggers { | ||
downstreamIds = append(downstreamIds, trigger.Id) | ||
} | ||
|
||
d.SetId(environmentId) | ||
d.Set("downstream_environment_ids", downstreamIds) | ||
return nil | ||
} | ||
|
||
func resourceWorkflowTriggersDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(ApiClientInterface) | ||
|
||
_, err := apiClient.WorkflowTriggerUpsert(d.Id(), WorkflowTriggerUpsertPayload{DownstreamEnvironmentIds: []string{}}) | ||
if err != nil { | ||
return diag.Errorf("could not remove workflow triggers: %v", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.