-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_square_catalog_item.go
165 lines (141 loc) · 3.84 KB
/
resource_square_catalog_item.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
164
165
package main
import (
"fmt"
"github.com/Houndie/square-go/objects"
"github.com/gofrs/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var variationSchema = &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"item_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"pricing_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"amount": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
},
}
func resourceCatalogItem() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"variation": &schema.Schema{
Type: schema.TypeSet,
Required: true,
Elem: variationSchema,
},
"version": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
},
CreateContext: resourceCatalogUpsert(catalogItemResourceToObject, catalogItemObjectToResource),
ReadContext: resourceCatalogRead(catalogItemObjectToResource),
UpdateContext: resourceCatalogUpsert(catalogItemResourceToObject, catalogItemObjectToResource),
DeleteContext: resourceCatalogDelete(),
}
}
func catalogItemResourceToObject(d *schema.ResourceData) (*objects.CatalogObject, error) {
id := d.Id()
if id == "" {
id = "#id"
}
dVariations := d.Get("variation").(*schema.Set)
variations := make([]*objects.CatalogObject, dVariations.Len())
for i, v := range dVariations.List() {
mv := v.(map[string]interface{})
vid := mv["id"].(string)
if vid == "" {
uid, err := uuid.NewV4()
if err != nil {
return nil, fmt.Errorf("error generating uuid for new variation: %w", err)
}
vid = "#" + uid.String()
}
var (
pricingType objects.CatalogPricingType
money *objects.Money
)
switch mv["pricing_type"].(string) {
case "FIXED_PRICING":
pricingType = objects.CatalogPricingTypeFixed
money = &objects.Money{
Amount: mv["amount"].(int),
Currency: "USD",
}
case "VARIABLE_PRICING":
pricingType = objects.CatalogPricingTypeVariable
}
variations[i] = &objects.CatalogObject{
ID: vid,
Type: &objects.CatalogItemVariation{
ItemID: id,
Name: mv["name"].(string),
PricingType: pricingType,
PriceMoney: money,
},
}
}
return &objects.CatalogObject{
ID: id,
Type: &objects.CatalogItem{
Name: d.Get("name").(string),
Variations: variations,
},
Version: d.Get("version").(int),
}, nil
}
func catalogItemObjectToResource(o *objects.CatalogObject, d *schema.ResourceData) error {
d.SetId(o.ID)
item, ok := o.Type.(*objects.CatalogItem)
if !ok {
return fmt.Errorf("catalog object is not a catalog item")
}
if err := d.Set("name", item.Name); err != nil {
return fmt.Errorf("error setting name: %w", err)
}
if len(item.Variations) < 1 {
return fmt.Errorf("expected at least one item variation")
}
variations := make([]interface{}, len(item.Variations))
for i, vo := range item.Variations {
v, ok := vo.Type.(*objects.CatalogItemVariation)
if !ok {
return fmt.Errorf("catalog object is not a catalog item variation")
}
var amount int
if v.PricingType == objects.CatalogPricingTypeFixed {
amount = v.PriceMoney.Amount
}
variations[i] = map[string]interface{}{
"id": vo.ID,
"name": v.Name,
"pricing_type": string(v.PricingType),
"amount": amount,
}
}
if err := d.Set("variation", schema.NewSet(schema.HashResource(variationSchema), variations)); err != nil {
return fmt.Errorf("error setting variations: %w", err)
}
if err := d.Set("version", o.Version); err != nil {
return fmt.Errorf("error setting version: %w", err)
}
return nil
}