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

Feat: add module testing project data source #791

Merged
merged 2 commits into from
Feb 12, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type ApiClientInterface interface {
ProjectUpdate(id string, payload ProjectUpdatePayload) (Project, error)
ProjectDelete(id string) error
ProjectMove(id string, targetProjectId string) error
ModuleTestingProject() (*ModuleTestingProject, error)
Template(id string) (Template, error)
Templates() ([]Template, error)
TemplateCreate(payload TemplateCreatePayload) (Template, error)
Expand Down
15 changes: 15 additions & 0 deletions client/api_client_mock.go

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

19 changes: 19 additions & 0 deletions client/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ type ProjectUpdatePayload struct {
Description string `json:"description"`
}

type ModuleTestingProject struct {
Name string `json:"name"`
Id string `json:"id"`
}

func (client *ApiClient) Projects() ([]Project, error) {
organizationId, err := client.OrganizationId()
if err != nil {
Expand Down Expand Up @@ -99,3 +104,17 @@ func (client *ApiClient) ProjectMove(id string, targetProjectId string) error {

return client.http.Post("/projects/"+id+"/move", payload, nil)
}

func (client *ApiClient) ModuleTestingProject() (*ModuleTestingProject, error) {
organizationId, err := client.OrganizationId()
if err != nil {
return nil, err
}

var result ModuleTestingProject
if err := client.http.Get("/projects/modules/testing/"+organizationId, nil, &result); err != nil {
return nil, err
}

return &result, nil
}
27 changes: 27 additions & 0 deletions client/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ const parentProjectId = "parent_project_id"

var _ = Describe("Project", func() {
var project Project
var moduleTestingProject *ModuleTestingProject
mockProject := Project{
Id: "idX",
Name: "projectX",
Description: "descriptionX",
OrganizationId: organizationId,
}

mockModuleTestingProject := ModuleTestingProject{
Id: "idx",
Name: "namex",
}

Describe("ProjectCreate", func() {
BeforeEach(func() {
mockOrganizationIdCall(organizationId)
Expand Down Expand Up @@ -180,4 +186,25 @@ var _ = Describe("Project", func() {

It("Should send POST request with project id and nil target project id", func() {})
})

Describe("ModuleTestingProject", func() {
BeforeEach(func() {
mockOrganizationIdCall(organizationId)

httpCall = mockHttpClient.EXPECT().
Get("/projects/modules/testing/"+organizationId, nil, gomock.Any()).
Do(func(path string, request interface{}, response *ModuleTestingProject) {
*response = mockModuleTestingProject
})
moduleTestingProject, _ = apiClient.ModuleTestingProject()
})

It("Should send GET request", func() {
httpCall.Times(1)
})

It("Should return module testing project", func() {
Expect(mockModuleTestingProject).To(Equal(*moduleTestingProject))
})
})
})
44 changes: 44 additions & 0 deletions env0/data_module_testing_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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,
},
},
}
}

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
}
65 changes: 65 additions & 0 deletions env0/data_module_testing_project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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",
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),
),
},
},
}
}

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"))
},
)
})

}
1 change: 1 addition & 0 deletions env0/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func Provider(version string) plugin.ProviderFunc {
"env0_provider": dataProvider(),
"env0_custom_flow": dataCustomFlow(),
"env0_projects": dataProjects(),
"env0_module_testing_project": dataModuleTestingProject(),
},
ResourcesMap: map[string]*schema.Resource{
"env0_project": resourceProject(),
Expand Down
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
}
2 changes: 2 additions & 0 deletions tests/integration/002_project/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ data "env0_project" "data_by_name_with_parent_id" {

data "env0_projects" "list_of_projects" {}

data "env0_module_testing_project" "module_testing_project" {}

output "test_project_name" {
value = replace(env0_project.test_project.name, random_string.random.result, "")
}
Expand Down
Loading