-
Notifications
You must be signed in to change notification settings - Fork 0
/
support_bundle_spec.go
146 lines (117 loc) · 3.08 KB
/
support_bundle_spec.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
package graphql
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/pkg/errors"
)
const customerSpecQuery = `
query {
supportBundleSpec {
spec
hydrated
}
}
`
const startUploadMutation = `
mutation GetPresignedURI($size: Int, $notes: String) {
uploadSupportBundle(size: $size, notes: $notes) {
uploadUri,
supportBundle {
id
}
}
}
`
const finishUploadMutation = `
mutation SetBundleStatus($id: String!, $status: String!) {
updateSupportBundle(id: $id, status: $status) {
id
}
}
`
type SupportBundleSpec struct {
CustomerID string
}
type Client struct {
endpoint string
client *http.Client
}
func NewClient(endpoint string, client *http.Client) *Client {
if client == nil {
client = http.DefaultClient
}
return &Client{
endpoint: endpoint,
client: client,
}
}
func (c *Client) GetCustomerSpec(id string) ([]byte, error) {
resp, err := c.executeGraphQLQuery(id, Request{
Query: customerSpecQuery,
})
if err != nil {
return nil, errors.Wrap(err, "executing graphql request")
}
decoder := json.NewDecoder(resp.Body)
specBody := SupportBundleResponse{}
if err := decoder.Decode(&specBody); err != nil {
return nil, errors.Wrap(err, "unmarshalling graphql response")
}
if specBody.Errors != nil && len(specBody.Errors) > 0 {
return nil, Errors{Errors: specBody.Errors}
}
return []byte(specBody.Data.Hydrated), nil
}
func (c *Client) GetSupportBundleUploadURI(id string, size int64, notes string) (string, *url.URL, error) {
resp, err := c.executeGraphQLQuery(id, Request{
Query: startUploadMutation,
Variables: map[string]interface{}{
"size": size,
"notes": notes,
},
})
if err != nil {
return "", nil, errors.Wrap(err, "executing graphql request")
}
decoder := json.NewDecoder(resp.Body)
uploadBody := SupportBundleUploadResponse{}
if err := decoder.Decode(&uploadBody); err != nil {
return "", nil, errors.Wrap(err, "unmarshalling graphql response")
}
if uploadBody.Errors != nil && len(uploadBody.Errors) > 0 {
return "", nil, fmt.Errorf("%v", uploadBody.Errors)
}
uri, err := url.Parse(uploadBody.Data.UploadURI)
if err != nil {
return "", nil, errors.Wrap(err, "parsing upload URI")
}
return uploadBody.Data.ID, uri, nil
}
func (c *Client) UpdateSupportBundleStatus(customerID string, bundleID, status string) error {
_, err := c.executeGraphQLQuery(customerID, Request{
Query: finishUploadMutation,
Variables: map[string]interface{}{
"id": bundleID,
"status": status,
},
})
return err
}
func (c *Client) executeGraphQLQuery(auth string, gr Request) (*http.Response, error) {
body, err := json.Marshal(gr)
if err != nil {
return nil, errors.Wrap(err, "marshalling graphql request")
}
bodyReader := bytes.NewReader(body)
req, err := http.NewRequest("POST", c.endpoint, bodyReader)
if err != nil {
return nil, errors.Wrap(err, "creating http request")
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(auth+":"))))
return c.client.Do(req)
}