/
resource_inventory_model.go
170 lines (150 loc) · 5.69 KB
/
resource_inventory_model.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
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 resourceInventoryModel() *schema.Resource {
return &schema.Resource{
Description: "An inventory model defines a list of stock locations ordered by priority. The priority and " +
"cutoff determine how the availability of SKU's gets calculated within a market.",
ReadContext: resourceInventoryModelReadFunc,
CreateContext: resourceInventoryModelCreateFunc,
UpdateContext: resourceInventoryModelUpdateFunc,
DeleteContext: resourceInventoryModelDeleteFunc,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"id": {
Description: "The inventory model 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{
"name": {
Description: "The inventory model's internal name.",
Type: schema.TypeString,
Required: true,
},
"strategy": {
Description: "The inventory model's shipping strategy: one between 'no_split' (default), " +
"'split_shipments', 'ship_from_primary' and 'ship_from_first_available_or_primary'.",
Type: schema.TypeString,
Default: "no_split",
Optional: true,
ValidateDiagFunc: inventoryModelStrategyValidation,
},
"stock_locations_cutoff": {
Description: "The maximum number of stock locations used for inventory computation",
Type: schema.TypeInt,
Optional: true,
Default: 2,
},
"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 " +
"InventoryModeling 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,
},
},
},
},
},
}
}
func resourceInventoryModelReadFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)
resp, _, err := c.InventoryModelsApi.GETInventoryModelsInventoryModelId(ctx, d.Id()).Execute()
if err != nil {
return diagErr(err)
}
inventoryModel, ok := resp.GetDataOk()
if !ok {
d.SetId("")
return nil
}
d.SetId(inventoryModel.GetId())
return nil
}
func resourceInventoryModelCreateFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)
attributes := nestedMap(d.Get("attributes"))
inventoryModelCreate := commercelayer.InventoryModelCreate{
Data: commercelayer.InventoryModelCreateData{
Type: inventoryModelType,
Attributes: commercelayer.POSTInventoryModels201ResponseDataAttributes{
Name: attributes["name"].(string),
Strategy: stringRef(attributes["strategy"]),
StockLocationsCutoff: intToInt32Ref(attributes["stock_locations_cutoff"]),
Reference: stringRef(attributes["reference"]),
ReferenceOrigin: stringRef(attributes["reference_origin"]),
Metadata: keyValueRef(attributes["metadata"]),
},
},
}
err := d.Set("type", inventoryModelType)
if err != nil {
return diagErr(err)
}
inventoryModel, _, err := c.InventoryModelsApi.POSTInventoryModels(ctx).InventoryModelCreate(inventoryModelCreate).Execute()
if err != nil {
return diagErr(err)
}
d.SetId(*inventoryModel.Data.Id)
return nil
}
func resourceInventoryModelDeleteFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)
_, err := c.InventoryModelsApi.DELETEInventoryModelsInventoryModelId(ctx, d.Id()).Execute()
return diag.FromErr(err)
}
func resourceInventoryModelUpdateFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)
attributes := nestedMap(d.Get("attributes"))
var inventoryModelUpdate = commercelayer.InventoryModelUpdate{
Data: commercelayer.InventoryModelUpdateData{
Type: inventoryModelType,
Id: d.Id(),
Attributes: commercelayer.PATCHInventoryModelsInventoryModelId200ResponseDataAttributes{
Name: stringRef(attributes["name"]),
Strategy: stringRef(attributes["strategy"]),
StockLocationsCutoff: intToInt32Ref(attributes["stock_locations_cutoff"]),
Reference: stringRef(attributes["reference"]),
ReferenceOrigin: stringRef(attributes["reference_origin"]),
Metadata: keyValueRef(attributes["metadata"]),
},
},
}
_, _, err := c.InventoryModelsApi.PATCHInventoryModelsInventoryModelId(ctx, d.Id()).
InventoryModelUpdate(inventoryModelUpdate).Execute()
return diag.FromErr(err)
}