Skip to content

Commit

Permalink
Merge pull request #4 from clearnote01/refactor-client-extension
Browse files Browse the repository at this point in the history
setup list support in macros
  • Loading branch information
clearnote01 authored Nov 11, 2024
2 parents 00c012a + 2101e0c commit 5bc12f2
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 24 deletions.
121 changes: 121 additions & 0 deletions zendesk/client/macro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package client

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

"github.com/nukosuke/terraform-provider-zendesk/zendesk/models"

"github.com/nukosuke/go-zendesk/zendesk"
)

// MacroAPI an interface containing all macro related methods
type MacroAPI interface {
GetMacros(ctx context.Context, options *zendesk.MacroListOptions) ([]models.Macro, zendesk.Page, error)
CreateMacro(ctx context.Context, macro models.Macro) (models.Macro, error)
DeleteMacro(ctx context.Context, id int64) error
UpdateMacro(ctx context.Context, id int64, form models.Macro) (models.Macro, error)
GetMacro(ctx context.Context, id int64) (models.Macro, error)
}

// GetMacros fetches macros
// ref: https://developer.zendesk.com/rest_api/docs/support/macro#list-macros
func (z *Client) GetMacros(ctx context.Context, options *zendesk.MacroListOptions) ([]models.Macro, zendesk.Page, error) {
var data struct {
Macros []models.Macro `json:"macros"`
zendesk.Page
}

tmp := options
if tmp == nil {
tmp = &zendesk.MacroListOptions{}
}

u, err := addOptions("/macros.json", tmp)
if err != nil {
return nil, zendesk.Page{}, err
}

body, err := z.Get(ctx, u)
if err != nil {
return []models.Macro{}, zendesk.Page{}, err
}

err = json.Unmarshal(body, &data)
if err != nil {
return []models.Macro{}, zendesk.Page{}, err
}
return data.Macros, data.Page, nil
}

// CreateMacro creates new macro
// ref: https://developer.zendesk.com/rest_api/docs/support/macro#create-macros
func (z *Client) CreateMacro(ctx context.Context, macro models.Macro) (models.Macro, error) {
var data, result struct {
Macro models.Macro `json:"macro"`
}
data.Macro = macro

body, err := z.Post(ctx, "/macros.json", data)
if err != nil {
return models.Macro{}, err
}

err = json.Unmarshal(body, &result)
if err != nil {
return models.Macro{}, err
}
return result.Macro, nil
}

// GetMacro returns the specified macro
// ref: https://developer.zendesk.com/rest_api/docs/support/macro#show-macro
func (z *Client) GetMacro(ctx context.Context, id int64) (models.Macro, error) {
var result struct {
Macro models.Macro `json:"macro"`
}

body, err := z.Get(ctx, fmt.Sprintf("/macros/%d.json", id))
if err != nil {
return models.Macro{}, err
}

err = json.Unmarshal(body, &result)
if err != nil {
return models.Macro{}, err
}
return result.Macro, nil
}

// UpdateMacro updates the specified macro and returns the updated form
// ref: https://developer.zendesk.com/rest_api/docs/support/macro#update-macros
func (z *Client) UpdateMacro(ctx context.Context, id int64, form models.Macro) (models.Macro, error) {
var data, result struct {
Macro models.Macro `json:"macro"`
}

data.Macro = form
body, err := z.Put(ctx, fmt.Sprintf("/macros/%d.json", id), data)
if err != nil {
return models.Macro{}, err
}

err = json.Unmarshal(body, &result)
if err != nil {
return models.Macro{}, err
}

return result.Macro, nil
}

// DeleteMacro deletes the specified macro
// ref: https://developer.zendesk.com/rest_api/docs/support/macro#delete-macro
func (z *Client) DeleteMacro(ctx context.Context, id int64) error {
err := z.Delete(ctx, fmt.Sprintf("/macros/%d.json", id))
if err != nil {
return err
}

return nil
}
21 changes: 21 additions & 0 deletions zendesk/models/macro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package models

import "time"

type Macro struct {
Actions []MacroAction `json:"actions"`
Active bool `json:"active"`
CreatedAt time.Time `json:"created_at,omitempty"`
Description interface{} `json:"description"`
ID int64 `json:"id,omitempty"`
Position int `json:"position,omitempty"`
Restriction interface{} `json:"restriction"`
Title string `json:"title"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
URL string `json:"url,omitempty"`
}

type MacroAction struct {
Field string `json:"field"`
Value interface{} `json:"value"`
}
2 changes: 1 addition & 1 deletion zendesk/resource_zendesk_automation.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func resourceZendeskAutomation() *schema.Resource {
Required: true,
},
"value": {
Description: "The new value of the field.",
Description: "The new value of the field. Can be a single string value or a jsonencode'ed list",
Type: schema.TypeString,
Required: true,
},
Expand Down
55 changes: 34 additions & 21 deletions zendesk/resource_zendesk_macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package zendesk

import (
"context"
"encoding/json"
"fmt"
"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"
client "github.com/nukosuke/go-zendesk/zendesk"
newClient "github.com/nukosuke/terraform-provider-zendesk/zendesk/client"
"github.com/nukosuke/terraform-provider-zendesk/zendesk/models"
)

// https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/
Expand Down Expand Up @@ -41,7 +43,7 @@ func resourceZendeskMacro() *schema.Resource {
Required: true,
},
"value": {
Description: "The new value of the field.",
Description: "The new value of the field. Can be a single string value or a jsonencode'ed list",
Type: schema.TypeString,
Required: true,
},
Expand Down Expand Up @@ -87,7 +89,7 @@ func resourceZendeskMacro() *schema.Resource {
}

// marshalMacros encodes the provided user field into the provided resource data
func marshalMacros(field client.Macro, d identifiableGetterSetter) error {
func marshalMacros(field models.Macro, d identifiableGetterSetter) error {
fields := map[string]interface{}{
"url": field.URL,
"title": field.Title,
Expand Down Expand Up @@ -118,23 +120,23 @@ func marshalMacros(field client.Macro, d identifiableGetterSetter) error {
var actions []map[string]interface{}
for _, action := range field.Actions {

// If the field value is a string, leave it be
// If the macro action value is a string, leave it be
// If it's a list, marshal it to a string
// var stringVal string
// switch action.Value.(type) {
// case []interface{}:
// tmp, err := json.Marshal(action.Value)
// if err != nil {
// return fmt.Errorf("error decoding field action value: %s", err)
// }
// stringVal = string(tmp)
// case string:
// stringVal = action.Value.(string)
// }
var stringVal string
switch action.Value.(type) {
case []interface{}:
tmp, err := json.Marshal(action.Value)
if err != nil {
return fmt.Errorf("error decoding macro action value: %s", err)
}
stringVal = string(tmp)
case string:
stringVal = action.Value.(string)
}

m := map[string]interface{}{
"field": action.Field,
"value": action.Value,
"value": stringVal,
}
actions = append(actions, m)
}
Expand All @@ -149,8 +151,8 @@ func marshalMacros(field client.Macro, d identifiableGetterSetter) error {
}

// unmarshalMacros parses the provided ResourceData and returns a user field
func unmarshalMacros(d identifiableGetterSetter) (client.Macro, error) {
tf := client.Macro{}
func unmarshalMacros(d identifiableGetterSetter) (models.Macro, error) {
tf := models.Macro{}

if v := d.Id(); v != "" {
id, err := strconv.ParseInt(v, 10, 64)
Expand Down Expand Up @@ -196,16 +198,27 @@ func unmarshalMacros(d identifiableGetterSetter) (client.Macro, error) {

if v, ok := d.GetOk("action"); ok {
macroActions := v.(*schema.Set).List()
actions := []client.MacroAction{}
actions := []models.MacroAction{}
for _, a := range macroActions {
action, ok := a.(map[string]interface{})
if !ok {
return tf, fmt.Errorf("could not parse actions for macro %v", tf)
}

actions = append(actions, client.MacroAction{
// If the action value is a list, unmarshal it
var actionValue interface{}
if strings.HasPrefix(action["value"].(string), "[") {
err := json.Unmarshal([]byte(action["value"].(string)), &actionValue)
if err != nil {
return tf, fmt.Errorf("error unmarshalling macro action value: %s", err)
}
} else {
actionValue = action["value"]
}

actions = append(actions, models.MacroAction{
Field: action["field"].(string),
Value: action["value"].(string),
Value: actionValue,
})
}
tf.Actions = actions
Expand Down
5 changes: 3 additions & 2 deletions zendesk/resource_zendesk_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ func resourceZendeskTrigger() *schema.Resource {
Required: true,
},
"value": {
Type: schema.TypeString,
Required: true,
Description: "Can be a single string value or a jsonencode'ed list",
Type: schema.TypeString,
Required: true,
},
},
},
Expand Down

0 comments on commit 5bc12f2

Please sign in to comment.