Skip to content

Commit

Permalink
feat: [CDS-76833]: GitOps flux agent (#662)
Browse files Browse the repository at this point in the history
* feat: [CDS-76833]: GitOps flux agent

* adding changelog

* updating the go mod with latest sdk version

* updating the schema based on the new name of the field

* updating the docs

* fixing the go.sum

* updating go mod

* updating go mod

* reverting go mod

* reverting to main for go sum

* updating the test and separating the create and update request creation

* updating the name

---------

Co-authored-by: Shivakumar Ningappa <shivakumar.ningappa@harness.io>
  • Loading branch information
manavjot-harness and shivagowda committed Sep 27, 2023
1 parent 689de5a commit 13f591b
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .changelog/660.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
```release-note:new-resource
resource_feature_flag_target - Added feature flag target resources to the Harness Terraform Provider.
```
```
3 changes: 3 additions & 0 deletions .changelog/674.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/harness_platform_gitops_agent: add support for new flux operator.
```
1 change: 1 addition & 0 deletions docs/data-sources/platform_gitops_agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/platform_gitops_agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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" {
Expand Down
17 changes: 16 additions & 1 deletion internal/service/platform/gitops/agent/resource_gitops_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
122 changes: 122 additions & 0 deletions internal/service/platform/gitops/agent/resource_gitops_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -135,6 +211,7 @@ func testAccResourceGitopsAgentAccountLevel(agentId string, accountId string, ag
namespace = "%[4]s"
high_availability = false
}
operator = "ARGO"
}
`, agentId, accountId, agentName, namespace)
}
Expand All @@ -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)
}

0 comments on commit 13f591b

Please sign in to comment.