Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: [CHAOS-6992]: Added support of chaos infrastructure #1138

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good to have Next Gen as subcategory

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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good to fix the indentation here

}
```

<!-- 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.
59 changes: 59 additions & 0 deletions docs/resources/chaos_infrastructure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "harness_chaos_infrastructure Resource - terraform-provider-harness"
subcategory: ""
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation fix

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 Harness application id and environment id.
terraform import harness_environment.dev <application_id>/<environment_id>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
terraform import harness_environment.dev <application_id>/<environment_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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation fixes at all places wherever required

namespace= "namespace"
service_account= "service_acc_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.23
github.com/harness/harness-go-sdk v0.4.13-0.20241209130746-db49476ebca0
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.23 h1:iPh1v1sdZ1So1CdtqRkX+YY2hXTApaJsDt0XZWwa+hs=
github.com/harness/harness-go-sdk v0.4.23/go.mod h1:CPXydorp4zd5Dz2u2FXiHyWL4yd5PQafOMN69cgPSvk=
github.com/harness/harness-go-sdk v0.4.13-0.20241209130746-db49476ebca0 h1:Rm8VezYx4AeReBbtwkMCUminb+8+B2GnDsS4GqEb7ss=
github.com/harness/harness-go-sdk v0.4.13-0.20241209130746-db49476ebca0/go.mod h1:CPXydorp4zd5Dz2u2FXiHyWL4yd5PQafOMN69cgPSvk=
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
16 changes: 16 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/harness/terraform-provider-harness/internal/service/platform/module_registry"
cdng_service "github.com/harness/terraform-provider-harness/internal/service/cd_nextgen/service"
"github.com/harness/terraform-provider-harness/internal/service/platform/service_account"
"github.com/harness/harness-go-sdk/harness/chaos"
"github.com/harness/terraform-provider-harness/internal/service/chaos/infrastructure"
"log"

pipeline_gitx "github.com/harness/terraform-provider-harness/internal/service/pipeline/gitx/webhook"
Expand Down Expand Up @@ -296,6 +298,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 @@ -444,6 +447,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 @@ -538,6 +542,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 @@ -549,6 +564,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,73 @@
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: dataSourceInfrastructureRead,

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 dataSourceInfrastructureRead(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