-
Notifications
You must be signed in to change notification settings - Fork 17
/
releases.go
224 lines (191 loc) · 6.33 KB
/
releases.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package pivnet
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/pivotal-cf/go-pivnet/v7/logger"
)
type ReleasesService struct {
client Client
l logger.Logger
}
type createReleaseBody struct {
Release Release `json:"release"`
CopyMetadata bool `json:"copy_metadata"`
}
type ReleasesResponse struct {
Releases []Release `json:"releases,omitempty"`
}
type CreateReleaseResponse struct {
Release Release `json:"release,omitempty"`
}
type Release struct {
ID int `json:"id,omitempty" yaml:"id,omitempty"`
Availability string `json:"availability,omitempty" yaml:"availability,omitempty"`
EULA *EULA `json:"eula,omitempty" yaml:"eula,omitempty"`
OSSCompliant string `json:"oss_compliant,omitempty" yaml:"oss_compliant,omitempty"`
ReleaseDate string `json:"release_date,omitempty" yaml:"release_date,omitempty"`
ReleaseType ReleaseType `json:"release_type,omitempty" yaml:"release_type,omitempty"`
Version string `json:"version,omitempty" yaml:"version,omitempty"`
Links *Links `json:"_links,omitempty" yaml:"_links,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
ReleaseNotesURL string `json:"release_notes_url,omitempty" yaml:"release_notes_url,omitempty"`
Controlled bool `json:"controlled,omitempty" yaml:"controlled,omitempty"`
ECCN string `json:"eccn,omitempty" yaml:"eccn,omitempty"`
LicenseException string `json:"license_exception,omitempty" yaml:"license_exception,omitempty"`
EndOfSupportDate string `json:"end_of_support_date,omitempty" yaml:"end_of_support_date,omitempty"`
EndOfGuidanceDate string `json:"end_of_guidance_date,omitempty" yaml:"end_of_guidance_date,omitempty"`
EndOfAvailabilityDate string `json:"end_of_availability_date,omitempty" yaml:"end_of_availability_date,omitempty"`
UpdatedAt string `json:"updated_at,omitempty" yaml:"updated_at,omitempty"`
SoftwareFilesUpdatedAt string `json:"software_files_updated_at,omitempty" yaml:"software_files_updated_at,omitempty"`
UserGroupsUpdatedAt string `json:"user_groups_updated_at,omitempty" yaml:"user_groups_updated_at,omitempty"`
}
type CreateReleaseConfig struct {
ProductSlug string
Version string
ReleaseType string
ReleaseDate string
EULASlug string
Description string
ReleaseNotesURL string
Controlled bool
ECCN string
LicenseException string
EndOfSupportDate string
EndOfGuidanceDate string
EndOfAvailabilityDate string
CopyMetadata bool
}
func (r ReleasesService) List(productSlug string, params ...QueryParameter) ([]Release, error) {
url := fmt.Sprintf("/products/%s/releases", productSlug)
var response ReleasesResponse
resp, err := r.client.MakeRequestWithParams("GET", url, http.StatusOK, params, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return nil, err
}
return response.Releases, nil
}
func (r ReleasesService) Get(productSlug string, releaseID int) (Release, error) {
url := fmt.Sprintf("/products/%s/releases/%d", productSlug, releaseID)
var response Release
resp, err := r.client.MakeRequest("GET", url, http.StatusOK, nil)
if err != nil {
return Release{}, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return Release{}, err
}
return response, nil
}
func (r ReleasesService) Create(config CreateReleaseConfig) (Release, error) {
url := fmt.Sprintf("/products/%s/releases", config.ProductSlug)
body := createReleaseBody{
Release: Release{
Availability: "Admins Only",
EULA: &EULA{
Slug: config.EULASlug,
},
OSSCompliant: "confirm",
ReleaseDate: config.ReleaseDate,
ReleaseType: ReleaseType(config.ReleaseType),
Version: config.Version,
Description: config.Description,
ReleaseNotesURL: config.ReleaseNotesURL,
Controlled: config.Controlled,
ECCN: config.ECCN,
LicenseException: config.LicenseException,
EndOfSupportDate: config.EndOfSupportDate,
EndOfGuidanceDate: config.EndOfGuidanceDate,
EndOfAvailabilityDate: config.EndOfAvailabilityDate,
},
CopyMetadata: config.CopyMetadata,
}
if config.ReleaseDate == "" {
body.Release.ReleaseDate = time.Now().Format("2006-01-02")
r.l.Info(
"No release date found - using default release date",
logger.Data{"release date": body.Release.ReleaseDate})
}
b, err := json.Marshal(body)
if err != nil {
// Untested as we cannot force an error because we are marshalling
// a known-good body
return Release{}, err
}
var response CreateReleaseResponse
resp, err := r.client.MakeRequest(
"POST",
url,
http.StatusCreated,
bytes.NewReader(b),
)
if err != nil {
return Release{}, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return Release{}, err
}
return response.Release, nil
}
func (r ReleasesService) Update(productSlug string, release Release) (Release, error) {
url := fmt.Sprintf(
"/products/%s/releases/%d",
productSlug,
release.ID,
)
release.OSSCompliant = "confirm"
var updatedRelease = createReleaseBody{
Release: release,
}
body, err := json.Marshal(updatedRelease)
if err != nil {
// Untested as we cannot force an error because we are marshalling
// a known-good body
return Release{}, err
}
var response CreateReleaseResponse
resp, err := r.client.MakeRequest(
"PATCH",
url,
http.StatusOK,
bytes.NewReader(body),
)
if err != nil {
return Release{}, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return Release{}, err
}
return response.Release, nil
}
func (r ReleasesService) Delete(productSlug string, release Release) error {
url := fmt.Sprintf(
"/products/%s/releases/%d",
productSlug,
release.ID,
)
resp, err := r.client.MakeRequest(
"DELETE",
url,
http.StatusNoContent,
nil,
)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}