-
Notifications
You must be signed in to change notification settings - Fork 124
/
buildpack.go
127 lines (110 loc) · 3.87 KB
/
buildpack.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
package client
import (
"context"
"io"
"net/url"
"github.com/cloudfoundry/go-cfclient/v3/internal/path"
"github.com/cloudfoundry/go-cfclient/v3/resource"
)
type BuildpackClient commonClient
// BuildpackListOptions list filters
type BuildpackListOptions struct {
*ListOptions
Names Filter `qs:"names"` // list of buildpack names to filter by
Stacks Filter `qs:"stacks"` // list of stack names to filter by
}
// NewBuildpackListOptions creates new options to pass to list
func NewBuildpackListOptions() *BuildpackListOptions {
return &BuildpackListOptions{
ListOptions: NewListOptions(),
}
}
func (o BuildpackListOptions) ToQueryString() (url.Values, error) {
return o.ListOptions.ToQueryString(o)
}
// Create a new buildpack
func (c *BuildpackClient) Create(ctx context.Context, r *resource.BuildpackCreateOrUpdate) (*resource.Buildpack, error) {
var bp resource.Buildpack
_, err := c.client.post(ctx, "/v3/buildpacks", r, &bp)
if err != nil {
return nil, err
}
return &bp, nil
}
// Delete the specified buildpack
func (c *BuildpackClient) Delete(ctx context.Context, guid string) error {
_, err := c.client.delete(ctx, path.Format("/v3/buildpacks/%s", guid))
return err
}
// First returns the first buildpack matching the options or an error when less than 1 match
func (c *BuildpackClient) First(ctx context.Context, opts *BuildpackListOptions) (*resource.Buildpack, error) {
return First[*BuildpackListOptions, *resource.Buildpack](opts, func(opts *BuildpackListOptions) ([]*resource.Buildpack, *Pager, error) {
return c.List(ctx, opts)
})
}
// Get retrieves the specified buildpack
func (c *BuildpackClient) Get(ctx context.Context, guid string) (*resource.Buildpack, error) {
var bp resource.Buildpack
err := c.client.get(ctx, path.Format("/v3/buildpacks/%s", guid), &bp)
if err != nil {
return nil, err
}
return &bp, nil
}
// List pages all buildpacks the user has access to
func (c *BuildpackClient) List(ctx context.Context, opts *BuildpackListOptions) ([]*resource.Buildpack, *Pager, error) {
if opts == nil {
opts = NewBuildpackListOptions()
}
var res resource.BuildpackList
err := c.client.list(ctx, "/v3/buildpacks", opts.ToQueryString, &res)
if err != nil {
return nil, nil, err
}
pager := NewPager(res.Pagination)
return res.Resources, pager, nil
}
// ListAll retrieves all buildpacks the user has access to
func (c *BuildpackClient) ListAll(ctx context.Context, opts *BuildpackListOptions) ([]*resource.Buildpack, error) {
if opts == nil {
opts = NewBuildpackListOptions()
}
var all []*resource.Buildpack
for {
page, pager, err := c.List(ctx, opts)
if err != nil {
return nil, err
}
all = append(all, page...)
if !pager.HasNextPage() {
break
}
pager.NextPage(opts)
}
return all, nil
}
// Single returns a single buildpack matching the options or an error if not exactly 1 match
func (c *BuildpackClient) Single(ctx context.Context, opts *BuildpackListOptions) (*resource.Buildpack, error) {
return Single[*BuildpackListOptions, *resource.Buildpack](opts, func(opts *BuildpackListOptions) ([]*resource.Buildpack, *Pager, error) {
return c.List(ctx, opts)
})
}
// Update the specified attributes of the buildpack
func (c *BuildpackClient) Update(ctx context.Context, guid string, r *resource.BuildpackCreateOrUpdate) (*resource.Buildpack, error) {
var bp resource.Buildpack
_, err := c.client.patch(ctx, path.Format("/v3/buildpacks/%s", guid), r, &bp)
if err != nil {
return nil, err
}
return &bp, nil
}
// Upload a gzip compressed (zip) file containing a Cloud Foundry compatible buildpack
func (c *BuildpackClient) Upload(ctx context.Context, guid string, zipFile io.Reader) (string, *resource.Buildpack, error) {
p := path.Format("/v3/buildpacks/%s/upload", guid)
var b resource.Buildpack
jobGUID, err := c.client.postFileUpload(ctx, p, "bits", "buildpack.zip", zipFile, &b)
if err != nil {
return "", nil, err
}
return jobGUID, &b, nil
}