/
addon.go
80 lines (71 loc) · 2.92 KB
/
addon.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* This file is part of AMS SDK
* Copyright 2021 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the Lesser GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, SATISFACTORY
* QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
* License for more details.
*
* You should have received a copy of the Lesser GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package client
import (
"strconv"
api "github.com/anbox-cloud/ams-sdk/api/ams"
errs "github.com/anbox-cloud/ams-sdk/internal/ams/shared/errors"
"github.com/anbox-cloud/ams-sdk/internal/ams/shared/rest/client"
)
// AddAddon adds a new addon and uploads the given addon package to AMS
func (c *clientImpl) AddAddon(name string, packagePath string, sentBytes chan float64) (client.Operation, error) {
details := api.AddonsPost{Name: name}
return c.upload("POST", client.APIPath("addons"), nil, packagePath, details, sentBytes)
}
// UpdateAddon updates an existing addon
func (c *clientImpl) UpdateAddon(name, packagePath string, sentBytes chan float64) (client.Operation, error) {
if len(name) == 0 {
return nil, errs.NewInvalidArgument("name")
}
details := api.AddonPatch{}
return c.upload("PATCH", client.APIPath("addons", name), nil, packagePath, details, sentBytes)
}
// RetrieveAddon loads an addon from the connected AMS service
func (c *clientImpl) RetrieveAddon(name string) (*api.Addon, string, error) {
if len(name) == 0 {
return nil, "", errs.NewInvalidArgument("name")
}
var details api.Addon
etag, err := c.QueryStruct("GET", client.APIPath("addons", name), nil, nil, nil, "", &details)
return &details, etag, err
}
// DeleteAddon deletes an existing addon
func (c *clientImpl) DeleteAddon(name string) (client.Operation, error) {
if len(name) == 0 {
return nil, errs.NewInvalidArgument("name")
}
op, _, err := c.QueryOperation("DELETE", client.APIPath("addons", name), nil, nil, nil, "")
return op, err
}
// DeleteAddonVersion deletes a specific version of the given addon
func (c *clientImpl) DeleteAddonVersion(name string, version int) (client.Operation, error) {
if len(name) == 0 {
return nil, errs.NewInvalidArgument("name")
}
op, _, err := c.QueryOperation("DELETE", client.APIPath("addons", name, strconv.Itoa(version)), nil, nil, nil, "")
return op, err
}
// ListAddons lists all currently available addons of the connected AMS service
func (c *clientImpl) ListAddons() ([]api.Addon, error) {
addons := []api.Addon{}
params := client.QueryParams{
"recursion": "1",
}
_, err := c.QueryStruct("GET", client.APIPath("addons"), params, nil, nil, "", &addons)
return addons, err
}