Skip to content

Commit c27f9f8

Browse files
committed
Merge branch 'variable_methods'
2 parents 2d518ec + 2393b33 commit c27f9f8

File tree

5 files changed

+68
-6
lines changed

5 files changed

+68
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ Have a look at the [examples directory](examples) for some use cases
2828
- `headers` (hash of strings, optional): A map of header names and values to set on all outbound requests. This is useful if you want to use a script via the 'external' provider or provide a pre-approved token or change Content-Type from `application/json`. If `username` and `password` are set and Authorization is one of the headers defined here, the BASIC auth credentials take precedence.
2929
- `timeout` (integer, optional): When set, will cause requests taking longer than this time (in seconds) to be aborted. Default is `0` which means no timeout is set.
3030
- `id_attribute` (string, optional): Defaults to `id`. When set, this key will be used to operate on REST objects. For example, if the ID is set to 'name', changes to the API object will be to http://foo.com/bar/VALUE_OF_NAME. This value may also be a '/'-delimeted path to the id attribute if it is multple levels deep in the data (such as `attributes/id` in the case of an object `{ \"attributes\": { \"id\": 1234 }, \"config\": { \"name\": \"foo\", \"something\": \"bar\"}}`. Lists are also supported, f.e. `attributes/items/0/id` in case of an object `{ \"attributes\": { \"items\": [{"id": 1234}] } }`.
31+
- `create_method` (string, optional): Defaults to `POST`. The HTTP method used to CREATE objects of this type on the API server.
32+
- `read_method` (string, optional): Defaults to `GET`. The HTTP method used to READ objects of this type on the API server.
33+
- `update_method` (string, optional): Defaults to `PUT`. The HTTP method used to UPDATE objects of this type on the API server.
34+
- `delete_method` (string, optional): Defaults to `DELETE`. The HTTP method used to DELETE objects of this type on the API server.
3135
- `copy_keys` (array of strings, optional): When set, any `PUT` to the API for an object will copy these keys from the data the provider has gathered about the object. This is useful if internal API information must also be provided with updates, such as the revision of the object.
3236
- `write_returns_object` (boolean, optional): Set this when the API returns the object created on all write operations (`POST`, `PUT`). This is used by the provider to refresh internal data structures.
3337
- `create_returns_object` (boolean, optional): Set this when the API returns the object created only on creation operations (`POST`). This is used by the provider to refresh internal data structures.

restapi/api_client.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ type apiClientOpt struct {
2222
use_cookie bool
2323
timeout int
2424
id_attribute string
25+
create_method string
26+
read_method string
27+
update_method string
28+
destroy_method string
2529
copy_keys []string
2630
write_returns_object bool
2731
create_returns_object bool
@@ -41,6 +45,10 @@ type api_client struct {
4145
use_cookie bool
4246
timeout int
4347
id_attribute string
48+
create_method string
49+
read_method string
50+
update_method string
51+
destroy_method string
4452
copy_keys []string
4553
write_returns_object bool
4654
create_returns_object bool
@@ -69,6 +77,19 @@ func NewAPIClient(opt *apiClientOpt) (*api_client, error) {
6977
opt.uri = opt.uri[:len(opt.uri)-1]
7078
}
7179

80+
if opt.create_method == "" {
81+
opt.create_method = "POST"
82+
}
83+
if opt.read_method == "" {
84+
opt.read_method = "GET"
85+
}
86+
if opt.update_method == "" {
87+
opt.create_method = "PUT"
88+
}
89+
if opt.destroy_method == "" {
90+
opt.destroy_method = "DELETE"
91+
}
92+
7293
/* Disable TLS verification if requested */
7394
tr := &http.Transport{
7495
TLSClientConfig: &tls.Config{InsecureSkipVerify: opt.insecure},
@@ -92,6 +113,10 @@ func NewAPIClient(opt *apiClientOpt) (*api_client, error) {
92113
password: opt.password,
93114
headers: opt.headers,
94115
id_attribute: opt.id_attribute,
116+
create_method: opt.create_method,
117+
read_method: opt.read_method,
118+
update_method: opt.update_method,
119+
destroy_method: opt.destroy_method,
95120
copy_keys: opt.copy_keys,
96121
write_returns_object: opt.write_returns_object,
97122
create_returns_object: opt.create_returns_object,

restapi/api_client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestAPIClient(t *testing.T) {
3434
client, _ := NewAPIClient(opt)
3535

3636
var res string
37-
var err error
37+
var err error
3838

3939
if debug {
4040
log.Printf("api_client_test.go: Testing standard OK request\n")

restapi/api_object.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func (obj *api_object) create_object() error {
192192
}
193193

194194
b, _ := json.Marshal(obj.data)
195-
res_str, err := obj.api_client.send_request("POST", strings.Replace(obj.post_path, "{id}", obj.id, -1), string(b))
195+
res_str, err := obj.api_client.send_request(obj.api_client.create_method, strings.Replace(obj.post_path, "{id}", obj.id, -1), string(b))
196196
if err != nil {
197197
return err
198198
}
@@ -224,7 +224,7 @@ func (obj *api_object) read_object() error {
224224
return errors.New("Cannot read an object unless the ID has been set.")
225225
}
226226

227-
res_str, err := obj.api_client.send_request("GET", strings.Replace(obj.get_path, "{id}", obj.id, -1), "")
227+
res_str, err := obj.api_client.send_request(obj.api_client.read_method, strings.Replace(obj.get_path, "{id}", obj.id, -1), "")
228228
if err != nil {
229229
return err
230230
}
@@ -239,7 +239,7 @@ func (obj *api_object) update_object() error {
239239
}
240240

241241
b, _ := json.Marshal(obj.data)
242-
res_str, err := obj.api_client.send_request("PUT", strings.Replace(obj.put_path, "{id}", obj.id, -1), string(b))
242+
res_str, err := obj.api_client.send_request(obj.api_client.update_method, strings.Replace(obj.put_path, "{id}", obj.id, -1), string(b))
243243
if err != nil {
244244
return err
245245
}
@@ -264,7 +264,7 @@ func (obj *api_object) delete_object() error {
264264
return nil
265265
}
266266

267-
_, err := obj.api_client.send_request("DELETE", strings.Replace(obj.delete_path, "{id}", obj.id, -1), "")
267+
_, err := obj.api_client.send_request(obj.api_client.destroy_method, strings.Replace(obj.delete_path, "{id}", obj.id, -1), "")
268268
if err != nil {
269269
return err
270270
}
@@ -290,7 +290,7 @@ func (obj *api_object) find_object(query_string string, search_key string, searc
290290
if obj.debug {
291291
log.Printf("datasource_api_object.go: Calling API on path '%s'", search_path)
292292
}
293-
res_str, err := obj.api_client.send_request("GET", search_path, "")
293+
res_str, err := obj.api_client.send_request(obj.api_client.read_method, search_path, "")
294294
if err != nil {
295295
return err
296296
}

restapi/provider.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ func Provider() terraform.ResourceProvider {
5656
DefaultFunc: schema.EnvDefaultFunc("REST_API_ID_ATTRIBUTE", nil),
5757
Description: "When set, this key will be used to operate on REST objects. For example, if the ID is set to 'name', changes to the API object will be to http://foo.com/bar/VALUE_OF_NAME. This value may also be a '/'-delimeted path to the id attribute if it is multple levels deep in the data (such as `attributes/id` in the case of an object `{ \"attributes\": { \"id\": 1234 }, \"config\": { \"name\": \"foo\", \"something\": \"bar\"}}`",
5858
},
59+
"create_method": &schema.Schema{
60+
Type: schema.TypeString,
61+
Description: "Defaults to `POST`. The HTTP method used to CREATE objects of this type on the API server.",
62+
Optional: true,
63+
},
64+
"read_method": &schema.Schema{
65+
Type: schema.TypeString,
66+
Description: "Defaults to `GET`. The HTTP method used to READ objects of this type on the API server.",
67+
Optional: true,
68+
},
69+
"update_method": &schema.Schema{
70+
Type: schema.TypeString,
71+
Description: "Defaults to `PUT`. The HTTP method used to UPDATE objects of this type on the API server.",
72+
Optional: true,
73+
},
74+
"destroy_method": &schema.Schema{
75+
Type: schema.TypeString,
76+
Description: "Defaults to `DELETE`. The HTTP method used to DELETE objects of this type on the API server.",
77+
Optional: true,
78+
},
5979
"copy_keys": &schema.Schema{
6080
Type: schema.TypeList,
6181
Elem: &schema.Schema{Type: schema.TypeString},
@@ -135,6 +155,19 @@ func configureProvider(d *schema.ResourceData) (interface{}, error) {
135155
debug: d.Get("debug").(bool),
136156
}
137157

158+
if v, ok := d.GetOk("create_method"); ok {
159+
opt.create_method = v.(string)
160+
}
161+
if v, ok := d.GetOk("read_method"); ok {
162+
opt.read_method = v.(string)
163+
}
164+
if v, ok := d.GetOk("update_method"); ok {
165+
opt.update_method = v.(string)
166+
}
167+
if v, ok := d.GetOk("destroy_method"); ok {
168+
opt.destroy_method = v.(string)
169+
}
170+
138171
client, err := NewAPIClient(opt)
139172
return client, err
140173
}

0 commit comments

Comments
 (0)