-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresource_square_catalog_discount.go
163 lines (141 loc) · 4.58 KB
/
resource_square_catalog_discount.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"fmt"
"github.com/Houndie/square-go/objects"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceCatalogDiscount() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"percentage": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"amount": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"version": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
},
CreateContext: resourceCatalogUpsert(catalogDiscountResourceToObject, catalogDiscountObjectToResource),
ReadContext: resourceCatalogRead(catalogDiscountObjectToResource),
UpdateContext: resourceCatalogUpsert(catalogDiscountResourceToObject, catalogDiscountObjectToResource),
DeleteContext: resourceCatalogDelete(),
}
}
const (
catalogDiscountFixedAmount = "FIXED_AMOUNT"
catalogDiscountVariableAmount = "VARIABLE_AMOUNT"
catalogDiscountFixedPercentage = "FIXED_PERCENTAGE"
catalogDiscountVariablePercentage = "VARIABLE_PERCENTAGE"
)
func catalogDiscountResourceToObject(d *schema.ResourceData) (*objects.CatalogObject, error) {
id := d.Id()
if id == "" {
id = "#id"
}
var discountType objects.CatalogDiscountType
switch d.Get("type") {
case catalogDiscountFixedPercentage:
percentage := d.Get("percentage").(string)
if percentage == "" {
return nil, fmt.Errorf("percentage required with a type of %s", catalogDiscountFixedPercentage)
}
discountType = &objects.CatalogDiscountFixedPercentage{
Percentage: percentage,
}
case catalogDiscountVariablePercentage:
percentage := d.Get("percentage").(string)
if percentage == "" {
return nil, fmt.Errorf("percentage required with a type of %s", catalogDiscountVariablePercentage)
}
discountType = &objects.CatalogDiscountVariablePercentage{
Percentage: percentage,
}
case catalogDiscountFixedAmount:
amount := d.Get("amount").(int)
if amount == 0 {
return nil, fmt.Errorf("amount required with a type of %s", catalogDiscountFixedAmount)
}
discountType = &objects.CatalogDiscountFixedAmount{
AmountMoney: &objects.Money{
Amount: amount,
Currency: "USD",
},
}
case catalogDiscountVariableAmount:
amount := d.Get("amount").(int)
if amount == 0 {
return nil, fmt.Errorf("amount required with a type of %s", catalogDiscountVariableAmount)
}
discountType = &objects.CatalogDiscountVariableAmount{
AmountMoney: &objects.Money{
Amount: amount,
Currency: "USD",
},
}
}
return &objects.CatalogObject{
ID: id,
Type: &objects.CatalogDiscount{
Name: d.Get("name").(string),
DiscountType: discountType,
},
Version: d.Get("version").(int),
}, nil
}
func catalogDiscountObjectToResource(o *objects.CatalogObject, d *schema.ResourceData) error {
d.SetId(o.ID)
discount, ok := o.Type.(*objects.CatalogDiscount)
if !ok {
return fmt.Errorf("catalog object is not a catalog discount")
}
if err := d.Set("name", discount.Name); err != nil {
return fmt.Errorf("error setting name: %w", err)
}
switch t := discount.DiscountType.(type) {
case *objects.CatalogDiscountFixedPercentage:
if err := d.Set("type", catalogDiscountFixedPercentage); err != nil {
return fmt.Errorf("error setting fixed percentage type")
}
if err := d.Set("percentage", t.Percentage); err != nil {
return fmt.Errorf("error setting fixed percentage amount")
}
case *objects.CatalogDiscountVariablePercentage:
if err := d.Set("type", catalogDiscountVariablePercentage); err != nil {
return fmt.Errorf("error setting variable percentage type")
}
if err := d.Set("percentage", t.Percentage); err != nil {
return fmt.Errorf("error setting variable percentage amount")
}
case *objects.CatalogDiscountFixedAmount:
if err := d.Set("type", catalogDiscountFixedAmount); err != nil {
return fmt.Errorf("error setting fixed amount type")
}
if err := d.Set("amount", t.AmountMoney.Amount); err != nil {
return fmt.Errorf("error setting fixed amount amount")
}
case *objects.CatalogDiscountVariableAmount:
if err := d.Set("type", catalogDiscountVariableAmount); err != nil {
return fmt.Errorf("error setting variable amount type")
}
if err := d.Set("amount", t.AmountMoney.Amount); err != nil {
return fmt.Errorf("error setting variable amount amount")
}
}
if err := d.Set("version", o.Version); err != nil {
return fmt.Errorf("error setting version: %w", err)
}
return nil
}