diff --git a/.changelog/660.txt b/.changelog/660.txt index b4d70a22a..ef022531b 100644 --- a/.changelog/660.txt +++ b/.changelog/660.txt @@ -1,3 +1,3 @@ ```release-note:new-resource resource_feature_flag_target - Added feature flag target resources to the Harness Terraform Provider. -``` \ No newline at end of file +``` diff --git a/.changelog/674.txt b/.changelog/674.txt new file mode 100644 index 000000000..58c3bb4ff --- /dev/null +++ b/.changelog/674.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/harness_platform_gitops_agent: add support for new flux operator. +``` \ No newline at end of file diff --git a/docs/data-sources/platform_gitops_agent.md b/docs/data-sources/platform_gitops_agent.md index c21278f3e..6091761d3 100644 --- a/docs/data-sources/platform_gitops_agent.md +++ b/docs/data-sources/platform_gitops_agent.md @@ -38,6 +38,7 @@ data "harness_platform_gitops_agent" "example" { - `agent_token` (String) Agent token to be used for authentication of the agent with Harness. - `description` (String) Description of the GitOps agent. +- `operator` (String) Operator to use for the Harness GitOps agent. Enum: "ARGO" "FLAMINGO" - `id` (String) The ID of this resource. - `metadata` (List of Object) Metadata of the agent. (see [below for nested schema](#nestedatt--metadata)) - `name` (String) Name of the GitOps agent. diff --git a/docs/resources/platform_gitops_agent.md b/docs/resources/platform_gitops_agent.md index 40ea13cf6..957877afd 100644 --- a/docs/resources/platform_gitops_agent.md +++ b/docs/resources/platform_gitops_agent.md @@ -41,6 +41,7 @@ Enum: "AGENT_TYPE_UNSET" "CONNECTED_ARGO_PROVIDER" "MANAGED_ARGO_PROVIDER" ### Optional - `description` (String) Description of the GitOps agent. +- `operator` (String) Operator to use for the Harness GitOps agent. Enum: "ARGO" "FLAMINGO" - `metadata` (Block List) Metadata of the agent. (see [below for nested schema](#nestedblock--metadata)) - `org_id` (String) Organization identifier of the GitOps agent. - `project_id` (String) Project identifier of the GitOps agent. diff --git a/internal/service/platform/gitops/agent/data_source_gitops_agent.go b/internal/service/platform/gitops/agent/data_source_gitops_agent.go index a9028e209..0224f0219 100644 --- a/internal/service/platform/gitops/agent/data_source_gitops_agent.go +++ b/internal/service/platform/gitops/agent/data_source_gitops_agent.go @@ -86,6 +86,11 @@ func DataSourceGitopsAgent() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "operator": { + Description: "The Operator to use for the Harness GitOps agent. Enum: \"ARGO\" \"FLAMINGO\"", + Type: schema.TypeString, + Computed: true, + }, }, } return resource diff --git a/internal/service/platform/gitops/agent/data_source_gitops_agent_test.go b/internal/service/platform/gitops/agent/data_source_gitops_agent_test.go index 811d3c1c6..7d8c52001 100644 --- a/internal/service/platform/gitops/agent/data_source_gitops_agent_test.go +++ b/internal/service/platform/gitops/agent/data_source_gitops_agent_test.go @@ -36,6 +36,31 @@ func TestAccDataSourceGitopsAgent(t *testing.T) { } +// FLAMINGO +func TestAccDataSourceGitopsAgentFlamingo(t *testing.T) { + id := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(5)) + id = strings.ReplaceAll(id, "_", "") + name := id + agentId := id + accountId := os.Getenv("HARNESS_ACCOUNT_ID") + resourceName := "data.harness_platform_gitops_agent.test" + resource.UnitTest(t, resource.TestCase{ + PreCheck: func() { acctest.TestAccPreCheck(t) }, + ProviderFactories: acctest.ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGitopsAgentFlamingo(agentId, name, accountId, agentId), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "identifier", id), + resource.TestCheckResourceAttr(resourceName, "org_id", id), + resource.TestCheckResourceAttr(resourceName, "project_id", id), + ), + }, + }, + }) + +} + func testAccDataSourceGitopsAgent(agentId string, name string, accountId string, agentName string) string { return fmt.Sprintf(` resource "harness_platform_organization" "test" { @@ -59,6 +84,43 @@ func testAccDataSourceGitopsAgent(agentId string, name string, accountId string, namespace = "terraform-test" high_availability = false } + operator = "ARGO" + } + + data "harness_platform_gitops_agent" "test" { + depends_on = [harness_platform_gitops_agent.test] + identifier = harness_platform_gitops_agent.test.id + account_id = "%[3]s" + project_id = harness_platform_project.test.id + org_id = harness_platform_organization.test.id + } + `, agentId, name, accountId, agentName) +} + +func testAccDataSourceGitopsAgentFlamingo(agentId string, name string, accountId string, agentName string) string { + return fmt.Sprintf(` + resource "harness_platform_organization" "test" { + identifier = "%[1]s" + name = "%[2]s" + } + + resource "harness_platform_project" "test" { + identifier = "%[1]s" + name = "%[2]s" + org_id = harness_platform_organization.test.id + } + resource "harness_platform_gitops_agent" "test" { + identifier = "%[1]s" + account_id = "%[3]s" + project_id = harness_platform_project.test.id + org_id = harness_platform_organization.test.id + name = "%[4]s" + type = "MANAGED_ARGO_PROVIDER" + metadata { + namespace = "terraform-test" + high_availability = false + } + operator = "FLAMINGO" } data "harness_platform_gitops_agent" "test" { diff --git a/internal/service/platform/gitops/agent/resource_gitops_agent.go b/internal/service/platform/gitops/agent/resource_gitops_agent.go index 93ae3f74f..8f0695a61 100644 --- a/internal/service/platform/gitops/agent/resource_gitops_agent.go +++ b/internal/service/platform/gitops/agent/resource_gitops_agent.go @@ -90,6 +90,11 @@ func ResourceGitopsAgent() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "operator": { + Description: "The Operator to use for the Harness GitOps agent. Enum: \"ARGO\" \"FLAMINGO\"", + Type: schema.TypeString, + Optional: true, + }, }, } return resource @@ -98,7 +103,7 @@ func ResourceGitopsAgent() *schema.Resource { func resourceGitopsAgentCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx) ctx = context.WithValue(ctx, nextgen.ContextAccessToken, hh.EnvVars.BearerToken.Get()) - createAgentRequest := buildCreateUpdateAgentRequest(d) + createAgentRequest := buildCreateAgentRequest(d) createAgentRequest.AccountIdentifier = c.AccountId resp, httpResp, err := c.AgentApi.AgentServiceForServerCreate(ctx, *createAgentRequest) @@ -228,6 +233,15 @@ func buildCreateUpdateAgentRequest(d *schema.ResourceData) *nextgen.V1Agent { return &v1Agent } +func buildCreateAgentRequest(d *schema.ResourceData) *nextgen.V1Agent { + v1Agent := buildCreateUpdateAgentRequest(d) + if attr, ok := d.GetOk("operator"); ok { + agentOperator := nextgen.V1AgentOperator(attr.(string)) + v1Agent.Operator = &agentOperator + } + return v1Agent +} + func readAgent(d *schema.ResourceData, agent *nextgen.V1Agent) { d.SetId(agent.Identifier) d.Set("identifier", agent.Identifier) @@ -237,6 +251,7 @@ func readAgent(d *schema.ResourceData, agent *nextgen.V1Agent) { d.Set("org_id", agent.OrgIdentifier) d.Set("type", agent.Type_) d.Set("project_id", agent.ProjectIdentifier) + d.Set("operator", agent.Operator) metadata := []interface{}{} metaDataMap := map[string]interface{}{} metaDataMap["namespace"] = agent.Metadata.Namespace diff --git a/internal/service/platform/gitops/agent/resource_gitops_agent_test.go b/internal/service/platform/gitops/agent/resource_gitops_agent_test.go index fcd6941a9..19b49f746 100644 --- a/internal/service/platform/gitops/agent/resource_gitops_agent_test.go +++ b/internal/service/platform/gitops/agent/resource_gitops_agent_test.go @@ -91,6 +91,82 @@ func TestAccResourceGitopsAgent(t *testing.T) { }) } +// FLAMINGO or FLUX +func TestAccResourceGitopsAgentFlamingo(t *testing.T) { + // Account Level + id := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(5)) + id = strings.ReplaceAll(id, "_", "") + accountId := os.Getenv("HARNESS_ACCOUNT_ID") + resourceName := "harness_platform_gitops_agent.test" + agentName := id + namespace := "terraform-test" + updatedNamespace := namespace + "-updated" + resource.UnitTest(t, resource.TestCase{ + PreCheck: func() { acctest.TestAccPreCheck(t) }, + ProviderFactories: acctest.ProviderFactories, + CheckDestroy: testAccResourceGitopsAgentDestroy(resourceName), + Steps: []resource.TestStep{ + { + Config: testAccResourceGitopsFluxAgentAccountLevel(id, accountId, agentName, namespace), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "name", agentName), + resource.TestCheckResourceAttrSet(resourceName, "agent_token"), + ), + }, + { + Config: testAccResourceGitopsFluxAgentAccountLevel(id, accountId, agentName, updatedNamespace), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "metadata.0.namespace", updatedNamespace), + resource.TestCheckResourceAttrSet(resourceName, "agent_token"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"account_id", "agent_token"}, + ImportStateIdFunc: acctest.ProjectResourceImportStateIdFunc(resourceName), + }, + }, + }) + + //Project level + id = fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(5)) + id = strings.ReplaceAll(id, "_", "") + resourceName = "harness_platform_gitops_agent.test" + agentName = id + namespace = "terraform-test" + updatedNamespace = namespace + "-updated" + resource.UnitTest(t, resource.TestCase{ + PreCheck: func() { acctest.TestAccPreCheck(t) }, + ProviderFactories: acctest.ProviderFactories, + CheckDestroy: testAccResourceGitopsAgentDestroy(resourceName), + Steps: []resource.TestStep{ + { + Config: testAccResourceGitopsAgentFluxProjectLevel(id, accountId, agentName, namespace), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "name", agentName), + resource.TestCheckResourceAttrSet(resourceName, "agent_token"), + ), + }, + { + Config: testAccResourceGitopsAgentFluxProjectLevel(id, accountId, agentName, updatedNamespace), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "metadata.0.namespace", updatedNamespace), + resource.TestCheckResourceAttrSet(resourceName, "agent_token"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"account_id", "agent_token"}, + ImportStateIdFunc: acctest.ProjectResourceImportStateIdFunc(resourceName), + }, + }, + }) +} + func testAccGetAgent(resourceName string, state *terraform.State) (*nextgen.V1Agent, error) { r := acctest.TestAccGetResource(resourceName, state) c, ctx := acctest.TestAccGetPlatformClientWithContext() @@ -135,6 +211,7 @@ func testAccResourceGitopsAgentAccountLevel(agentId string, accountId string, ag namespace = "%[4]s" high_availability = false } + operator = "ARGO" } `, agentId, accountId, agentName, namespace) } @@ -161,6 +238,51 @@ func testAccResourceGitopsAgentProjectLevel(agentId string, accountId string, ag namespace = "%[4]s" high_availability = false } + operator = "ARGO" + } + `, agentId, accountId, agentName, namespace) +} + +func testAccResourceGitopsFluxAgentAccountLevel(agentId string, accountId string, agentName string, namespace string) string { + return fmt.Sprintf(` + resource "harness_platform_gitops_agent" "test" { + identifier = "%[1]s" + account_id = "%[2]s" + name = "%[3]s" + type = "MANAGED_ARGO_PROVIDER" + metadata { + namespace = "%[4]s" + high_availability = false + } + operator = "FLAMINGO" + } + `, agentId, accountId, agentName, namespace) +} + +func testAccResourceGitopsAgentFluxProjectLevel(agentId string, accountId string, agentName string, namespace string) string { + return fmt.Sprintf(` + resource "harness_platform_organization" "test" { + identifier = "%[1]s" + name = "%[3]s" + } + + resource "harness_platform_project" "test" { + identifier = "%[1]s" + name = "%[3]s" + org_id = harness_platform_organization.test.id + } + resource "harness_platform_gitops_agent" "test" { + identifier = "%[1]s" + account_id = "%[2]s" + project_id = harness_platform_project.test.id + org_id = harness_platform_organization.test.id + name = "%[3]s" + type = "MANAGED_ARGO_PROVIDER" + metadata { + namespace = "%[4]s" + high_availability = false + } + operator = "FLAMINGO" } `, agentId, accountId, agentName, namespace) }