forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v3packages.go
149 lines (125 loc) · 4.18 KB
/
v3packages.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
147
148
149
package cfclient
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/pkg/errors"
)
type V3PackageState string
const (
AwaitingUpload V3PackageState = "AWAITING_UPLOAD"
ProcessingUpload V3PackageState = "PROCESSING_UPLOAD"
Ready V3PackageState = "READY"
Failed V3PackageState = "FAILED"
Copying V3PackageState = "COPYING"
Expired V3PackageState = "EXPIRED"
)
type V3Package struct {
Type string `json:"type,omitempty"` // bits or docker
Data json.RawMessage `json:"data,omitempty"` // depends on value of Type
State V3PackageState `json:"state,omitempty"`
GUID string `json:"guid,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
Links map[string]Link `json:"links,omitempty"`
Metadata V3Metadata `json:"metadata,omitempty"`
}
func (v *V3Package) BitsData() (V3BitsPackage, error) {
var bits V3BitsPackage
if v.Type != "bits" {
return bits, errors.New("this package is not of type bits")
}
if err := json.Unmarshal(v.Data, &bits); err != nil {
return bits, err
}
return bits, nil
}
func (v *V3Package) DockerData() (V3DockerPackage, error) {
var docker V3DockerPackage
if v.Type != "docker" {
return docker, errors.New("this package is not of type docker")
}
if err := json.Unmarshal(v.Data, &docker); err != nil {
return docker, err
}
return docker, nil
}
// V3BitsPackage is the data for V3Packages of type bits.
// It provides an upload link to which a zip file should be uploaded.
type V3BitsPackage struct {
Error string `json:"error,omitempty"`
Checksum struct {
Type string `json:"type,omitempty"` // eg. sha256
Value string `json:"value,omitempty"` // populated after the bits are uploaded
} `json:"checksum,omitempty"`
}
// V3DockerPackage is the data for V3Packages of type docker.
// It references a docker image from a registry.
type V3DockerPackage struct {
Image string `json:"image,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}
type listV3PackagesResponse struct {
Pagination Pagination `json:"pagination,omitempty"`
Resources []V3Package `json:"resources,omitempty"`
}
func (c *Client) ListPackagesForAppV3(appGUID string, query url.Values) ([]V3Package, error) {
var packages []V3Package
requestURL := "/v3/apps/" + appGUID + "/packages"
if e := query.Encode(); len(e) > 0 {
requestURL += "?" + e
}
for {
resp, err := c.DoRequest(c.NewRequest("GET", requestURL))
if err != nil {
return nil, errors.Wrapf(err, "Error requesting packages for app %s", appGUID)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Error listing v3 app packages, response code: %d", resp.StatusCode)
}
var data listV3PackagesResponse
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, errors.Wrap(err, "Error parsing JSON from list v3 app packages")
}
packages = append(packages, data.Resources...)
requestURL = data.Pagination.Next.Href
if requestURL == "" {
break
}
requestURL, err = extractPathFromURL(requestURL)
if err != nil {
return nil, errors.Wrap(err, "Error parsing the next page request url for v3 packages")
}
}
return packages, nil
}
// CopyPackageV3 makes a copy of a package that is associated with one app
// and associates the copy with a new app.
func (c *Client) CopyPackageV3(packageGUID, appGUID string) (*V3Package, error) {
req := c.NewRequest("POST", "/v3/packages?source_guid="+packageGUID)
req.obj = map[string]interface{}{
"relationships": map[string]interface{}{
"app": V3ToOneRelationship{
Data: V3Relationship{
GUID: appGUID,
},
},
},
}
resp, err := c.DoRequest(req)
if err != nil {
return nil, errors.Wrap(err, "Error while copying v3 package")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("Error copying v3 package %s, response code: %d", packageGUID, resp.StatusCode)
}
var pkg V3Package
if err := json.NewDecoder(resp.Body).Decode(&pkg); err != nil {
return nil, errors.Wrap(err, "Error reading v3 app package")
}
return &pkg, nil
}