-
Notifications
You must be signed in to change notification settings - Fork 929
/
buildpacks.go
118 lines (99 loc) · 3.57 KB
/
buildpacks.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
package api
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"code.cloudfoundry.org/cli/cf/api/resources"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/errors"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/net"
)
//go:generate counterfeiter . BuildpackRepository
type BuildpackRepository interface {
FindByName(name string) (buildpack models.Buildpack, apiErr error)
ListBuildpacks(func(models.Buildpack) bool) error
Create(name string, position *int, enabled *bool, locked *bool) (createdBuildpack models.Buildpack, apiErr error)
Delete(buildpackGUID string) (apiErr error)
Update(buildpack models.Buildpack) (updatedBuildpack models.Buildpack, apiErr error)
}
type CloudControllerBuildpackRepository struct {
config coreconfig.Reader
gateway net.Gateway
}
func NewCloudControllerBuildpackRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerBuildpackRepository) {
repo.config = config
repo.gateway = gateway
return
}
func (repo CloudControllerBuildpackRepository) ListBuildpacks(cb func(models.Buildpack) bool) error {
return repo.gateway.ListPaginatedResources(
repo.config.APIEndpoint(),
buildpacksPath,
resources.BuildpackResource{},
func(resource interface{}) bool {
return cb(resource.(resources.BuildpackResource).ToFields())
})
}
func (repo CloudControllerBuildpackRepository) FindByName(name string) (buildpack models.Buildpack, apiErr error) {
foundIt := false
apiErr = repo.gateway.ListPaginatedResources(
repo.config.APIEndpoint(),
fmt.Sprintf("%s?q=%s", buildpacksPath, url.QueryEscape("name:"+name)),
resources.BuildpackResource{},
func(resource interface{}) bool {
buildpack = resource.(resources.BuildpackResource).ToFields()
foundIt = true
return false
})
if !foundIt {
apiErr = errors.NewModelNotFoundError("Buildpack", name)
}
return
}
func (repo CloudControllerBuildpackRepository) Create(name string, position *int, enabled *bool, locked *bool) (createdBuildpack models.Buildpack, apiErr error) {
entity := resources.BuildpackEntity{Name: name, Position: position, Enabled: enabled, Locked: locked}
body, err := json.Marshal(entity)
if err != nil {
apiErr = fmt.Errorf("%s: %s", T("Could not serialize information"), err.Error())
return
}
resource := new(resources.BuildpackResource)
apiErr = repo.gateway.CreateResource(repo.config.APIEndpoint(), buildpacksPath, bytes.NewReader(body), resource)
if apiErr != nil {
return
}
createdBuildpack = resource.ToFields()
return
}
func (repo CloudControllerBuildpackRepository) Delete(buildpackGUID string) (apiErr error) {
path := fmt.Sprintf("%s/%s", buildpacksPath, buildpackGUID)
apiErr = repo.gateway.DeleteResource(repo.config.APIEndpoint(), path)
return
}
func (repo CloudControllerBuildpackRepository) Update(buildpack models.Buildpack) (updatedBuildpack models.Buildpack, apiErr error) {
path := fmt.Sprintf("%s/%s", buildpacksPath, buildpack.GUID)
entity := resources.BuildpackEntity{
Name: buildpack.Name,
Position: buildpack.Position,
Enabled: buildpack.Enabled,
Key: "",
Filename: "",
Locked: buildpack.Locked,
}
body, err := json.Marshal(entity)
if err != nil {
apiErr = fmt.Errorf("%s: %s", T("Could not serialize updates."), err.Error())
return
}
resource := new(resources.BuildpackResource)
apiErr = repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, bytes.NewReader(body), resource)
if apiErr != nil {
return
}
updatedBuildpack = resource.ToFields()
return
}
const buildpacksPath = "/v2/buildpacks"