Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
[SYM-3790] Add support for new schedule_deescalation flag (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arianna2028 authored Jun 6, 2022
1 parent 4fcda6a commit 4d9d9bc
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 39 deletions.
1 change: 1 addition & 0 deletions example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ resource "sym_flow" "this" {

params = {
strategy_id = sym_strategy.sso_main.id
schedule_deescalation = false

# This is called `fields` in the API
prompt_fields_json = jsonencode([
Expand Down
8 changes: 5 additions & 3 deletions sym/provider/acceptance_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ func (r flowResource) String() string {
p.WriteString(fmt.Sprintf(" strategy_id = %s\n", r.params.strategyId))
}
p.WriteString(fmt.Sprintf(" allow_revoke = %v\n", r.params.allowRevoke))
p.WriteString(fmt.Sprintf(" schedule_deescalation = %v\n", r.params.scheduleDeescalation))
p.WriteString(" prompt_fields_json = jsonencode([\n")
for _, f := range r.params.promptFields {
p.WriteString(" {\n")
Expand Down Expand Up @@ -400,9 +401,10 @@ resource "sym_flow" %[1]q {
}

type params struct {
strategyId string
allowRevoke bool
promptFields []field
strategyId string
allowRevoke bool
scheduleDeescalation bool
promptFields []field
}

type field struct {
Expand Down
12 changes: 8 additions & 4 deletions sym/provider/acceptance_test_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,9 @@ func Test_flowResource_String(t *testing.T) {
"internal/testdata/impl.py",
"sym_environment.this.id",
params{
strategyId: "sym_strategy.sso_main.id",
allowRevoke: false,
strategyId: "sym_strategy.sso_main.id",
allowRevoke: false,
scheduleDeescalation: false,
promptFields: []field{
{
name: "reason",
Expand Down Expand Up @@ -472,6 +473,7 @@ resource "sym_flow" "this" {
params = {
strategy_id = sym_strategy.sso_main.id
allow_revoke = false
schedule_deescalation = false
prompt_fields_json = jsonencode([
{
name = "reason"
Expand Down Expand Up @@ -505,8 +507,9 @@ resource "sym_flow" "this" {
"internal/testdata/impl.py",
"sym_environment.this.id",
params{
strategyId: "",
allowRevoke: false,
strategyId: "",
allowRevoke: false,
scheduleDeescalation: false,
promptFields: []field{
{
name: "reason",
Expand Down Expand Up @@ -538,6 +541,7 @@ resource "sym_flow" "this" {
params = {
allow_revoke = false
schedule_deescalation = false
prompt_fields_json = jsonencode([
{
name = "reason"
Expand Down
32 changes: 23 additions & 9 deletions sym/provider/flow_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,22 +273,36 @@ func deleteFlow(_ context.Context, data *schema.ResourceData, meta interface{})
}

func validateParams(input interface{}, path cty.Path) diag.Diagnostics {
diags := diag.Diagnostics{}

if params, ok := input.(map[string]interface{}); ok {
if allowRevoke, ok := params["allow_revoke"]; ok {
if allowRevoke, ok := allowRevoke.(string); ok {
_, err := strconv.ParseBool(allowRevoke)
if err != nil {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "allow_revoke must be a boolean value",
Detail: fmt.Sprintf("failed to parse %q to bool", allowRevoke),
AttributePath: append(path, cty.IndexStep{Key: cty.StringVal("allow_revoke")}),
},
}
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "allow_revoke must be a boolean value",
Detail: fmt.Sprintf("failed to parse %q to bool", allowRevoke),
AttributePath: append(path, cty.IndexStep{Key: cty.StringVal("allow_revoke")}),
})
}
}
}

if scheduleDeescalation, ok := params["schedule_deescalation"]; ok {
if scheduleDeescalation, ok := scheduleDeescalation.(string); ok {
if _, err := strconv.ParseBool(scheduleDeescalation); err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "schedule_deescalation must be a boolean value",
Detail: fmt.Sprintf("failed to parse %q to bool", scheduleDeescalation),
AttributePath: append(path, cty.IndexStep{Key: cty.StringVal("schedule_deescalation")}),
})
}
}
}
}
return nil

return diags
}
27 changes: 16 additions & 11 deletions sym/provider/flow_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func TestAccSymFlow_basic(t *testing.T) {
resource.TestCheckResourceAttrSet("sym_flow.this", "implementation"),
resource.TestCheckResourceAttrPair("sym_flow.this", "environment_id", "sym_environment.this", "id"),
resource.TestCheckResourceAttrPair("sym_flow.this", "params.strategy_id", "sym_strategy.sso_main", "id"),
resource.TestCheckResourceAttr("sym_flow.this", "params.allow_revoke", "false"),
resource.TestCheckResourceAttr("sym_flow.this", "params.allow_revoke", "true"),
resource.TestCheckResourceAttr("sym_flow.this", "params.schedule_deescalation", "false"),
resource.TestCheckResourceAttr("sym_flow.this", "params.prompt_fields_json", `[{"name":"reason","type":"string","required":true,"label":"Reason"},{"name":"urgency","type":"list","required":true,"default":"Low","allowed_values":["Low","Medium","High"]}]`),
),
},
Expand All @@ -36,7 +37,8 @@ func TestAccSymFlow_basic(t *testing.T) {
resource.TestCheckResourceAttrSet("sym_flow.this", "implementation"),
resource.TestCheckResourceAttrPair("sym_flow.this", "environment_id", "sym_environment.this", "id"),
resource.TestCheckResourceAttrPair("sym_flow.this", "params.strategy_id", "sym_strategy.sso_main", "id"),
resource.TestCheckResourceAttr("sym_flow.this", "params.allow_revoke", "true"),
resource.TestCheckResourceAttr("sym_flow.this", "params.allow_revoke", "false"),
resource.TestCheckResourceAttr("sym_flow.this", "params.schedule_deescalation", "true"),
),
},
},
Expand Down Expand Up @@ -95,7 +97,8 @@ func TestAccSymFlow_noStrategy(t *testing.T) {
resource.TestCheckResourceAttrSet("sym_flow.this", "implementation"),
resource.TestCheckResourceAttrPair("sym_flow.this", "environment_id", "sym_environment.this", "id"),
resource.TestCheckNoResourceAttr("sym_flow.this", "params.strategy_id"),
resource.TestCheckResourceAttr("sym_flow.this", "params.allow_revoke", "false"),
resource.TestCheckResourceAttr("sym_flow.this", "params.allow_revoke", "true"),
resource.TestCheckResourceAttr("sym_flow.this", "params.schedule_deescalation", "false"),
resource.TestCheckResourceAttr("sym_flow.this", "params.prompt_fields_json", `[{"name":"reason","type":"string","required":true,"label":"Reason"},{"name":"urgency","type":"list","required":true,"default":"Low","allowed_values":["Low","Medium","High"]}]`),
),
},
Expand All @@ -108,14 +111,15 @@ func TestAccSymFlow_noStrategy(t *testing.T) {
resource.TestCheckResourceAttrSet("sym_flow.this", "implementation"),
resource.TestCheckResourceAttrPair("sym_flow.this", "environment_id", "sym_environment.this", "id"),
resource.TestCheckNoResourceAttr("sym_flow.this", "params.strategy_id"),
resource.TestCheckResourceAttr("sym_flow.this", "params.allow_revoke", "true"),
resource.TestCheckResourceAttr("sym_flow.this", "params.allow_revoke", "false"),
resource.TestCheckResourceAttr("sym_flow.this", "params.schedule_deescalation", "true"),
),
},
},
})
}

func flowConfig(data TestData, implPath string, allowRevoke bool, strategyId string) string {
func flowConfig(data TestData, implPath string, allowRevoke bool, strategyId string, scheduleDeescalation bool) string {
return makeTerraformConfig(
providerResource{org: data.OrgSlug},
integrationResource{
Expand Down Expand Up @@ -198,8 +202,9 @@ func flowConfig(data TestData, implPath string, allowRevoke bool, strategyId str
implementation: implPath,
environmentId: "sym_environment.this.id",
params: params{
strategyId: strategyId,
allowRevoke: allowRevoke,
strategyId: strategyId,
allowRevoke: allowRevoke,
scheduleDeescalation: scheduleDeescalation,
promptFields: []field{
{
name: "reason",
Expand All @@ -221,17 +226,17 @@ func flowConfig(data TestData, implPath string, allowRevoke bool, strategyId str
}

func createFlowConfig(data TestData) string {
return flowConfig(data, "internal/testdata/before_impl.py", false, "sym_strategy.sso_main.id")
return flowConfig(data, "internal/testdata/before_impl.py", true, "sym_strategy.sso_main.id", false)
}

func updateFlowConfig(data TestData) string {
return flowConfig(data, "internal/testdata/after_impl.py", true, "sym_strategy.sso_main.id")
return flowConfig(data, "internal/testdata/after_impl.py", false, "sym_strategy.sso_main.id", true)
}

func createFlowNoStrategyConfig(data TestData) string {
return flowConfig(data, "internal/testdata/before_impl.py", false, "")
return flowConfig(data, "internal/testdata/before_impl.py", true, "", false)
}

func updateFlowNoStrategyConfig(data TestData) string {
return flowConfig(data, "internal/testdata/after_impl.py", true, "")
return flowConfig(data, "internal/testdata/after_impl.py", false, "", true)
}
25 changes: 20 additions & 5 deletions sym/templates/sym_approval.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ func fieldResource() *schema.Resource {
func (t *SymApprovalTemplate) ParamResource() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"strategy_id": utils.Optional(schema.TypeString),
"allow_revoke": utils.OptionalWithDefault(schema.TypeBool, true),
"prompt_fields": utils.OptionalList(fieldResource()),
"strategy_id": utils.Optional(schema.TypeString),
"allow_revoke": utils.OptionalWithDefault(schema.TypeBool, true),
"schedule_deescalation": utils.OptionalWithDefault(schema.TypeBool, true),
"prompt_fields": utils.OptionalList(fieldResource()),
},
}
}
Expand Down Expand Up @@ -72,6 +73,18 @@ func (t *SymApprovalTemplate) terraformToAPI(params *HCLParamMap) client.APIPara
raw["allow_revoke"] = true
}

if field := params.checkKey("schedule_deescalation"); field != nil {
// If schedule_deescalation is set, validate that it is a boolean and add it to params
scheduleDeescalation, err := strconv.ParseBool(field.Value())
if err != nil {
_ = params.checkError("schedule_deescalation", "schedule_deescalation must be a boolean value", err)
}
raw["schedule_deescalation"] = scheduleDeescalation
} else {
// Default schedule_deescalation to true
raw["schedule_deescalation"] = true
}

return raw
}

Expand Down Expand Up @@ -100,10 +113,12 @@ func apiParamsToTFParams(apiParams client.APIParams) (*HCLParamMap, error) {
}

allowRevoke, _ := apiParams["allow_revoke"].(bool)
scheduleDeescalation, _ := apiParams["schedule_deescalation"].(bool)

params := map[string]string{
"allow_revoke": strconv.FormatBool(allowRevoke),
"prompt_fields_json": string(fieldsJSON),
"allow_revoke": strconv.FormatBool(allowRevoke),
"schedule_deescalation": strconv.FormatBool(scheduleDeescalation),
"prompt_fields_json": string(fieldsJSON),
}

if apiParamsStrategyID, ok := apiParams["strategy_id"].(string); ok {
Expand Down
15 changes: 9 additions & 6 deletions sym/templates/sym_approval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func Test_apiParamsToTFParams(t *testing.T) {
},
&HCLParamMap{
Params: map[string]string{
"allow_revoke": "false",
"prompt_fields_json": `[{"name":"reason","type":"string","required":true,"label":"Reason"},{"name":"urgency","type":"string","required":true,"allowed_values":["Low","Medium","High"]}]`,
"allow_revoke": "false",
"schedule_deescalation": "false",
"prompt_fields_json": `[{"name":"reason","type":"string","required":true,"label":"Reason"},{"name":"urgency","type":"string","required":true,"allowed_values":["Low","Medium","High"]}]`,
},
},
false,
Expand All @@ -52,13 +53,15 @@ func Test_apiParamsToTFParams(t *testing.T) {
map[string]interface{}{"name": "reason", "type": "string", "required": true, "label": "Reason"},
map[string]interface{}{"name": "urgency", "type": "string", "required": true, "allowed_values": []interface{}{"Low", "Medium", "High"}},
},
"allow_revoke": true,
"allow_revoke": true,
"schedule_deescalation": true,
},
&HCLParamMap{
Params: map[string]string{
"strategy_id": "haha-business",
"allow_revoke": "true",
"prompt_fields_json": `[{"name":"reason","type":"string","required":true,"label":"Reason"},{"name":"urgency","type":"string","required":true,"allowed_values":["Low","Medium","High"]}]`,
"strategy_id": "haha-business",
"allow_revoke": "true",
"schedule_deescalation": "true",
"prompt_fields_json": `[{"name":"reason","type":"string","required":true,"label":"Reason"},{"name":"urgency","type":"string","required":true,"allowed_values":["Low","Medium","High"]}]`,
},
},
false,
Expand Down
5 changes: 4 additions & 1 deletion sym/utils/diff_suppress.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ func SuppressFlowDiffs(k string, old string, new string, d *schema.ResourceData)
// allow_revoke defaults to true, so don't show a diff if allow_revoke is not specified
suppressAllowRevokeDiffs := k == "params.allow_revoke" && old == "true" && new == ""

return suppressJsonDiffs || suppressAllowRevokeDiffs
// schedule_deescalation defaults to true, so don't show a diff if schedule_deescalation is not specified
suppressScheduleDeescalationDiffs := k == "params.schedule_deescalation" && old == "true" && new == ""

return suppressJsonDiffs || suppressAllowRevokeDiffs || suppressScheduleDeescalationDiffs
}

// SuppressNullSettingsDiffs is a DiffSuppressFunc that can be passed into
Expand Down

0 comments on commit 4d9d9bc

Please sign in to comment.