Skip to content

Commit

Permalink
Increase custom rule condition data types accepted (#74)
Browse files Browse the repository at this point in the history
* chore: update version and OS architecture

* chore: format packages and add new region

* impr: custom rule resource can transform multiple data types

* chore: add terraform log library

* fix: udpate request address

* impr: update Value data type to interface

* chore: remove region

* impr: comment to explain new logic

* docs: update use and example for custom rule page

* impr: remove omitempty for a required attribute

* chore: update go version used

* fix: add beginning slash to path

* chore: add go mod file

* fix: add in trailing slash for endpoint address

* docs: update custom rule documentation

* docs: describe custom rule value in depth

* fix: get API call can handle multiple data types as well

* fix: remove debugging lines
  • Loading branch information
kim-cloudconformity authored Jan 31, 2024
1 parent 47afc7a commit 438b81a
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 508 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.17
go-version: 1.18
-
name: Install Terraform
uses: hashicorp/setup-terraform@v2
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ HOSTNAME=trendmicro.com
NAMESPACE=cloudone
NAME=conformity
BINARY=terraform-provider-${NAME}
VERSION=0.3
OS_ARCH=linux_amd64
VERSION=0.4
OS_ARCH=darwin_arm64

default: install

Expand Down
1 change: 1 addition & 0 deletions conformity/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package conformity

import (
"context"

"github.com/trendmicro/terraform-provider-conformity/pkg/cloudconformity"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down
31 changes: 29 additions & 2 deletions conformity/resource_conformity_custom_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ package conformity

import (
"context"
"encoding/json"
"strconv"
"strings"

"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"
"github.com/trendmicro/terraform-provider-conformity/pkg/cloudconformity"
)

type ObjectValue struct {
Days int `json:"days"`
Operator string `json:"operator"`
}

func resourceConformityCustomRule() *schema.Resource {
return &schema.Resource{
CreateContext: resourceConformityCustomRuleCreate,
Expand Down Expand Up @@ -349,7 +358,24 @@ func processInputCustomRuleConditions(conditionsIn []interface{}) []cloudconform
obj := cloudconformity.CustomRuleCondition{}
obj.Fact = m["fact"].(string)
obj.Path = m["path"].(string)
obj.Value = m["value"].(string)
/*
Custom Rule Conditions has an attribute of `value` that can accept a
string, boolean, integer, or an object. Anything other than string needs
to be encoded using the built-in Terraform function `jsonencode()`.
Below we are assigning objValue with an instance of the ObjectValue struct
that defines the variables that the Custom Rules API will accept.
*/
objValue := ObjectValue{}
if strings.ToLower(m["value"].(string)) == "true" || strings.ToLower(m["value"].(string)) == "false" {
obj.Value, _ = strconv.ParseBool(m["value"].(string))
} else if numValue, err := strconv.Atoi(m["value"].(string)); err == nil {
obj.Value = numValue
} else if err := json.Unmarshal([]byte(m["value"].(string)), &objValue); err == nil {
obj.Value = objValue
} else {
obj.Value = m["value"]
}

if operator, ok := m["operator"]; ok {
obj.Operator = operator.(string)
}
Expand Down Expand Up @@ -397,7 +423,8 @@ func flattenConditions(conditionsIn []cloudconformity.CustomRuleCondition) []int
m["fact"] = conditions.Fact
m["operator"] = conditions.Operator
m["path"] = conditions.Path
m["value"] = conditions.Value
conditionsValueByte, _ := json.Marshal(conditions.Value)
m["value"] = string(conditionsValueByte)
conditionsOut[i] = m
}
return conditionsOut
Expand Down
Loading

0 comments on commit 438b81a

Please sign in to comment.