-
Notifications
You must be signed in to change notification settings - Fork 24
/
api_handle.go
123 lines (107 loc) · 3.29 KB
/
api_handle.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package iamutil
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/hashicorp/errwrap"
"google.golang.org/api/googleapi"
)
type ApiHandle struct {
c *http.Client
userAgent string
}
func GetApiHandle(client *http.Client, userAgent string) *ApiHandle {
return &ApiHandle{
c: client,
userAgent: userAgent,
}
}
func (h *ApiHandle) DoGetRequest(ctx context.Context, r Resource, out interface{}) (err error) {
config := r.GetConfig()
req, err := constructRequest(r, &config.GetMethod, nil)
if err != nil {
return errwrap.Wrapf("Unable to construct Get request: {{err}}", err)
}
return h.doRequest(ctx, req, out)
}
func (h *ApiHandle) DoSetRequest(ctx context.Context, r Resource, data io.Reader, out interface{}) error {
config := r.GetConfig()
req, err := constructRequest(r, &config.SetMethod, data)
if err != nil {
return errwrap.Wrapf("Unable to construct Set request: {{err}}", err)
}
return h.doRequest(ctx, req, out)
}
func (h *ApiHandle) doRequest(ctx context.Context, req *http.Request, out interface{}) error {
if req.Header == nil {
req.Header = make(http.Header)
}
if h.userAgent != "" {
req.Header.Set("User-Agent", h.userAgent)
}
resp, err := h.c.Do(req.WithContext(ctx))
if err != nil {
return err
}
defer googleapi.CloseBody(resp)
if err := googleapi.CheckResponse(resp); err != nil {
return err
}
if err := json.NewDecoder(resp.Body).Decode(out); err != nil {
return errwrap.Wrapf("unable to decode JSON resp to output interface: {{err}}", err)
}
return nil
}
func constructRequest(r Resource, restMethod *RestMethod, data io.Reader) (*http.Request, error) {
config := r.GetConfig()
if data == nil && config != nil && config.Service == "cloudresourcemanager" {
// In order to support Resource Manager policies with conditional bindings,
// we need to request the policy version of 3. This request parameter is backwards compatible
// and will return version 1 policies if they are not yet updated to version 3.
requestPolicyVersion3 := `{"options": {"requestedPolicyVersion": 3}}`
data = strings.NewReader(requestPolicyVersion3)
}
req, err := http.NewRequest(
restMethod.HttpMethod,
googleapi.ResolveRelative(restMethod.BaseURL, restMethod.Path),
data)
if err != nil {
return nil, err
}
if req.Header == nil {
req.Header = make(http.Header)
}
if data != nil {
req.Header.Set("Content-Type", "application/json")
}
relId := r.GetRelativeId()
replacementMap := make(map[string]string)
if strings.Contains(restMethod.Path, "{+resource}") {
// +resource is used to represent full relative resource name
if len(config.Parameters) == 1 && config.Parameters[0] == "resource" {
relName := ""
tkns := strings.Split(config.TypeKey, "/")
for _, colId := range tkns {
if colName, ok := relId.IdTuples[colId]; ok {
relName += fmt.Sprintf("%s/%s/", colId, colName)
}
}
replacementMap["resource"] = strings.Trim(relName, "/")
}
} else {
for colId, resId := range relId.IdTuples {
rId, ok := config.CollectionReplacementKeys[colId]
if !ok {
return nil, fmt.Errorf("expected value for collection id %s", colId)
}
replacementMap[rId] = resId
}
}
googleapi.Expand(req.URL, replacementMap)
return req, nil
}