Skip to content

Commit

Permalink
fix: [CDS-99254]: Fix import flow for app project mapping (#1028)
Browse files Browse the repository at this point in the history
* fix: [CDS-99254]: Fix import flow for app project mapping

Signed-off-by: Mirko Teodorovic <[email protected]>

* tests

Signed-off-by: Mirko Teodorovic <[email protected]>

* fix test

Signed-off-by: Mirko Teodorovic <[email protected]>

* fix test

Signed-off-by: Mirko Teodorovic <[email protected]>

* adding some comments and docs

Signed-off-by: Mirko Teodorovic <[email protected]>

* update go.mod

Signed-off-by: Mirko Teodorovic <[email protected]>

* changelog

Signed-off-by: Mirko Teodorovic <[email protected]>

* update go.sum

Signed-off-by: Mirko Teodorovic <[email protected]>

---------

Signed-off-by: Mirko Teodorovic <[email protected]>
Co-authored-by: Alexandru Casian <[email protected]>
  • Loading branch information
mteodor and acasian-harness authored Aug 9, 2024
1 parent 9098a17 commit 600901f
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .changelog/1020.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:fix
resource/harness_platform_gitops_app_project_mapping fix import flow
```
2 changes: 1 addition & 1 deletion docs/data-sources/platform_gitops_app_project_mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ Resource for managing the Harness GitOps Application Project Mappings.

```terraform
data "harness_platform_gitops_app_project_mapping" "example" {
identifier = "identifier"
account_id = "account_id"
org_id = "organization_id"
project_id = "project_id"
agent_id = "agent_id"
argo_proj_name = "argo_proj_name"
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
data "harness_platform_gitops_app_project_mapping" "example" {
identifier = "identifier"
account_id = "account_id"
org_id = "organization_id"
project_id = "project_id"
agent_id = "agent_id"
argo_proj_name = "argo_proj_name"
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Import a GitOps agent app project mapping
terraform import harness_platform_gitops_app_project_mapping.example <organization_id>/<project_id>/<agent_id>/<appproject_id>
terraform import harness_platform_gitops_app_project_mapping.example <organization_id>/<project_id>/<agent_id>/<appproject_name>
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/antihax/optional v1.0.0
github.com/aws/aws-sdk-go v1.46.4
github.com/docker/docker v24.0.5+incompatible
github.com/harness/harness-go-sdk v0.4.0
github.com/harness/harness-go-sdk v0.4.1
github.com/harness/harness-openapi-go-client v0.0.21
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,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.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/harness/harness-go-sdk v0.4.0 h1:+3VTKMNkXEeg35771ybcFqZDK/vgLQsqd7XaFWIRzrE=
github.com/harness/harness-go-sdk v0.4.0/go.mod h1:a/1HYTgVEuNEoh3Z3IsOHZdlUNxl94KcX57ZSNVGll0=
github.com/harness/harness-go-sdk v0.4.1 h1:T42mXbfJDkZrroY79pzN8MolDcDhvHje0EpgeRjv+pY=
github.com/harness/harness-go-sdk v0.4.1/go.mod h1:a/1HYTgVEuNEoh3Z3IsOHZdlUNxl94KcX57ZSNVGll0=
github.com/harness/harness-openapi-go-client v0.0.21 h1:VtJnpQKZvCAlaCmUPbNR69OT3c5WRdhNN5TOgUwtwZ4=
github.com/harness/harness-openapi-go-client v0.0.21/go.mod h1:u0vqYb994BJGotmEwJevF4L3BNAdU9i8ui2d22gmLPA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down
23 changes: 23 additions & 0 deletions helpers/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,29 @@ var GitopsAgentResourceImporter = &schema.ResourceImporter{
},
}

// GitopsAppProjectMappingImporter defines the importer configuration for app project mapping.
// The id used for the import should be in the format <org_id>/<project_id>/<identifier>/<argo_project_name>\
// It is used always at project level.
var GitopsAppProjectMappingImporter = &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")

// Approject mapping always has 4 parts
if len(parts) == 4 { //Project level
d.Set("org_id", parts[0])
d.Set("project_id", parts[1])
d.Set("agent_id", parts[2])
d.Set("argo_project_name", parts[3])
// During import we are using argo_project_name as identifier not the actual identifier which is mongo id
// that way we are not fetching mapping by mongo id but by argo_project_name, agent_id, account_id, org_id and project_id.
d.SetId(parts[3])
return []*schema.ResourceData{d}, nil
}

return nil, fmt.Errorf("invalid identifier: %s", d.Id())
},
}

// GitopsAgentResourceImporter defines the importer configuration for all project level gitops agent resources.
// The id used for the import should be in the format <agent_id>/<query_name>
var GitopsAgentProjectImporter = &schema.ResourceImporter{
Expand Down
15 changes: 15 additions & 0 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,28 @@ func GitopsAgentProjectLevelResourceImportStateIdFunc(resourceName string) resou
return func(s *terraform.State) (string, error) {
primary := s.RootModule().Resources[resourceName].Primary
id := primary.ID

orgId := primary.Attributes["org_id"]
projId := primary.Attributes["project_id"]
agentId := primary.Attributes["agent_id"]
return fmt.Sprintf("%s/%s/%s/%s", orgId, projId, agentId, id), nil
}
}

// Import of GitopsAppProjectMapping resource is always on project level
// terraform import harness_platform_gitops_app_project_mapping.example org_id/projec_id/scope_prefixed_agent_id/argo_proj_name
func GitopsAppProjectMappingResourceImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
primary := s.RootModule().Resources[resourceName].Primary

orgId := primary.Attributes["org_id"]
projId := primary.Attributes["project_id"]
agentId := primary.Attributes["agent_id"]
argoProjName := primary.Attributes["argo_project_name"]
return fmt.Sprintf("%s/%s/%s/%s", orgId, projId, agentId, argoProjName), nil
}
}

func GitopsAgentOrgLevelResourceImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
primary := s.RootModule().Resources[resourceName].Primary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func DatasourceGitopsAppProjectMapping() *schema.Resource {
Description: "Resource for managing the Harness GitOps Application Project Mappings.",

ReadContext: datasourceGitopsAppProjectMappingRead,
Importer: helpers.ProjectResourceImporter,
Importer: helpers.GitopsAppProjectMappingImporter,

Schema: map[string]*schema.Schema{
"account_id": {
Expand Down Expand Up @@ -59,10 +59,15 @@ func datasourceGitopsAppProjectMappingRead(ctx context.Context, d *schema.Resour
ctx = context.WithValue(ctx, nextgen.ContextAccessToken, hh.EnvVars.BearerToken.Get())
agentIdentifier := d.Get("agent_id").(string)
identifier := d.Get("identifier").(string)
argo_proj_name := d.Get("argo_project_name").(string)
if identifier == argo_proj_name {
identifier = ""
}
resp, httpResp, err := c.ProjectMappingsApi.AppProjectMappingServiceGetAppProjectMappingV2(ctx, agentIdentifier, identifier, &nextgen.ProjectMappingsApiAppProjectMappingServiceGetAppProjectMappingV2Opts{
AccountIdentifier: optional.NewString(c.AccountId),
OrgIdentifier: optional.NewString(d.Get("org_id").(string)),
ProjectIdentifier: optional.NewString(d.Get("project_id").(string)),
ArgoProjectName: optional.NewString(argo_proj_name),
})

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ func testAccDatasourceGitopsAppProjectMapping(agentId string, accountId string,
data "harness_platform_gitops_app_project_mapping" test1 {
depends_on = [harness_platform_gitops_app_project_mapping.test]
identifier = harness_platform_gitops_app_project_mapping.test.identifier
org_id = harness_platform_organization.test.id
account_id = "%[2]s"
project_id = harness_platform_project.test.id
agent_id = "%[1]s"
argo_project_name = "%[3]s"
}
`, agentId, accountId, argoProject)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package app_project

import (
"context"

"github.com/antihax/optional"
hh "github.com/harness/harness-go-sdk/harness/helpers"
"github.com/harness/harness-go-sdk/harness/nextgen"
Expand All @@ -20,7 +19,7 @@ func ResourceGitopsAppProjectMapping() *schema.Resource {
ReadContext: resourceGitopsAppProjectMappingRead,
UpdateContext: resourceGitopsAppProjectMappingUpdate,
DeleteContext: resourceGitopsAppProjectMappingDelete,
Importer: helpers.GitopsAgentResourceImporter,
Importer: helpers.GitopsAppProjectMappingImporter,

Schema: map[string]*schema.Schema{
"account_id": {
Expand Down Expand Up @@ -83,10 +82,18 @@ func resourceGitopsAppProjectMappingRead(ctx context.Context, d *schema.Resource
ctx = context.WithValue(ctx, nextgen.ContextAccessToken, hh.EnvVars.BearerToken.Get())
agentIdentifier := d.Get("agent_id").(string)
identifier := d.Get("identifier").(string)
argo_proj_name := d.Get("argo_project_name").(string)
// During import we are using argo_project_name as identifier not the actual identifier which is mongo id
// So we are not fetching mapping by mongo id but by argo_project_name, agent_id, account_id, org_id and project_id.
// argo_project_name, agent_id, account_id, org_id and project_id uniquely identify mapping.
if identifier == argo_proj_name {
identifier = ""
}
resp, httpResp, err := c.ProjectMappingsApi.AppProjectMappingServiceGetAppProjectMappingV2(ctx, agentIdentifier, identifier, &nextgen.ProjectMappingsApiAppProjectMappingServiceGetAppProjectMappingV2Opts{
AccountIdentifier: optional.NewString(c.AccountId),
OrgIdentifier: optional.NewString(d.Get("org_id").(string)),
ProjectIdentifier: optional.NewString(d.Get("project_id").(string)),
ArgoProjectName: optional.NewString(argo_proj_name),
})

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ func TestAccResourceGitopsAppProjectMapping(t *testing.T) {
// CheckDestroy: testAccResourceGitopsAppProjectMappingDestroy(resourceName, agentId), //commenting this since app project mapping cannot exist without an agent.
Steps: []resource.TestStep{
{
Config: testAccResourceGitopsAppProjectMapping(id, accountId, argoProject),
Config: testAccResourceGitopsAppProjectMapping(id, accountId, argoProject, id),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "argo_project_name", argoProject),
),
},
{
Config: testAccResourceGitopsAppProjectMapping(id, accountId, argoProjectUpdated),
Config: testAccResourceGitopsAppProjectMapping(id, accountId, argoProjectUpdated, id),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "argo_project_name", argoProjectUpdated),
),
Expand All @@ -44,7 +44,7 @@ func TestAccResourceGitopsAppProjectMapping(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: acctest.GitopsAgentProjectLevelResourceImportStateIdFunc(resourceName),
ImportStateIdFunc: acctest.GitopsAppProjectMappingResourceImportStateIdFunc(resourceName),
},
},
})
Expand Down Expand Up @@ -77,16 +77,16 @@ func testAccResourceGitopsAppProjectMappingDestroy(resourceName string, agentId
}
}

func testAccResourceGitopsAppProjectMapping(agentId string, accountId string, argoProject string) string {
func testAccResourceGitopsAppProjectMapping(id string, accountId string, argoProject string, projectId string) string {
return fmt.Sprintf(`
resource "harness_platform_organization" "test" {
identifier = "%[1]s"
name = "%[1]s"
}
resource "harness_platform_project" "test" {
identifier = "%[1]s"
name = "%[1]s"
identifier = "%[4]s"
name = "%[4]s"
org_id = harness_platform_organization.test.id
}
resource "harness_platform_gitops_agent" "test" {
Expand All @@ -105,10 +105,10 @@ func testAccResourceGitopsAppProjectMapping(agentId string, accountId string, ar
resource "harness_platform_gitops_app_project_mapping" "test" {
depends_on = [harness_platform_gitops_agent.test]
account_id = "%[2]s"
org_id = harness_platform_organization.test.id
project_id = harness_platform_project.test.id
org_id = "%[1]s"
project_id = "%[4]s"
agent_id = "%[1]s"
argo_project_name = "%[3]s"
}
`, agentId, accountId, argoProject)
`, id, accountId, argoProject, projectId)
}

0 comments on commit 600901f

Please sign in to comment.