From 038e05794a0a1e44a5866eacff43bfc4c604ffcc Mon Sep 17 00:00:00 2001 From: Sebastian Nallar Date: Thu, 12 Dec 2024 16:38:12 -0300 Subject: [PATCH] revert last commit --- nullplatform/approval_policy.go | 10 ++--- nullplatform/resource_approval_policy.go | 50 +++++++++--------------- 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/nullplatform/approval_policy.go b/nullplatform/approval_policy.go index 8de1f5b..664c478 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 map[string]interface{} `json:"conditions,omitempty"` - Status string `json:"status,omitempty"` + 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"` } func (c *NullClient) CreateApprovalPolicy(policy *ApprovalPolicy) (*ApprovalPolicy, error) { diff --git a/nullplatform/resource_approval_policy.go b/nullplatform/resource_approval_policy.go index ac51b12..038b25d 100644 --- a/nullplatform/resource_approval_policy.go +++ b/nullplatform/resource_approval_policy.go @@ -2,8 +2,6 @@ package nullplatform import ( "context" - "encoding/json" - "fmt" "reflect" "strconv" @@ -39,26 +37,20 @@ 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 string.", - DiffSuppressFunc: suppressEquivalentJSON, + Type: schema.TypeString, + Required: true, + Description: "The conditions that the policy applies to, as a JSON object.", }, }, } } -func ApprovalPolicyCreate(d *schema.ResourceData, m interface{}) error { +func ApprovalPolicyCreate(d *schema.ResourceData, m any) error { nullOps := m.(NullOps) nrn := d.Get("nrn").(string) name := d.Get("name").(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) - } + conditions := d.Get("conditions").(string) newApprovalPolicy := &ApprovalPolicy{ Nrn: nrn, @@ -67,15 +59,17 @@ func ApprovalPolicyCreate(d *schema.ResourceData, m interface{}) 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 interface{}) error { +func ApprovalPolicyRead(d *schema.ResourceData, m any) error { nullOps := m.(NullOps) approvalPolicyId := d.Id() @@ -87,7 +81,6 @@ func ApprovalPolicyRead(d *schema.ResourceData, m interface{}) error { } return err } - if err := d.Set("nrn", approvalPolicy.Nrn); err != nil { return err } @@ -96,38 +89,32 @@ func ApprovalPolicyRead(d *schema.ResourceData, m interface{}) error { 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) + if err := d.Set("conditions", approvalPolicy.Conditions); err != nil { + return err } return nil } -func ApprovalPolicyUpdate(d *schema.ResourceData, m interface{}) error { +func ApprovalPolicyUpdate(d *schema.ResourceData, m any) 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") { - 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 + approvalPolicy.Conditions = d.Get("conditions").(string) } - if !reflect.DeepEqual(*approvalPolicy, ApprovalPolicy{}) { + if !reflect.DeepEqual(*approvalPolicy, Scope{}) { err := nullOps.PatchApprovalPolicy(approvalPolicyId, approvalPolicy) if err != nil { return err @@ -137,7 +124,7 @@ func ApprovalPolicyUpdate(d *schema.ResourceData, m interface{}) error { return ApprovalPolicyRead(d, m) } -func ApprovalPolicyDelete(d *schema.ResourceData, m interface{}) error { +func ApprovalPolicyDelete(d *schema.ResourceData, m any) error { nullOps := m.(NullOps) approvalPolicyId := d.Id() @@ -147,5 +134,6 @@ func ApprovalPolicyDelete(d *schema.ResourceData, m interface{}) error { } d.SetId("") + return nil }