Skip to content

Commit

Permalink
chore: [CHAOS-6992]: Added support of chaos infrastructure (#1138)
Browse files Browse the repository at this point in the history
* chore: [CHAOS-6992]: Added support of chaos infrastructure

Signed-off-by: Saranya-jena <[email protected]>

* chore: [CHAOS-6992]: resolved comments

Signed-off-by: Saranya-jena <[email protected]>

* chore: [CHAOS-6992]: fixed imports

Signed-off-by: Saranya-jena <[email protected]>

---------

Signed-off-by: Saranya-jena <[email protected]>
  • Loading branch information
Saranya-jena authored Jan 9, 2025
1 parent 437a79a commit 43d3f98
Show file tree
Hide file tree
Showing 9 changed files with 488 additions and 1 deletion.
37 changes: 37 additions & 0 deletions docs/data-sources/chaos_infrastructure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "harness_chaos_infrastructure Data Source - terraform-provider-harness"
subcategory: "Next Gen"
description: |-
Data source for retrieving a chaos infrastructure.
---

# harness_chaos_infrastructure (Data Source)

Data source for retrieving a chaos infrastructure.

## Example Usage

```terraform
data "harness_chaos_infrastructure" "example" {
identifier = "identifier"
org_id = "org_id"
project_id = "project_id"
environment_id = "env_id"
}
```

<!-- schema generated by tfplugindocs -->

## Schema

### Required

- `environment_id` (String) Environment identifier of the chaos infrastructure.
- `identifier` (String) Identifier of the chaos infrastructure.
- `org_id` (String) Identifier of the organization in which the chaos infrastructure is configured.
- `project_id` (String) Identifier of the project in which the chaos infrastructure is configured.

### Read-Only

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

# harness_chaos_infrastructure (Resource)

Resource for creating a Chaos Infrastructure.

### References:

- For details on how to onboard with Terraform, please see [Harness Terraform Provider Overview](https://developer.harness.io/docs/platform/automation/terraform/harness-terraform-provider-overview/)

## Example to create Chaos Infrastructure

```terraform
resource "harness_chaos_infrastructure" "example" {
identifier = "identifier"
name = "name"
org_id = "org_id"
project_id = "project_id"
environment_id = "env_id"
namespace = "namespace"
service_account = "service_acc_name"
}
```

<!-- schema generated by tfplugindocs -->

## Schema

### Required

- `org_id` (String) Unique identifier of the organization.
- `project_id` (String) Unique identifier of the project.
- `environment_id` (String) Environment ID of the chaos infrastructure.
- `identifier` (String) Unique identifier of the resource.
- `name` (String) Name of the resource.
- `namespace` (String) Namespace of the chaos infrastructure.
- `service_account` (String) Service Account of the chaos infrastructure.

### Optional

- `description` (String) Description of the resource.
- `tags` (Set of String) Tags to associate with the resource.

### Read-Only

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

## Import

Import is supported using the following syntax:

```shell
# Import using the Chaos Infra ID.
terraform import harness_chaos_infrastructure.example <chaos_infra_id>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
data "harness_chaos_infrastructure" "example" {
identifier = "identifier"
org_id = "org_id"
project_id = "project_id"
environment_id= "env_id"
}
2 changes: 2 additions & 0 deletions examples/resources/harness_chaos_infrastructure/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Import using the Harness chaos_infra_id.
terraform import harness_chaos_infrastructure.example <chaos_infra_id>
11 changes: 11 additions & 0 deletions examples/resources/harness_chaos_infrastructure/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

# Sample resource for chaos infrastructure
resource "harness_chaos_infrastructure" "example" {
identifier = "identifier"
name = "name"
org_id = "org_id"
project_id = "project_id"
environment_id = "env_id"
namespace = "namespace"
service_account = "service_acc_name"
}
16 changes: 16 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"
"log"

"github.com/harness/harness-go-sdk/harness/chaos"
cdng_service "github.com/harness/terraform-provider-harness/internal/service/cd_nextgen/service"
"github.com/harness/terraform-provider-harness/internal/service/chaos/infrastructure"
"github.com/harness/terraform-provider-harness/internal/service/platform/module_registry"
"github.com/harness/terraform-provider-harness/internal/service/platform/service_account"

Expand Down Expand Up @@ -297,6 +299,7 @@ func Provider(version string) func() *schema.Provider {
"harness_governance_rule_set": governance_rule_set.DatasourceRuleSet(),
"harness_cluster_orchestrator": cluster_orchestrator.DataSourceClusterOrchestrator(),
"harness_platform_infra_module": module_registry.DataSourceInfraModule(),
"harness_chaos_infrastructure": infrastructure.DataSourceChaosInfrastructureService(),
},
ResourcesMap: map[string]*schema.Resource{
"harness_platform_template": pipeline_template.ResourceTemplate(),
Expand Down Expand Up @@ -446,6 +449,7 @@ func Provider(version string) func() *schema.Provider {
"harness_governance_rule_set": governance_rule_set.ResourceRuleSet(),
"harness_cluster_orchestrator": cluster_orchestrator.ResourceClusterOrchestrator(),
"harness_platform_infra_module": module_registry.ResourceInfraModule(),
"harness_chaos_infrastructure": infrastructure.ResourceChaosInfrastructure(),
},
}

Expand Down Expand Up @@ -540,6 +544,17 @@ func getCodeClient(d *schema.ResourceData, version string) *code.APIClient {
return client
}

func getChaosClient(d *schema.ResourceData, version string) *chaos.APIClient {
client := chaos.NewAPIClient(&chaos.Configuration{
AccountId: d.Get("account_id").(string),
BasePath: d.Get("endpoint").(string) + "/chaos/manager/api", // check if this can be taken from sdk
ApiKey: d.Get("platform_api_key").(string),
UserAgent: fmt.Sprintf("terraform-provider-harness-platform-%s", version),
DefaultHeader: map[string]string{"X-Api-Key": d.Get("platform_api_key").(string)},
})
return client
}

// Setup the client for interacting with the Harness API
func configure(version string, p *schema.Provider) func(context.Context, *schema.ResourceData) (interface{}, diag.Diagnostics) {
return func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
Expand All @@ -551,6 +566,7 @@ func configure(version string, p *schema.Provider) func(context.Context, *schema
Client: getClient(d, version),
CodeClient: getCodeClient(d, version),
DBOpsClient: getDBOpsClient(d, version),
ChaosClient: getChaosClient(d, version),
}, nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package infrastructure

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 DataSourceChaosInfrastructureService() *schema.Resource {
resource := &schema.Resource{
Description: "Data source for retrieving a chaos infrastructure.",

ReadContext: dataSourceChaosInfrastructureRead,

Schema: map[string]*schema.Schema{
"org_id": {
Description: "Identifier of the organization in which the chaos infrastructure is configured.",
Type: schema.TypeString,
Required: true,
},
"project_id": {
Description: "Identifier of the project in which the chaos infrastructure is configured.",
Type: schema.TypeString,
Required: true,
},
"identifier": {
Description: "Identifier of the chaos infrastructure.",
Type: schema.TypeString,
Required: true,
},
"environment_id": {
Description: "Environment identifier of the chaos infrastructure.",
Type: schema.TypeString,
Required: true,
},
},
}

return resource
}

func dataSourceChaosInfrastructureRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetChaosClientWithContext(ctx)
var accountIdentifier, orgIdentifier, projectIdentifier, identifier, envIdentifier string
accountIdentifier = c.AccountId
if attr, ok := d.GetOk("org_id"); ok {
orgIdentifier = attr.(string)
}
if attr, ok := d.GetOk("project_id"); ok {
projectIdentifier = attr.(string)
}
if attr, ok := d.GetOk("identifier"); ok {
identifier = attr.(string)
}
if attr, ok := d.GetOk("environment_id"); ok {
envIdentifier = attr.(string)
}
resp, httpResp, err := c.ChaosSdkApi.GetInfraV2(ctx, identifier, accountIdentifier, orgIdentifier, projectIdentifier, envIdentifier)

if err != nil {
if err.Error() == "404 Not Found" {
d.SetId("")
d.MarkNewResource()
return nil
}
return helpers.HandleReadApiError(err, d, httpResp)
}
readChaosInfrastructure(d, resp)

return nil
}
Loading

0 comments on commit 43d3f98

Please sign in to comment.