-
Notifications
You must be signed in to change notification settings - Fork 124
/
deployment.go
112 lines (96 loc) · 3.58 KB
/
deployment.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
package client
import (
"context"
"errors"
"net/url"
"github.com/cloudfoundry/go-cfclient/v3/internal/path"
"github.com/cloudfoundry/go-cfclient/v3/resource"
)
type DeploymentClient commonClient
// DeploymentListOptions list filters
type DeploymentListOptions struct {
*ListOptions
AppGUIDs Filter `qs:"app_guids"`
States Filter `qs:"states"`
StatusReasons Filter `qs:"status_reasons"`
StatusValues Filter `qs:"status_values"`
}
// NewDeploymentListOptions creates new options to pass to list
func NewDeploymentListOptions() *DeploymentListOptions {
return &DeploymentListOptions{
ListOptions: NewListOptions(),
}
}
func (o DeploymentListOptions) ToQueryString() (url.Values, error) {
return o.ListOptions.ToQueryString(o)
}
// Cancel the ongoing deployment
func (c *DeploymentClient) Cancel(ctx context.Context, guid string) error {
_, err := c.client.post(ctx, path.Format("/v3/deployments/%s/actions/cancel", guid), nil, nil)
return err
}
// Create a new deployment
func (c *DeploymentClient) Create(ctx context.Context, r *resource.DeploymentCreate) (*resource.Deployment, error) {
// validate the params
if r.Droplet != nil && r.Revision != nil {
return nil, errors.New("droplet and revision cannot both be set")
}
var d resource.Deployment
_, err := c.client.post(ctx, "/v3/deployments", r, &d)
if err != nil {
return nil, err
}
return &d, nil
}
// First returns the first deployment matching the options or an error when less than 1 match
func (c *DeploymentClient) First(ctx context.Context, opts *DeploymentListOptions) (*resource.Deployment, error) {
return First[*DeploymentListOptions, *resource.Deployment](opts, func(opts *DeploymentListOptions) ([]*resource.Deployment, *Pager, error) {
return c.List(ctx, opts)
})
}
// Get the specified deployment
func (c *DeploymentClient) Get(ctx context.Context, guid string) (*resource.Deployment, error) {
var d resource.Deployment
err := c.client.get(ctx, path.Format("/v3/deployments/%s", guid), &d)
if err != nil {
return nil, err
}
return &d, nil
}
// List pages deployments the user has access to
func (c *DeploymentClient) List(ctx context.Context, opts *DeploymentListOptions) ([]*resource.Deployment, *Pager, error) {
if opts == nil {
opts = NewDeploymentListOptions()
}
var res resource.DeploymentList
err := c.client.list(ctx, "/v3/deployments", opts.ToQueryString, &res)
if err != nil {
return nil, nil, err
}
pager := NewPager(res.Pagination)
return res.Resources, pager, nil
}
// ListAll retrieves all deployments the user has access to
func (c *DeploymentClient) ListAll(ctx context.Context, opts *DeploymentListOptions) ([]*resource.Deployment, error) {
if opts == nil {
opts = NewDeploymentListOptions()
}
return AutoPage[*DeploymentListOptions, *resource.Deployment](opts, func(opts *DeploymentListOptions) ([]*resource.Deployment, *Pager, error) {
return c.List(ctx, opts)
})
}
// Single returns a single deployment matching the options or an error if not exactly 1 match
func (c *DeploymentClient) Single(ctx context.Context, opts *DeploymentListOptions) (*resource.Deployment, error) {
return Single[*DeploymentListOptions, *resource.Deployment](opts, func(opts *DeploymentListOptions) ([]*resource.Deployment, *Pager, error) {
return c.List(ctx, opts)
})
}
// Update the specified attributes of the deployment
func (c *DeploymentClient) Update(ctx context.Context, guid string, r *resource.DeploymentUpdate) (*resource.Deployment, error) {
var d resource.Deployment
_, err := c.client.patch(ctx, path.Format("/v3/deployments/%s", guid), r, &d)
if err != nil {
return nil, err
}
return &d, nil
}