generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [CCM-19505]: Terraform Support for Governance Rule Sets (#1075)
* feat: [CCM-19505]: Terraform Support for Governance Rule Sets * feat: [CCM-19505]: Terraform Support for Governance Rule Sets * feat: [CCM-19505]: Terraform Support for Governance Rule Sets * feat: [CCM-19505]: Lint * feat: [CCM-19505]: Terraform Support for Governance Rule Sets * feat: [CCM-19505]: Terraform Support for Governance Rule Sets
- Loading branch information
1 parent
ff81fff
commit 7d78977
Showing
10 changed files
with
460 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
harness_governance_rule_set - Added Governance Rule Set resource in Harness terraform provider | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "harness_governance_rule_set Data Source - terraform-provider-harness" | ||
subcategory: "Next Gen" | ||
description: |- | ||
Datasource for looking up a rule. | ||
--- | ||
|
||
# harness_governance_rule_set (Data Source) | ||
|
||
Datasource for looking up a rule. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `rule_set_id` (String) Id of rule set. | ||
|
||
### Read-Only | ||
|
||
- `cloud_provider` (String) The cloud provider for the rule set. | ||
- `description` (String) Description for rule set. | ||
- `id` (String) The ID of this resource. | ||
- `name` (String) Name of the rule set. | ||
- `rule_ids` (List of String) List of target regions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "harness_governance_rule_set Resource - terraform-provider-harness" | ||
subcategory: "Next Gen" | ||
description: |- | ||
Resource for creating, updating, and managing rule. | ||
--- | ||
|
||
# harness_governance_rule_set (Resource) | ||
|
||
Resource for creating, updating, and managing rule. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `cloud_provider` (String) The cloud provider for the rule set. It should be either AWS, AZURE or GCP. | ||
- `name` (String) Name of the rule set. | ||
- `rule_ids` (List of String) List of rule IDs | ||
|
||
### Optional | ||
|
||
- `description` (String) Description for rule set. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
- `rule_set_id` (String) Id of the rule. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
internal/service/platform/governance/rule_set/rule_set.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package governance_rule_set | ||
|
||
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" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
func ResourceRuleSet() *schema.Resource { | ||
resource := &schema.Resource{ | ||
Description: "Resource for creating, updating, and managing rule.", | ||
ReadContext: resourceRuleSetRead, | ||
CreateContext: resourceRuleSetCreateOrUpdate, | ||
UpdateContext: resourceRuleSetCreateOrUpdate, | ||
DeleteContext: resourceRuleDelete, | ||
Importer: helpers.AccountLevelResourceImporter, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Description: "Name of the rule set.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"description": { | ||
Description: "Description for rule set.", | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"cloud_provider": { | ||
Description: "The cloud provider for the rule set. It should be either AWS, AZURE or GCP.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{"AWS", "GCP", "AZURE"}, false), | ||
}, | ||
"rule_ids": { | ||
Description: "List of rule IDs", | ||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"rule_set_id": { | ||
Description: "Id of the rule.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
|
||
return resource | ||
} | ||
|
||
func resourceRuleSetRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx) | ||
|
||
id := d.Id() | ||
resp, httpResp, err := c.RuleSetsApi.ListRuleSets(ctx, readRuleSetRequest(id), c.AccountId) | ||
|
||
if err != nil { | ||
return helpers.HandleReadApiError(err, d, httpResp) | ||
} | ||
|
||
if resp.Data != nil { | ||
err := readRuleSetResponse(d, resp.Data) | ||
if err != nil { | ||
return helpers.HandleReadApiError(err, d, httpResp) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func readRuleSetRequest(id string) nextgen.CreateRuleSetFilterDto { | ||
return nextgen.CreateRuleSetFilterDto{ | ||
RuleSet: &nextgen.RuleSetRequest{ | ||
RuleSetIds: []string{id}, | ||
}, | ||
} | ||
} | ||
|
||
func readRuleSetResponse(d *schema.ResourceData, ruleSetsList *nextgen.RuleSetList) error { | ||
ruleSet := ruleSetsList.RuleSet[0] | ||
|
||
d.Set("name", ruleSet.Name) | ||
d.Set("cloud_provider", ruleSet.CloudProvider) | ||
d.Set("description", ruleSet.Description) | ||
d.Set("rule_ids", ruleSet.RulesIdentifier) | ||
|
||
return nil | ||
} | ||
|
||
func resourceRuleSetCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx) | ||
|
||
var err error | ||
var resp nextgen.ResponseDtoRuleSet | ||
var httpResp *http.Response | ||
|
||
id := d.Id() | ||
|
||
if id == "" { | ||
resp, httpResp, err = c.RuleSetsApi.AddRuleSet(ctx, buildRuleSet(d, false), c.AccountId) | ||
} else { | ||
resp, httpResp, err = c.RuleSetsApi.UpdateRuleSet(ctx, buildRuleSet(d, true), c.AccountId) | ||
} | ||
|
||
if err != nil { | ||
return helpers.HandleApiError(err, d, httpResp) | ||
} | ||
|
||
if resp.Data != nil { | ||
createOrUpdateRuleSetResponse(d, resp.Data) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func buildRuleSet(d *schema.ResourceData, update bool) nextgen.CreateRuleSetDto { | ||
ruleSet := &nextgen.RuleSet{ | ||
Name: d.Get("name").(string), | ||
CloudProvider: d.Get("cloud_provider").(string), | ||
Description: d.Get("description").(string), | ||
RulesIdentifier: expandStringList(d.Get("rule_ids").([]interface{})), | ||
IsOOTB: false, | ||
} | ||
|
||
if update { | ||
ruleSet.Uuid = d.Id() | ||
} | ||
|
||
return nextgen.CreateRuleSetDto{ | ||
RuleSet: ruleSet, | ||
} | ||
} | ||
|
||
func createOrUpdateRuleSetResponse(d *schema.ResourceData, ruleSet *nextgen.RuleSet) error { | ||
d.SetId(ruleSet.Uuid) | ||
d.Set("rule_set_id", ruleSet.Uuid) | ||
d.Set("name", ruleSet.Name) | ||
d.Set("cloud_provider", ruleSet.CloudProvider) | ||
d.Set("description", ruleSet.Description) | ||
d.Set("rule_ids", ruleSet.RulesIdentifier) | ||
|
||
return nil | ||
} | ||
|
||
func resourceRuleDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx) | ||
|
||
id := d.Id() | ||
|
||
_, httpResp, err := c.RuleSetsApi.DeleteRuleSet(ctx, c.AccountId, id) | ||
|
||
if err != nil { | ||
return helpers.HandleApiError(err, d, httpResp) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func expandStringList(givenStringListInterface []interface{}) []string { | ||
var expandedStringList []string | ||
|
||
if len(givenStringListInterface) > 0 { | ||
for _, id := range givenStringListInterface { | ||
expandedStringList = append(expandedStringList, id.(string)) | ||
} | ||
} | ||
return expandedStringList | ||
} |
71 changes: 71 additions & 0 deletions
71
internal/service/platform/governance/rule_set/rule_set_data_source.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package governance_rule_set | ||
|
||
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 DatasourceRuleSet() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Datasource for looking up a rule.", | ||
|
||
ReadContext: resourceRuleSetReadDataSource, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"rule_set_id": { | ||
Description: "Id of rule set.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"name": { | ||
Description: "Name of the rule set.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"cloud_provider": { | ||
Description: "The cloud provider for the rule set.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"description": { | ||
Description: "Description for rule set.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"rule_ids": { | ||
Description: "List of target regions.", | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceRuleSetReadDataSource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx) | ||
|
||
id := d.Get("rule_set_id").(string) | ||
resp, httpResp, err := c.RuleSetsApi.ListRuleSets(ctx, readRuleSetRequest(id), c.AccountId) | ||
|
||
if err != nil { | ||
return helpers.HandleReadApiError(err, d, httpResp) | ||
} | ||
|
||
if resp.Data != nil { | ||
err := readRuleSetResponse(d, resp.Data) | ||
if err != nil { | ||
return helpers.HandleReadApiError(err, d, httpResp) | ||
} | ||
} | ||
|
||
d.SetId(id) | ||
|
||
return nil | ||
} |
Oops, something went wrong.