Skip to content

Commit

Permalink
revert last commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasnallar committed Dec 12, 2024
1 parent 99d77be commit 038e057
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
10 changes: 5 additions & 5 deletions nullplatform/approval_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
50 changes: 19 additions & 31 deletions nullplatform/resource_approval_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package nullplatform

import (
"context"
"encoding/json"
"fmt"
"reflect"
"strconv"

Expand Down Expand Up @@ -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,
Expand All @@ -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()

Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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()

Expand All @@ -147,5 +134,6 @@ func ApprovalPolicyDelete(d *schema.ResourceData, m interface{}) error {
}

d.SetId("")

return nil
}

0 comments on commit 038e057

Please sign in to comment.