/
resource_delivery_lead_time.go
206 lines (185 loc) · 7.26 KB
/
resource_delivery_lead_time.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package commercelayer
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
commercelayer "github.com/incentro-dc/go-commercelayer-sdk/api"
)
func resourceDeliveryLeadTime() *schema.Resource {
return &schema.Resource{
Description: "Delivery lead times provide customers with detailed information about their shipments. " +
"This is useful if you ship from many stock locations or offer more shipping method options within a market.",
ReadContext: resourceDeliveryLeadTimesReadFunc,
CreateContext: resourceDeliveryLeadTimesCreateFunc,
UpdateContext: resourceDeliveryLeadTimesUpdateFunc,
DeleteContext: resourceDeliveryLeadTimesDeleteFunc,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"id": {
Description: "The delivery lead time unique identifier",
Type: schema.TypeString,
Computed: true,
},
"type": {
Description: "The resource type",
Type: schema.TypeString,
Computed: true,
},
"attributes": {
Description: "Resource attributes",
Type: schema.TypeList,
MaxItems: 1,
MinItems: 1,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"min_hours": {
Description: "The delivery lead minimum time (in hours) when shipping from the associated stock location with the associated shipping method.",
Type: schema.TypeInt,
Required: true,
},
"max_hours": {
Description: "The delivery lead maximum time (in hours) when shipping from the associated stock location with the associated shipping method.",
Type: schema.TypeInt,
Required: true,
},
"reference": {
Description: "A string that you can use to add any external identifier to the resource. This " +
"can be useful for integrating the resource to an external system, like an ERP, a " +
"marketing tool, a CRM, or whatever.",
Type: schema.TypeString,
Optional: true,
},
"reference_origin": {
Description: "Any identifier of the third party system that defines the reference code",
Type: schema.TypeString,
Optional: true,
},
"metadata": {
Description: "Set of key-value pairs that you can attach to the resource. This can be useful " +
"for storing additional information about the resource in a structured format",
Type: schema.TypeMap,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
},
},
},
},
"relationships": {
Description: "Resource relationships",
Type: schema.TypeList,
MaxItems: 1,
MinItems: 1,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"stock_location_id": {
Description: "The associated stock location id.",
Type: schema.TypeString,
Required: true,
},
"shipping_method_id": {
Description: "The associated shipping method id.",
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
func resourceDeliveryLeadTimesReadFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)
resp, _, err := c.DeliveryLeadTimesApi.GETDeliveryLeadTimesDeliveryLeadTimeId(ctx, d.Id()).Execute()
if err != nil {
return diagErr(err)
}
deliveryLeadTime, ok := resp.GetDataOk()
if !ok {
d.SetId("")
return nil
}
d.SetId(deliveryLeadTime.GetId())
return nil
}
func resourceDeliveryLeadTimesCreateFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)
attributes := nestedMap(d.Get("attributes"))
relationships := nestedMap(d.Get("relationships"))
deliveryLeadTimeCreate := commercelayer.DeliveryLeadTimeCreate{
Data: commercelayer.DeliveryLeadTimeCreateData{
Type: deliveryLeadTimesType,
Attributes: commercelayer.POSTDeliveryLeadTimes201ResponseDataAttributes{
MinHours: int32((attributes["min_hours"]).(int)),
MaxHours: int32((attributes["max_hours"]).(int)),
Reference: stringRef(attributes["reference"]),
ReferenceOrigin: stringRef(attributes["reference_origin"]),
Metadata: keyValueRef(attributes["metadata"]),
},
Relationships: &commercelayer.DeliveryLeadTimeCreateDataRelationships{
StockLocation: commercelayer.DeliveryLeadTimeCreateDataRelationshipsStockLocation{
Data: commercelayer.DeliveryLeadTimeDataRelationshipsStockLocationData{
Type: stringRef(stockLocationType),
Id: stringRef(relationships["stock_location_id"]),
}},
ShippingMethod: commercelayer.DeliveryLeadTimeCreateDataRelationshipsShippingMethod{
Data: commercelayer.DeliveryLeadTimeDataRelationshipsShippingMethodData{
Type: stringRef(shippingMethodType),
Id: stringRef(relationships["shipping_method_id"]),
}},
},
},
}
err := d.Set("type", deliveryLeadTimesType)
if err != nil {
return diagErr(err)
}
deliveryLeadTimes, _, err := c.DeliveryLeadTimesApi.POSTDeliveryLeadTimes(ctx).DeliveryLeadTimeCreate(deliveryLeadTimeCreate).Execute()
if err != nil {
return diagErr(err)
}
d.SetId(*deliveryLeadTimes.Data.Id)
return nil
}
func resourceDeliveryLeadTimesDeleteFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)
_, err := c.DeliveryLeadTimesApi.DELETEDeliveryLeadTimesDeliveryLeadTimeId(ctx, d.Id()).Execute()
return diag.FromErr(err)
}
func resourceDeliveryLeadTimesUpdateFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)
attributes := nestedMap(d.Get("attributes"))
relationships := nestedMap(d.Get("relationships"))
var deliveryLeadTimeUpdate = commercelayer.DeliveryLeadTimeUpdate{
Data: commercelayer.DeliveryLeadTimeUpdateData{
Type: deliveryLeadTimesType,
Id: d.Id(),
Attributes: commercelayer.PATCHDeliveryLeadTimesDeliveryLeadTimeId200ResponseDataAttributes{
MinHours: intToInt32Ref(attributes["min_hours"]),
MaxHours: intToInt32Ref(attributes["max_hours"]),
Reference: stringRef(attributes["reference"]),
ReferenceOrigin: stringRef(attributes["reference_origin"]),
Metadata: keyValueRef(attributes["metadata"]),
},
Relationships: &commercelayer.DeliveryLeadTimeUpdateDataRelationships{
StockLocation: &commercelayer.DeliveryLeadTimeCreateDataRelationshipsStockLocation{
Data: commercelayer.DeliveryLeadTimeDataRelationshipsStockLocationData{
Type: stringRef(stockLocationType),
Id: stringRef(relationships["stock_location_id"]),
}},
ShippingMethod: &commercelayer.DeliveryLeadTimeCreateDataRelationshipsShippingMethod{
Data: commercelayer.DeliveryLeadTimeDataRelationshipsShippingMethodData{
Type: stringRef(shippingMethodType),
Id: stringRef(relationships["shipping_method_id"]),
}},
},
},
}
_, _, err := c.DeliveryLeadTimesApi.PATCHDeliveryLeadTimesDeliveryLeadTimeId(ctx, d.Id()).DeliveryLeadTimeUpdate(deliveryLeadTimeUpdate).Execute()
return diag.FromErr(err)
}