Skip to content

Commit

Permalink
[fix]: [CDS-77080]: Support org and account level env groups (#655)
Browse files Browse the repository at this point in the history
* [fix]: [CDS-77080]: support org and account level env groups

* [fix]: [CDS-77080]: support org and account level env groups - 2
  • Loading branch information
lovish1999 authored Aug 17, 2023
1 parent efe75cd commit 8d54aa5
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 44 deletions.
3 changes: 3 additions & 0 deletions .changelog/655.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
Fixed the environment group resource to support org and account level environment groups also.
```
4 changes: 2 additions & 2 deletions docs/data-sources/platform_environment_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ data "harness_platform_environment_group" "example" {
### Required

- `identifier` (String) identifier of the environment group.
- `org_id` (String) org_id of the environment group.
- `project_id` (String) project_id of the environment group.

### Optional

- `color` (String) Color of the environment group.
- `org_id` (String) org_id of the environment group.
- `project_id` (String) project_id of the environment group.

### Read-Only

Expand Down
8 changes: 7 additions & 1 deletion docs/resources/platform_environment_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ resource "harness_platform_environment_group" "example" {
Import is supported using the following syntax:

```shell
# Import using the environment group id.
# Import account level environment group.
terraform import harness_platform_environment_group.example <environment_group_id>

# Import org level environment group.
terraform import harness_platform_environment_group.example <org_id>/<environment_group_id>

# Import project level environment group.
terraform import harness_platform_environment_group.example <org_id>/<project_id>/<environment_group_id>
```
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# Import using the environment group id.
# Import account level environment group.
terraform import harness_platform_environment_group.example <environment_group_id>

# Import org level environment group.
terraform import harness_platform_environment_group.example <org_id>/<environment_group_id>

# Import project level environment group.
terraform import harness_platform_environment_group.example <org_id>/<project_id>/<environment_group_id>
11 changes: 4 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ go 1.18
require (
github.com/antihax/optional v1.0.0
github.com/docker/docker v24.0.5+incompatible
github.com/harness/harness-go-sdk v0.3.43
github.com/harness/harness-go-sdk v0.3.44
github.com/harness/harness-openapi-go-client v0.0.17
github.com/hashicorp/terraform-plugin-sdk/v2 v2.27.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.1
)

require (
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/go-retryablehttp v0.7.4
github.com/hashicorp/terraform-plugin-sdk/v2 v2.27.0
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.1
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/harness/harness-go-sdk v0.3.43 h1:kzCAO06zjfecAwh9khggsNQ2L/8YsNEPPb6BYkUADYw=
github.com/harness/harness-go-sdk v0.3.43/go.mod h1:CPXydorp4zd5Dz2u2FXiHyWL4yd5PQafOMN69cgPSvk=
github.com/harness/harness-go-sdk v0.3.44 h1:DMcLBqtRiuQGeBT/cqx6jax4oe0NrDJ9Fbjspz7NNdM=
github.com/harness/harness-go-sdk v0.3.44/go.mod h1:CPXydorp4zd5Dz2u2FXiHyWL4yd5PQafOMN69cgPSvk=
github.com/harness/harness-openapi-go-client v0.0.17 h1:EZneIyi6sV+dlTgXbawxdVD0OoDmG3mnGHEJbwslRzc=
github.com/harness/harness-openapi-go-client v0.0.17/go.mod h1:u0vqYb994BJGotmEwJevF4L3BNAdU9i8ui2d22gmLPA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ func DataSourceEnvironmentGroup() *schema.Resource {
"org_id": {
Description: "org_id of the environment group.",
Type: schema.TypeString,
Required: true,
Optional: true,
},
"project_id": {
Description: "project_id of the environment group.",
Type: schema.TypeString,
Required: true,
Optional: true,
},
"color": {
Description: "Color of the environment group.",
Expand All @@ -57,20 +57,19 @@ func dataSourceEnvironmentGroupRead(ctx context.Context, d *schema.ResourceData,
var err error
var env *nextgen.EnvironmentGroupResponse
var httpResp *http.Response
var resp nextgen.ResponseDtoEnvironmentGroup

id := d.Get("identifier").(string)

if id != "" {
var resp nextgen.ResponseDtoEnvironmentGroup

orgIdentifier := (d.Get("org_id").(string))
projectIdentifier := (d.Get("project_id").(string))

resp, httpResp, err = c.EnvironmentGroupApi.GetEnvironmentGroup(ctx, d.Get("identifier").(string), c.AccountId, orgIdentifier, projectIdentifier, &nextgen.EnvironmentGroupApiGetEnvironmentGroupOpts{
Branch: helpers.BuildField(d, "branch"),
RepoIdentifier: helpers.BuildField(d, "repo_id"),
resp, httpResp, err = c.EnvironmentGroupApi.GetEnvironmentGroup(ctx, d.Get("identifier").(string), c.AccountId, &nextgen.EnvironmentGroupApiGetEnvironmentGroupOpts{
Branch: helpers.BuildField(d, "branch"),
RepoIdentifier: helpers.BuildField(d, "repo_id"),
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
})
env = resp.Data.EnvGroup

} else {
return diag.FromErr(errors.New("identifier must be specified"))
}
Expand All @@ -79,6 +78,8 @@ func dataSourceEnvironmentGroupRead(ctx context.Context, d *schema.ResourceData,
return helpers.HandleApiError(err, d, httpResp)
}

env = resp.Data.EnvGroup

// Soft delete lookup error handling
// https://harness.atlassian.net/browse/PL-23765
if env == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,49 @@ func TestAccDataSourceEnvironmentGroup(t *testing.T) {
})
}

func TestAccDataSourceEnvironmentGroupOrgLevel(t *testing.T) {

id := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(6))
name := id
resourceName := "data.harness_platform_environment_group.test"

resource.UnitTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProviderFactories: acctest.ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceEnvironmentGroupOrgLevel(id, name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "identifier", id),
resource.TestCheckResourceAttr(resourceName, "org_id", id),
resource.TestCheckResourceAttr(resourceName, "color", "#0063F7"),
),
},
},
})
}

func TestAccDataSourceEnvironmentGroupAccountLevel(t *testing.T) {

id := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(6))
name := id
resourceName := "data.harness_platform_environment_group.test"

resource.UnitTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProviderFactories: acctest.ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceEnvironmentGroupOrgLevel(id, name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "identifier", id),
resource.TestCheckResourceAttr(resourceName, "color", "#0063F7"),
),
},
},
})
}

func testAccDataSourceEnvironmentGroup(id string, name string) string {
return fmt.Sprintf(`
resource "harness_platform_organization" "test" {
Expand Down Expand Up @@ -69,3 +112,51 @@ func testAccDataSourceEnvironmentGroup(id string, name string) string {
}
`, id, name)
}

func testAccDataSourceEnvironmentGroupOrgLevel(id string, name string) string {
return fmt.Sprintf(`
resource "harness_platform_organization" "test" {
identifier = "%[1]s"
name = "%[2]s"
}
resource "harness_platform_environment_group" "test" {
identifier = "%[1]s"
org_id = harness_platform_organization.test.id
color = "#0063F7"
yaml = <<-EOT
environmentGroup:
name: "%[1]s"
identifier: "%[1]s"
description: "temp"
orgIdentifier: ${harness_platform_organization.test.id}
envIdentifiers: []
EOT
}
data "harness_platform_environment_group" "test" {
identifier = "%[1]s"
org_id = harness_platform_environment_group.test.id
}
`, id, name)
}

func testAccDataSourceEnvironmentGroupAccountLevel(id string, name string) string {
return fmt.Sprintf(`
resource "harness_platform_environment_group" "test" {
identifier = "%[1]s"
color = "#0063F7"
yaml = <<-EOT
environmentGroup:
name: "%[1]s"
identifier: "%[1]s"
description: "temp"
envIdentifiers: []
EOT
}
data "harness_platform_environment_group" "test" {
identifier = "%[1]s"
}
`, id, name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func ResourceEnvironmentGroup() *schema.Resource {
UpdateContext: resourceEnvironmentGroupCreateOrUpdate,
DeleteContext: resourceEnvironmentGroupDelete,
CreateContext: resourceEnvironmentGroupCreateOrUpdate,
Importer: helpers.ProjectResourceImporter,
Importer: helpers.MultiLevelResourceImporter,

Schema: map[string]*schema.Schema{
"identifier": {
Expand Down Expand Up @@ -67,28 +67,25 @@ func resourceEnvironmentGroupRead(ctx context.Context, d *schema.ResourceData, m
var err error
var envGroup *nextgen.EnvironmentGroupResponse
var httpResp *http.Response
var resp nextgen.ResponseDtoEnvironmentGroup

id := d.Get("identifier").(string)

if id != "" {
var resp nextgen.ResponseDtoEnvironmentGroup

orgIdentifier := (d.Get("org_id").(string))
projectIdentifier := (d.Get("project_id").(string))

resp, httpResp, err = c.EnvironmentGroupApi.GetEnvironmentGroup(ctx, d.Get("identifier").(string), c.AccountId, orgIdentifier, projectIdentifier, &nextgen.EnvironmentGroupApiGetEnvironmentGroupOpts{
Branch: helpers.BuildField(d, "branch"),
RepoIdentifier: helpers.BuildField(d, "repo_id"),
resp, httpResp, err = c.EnvironmentGroupApi.GetEnvironmentGroup(ctx, d.Get("identifier").(string), c.AccountId, &nextgen.EnvironmentGroupApiGetEnvironmentGroupOpts{
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
Branch: helpers.BuildField(d, "branch"),
RepoIdentifier: helpers.BuildField(d, "repo_id"),
})
envGroup = resp.Data.EnvGroup
} else {
return diag.FromErr(errors.New("identifier must be specified"))
}

if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}

envGroup = resp.Data.EnvGroup
// Soft delete lookup error handling
// https://harness.atlassian.net/browse/PL-23765
if envGroup == nil {
Expand Down Expand Up @@ -132,13 +129,12 @@ func resourceEnvironmentGroupCreateOrUpdate(ctx context.Context, d *schema.Resou
func resourceEnvironmentGroupDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)

orgIdentifier := (d.Get("org_id").(string))
projectIdentifier := (d.Get("project_id").(string))

_, httpResp, err := c.EnvironmentGroupApi.DeleteEnvironmentGroup(ctx, d.Id(), c.AccountId, orgIdentifier, projectIdentifier, &nextgen.EnvironmentGroupApiDeleteEnvironmentGroupOpts{
Branch: helpers.BuildField(d, "branch"),
RepoIdentifier: helpers.BuildField(d, "repo_id"),
ForceDelete: helpers.BuildFieldForBoolean(d, "force_delete"),
_, httpResp, err := c.EnvironmentGroupApi.DeleteEnvironmentGroup(ctx, d.Id(), c.AccountId, &nextgen.EnvironmentGroupApiDeleteEnvironmentGroupOpts{
Branch: helpers.BuildField(d, "branch"),
RepoIdentifier: helpers.BuildField(d, "repo_id"),
ForceDelete: helpers.BuildFieldForBoolean(d, "force_delete"),
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
})

if err != nil {
Expand Down
Loading

0 comments on commit 8d54aa5

Please sign in to comment.