forked from kradalby/govcloudair
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
61 lines (49 loc) · 1.37 KB
/
helper.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
package govcloudair
import (
"bytes"
"encoding/xml"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/kublr/govcloudair/types/v56"
)
func GetVersionHeader(version types.ApiVersionType) (key, value string) {
return "Accept", fmt.Sprintf("application/*+xml;version=%s", version)
}
func GetAuthorizationHeader(token string) (key, value string) {
return "x-vcloud-authorization", token
}
func ExecuteRequest(payload, path, type_, contentType string, client *Client) (Task, error) {
s, _ := url.ParseRequestURI(path)
var req *http.Request
switch type_ {
case "POST":
b := bytes.NewBufferString(xml.Header + payload)
req = client.NewRequest(map[string]string{}, type_, *s, b)
default:
req = client.NewRequest(map[string]string{}, type_, *s, nil)
}
if contentType != "" {
req.Header.Add("Content-Type", contentType)
}
resp, err := checkResp(client.Http.Do(req))
if err != nil {
return Task{}, err
}
defer resp.Body.Close()
task := NewTask(client)
if err = decodeBody(resp, task.Task); err != nil {
return Task{}, fmt.Errorf("error decoding Task response: %s", err)
}
return *task, nil
}
// Extract ID from an URN.
// 'urn:vcloud:catalog:39867ab4-04e0-4b13-b468-08abcc1de810' will produce '39867ab4-04e0-4b13-b468-08abcc1de810'
func ExtractID(urn string) string {
parts := strings.Split(urn, ":")
if len(parts) > 0 {
return parts[len(parts)-1]
}
return urn
}