-
Notifications
You must be signed in to change notification settings - Fork 10
/
resources.go
132 lines (112 loc) · 4.06 KB
/
resources.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
// © 2023 Snyk Limited All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cloudapi
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"github.com/google/go-querystring/query"
)
type (
ResourcesParameters struct {
EnvironmentID []string `url:"environment_id,omitempty"`
ResourceType []string `url:"resource_type,omitempty"`
ResourceID []string `url:"resource_id,omitempty"`
NativeID []string `url:"native_id,omitempty"`
ID []string `url:"id,omitempty"`
Platform []string `url:"platform,omitempty"`
Name []string `url:"name,omitempty"`
Location []string `url:"location,omitempty"`
}
CollectionDocumentRes struct {
Data []ResourceObject `json:"data"`
Links Links
}
ResourceObject struct {
ID string `json:"id,omitempty"`
Type string `json:"type"`
Attributes ResourceAttributes `json:"attributes,omitempty"`
}
ResourceAttributes struct {
Namespace string `json:"namespace"`
ResourceType string `json:"resource_type"`
ResourceID string `json:"resource_id"`
State map[string]interface{} `json:"state"`
Tags map[string]interface{} `json:"tags"`
}
Links struct {
Next string `json:"next"`
}
)
var ErrInitializingResourcesRequest = errors.New("failed to initialize resources request")
var ErrEncodingResourcesQuery = errors.New("failed to encode resources query")
var ErrFetchingResources = errors.New("failed to fetch resources")
func (c *Client) Resources(ctx context.Context, orgID string, params ResourcesParameters) (resources []ResourceObject, e error) {
url := fmt.Sprintf("%s/rest/orgs/%s/cloud/resources", c.url, orgID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrInitializingResourcesRequest, err)
}
q, err := query.Values(params)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrEncodingResourcesQuery, err)
}
q.Add("version", c.version)
// We're hardcoding cloud here just to simplify things for now. If we do end
// up with a use-case for IaC resources from the Cloud API, we'll need to
// keep the input types in mind and produce multiple inputs from the
// response.
q.Add("kind", "cloud")
req.URL.RawQuery = q.Encode()
results, err := c.resourcesPage(ctx, req)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrFetchingResources, err)
}
resources = append(resources, results.Data...)
for results.Links.Next != "" {
url := fmt.Sprintf("%s/%s", c.url, strings.TrimPrefix(results.Links.Next, "/"))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrInitializingResourcesRequest, err)
}
results, err = c.resourcesPage(ctx, req)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrFetchingResources, err)
}
resources = append(resources, results.Data...)
}
return resources, nil
}
func (c *Client) resourcesPage(ctx context.Context, req *http.Request) (CollectionDocumentRes, error) {
var results CollectionDocumentRes
req.Header.Set("Content-Type", "application/vnd.api+json")
req.Header.Set("Authorization", c.authorization)
res, err := c.Do(req)
if err != nil {
return results, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return results, fmt.Errorf("invalid status code: %v", res.StatusCode)
}
body, _ := io.ReadAll(res.Body)
if err := json.Unmarshal(body, &results); err != nil {
return results, err
}
return results, nil
}