Skip to content

Commit

Permalink
[CCM-19455]: Added Support for Cluster Orchestrator Resource (#1084)
Browse files Browse the repository at this point in the history
* [CCM-19455]: Added Support for Cluster Orchestrator

* updated doc

* added subcategory in doc

* Updated the go sdk

* removed import.sh file, import will be supported in a subsequent version

* added changelog
  • Loading branch information
riyasyash authored Oct 8, 2024
1 parent ca7463d commit 00f0800
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/1084.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
harness_cluster_orchestrator - Added Cluster Orchestrator resource in Harness terraform provider
```
32 changes: 32 additions & 0 deletions docs/data-sources/cluster_orchestrator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "harness_cluster_orchestrator Data Source - terraform-provider-harness"
subcategory: "Next Gen"
description: |-
Data source for retrieving a Harness ClusterOrchestrator.
---

# harness_cluster_orchestrator (Data Source)

Data source for retrieving a Harness ClusterOrchestrator.

## Example Usage

```terraform
data "harness_cluster_orchestrator" "example" {
identifier = "identifier"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `cluster_endpoint` (String) Endpoint of the k8s cluster being onboarded under the orchestrator
- `k8s_connector_id` (String) ID of the Harness Kubernetes Connector Being used
- `name` (String) Name of the Orchestrator

### Read-Only

- `id` (String) The ID of this resource.
43 changes: 43 additions & 0 deletions docs/resources/cluster_orchestrator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "harness_cluster_orchestrator Resource - terraform-provider-harness"
subcategory: "Next Gen"
description: |-
Resource for creating ClusterOrchestrators.
---

# harness_cluster_orchestrator (Resource)

Resource for creating ClusterOrchestrators.

## Example Usage

```terraform
resource "harness_cluster_orchestrator" "test" {
name = "name"
cluster_endpoint = "http://test.test.com"
k8s_connector_id = "test"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `cluster_endpoint` (String) Endpoint of the k8s cluster being onboarded under the orchestrator
- `k8s_connector_id` (String) ID of the Harness Kubernetes Connector Being used
- `name` (String) Name of the Orchestrator

### Read-Only

- `id` (String) The ID of this resource.

## Import

Import is supported using the following syntax:

```shell
# Import using the Harness aws cloud provider id.
terraform import harness_cluster_orchestrator.example <provider_id>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "harness_cluster_orchestrator" "example" {
identifier = "identifier"
}
5 changes: 5 additions & 0 deletions examples/resources/harness_cluster_orchestrator/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "harness_cluster_orchestrator" "test" {
name = "name"
cluster_endpoint = "http://test.test.com"
k8s_connector_id = "test"
}
3 changes: 3 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"

"github.com/harness/terraform-provider-harness/internal/service/platform/cluster_orchestrator"
dbinstance "github.com/harness/terraform-provider-harness/internal/service/platform/db_instance"
dbschema "github.com/harness/terraform-provider-harness/internal/service/platform/db_schema"
"github.com/harness/terraform-provider-harness/internal/service/platform/gitx/webhook"
Expand Down Expand Up @@ -286,6 +287,7 @@ func Provider(version string) func() *schema.Provider {
"harness_governance_rule_enforcement": governance_enforcement.DatasourceRuleEnforcement(),
"harness_governance_rule": governance_rule.DatasourceRule(),
"harness_governance_rule_set": governance_rule_set.DatasourceRuleSet(),
"harness_cluster_orchestrator": cluster_orchestrator.DataSourceClusterOrchestrator(),
},
ResourcesMap: map[string]*schema.Resource{
"harness_platform_template": pl_template.ResourceTemplate(),
Expand Down Expand Up @@ -431,6 +433,7 @@ func Provider(version string) func() *schema.Provider {
"harness_governance_rule_enforcement": governance_enforcement.ResourceRuleEnforcement(),
"harness_governance_rule": governance_rule.ResourceRule(),
"harness_governance_rule_set": governance_rule_set.ResourceRuleSet(),
"harness_cluster_orchestrator": cluster_orchestrator.ResourceClusterOrchestrator(),
},
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cluster_orchestrator

import (
"context"

"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DataSourceClusterOrchestrator() *schema.Resource {
resource := &schema.Resource{
Description: "Data source for retrieving a Harness ClusterOrchestrator.",

ReadContext: resourceClusterOrchestratorRead,

Schema: map[string]*schema.Schema{
"name": {
Description: "Name of the Orchestrator",
Type: schema.TypeString,
Required: true,
},
"cluster_endpoint": {
Description: "Endpoint of the k8s cluster being onboarded under the orchestrator",
Type: schema.TypeString,
Required: true,
},
"k8s_connector_id": {
Description: "ID of the Harness Kubernetes Connector Being used",
Type: schema.TypeString,
Optional: true,
},
},
}

return resource
}

func resourceClusterOrchestratorRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)

Identifier := d.Id()

resp, httpResp, err := c.CloudCostClusterOrchestratorApi.ClusterOrchestratorDetails(ctx, c.AccountId, Identifier)

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

if resp.Response != nil {
setId(d, resp.Response.ID)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cluster_orchestrator

import (
"context"
"net/http"

"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func ResourceClusterOrchestrator() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating ClusterOrchestrators.",

CreateContext: resourceClusterOrchestratorCreate,
UpdateContext: resourceClusterOrchestratorCreate,
ReadContext: resourceClusterOrchestratorCreate,
DeleteContext: resourceClusterOrchestratorDelete,

Schema: map[string]*schema.Schema{
"name": {
Description: "Name of the Orchestrator",
Type: schema.TypeString,
Required: true,
},
"cluster_endpoint": {
Description: "Endpoint of the k8s cluster being onboarded under the orchestrator",
Type: schema.TypeString,
Required: true,
},
"k8s_connector_id": {
Description: "ID of the Harness Kubernetes Connector Being used",
Type: schema.TypeString,
Required: true,
},
},
}

return resource
}

func resourceClusterOrchestratorCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
body := buildClusterOrch(d)
var err error
var resp nextgen.ClusterOrchestratorResponse
var httpResp *http.Response

resp, httpResp, err = c.CloudCostClusterOrchestratorApi.CreateClusterOrchestrator(ctx, c.AccountId, body)

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

if resp.Response != nil {
setId(d, resp.Response.ID)
}

return nil
}

func resourceClusterOrchestratorDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return nil
}
38 changes: 38 additions & 0 deletions internal/service/platform/cluster_orchestrator/clusterorch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cluster_orchestrator_test

import (
"fmt"
"testing"

"github.com/harness/terraform-provider-harness/internal/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestResourceClusterOrchestrator(t *testing.T) {
name := "terraform-clusterorch-test"
resourceName := "harness_cluster_orchestrator.test"

resource.UnitTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProviderFactories: acctest.ProviderFactories,
// CheckDestroy: testRuleDestroy(resourceName),
Steps: []resource.TestStep{
{
Config: testClusterOrch(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", name),
),
},
},
})
}

func testClusterOrch(name string) string {
return fmt.Sprintf(`
resource "harness_cluster_orchestrator" "test" {
name = "%s"
cluster_endpoint = "http://test.com"
k8s_connector_id = "TestDoNotDelete"
}
`, name)
}
32 changes: 32 additions & 0 deletions internal/service/platform/cluster_orchestrator/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cluster_orchestrator

import (
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func buildClusterOrch(d *schema.ResourceData) nextgen.CreateClusterOrchestratorDto {

clusterOrch := &nextgen.CreateClusterOrchestratorDto{}

if attr, ok := d.GetOk("name"); ok {
clusterOrch.Name = attr.(string)
}
if attr, ok := d.GetOk("k8s_connector_id"); ok {
clusterOrch.K8sConnID = attr.(string)
}
userCfg := nextgen.ClusterOrchestratorUserConfig{}

if attr, ok := d.GetOk("cluster_endpoint"); ok {
userCfg.ClusterEndPoint = attr.(string)
}
clusterOrch.UserConfig = userCfg

return *clusterOrch

}

func setId(d *schema.ResourceData, id string) {
d.SetId(id)
d.Set("identifier", id)
}

0 comments on commit 00f0800

Please sign in to comment.