-
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: add module testing project data source
- Loading branch information
1 parent
ae24b24
commit 6eefe70
Showing
9 changed files
with
191 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,49 @@ | ||
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 dataModuleTestingProject() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataModuleTestingProjectRead, | ||
Description: "Can be used to get the project_id for agent_project_assignment and cloud_credentials_project_assignment", | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Description: "the module testing project id", | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Description: "the module testing project id", | ||
Computed: true, | ||
}, | ||
"parent_project_id": { | ||
Type: schema.TypeString, | ||
Description: "the module testing parent project id", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataModuleTestingProjectRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(client.ApiClientInterface) | ||
|
||
moduleTestingProject, err := client.ModuleTestingProject() | ||
if err != nil { | ||
return diag.Errorf("could not get module testing project: %v", err) | ||
} | ||
|
||
if err := writeResourceData(moduleTestingProject, 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,67 @@ | ||
package env0 | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestModuleTestingProjectDataSource(t *testing.T) { | ||
resourceType := "env0_module_testing_project" | ||
resourceName := "test" | ||
accessor := dataSourceAccessor(resourceType, resourceName) | ||
|
||
moduleTestingProject := client.ModuleTestingProject{ | ||
Name: "namex", | ||
ParentProjectId: "pidx", | ||
Id: "idx", | ||
} | ||
|
||
getTestCase := func() resource.TestCase { | ||
return resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{}), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(accessor, "name", moduleTestingProject.Name), | ||
resource.TestCheckResourceAttr(accessor, "id", moduleTestingProject.Id), | ||
resource.TestCheckResourceAttr(accessor, "parent_project_id", moduleTestingProject.ParentProjectId), | ||
), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
mockModuleTestingProject := func() func(mockFunc *client.MockApiClientInterface) { | ||
return func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().ModuleTestingProject().AnyTimes().Return(&moduleTestingProject, nil) | ||
} | ||
} | ||
|
||
t.Run("Success", func(t *testing.T) { | ||
runUnitTest(t, | ||
getTestCase(), | ||
mockModuleTestingProject(), | ||
) | ||
}) | ||
|
||
t.Run("API Call Error", func(t *testing.T) { | ||
runUnitTest(t, | ||
resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{}), | ||
ExpectError: regexp.MustCompile("could not get module testing project: error"), | ||
}, | ||
}, | ||
}, | ||
func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().ModuleTestingProject().AnyTimes().Return(nil, 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
8 changes: 8 additions & 0 deletions
8
examples/data-sources/env0_module_testing_project/data-source.tf
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,8 @@ | ||
data "env0_module_testing_project" "project" {} | ||
|
||
data "env0_agents" "agents" {} | ||
|
||
resource "env0_agent_project_assignment" "example" { | ||
agent_id = data.env0_agents.agents.0.agent_key | ||
project_id = env0_module_testing_project.project.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