-
Notifications
You must be signed in to change notification settings - Fork 929
/
buildpacks.go
162 lines (138 loc) · 4.96 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
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
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)
FindByNameAndStack(name, stack string) (buildpack models.Buildpack, apiErr error)
FindByNameWithNilStack(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) {
found := 0
apiErr = repo.gateway.ListPaginatedResources(
repo.config.APIEndpoint(),
fmt.Sprintf("%s?q=%s", buildpacksPath, url.QueryEscape("name:"+name)),
resources.BuildpackResource{},
func(resource interface{}) bool {
found++
buildpack = resource.(resources.BuildpackResource).ToFields()
return true
})
if found == 0 {
apiErr = errors.NewModelNotFoundError("Buildpack", name)
} else if found > 1 {
apiErr = errors.NewAmbiguousModelError("Buildpack", name)
}
return
}
func (repo CloudControllerBuildpackRepository) FindByNameAndStack(name, stack 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+";stack:"+stack)),
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) FindByNameWithNilStack(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()
if buildpack.Stack == "" {
foundIt = true
return false
}
return true
})
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"