generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [CCM-19504]: Terraform Support for Governance Rules (#1068)
* feat: [CCM-19504]: Terraform Support for Governance Rules * feat: [CCM-19504]: Add TF Docs * feat: [CCM-19504]: Add TF Docs * feat: [CCM-19504]: Terraform Support for Governance Rules * feat: [CCM-19504]: Terraform Support for Governance Rules * feat: [CCM-19504]: Terraform Support for Governance Rules
- Loading branch information
1 parent
c6f296f
commit eeb35f8
Showing
13 changed files
with
470 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 - Added Governance Rule 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,34 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "harness_governance_rule Data Source - terraform-provider-harness" | ||
subcategory: "Next Gen" | ||
description: |- | ||
Datasource for looking up a rule. | ||
--- | ||
|
||
# harness_governance_rule (Data Source) | ||
|
||
Datasource for looking up a rule. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "harness_governance_rule" "example" { | ||
rule_id = "rule_id" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `rule_id` (String) Id of rule. | ||
|
||
### Read-Only | ||
|
||
- `cloud_provider` (String) The cloud provider for the rule. | ||
- `description` (String) Description for rule. | ||
- `id` (String) The ID of this resource. | ||
- `name` (String) Name of the rule. | ||
- `rules_yaml` (String) Policy YAML 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "harness_governance_rule Resource - terraform-provider-harness" | ||
subcategory: "Next Gen" | ||
description: |- | ||
Resource for creating, updating, and managing rule. | ||
--- | ||
|
||
# harness_governance_rule (Resource) | ||
|
||
Resource for creating, updating, and managing rule. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `cloud_provider` (String) The cloud provider for the rule. It should be either AWS, AZURE or GCP. | ||
- `name` (String) Name of the rule. | ||
- `rules_yaml` (String) The policy YAML of the rule | ||
|
||
### Optional | ||
|
||
- `description` (String) Description for rule. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
- `rule_id` (String) Id of the rule. | ||
|
||
## Import | ||
|
||
Import is supported using the following syntax: | ||
|
||
```shell | ||
# Import governance enforcement | ||
terraform import harness_governance_rule.example <rule_id> | ||
``` |
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 @@ | ||
data "harness_governance_rule" "example" { | ||
rule_id = "rule_id" | ||
} |
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,2 @@ | ||
# Import governance enforcement | ||
terraform import harness_governance_rule.example <rule_id> |
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,7 @@ | ||
resource "harness_governance_rule" "example" { | ||
identifier = "identifier" | ||
name = "name" | ||
cloud_provider = "AWS/AZURE/GCP" | ||
description = "description" | ||
rules_yaml = "policies:\n - name: aws-list-ec2\n resource: aws.ec2" | ||
} |
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
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,162 @@ | ||
package governance_rule | ||
|
||
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 ResourceRule() *schema.Resource { | ||
resource := &schema.Resource{ | ||
Description: "Resource for creating, updating, and managing rule.", | ||
ReadContext: resourceRuleRead, | ||
CreateContext: resourceRuleCreateOrUpdate, | ||
UpdateContext: resourceRuleCreateOrUpdate, | ||
DeleteContext: resourceRuleDelete, | ||
Importer: helpers.AccountLevelResourceImporter, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Description: "Name of the rule.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"description": { | ||
Description: "Description for rule.", | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"cloud_provider": { | ||
Description: "The cloud provider for the rule. It should be either AWS, AZURE or GCP.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{"AWS", "GCP", "AZURE"}, false), | ||
}, | ||
"rules_yaml": { | ||
Description: "The policy YAML of the rule", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"rule_id": { | ||
Description: "Id of the rule.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
|
||
return resource | ||
} | ||
|
||
func resourceRuleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx) | ||
|
||
id := d.Id() | ||
resp, httpResp, err := c.RuleApi.GetPolicies(ctx, readRuleRequest(id), c.AccountId, nil) | ||
|
||
if err != nil { | ||
return helpers.HandleReadApiError(err, d, httpResp) | ||
} | ||
|
||
if resp.Data != nil { | ||
err := readRuleResponse(d, resp.Data) | ||
if err != nil { | ||
return helpers.HandleReadApiError(err, d, httpResp) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func readRuleRequest(id string) nextgen.ListDto { | ||
return nextgen.ListDto{ | ||
Query: &nextgen.RuleRequest{ | ||
PolicyIds: []string{id}, | ||
}, | ||
} | ||
} | ||
|
||
func readRuleResponse(d *schema.ResourceData, ruleList *nextgen.RuleList) error { | ||
rule := ruleList.Rules[0] | ||
|
||
d.Set("name", rule.Name) | ||
d.Set("cloud_provider", rule.CloudProvider) | ||
d.Set("description", rule.Description) | ||
d.Set("rules_yaml", rule.RulesYaml) | ||
|
||
return nil | ||
} | ||
|
||
func resourceRuleCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx) | ||
|
||
var err error | ||
var resp nextgen.ResponseDtoRule | ||
var httpResp *http.Response | ||
|
||
id := d.Id() | ||
|
||
if id == "" { | ||
resp, httpResp, err = c.RuleApi.CreateNewRule(ctx, buildRule(d, false), c.AccountId) | ||
} else { | ||
resp, httpResp, err = c.RuleApi.UpdateRule(ctx, buildRule(d, true), c.AccountId) | ||
} | ||
|
||
if err != nil { | ||
return helpers.HandleApiError(err, d, httpResp) | ||
} | ||
|
||
if resp.Data != nil { | ||
createOrUpdateRuleResponse(d, resp.Data) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func buildRule(d *schema.ResourceData, update bool) nextgen.CreateRuleDto { | ||
rule := &nextgen.CcmRule{ | ||
Name: d.Get("name").(string), | ||
CloudProvider: d.Get("cloud_provider").(string), | ||
Description: d.Get("description").(string), | ||
RulesYaml: d.Get("rules_yaml").(string), | ||
IsOOTB: false, | ||
} | ||
|
||
if update { | ||
rule.Uuid = d.Id() | ||
} | ||
|
||
return nextgen.CreateRuleDto{ | ||
Rule: rule, | ||
} | ||
} | ||
|
||
func createOrUpdateRuleResponse(d *schema.ResourceData, rule *nextgen.CcmRule) error { | ||
d.SetId(rule.Uuid) | ||
d.Set("rule_id", rule.Uuid) | ||
d.Set("name", rule.Name) | ||
d.Set("cloud_provider", rule.CloudProvider) | ||
d.Set("description", rule.Description) | ||
d.Set("rules_yaml", rule.RulesYaml) | ||
|
||
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.RuleApi.DeleteRule(ctx, c.AccountId, id) | ||
|
||
if err != nil { | ||
return helpers.HandleApiError(err, d, httpResp) | ||
} | ||
|
||
return nil | ||
} |
68 changes: 68 additions & 0 deletions
68
internal/service/platform/governance/rule/rule_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,68 @@ | ||
package governance_rule | ||
|
||
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 DatasourceRule() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Datasource for looking up a rule.", | ||
|
||
ReadContext: resourceRuleReadDataSource, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"rule_id": { | ||
Description: "Id of rule.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"name": { | ||
Description: "Name of the rule.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"rules_yaml": { | ||
Description: "Policy YAML of the rule.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"cloud_provider": { | ||
Description: "The cloud provider for the rule.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"description": { | ||
Description: "Description for rule.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceRuleReadDataSource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx) | ||
|
||
id := d.Get("rule_id").(string) | ||
resp, httpResp, err := c.RuleApi.GetPolicies(ctx, readRuleRequest(id), c.AccountId, nil) | ||
|
||
if err != nil { | ||
return helpers.HandleReadApiError(err, d, httpResp) | ||
} | ||
|
||
if resp.Data != nil { | ||
err := readRuleResponse(d, resp.Data) | ||
if err != nil { | ||
return helpers.HandleReadApiError(err, d, httpResp) | ||
} | ||
} | ||
|
||
d.SetId(id) | ||
|
||
return nil | ||
} |
Oops, something went wrong.