Skip to content

Commit

Permalink
updates based on PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Sep 11, 2024
1 parent 04b82da commit 40dcdef
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
6 changes: 3 additions & 3 deletions env0/data_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ func dataProject() *schema.Resource {
Type: schema.TypeString,
Description: "the name of the parent project. Can be used as a filter when there are multiple subprojects with the same name under different parent projects",
Optional: true,
ConflictsWith: []string{"parent_project_path"},
ConflictsWith: []string{"parent_project_path", "parent_project_id"},
},
"parent_project_path": {
Type: schema.TypeString,
Description: "a path of ancestors projects divided by the prefix '|'. Can be used as a filter when there are multiple subprojects with the same name under different parent projects. For example: 'App|Dev|us-east-1' will search for a project with the hierarchy 'App -> Dev -> us-east-1' ('us-east-1' being the parent)",
Optional: true,
ConflictsWith: []string{"parent_project_name"},
ConflictsWith: []string{"parent_project_name", "parent_project_id"},
},
"parent_project_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -183,7 +183,7 @@ func getProjectByName(name string, parentId string, parentName string, parentPat
}

func pathMatches(path, parentIds []string, meta interface{}) (bool, error) {
if len(path) != len(parentIds) {
if len(path) > len(parentIds) {
return false, nil
}

Expand Down
74 changes: 52 additions & 22 deletions env0/data_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,37 +177,67 @@ func TestProjectDataSource(t *testing.T) {
p1 := createProject("p1", nil)
p2 := createProject("p2", []client.Project{*p1})
p3 := createProject("p3", []client.Project{*p1, *p2})
p4 := createProject("p4", []client.Project{*p1, *p2, *p3})

p3other := createProject("p3", []client.Project{*p1})
p4other := createProject("p4", []client.Project{*p1})

pother1 := createProject("pother1", nil)
pother2 := createProject("p2", []client.Project{*pother1})
pother3 := createProject("p3", []client.Project{*pother1, *pother2})

runUnitTest(t,
resource.TestCase{
Steps: []resource.TestStep{
{
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{"name": "p3", "parent_project_path": "p1|p2"}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "id", p3.Id),
resource.TestCheckResourceAttr(accessor, "name", p3.Name),
resource.TestCheckResourceAttr(accessor, "hierarchy", p1.Id+"|"+p2.Id+"|"+p3.Id),
),
t.Run("exact match", func(t *testing.T) {
runUnitTest(t,
resource.TestCase{
Steps: []resource.TestStep{
{
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{"name": "p3", "parent_project_path": "p1|p2"}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "id", p3.Id),
resource.TestCheckResourceAttr(accessor, "name", p3.Name),
resource.TestCheckResourceAttr(accessor, "hierarchy", p1.Id+"|"+p2.Id+"|"+p3.Id),
),
},
},
},
},
func(mock *client.MockApiClientInterface) {
mock.EXPECT().Projects().AnyTimes().Return([]client.Project{*p3, *p3other, *pother3}, nil)
mock.EXPECT().Project(p1.Id).AnyTimes().Return(*p1, nil)
mock.EXPECT().Project(p2.Id).AnyTimes().Return(*p2, nil)
mock.EXPECT().Project(p3.Id).AnyTimes().Return(*p3, nil)
mock.EXPECT().Project(p3other.Id).AnyTimes().Return(*p3other, nil)
mock.EXPECT().Project(pother1.Id).AnyTimes().Return(*pother1, nil)
mock.EXPECT().Project(pother2.Id).AnyTimes().Return(*pother2, nil)
mock.EXPECT().Project(pother3.Id).AnyTimes().Return(*pother3, nil)
},
)
func(mock *client.MockApiClientInterface) {
mock.EXPECT().Projects().AnyTimes().Return([]client.Project{*p3, *p3other, *pother3}, nil)
mock.EXPECT().Project(p1.Id).AnyTimes().Return(*p1, nil)
mock.EXPECT().Project(p2.Id).AnyTimes().Return(*p2, nil)
mock.EXPECT().Project(p3.Id).AnyTimes().Return(*p3, nil)
mock.EXPECT().Project(p3other.Id).AnyTimes().Return(*p3other, nil)
mock.EXPECT().Project(pother1.Id).AnyTimes().Return(*pother1, nil)
mock.EXPECT().Project(pother2.Id).AnyTimes().Return(*pother2, nil)
mock.EXPECT().Project(pother3.Id).AnyTimes().Return(*pother3, nil)
},
)
})

t.Run("prefix match", func(t *testing.T) {
runUnitTest(t,
resource.TestCase{
Steps: []resource.TestStep{
{
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{"name": "p4", "parent_project_path": "p1|p2"}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "id", p4.Id),
resource.TestCheckResourceAttr(accessor, "name", p4.Name),
resource.TestCheckResourceAttr(accessor, "hierarchy", p1.Id+"|"+p2.Id+"|"+p3.Id+"|"+p4.Id),
),
},
},
},
func(mock *client.MockApiClientInterface) {
mock.EXPECT().Projects().AnyTimes().Return([]client.Project{*p4, *p4other}, nil)
mock.EXPECT().Project(p1.Id).AnyTimes().Return(*p1, nil)
mock.EXPECT().Project(p2.Id).AnyTimes().Return(*p2, nil)
mock.EXPECT().Project(p3.Id).AnyTimes().Return(*p3, nil)
mock.EXPECT().Project(p4.Id).AnyTimes().Return(*p3, nil)
mock.EXPECT().Project(p4other.Id).AnyTimes().Return(*p4other, nil)
},
)
})

})

t.Run("By Name with Parent Id", func(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/002_project/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,8 @@ data "env0_project" "data_by_name_with_parent_path" {
name = env0_project.project_by_path3.name
parent_project_path = "project-${random_string.random.result}-p1|project-${random_string.random.result}-p2"
}

data "env0_project" "data_by_name_with_parent_prefix_path" {
name = env0_project.project_by_path3.name
parent_project_path = "project-${random_string.random.result}-p1"
}

0 comments on commit 40dcdef

Please sign in to comment.