Skip to content

Commit f3188cb

Browse files
feat(client): add update plugin methods
1 parent 23497fa commit f3188cb

File tree

2 files changed

+91
-8
lines changed

2 files changed

+91
-8
lines changed

pkg/client/client.go

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,38 @@ func New(registryURL string) *Client {
3131
return &Client{
3232
registryURL: registryURL,
3333
httpClient: &http.Client{
34-
Timeout: 30 * time.Second,
34+
Timeout: 5 * time.Minute,
3535
},
3636
}
3737
}
3838

39-
func (c *Client) sendRequest(ctx context.Context, method, endpoint string, body io.Reader) (*http.Response, error) {
40-
url, err := url.JoinPath(c.registryURL, endpoint)
39+
func setAuth(adminAccessToken string) func(r *http.Request) {
40+
return func(r *http.Request) {
41+
r.Header.Set("Authorization", adminAccessToken)
42+
}
43+
}
44+
45+
func getPluginURL(pluginName string) string {
46+
return fmt.Sprintf("plugins/%s", pluginName)
47+
}
48+
49+
func getPluginReleaseURL(pluginName, version string) string {
50+
return fmt.Sprintf("%s/versions/%s", getPluginURL(pluginName), version)
51+
}
52+
53+
func (c *Client) sendRequest(ctx context.Context, method, endpoint string, body io.Reader, modifyRequestFns ...func(r *http.Request)) (*http.Response, error) {
54+
apiEndpoint, err := url.JoinPath(c.registryURL, endpoint)
4155
if err != nil {
4256
return nil, err
4357
}
44-
req, err := http.NewRequestWithContext(ctx, method, url, body)
58+
req, err := http.NewRequestWithContext(ctx, method, apiEndpoint, body)
4559
if err != nil {
4660
return nil, err
4761
}
4862
req.Header.Set("Accept", "application/json; charset=utf-8")
63+
for _, f := range modifyRequestFns {
64+
f(req)
65+
}
4966
return c.httpClient.Do(req)
5067
}
5168

@@ -79,8 +96,8 @@ func (c *Client) GetPlugins(ctx context.Context) ([]string, error) {
7996
return plugins, nil
8097
}
8198

82-
func (c *Client) GetPlugin(ctx context.Context, plugin string) (*registry.Plugin, error) {
83-
resp, err := c.sendRequest(ctx, http.MethodGet, fmt.Sprintf("plugins/%s", plugin), nil)
99+
func (c *Client) GetPlugin(ctx context.Context, pluginName string) (*registry.Plugin, error) {
100+
resp, err := c.sendRequest(ctx, http.MethodGet, getPluginURL(pluginName), nil)
84101
if err != nil {
85102
return nil, err
86103
}
@@ -92,8 +109,8 @@ func (c *Client) GetPlugin(ctx context.Context, plugin string) (*registry.Plugin
92109
return &p, nil
93110
}
94111

95-
func (c *Client) GetPluginRelease(ctx context.Context, plugin, version string) (*registry.PluginRelease, error) {
96-
resp, err := c.sendRequest(ctx, http.MethodGet, fmt.Sprintf("plugins/%s/versions/%s", plugin, version), nil)
112+
func (c *Client) GetPluginRelease(ctx context.Context, pluginName, version string) (*registry.PluginRelease, error) {
113+
resp, err := c.sendRequest(ctx, http.MethodGet, getPluginReleaseURL(pluginName, version), nil)
97114
if err != nil {
98115
return nil, err
99116
}
@@ -122,3 +139,38 @@ func (c *Client) SendBatchRequest(ctx context.Context, batch *registry.BatchRequ
122139
}
123140
return &br, nil
124141
}
142+
143+
func (c *Client) UpdatePlugins(ctx context.Context, adminAccessToken string) error {
144+
return c.UpdatePluginRelease(ctx, adminAccessToken, "", "")
145+
}
146+
147+
func (c *Client) UpdatePlugin(ctx context.Context, adminAccessToken, pluginName string) error {
148+
return c.UpdatePluginRelease(ctx, adminAccessToken, pluginName, "")
149+
}
150+
151+
func (c *Client) UpdatePluginRelease(ctx context.Context, adminAccessToken, pluginName, version string) error {
152+
var apiURL string
153+
switch {
154+
case pluginName == "" && version == "":
155+
apiURL = "plugins"
156+
case pluginName == "" && version != "":
157+
return fmt.Errorf("plugin name is required when version is specified")
158+
case pluginName != "" && version == "":
159+
apiURL = getPluginURL(pluginName)
160+
case pluginName != "" && version != "":
161+
apiURL = getPluginReleaseURL(pluginName, version)
162+
}
163+
resp, err := c.sendRequest(ctx, http.MethodPut, apiURL, nil, setAuth(adminAccessToken))
164+
if err != nil {
165+
return err
166+
}
167+
var updateResponse map[string]bool
168+
err = c.decodeResponse(resp, &updateResponse)
169+
if err != nil {
170+
return err
171+
}
172+
if !updateResponse["ok"] {
173+
return fmt.Errorf("update plugin %s@%s failed: reason unknown", pluginName, version)
174+
}
175+
return nil
176+
}

pkg/client/client_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,34 @@ func TestSendBatchRequest(t *testing.T) {
8181
require.Equal(t, "darwin", batchResponse.OS)
8282
require.Equal(t, "amd64", batchResponse.Arch)
8383
}
84+
85+
func TestUpdatePlugins(t *testing.T) {
86+
reqCount := 0
87+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
88+
assert.Equal(t, http.MethodPut, r.Method)
89+
assert.Equal(t, "admin-token", r.Header.Get("Authorization"))
90+
switch reqCount {
91+
case 0:
92+
assert.Equal(t, "/plugins", r.URL.Path)
93+
case 1:
94+
assert.Equal(t, "/plugins/provider-git", r.URL.Path)
95+
case 2:
96+
assert.Equal(t, "/plugins/provider-git/versions/2.0.0", r.URL.Path)
97+
}
98+
require.NoError(t, json.NewEncoder(w).Encode(map[string]bool{"ok": true}))
99+
reqCount++
100+
}))
101+
defer ts.Close()
102+
c := New(ts.URL)
103+
104+
err := c.UpdatePlugins(context.Background(), "admin-token")
105+
require.NoError(t, err)
106+
107+
err = c.UpdatePlugin(context.Background(), "admin-token", "provider-git")
108+
require.NoError(t, err)
109+
110+
err = c.UpdatePluginRelease(context.Background(), "admin-token", "provider-git", "2.0.0")
111+
require.NoError(t, err)
112+
113+
require.Equal(t, 3, reqCount)
114+
}

0 commit comments

Comments
 (0)