Skip to content

Commit fef4e07

Browse files
committed
chore(deps): reduce gcp dependencies
1 parent c40dc36 commit fef4e07

30 files changed

Lines changed: 2330 additions & 324 deletions

go.mod

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,17 @@ go 1.24
55
require (
66
github.com/404tk/go-prompt v0.0.1
77
github.com/404tk/table v0.0.4
8-
golang.org/x/oauth2 v0.12.0
98
gopkg.in/yaml.v3 v3.0.1
109
)
1110

1211
require (
13-
cloud.google.com/go/compute v1.20.1 // indirect
14-
cloud.google.com/go/compute/metadata v0.2.3 // indirect
15-
github.com/golang/protobuf v1.5.3 // indirect
1612
github.com/kr/pretty v0.2.0 // indirect
1713
github.com/mattn/go-colorable v0.1.12 // indirect
1814
github.com/mattn/go-isatty v0.0.14 // indirect
1915
github.com/mattn/go-runewidth v0.0.9 // indirect
2016
github.com/mattn/go-tty v0.0.5 // indirect
2117
github.com/olekukonko/tablewriter v0.0.5 // indirect
2218
github.com/pkg/term v1.2.0-beta.2 // indirect
23-
golang.org/x/net v0.15.0 // indirect
2419
golang.org/x/sys v0.15.0 // indirect
25-
google.golang.org/appengine v1.6.7 // indirect
26-
google.golang.org/protobuf v1.31.0 // indirect
2720
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
2821
)

go.sum

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
1-
cloud.google.com/go/compute v1.20.1 h1:6aKEtlUiwEpJzM001l0yFkpXmUVXaN8W+fbkb2AZNbg=
2-
cloud.google.com/go/compute v1.20.1/go.mod h1:4tCnrn48xsqlwSAiLf1HXMQk8CONslYbdiEZc9FEIbM=
3-
cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY=
4-
cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA=
51
github.com/404tk/go-prompt v0.0.1 h1:PUa2qXxzidYrn0sZITvMTCQ6wKqpODB06ohcjPs60RU=
62
github.com/404tk/go-prompt v0.0.1/go.mod h1:BU3cEVX1xhNDco8T27cBQ8lzkY8P+xUCLotUpjTJR7o=
73
github.com/404tk/table v0.0.4 h1:WlhqtfsX8zEHMwBJRknq2D5J2ew9EvMHw4ahD8EBSq0=
84
github.com/404tk/table v0.0.4/go.mod h1:bMAHHOKSl6wXuSY6TX6t/f3/XfCNsbjmzZKb6S9PjsQ=
9-
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
10-
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
11-
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
12-
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
13-
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
14-
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
15-
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
165
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
176
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
187
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -36,13 +25,6 @@ github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N
3625
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
3726
github.com/pkg/term v1.2.0-beta.2 h1:L3y/h2jkuBVFdWiJvNfYfKmzcCnILw7mJWm2JQuMppw=
3827
github.com/pkg/term v1.2.0-beta.2/go.mod h1:E25nymQcrSllhX42Ok8MRm1+hyBdHY0dCeiKZ9jpNGw=
39-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
40-
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
41-
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
42-
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
43-
golang.org/x/oauth2 v0.12.0 h1:smVPGxink+n1ZI5pkQa8y6fZT0RW0MgCO5bFpepy4B4=
44-
golang.org/x/oauth2 v0.12.0/go.mod h1:A74bZ3aGXgCY0qaIC9Ahg6Lglin4AMAco8cIv9baba4=
45-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
4628
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
4729
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4830
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -54,16 +36,6 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBc
5436
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5537
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
5638
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
57-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
58-
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
59-
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
60-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
61-
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
62-
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
63-
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
64-
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
65-
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
66-
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
6739
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6840
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
6941
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

pkg/providers/gcp/api/client.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package api
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"fmt"
8+
"io"
9+
"net/http"
10+
"net/url"
11+
"strings"
12+
13+
"github.com/404tk/cloudtoolkit/pkg/providers/gcp/auth"
14+
)
15+
16+
type Request struct {
17+
Method string
18+
BaseURL string
19+
Path string
20+
Query url.Values
21+
Headers http.Header
22+
Body []byte
23+
Idempotent bool
24+
}
25+
26+
type Option func(*Client)
27+
28+
type Client struct {
29+
tokenSource *auth.TokenSource
30+
httpClient *http.Client
31+
retryPolicy RetryPolicy
32+
}
33+
34+
func NewClient(ts *auth.TokenSource, opts ...Option) *Client {
35+
client := &Client{
36+
tokenSource: ts,
37+
httpClient: NewHTTPClient(),
38+
retryPolicy: DefaultRetryPolicy(),
39+
}
40+
for _, opt := range opts {
41+
opt(client)
42+
}
43+
return client
44+
}
45+
46+
func WithHTTPClient(hc *http.Client) Option {
47+
return func(c *Client) {
48+
if hc != nil {
49+
c.httpClient = hc
50+
}
51+
}
52+
}
53+
54+
func WithRetryPolicy(p RetryPolicy) Option {
55+
return func(c *Client) {
56+
if p != nil {
57+
c.retryPolicy = p
58+
}
59+
}
60+
}
61+
62+
func (c *Client) Do(ctx context.Context, req Request, out any) error {
63+
if ctx == nil {
64+
ctx = context.Background()
65+
}
66+
if c.tokenSource == nil {
67+
return fmt.Errorf("gcp client: nil token source")
68+
}
69+
70+
token, err := c.tokenSource.Token(ctx)
71+
if err != nil {
72+
return err
73+
}
74+
75+
method := strings.ToUpper(strings.TrimSpace(req.Method))
76+
if method == "" {
77+
method = http.MethodGet
78+
}
79+
requestURL, err := resolveURL(req.BaseURL, req.Path, req.Query)
80+
if err != nil {
81+
return err
82+
}
83+
84+
headers := cloneHeader(req.Headers)
85+
headers.Set("Authorization", "Bearer "+token.AccessToken)
86+
if req.Body != nil && strings.TrimSpace(headers.Get("Content-Type")) == "" {
87+
headers.Set("Content-Type", "application/json; charset=UTF-8")
88+
}
89+
90+
httpResp, err := c.retryPolicy.Do(ctx, req.Idempotent, func() (*http.Response, error) {
91+
httpReq, err := http.NewRequestWithContext(ctx, method, requestURL, bytes.NewReader(req.Body))
92+
if err != nil {
93+
return nil, err
94+
}
95+
httpReq.Header = headers.Clone()
96+
return c.httpClient.Do(httpReq)
97+
})
98+
if err != nil {
99+
return err
100+
}
101+
defer closeResponse(httpResp)
102+
103+
body, err := io.ReadAll(httpResp.Body)
104+
if err != nil {
105+
return fmt.Errorf("read gcp response: %w", err)
106+
}
107+
if err := DecodeError(httpResp.StatusCode, body); err != nil {
108+
return err
109+
}
110+
if out == nil || len(body) == 0 || httpResp.StatusCode == http.StatusNoContent {
111+
return nil
112+
}
113+
if err := json.Unmarshal(body, out); err != nil {
114+
return fmt.Errorf("decode gcp response: %w", err)
115+
}
116+
return nil
117+
}
118+
119+
func resolveURL(baseURL, path string, query url.Values) (string, error) {
120+
baseURL = strings.TrimRight(strings.TrimSpace(baseURL), "/")
121+
if baseURL == "" {
122+
return "", fmt.Errorf("gcp client: empty base url")
123+
}
124+
parsed, err := url.Parse(baseURL + ensureLeadingSlash(path))
125+
if err != nil {
126+
return "", err
127+
}
128+
parsed.RawQuery = cloneValues(query).Encode()
129+
return parsed.String(), nil
130+
}
131+
132+
func cloneHeader(headers http.Header) http.Header {
133+
if headers == nil {
134+
return http.Header{}
135+
}
136+
return headers.Clone()
137+
}
138+
139+
func cloneValues(values url.Values) url.Values {
140+
if values == nil {
141+
return url.Values{}
142+
}
143+
cloned := make(url.Values, len(values))
144+
for key, items := range values {
145+
cloned[key] = append([]string(nil), items...)
146+
}
147+
return cloned
148+
}
149+
150+
func ensureLeadingSlash(path string) string {
151+
path = strings.TrimSpace(path)
152+
if path == "" {
153+
return "/"
154+
}
155+
if strings.HasPrefix(path, "/") {
156+
return path
157+
}
158+
return "/" + path
159+
}
160+
161+
func closeResponse(resp *http.Response) {
162+
if resp == nil || resp.Body == nil {
163+
return
164+
}
165+
_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 8<<10))
166+
_ = resp.Body.Close()
167+
}

0 commit comments

Comments
 (0)