-
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.
- Loading branch information
1 parent
55de94d
commit 8ff19c9
Showing
2 changed files
with
155 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
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 dataCustomFlow() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataCustomFlowRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Description: "The name of the custom flow", | ||
Optional: true, | ||
ExactlyOneOf: []string{"name", "id"}, | ||
}, | ||
"id": { | ||
Type: schema.TypeString, | ||
Description: "ID of the custom flow", | ||
Optional: true, | ||
ExactlyOneOf: []string{"name", "id"}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataCustomFlowRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var err error | ||
var customFlow *client.CustomFlow | ||
|
||
id, ok := d.GetOk("id") | ||
if ok { | ||
customFlow, err = meta.(client.ApiClientInterface).CustomFlow(id.(string)) | ||
if err != nil { | ||
return diag.Errorf("failed to get custom flow by id: %v", err) | ||
} | ||
} else { | ||
name := d.Get("name") | ||
customFlow, err = getCustomFlowByName(name.(string), meta) | ||
if err != nil { | ||
return diag.Errorf("failed to get custom flow by name: %v", err) | ||
} | ||
} | ||
|
||
if err := writeResourceData(customFlow, d); err != nil { | ||
return diag.Errorf("schema resource data serialization failed: %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,100 @@ | ||
package env0 | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/env0/terraform-provider-env0/client/http" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestCustomFlowDataSource(t *testing.T) { | ||
customFlow := client.CustomFlow{ | ||
Id: "id0", | ||
Name: "name0", | ||
Repository: "rep1", | ||
Path: "path", | ||
} | ||
|
||
otherCustomFlow := client.CustomFlow{ | ||
Id: "id1", | ||
Name: "name1", | ||
Repository: "rep2", | ||
Path: "path", | ||
} | ||
|
||
customFlowFieldsByName := map[string]interface{}{"name": customFlow.Name} | ||
customFlowFieldsById := map[string]interface{}{"id": customFlow.Id} | ||
|
||
resourceType := "env0_custom_flow" | ||
resourceName := "test_custom_flow" | ||
accessor := dataSourceAccessor(resourceType, resourceName) | ||
|
||
getValidTestCase := func(input map[string]interface{}) resource.TestCase { | ||
return resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, input), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(accessor, "id", customFlow.Id), | ||
resource.TestCheckResourceAttr(accessor, "name", customFlow.Name), | ||
), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
getErrorTestCase := func(input map[string]interface{}, expectedError string) resource.TestCase { | ||
return resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, input), | ||
ExpectError: regexp.MustCompile(expectedError), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
mockListCustomFlowsCall := func(returnValue []client.CustomFlow) func(mockFunc *client.MockApiClientInterface) { | ||
return func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().CustomFlows(customFlow.Name).AnyTimes().Return(returnValue, nil) | ||
} | ||
} | ||
|
||
mockCustomFlowCall := func(returnValue *client.CustomFlow) func(mockFunc *client.MockApiClientInterface) { | ||
return func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().CustomFlow(customFlow.Id).AnyTimes().Return(returnValue, nil) | ||
} | ||
} | ||
|
||
t.Run("By ID", func(t *testing.T) { | ||
runUnitTest(t, | ||
getValidTestCase(customFlowFieldsById), | ||
mockCustomFlowCall(&customFlow), | ||
) | ||
}) | ||
|
||
t.Run("By Name", func(t *testing.T) { | ||
runUnitTest(t, | ||
getValidTestCase(customFlowFieldsByName), | ||
mockListCustomFlowsCall([]client.CustomFlow{customFlow}), | ||
) | ||
}) | ||
|
||
t.Run("Throw error when by name and more than one custom flow exists", func(t *testing.T) { | ||
runUnitTest(t, | ||
getErrorTestCase(customFlowFieldsByName, "found multiple custom flows with name"), | ||
mockListCustomFlowsCall([]client.CustomFlow{customFlow, otherCustomFlow, customFlow}), | ||
) | ||
}) | ||
|
||
t.Run("Throw error when by id and no custom flow found with that id", func(t *testing.T) { | ||
runUnitTest(t, | ||
getErrorTestCase(customFlowFieldsById, "failed to get custom flow by id"), | ||
func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().CustomFlow(customFlow.Id).Times(1).Return(nil, http.NewMockFailedResponseError(404)) | ||
}, | ||
) | ||
}) | ||
} |