From 9c3a860619b778a894f6477bd0f31cd185967dfb Mon Sep 17 00:00:00 2001 From: Sebastian Nallar Date: Thu, 12 Dec 2024 15:42:29 -0300 Subject: [PATCH] fix: approval policy should srialize to json the conditions stirng --- nullplatform/approval_policy.go | 10 ++--- nullplatform/resource_approval_policy.go | 50 +++++++++++++++--------- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/nullplatform/approval_policy.go b/nullplatform/approval_policy.go index 664c478..8de1f5b 100644 --- a/nullplatform/approval_policy.go +++ b/nullplatform/approval_policy.go @@ -10,11 +10,11 @@ import ( const APPROVAL_POLICY_PATH = "/approval/policy" type ApprovalPolicy struct { - Id int `json:"id,omitempty"` - Nrn string `json:"nrn,omitempty"` - Name string `json:"name,omitempty"` - Conditions string `json:"conditions,omitempty"` - Status string `json:"status,omitempty"` + Id int `json:"id,omitempty"` + Nrn string `json:"nrn,omitempty"` + Name string `json:"name,omitempty"` + Conditions map[string]interface{} `json:"conditions,omitempty"` + Status string `json:"status,omitempty"` } func (c *NullClient) CreateApprovalPolicy(policy *ApprovalPolicy) (*ApprovalPolicy, error) { diff --git a/nullplatform/resource_approval_policy.go b/nullplatform/resource_approval_policy.go index 038b25d..ac51b12 100644 --- a/nullplatform/resource_approval_policy.go +++ b/nullplatform/resource_approval_policy.go @@ -2,6 +2,8 @@ package nullplatform import ( "context" + "encoding/json" + "fmt" "reflect" "strconv" @@ -37,20 +39,26 @@ func resourceApprovalPolicy() *schema.Resource { Description: "The name of the policy.", }, "conditions": { - Type: schema.TypeString, - Required: true, - Description: "The conditions that the policy applies to, as a JSON object.", + Type: schema.TypeString, + Required: true, + Description: "The conditions that the policy applies to, as a JSON string.", + DiffSuppressFunc: suppressEquivalentJSON, }, }, } } -func ApprovalPolicyCreate(d *schema.ResourceData, m any) error { +func ApprovalPolicyCreate(d *schema.ResourceData, m interface{}) error { nullOps := m.(NullOps) nrn := d.Get("nrn").(string) name := d.Get("name").(string) - conditions := d.Get("conditions").(string) + conditionsJSON := d.Get("conditions").(string) + + var conditions map[string]interface{} + if err := json.Unmarshal([]byte(conditionsJSON), &conditions); err != nil { + return fmt.Errorf("error parsing conditions JSON: %v", err) + } newApprovalPolicy := &ApprovalPolicy{ Nrn: nrn, @@ -59,17 +67,15 @@ func ApprovalPolicyCreate(d *schema.ResourceData, m any) error { } approvalPolicy, err := nullOps.CreateApprovalPolicy(newApprovalPolicy) - if err != nil { return err } d.SetId(strconv.Itoa(approvalPolicy.Id)) - return ApprovalPolicyRead(d, m) } -func ApprovalPolicyRead(d *schema.ResourceData, m any) error { +func ApprovalPolicyRead(d *schema.ResourceData, m interface{}) error { nullOps := m.(NullOps) approvalPolicyId := d.Id() @@ -81,6 +87,7 @@ func ApprovalPolicyRead(d *schema.ResourceData, m any) error { } return err } + if err := d.Set("nrn", approvalPolicy.Nrn); err != nil { return err } @@ -89,32 +96,38 @@ func ApprovalPolicyRead(d *schema.ResourceData, m any) error { return err } - if err := d.Set("conditions", approvalPolicy.Conditions); err != nil { - return err + conditionsJSON, err := json.Marshal(approvalPolicy.Conditions) + if err != nil { + return fmt.Errorf("error serializing conditions to JSON: %v", err) + } + + if err := d.Set("conditions", string(conditionsJSON)); err != nil { + return fmt.Errorf("error setting conditions in state: %v", err) } return nil } -func ApprovalPolicyUpdate(d *schema.ResourceData, m any) error { +func ApprovalPolicyUpdate(d *schema.ResourceData, m interface{}) error { nullOps := m.(NullOps) approvalPolicyId := d.Id() approvalPolicy := &ApprovalPolicy{} - if d.HasChange("nrn") { - approvalPolicy.Nrn = d.Get("nrn").(string) - } - if d.HasChange("name") { approvalPolicy.Name = d.Get("name").(string) } if d.HasChange("conditions") { - approvalPolicy.Conditions = d.Get("conditions").(string) + conditionsJSON := d.Get("conditions").(string) + var conditions map[string]interface{} + if err := json.Unmarshal([]byte(conditionsJSON), &conditions); err != nil { + return fmt.Errorf("error parsing conditions JSON: %v", err) + } + approvalPolicy.Conditions = conditions } - if !reflect.DeepEqual(*approvalPolicy, Scope{}) { + if !reflect.DeepEqual(*approvalPolicy, ApprovalPolicy{}) { err := nullOps.PatchApprovalPolicy(approvalPolicyId, approvalPolicy) if err != nil { return err @@ -124,7 +137,7 @@ func ApprovalPolicyUpdate(d *schema.ResourceData, m any) error { return ApprovalPolicyRead(d, m) } -func ApprovalPolicyDelete(d *schema.ResourceData, m any) error { +func ApprovalPolicyDelete(d *schema.ResourceData, m interface{}) error { nullOps := m.(NullOps) approvalPolicyId := d.Id() @@ -134,6 +147,5 @@ func ApprovalPolicyDelete(d *schema.ResourceData, m any) error { } d.SetId("") - return nil }