/
resource_google_geocoder.go
160 lines (139 loc) · 5.14 KB
/
resource_google_geocoder.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
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 resourceGoogleGeocoders() *schema.Resource {
return &schema.Resource{
Description: "Geocoders lets you convert an address in text form into geographic coordinates " +
"(like latitude and longitude). A geocoder can be optionally associated with a market. " +
"By connecting a geocoder to a market, all the shipping and billing addresses belonging " +
"to that market orders will be geocoded",
ReadContext: resourceGoogleGeocodersReadFunc,
CreateContext: resourceGoogleGeocodersCreateFunc,
UpdateContext: resourceGoogleGeocodersUpdateFunc,
DeleteContext: resourceGoogleGeocodersDeleteFunc,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"id": {
Description: "The google geocoder 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 geocoder's internal name.",
Type: schema.TypeString,
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,
},
"api_key": {
Description: "The Google Map API key",
Type: schema.TypeString,
Required: 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 resourceGoogleGeocodersReadFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)
resp, _, err := c.GoogleGeocodersApi.GETGoogleGeocodersGoogleGeocoderId(ctx, d.Id()).Execute()
if err != nil {
return diagErr(err)
}
googleGeocoder, ok := resp.GetDataOk()
if !ok {
d.SetId("")
return nil
}
d.SetId(googleGeocoder.GetId())
return nil
}
func resourceGoogleGeocodersCreateFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)
attributes := nestedMap(d.Get("attributes"))
googleGeocoderCreate := commercelayer.GoogleGeocoderCreate{
Data: commercelayer.GoogleGeocoderCreateData{
Type: googleGeocodersType,
Attributes: commercelayer.POSTGoogleGeocoders201ResponseDataAttributes{
Name: attributes["name"].(string),
Reference: stringRef(attributes["reference"]),
ReferenceOrigin: stringRef(attributes["reference_origin"]),
Metadata: keyValueRef(attributes["metadata"]),
ApiKey: attributes["api_key"].(string),
},
},
}
err := d.Set("type", googleGeocodersType)
if err != nil {
return diagErr(err)
}
googleGeocoders, _, err := c.GoogleGeocodersApi.POSTGoogleGeocoders(ctx).GoogleGeocoderCreate(googleGeocoderCreate).Execute()
if err != nil {
return diagErr(err)
}
d.SetId(*googleGeocoders.Data.Id)
return nil
}
func resourceGoogleGeocodersDeleteFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)
_, err := c.GoogleGeocodersApi.DELETEGoogleGeocodersGoogleGeocoderId(ctx, d.Id()).Execute()
return diag.FromErr(err)
}
func resourceGoogleGeocodersUpdateFunc(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
c := i.(*commercelayer.APIClient)
attributes := nestedMap(d.Get("attributes"))
var googleGeocodersUpdate = commercelayer.GoogleGeocoderUpdate{
Data: commercelayer.GoogleGeocoderUpdateData{
Type: googleGeocodersType,
Id: d.Id(),
Attributes: commercelayer.PATCHGoogleGeocodersGoogleGeocoderId200ResponseDataAttributes{
Name: stringRef(attributes["name"]),
Reference: stringRef(attributes["reference"]),
ReferenceOrigin: stringRef(attributes["reference_origin"]),
Metadata: keyValueRef(attributes["metadata"]),
ApiKey: stringRef(attributes["api_key"]),
},
},
}
_, _, err := c.GoogleGeocodersApi.PATCHGoogleGeocodersGoogleGeocoderId(ctx, d.Id()).GoogleGeocoderUpdate(googleGeocodersUpdate).Execute()
return diag.FromErr(err)
}