-
Notifications
You must be signed in to change notification settings - Fork 929
/
domains.go
184 lines (153 loc) · 5.58 KB
/
domains.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
package api
import (
"bytes"
"encoding/json"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/api/resources"
"github.com/cloudfoundry/cli/cf/api/strategy"
"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/net"
)
//go:generate counterfeiter . DomainRepository
type DomainRepository interface {
ListDomainsForOrg(orgGUID string, cb func(models.DomainFields) bool) error
FindSharedByName(name string) (domain models.DomainFields, apiErr error)
FindPrivateByName(name string) (domain models.DomainFields, apiErr error)
FindByNameInOrg(name string, owningOrgGUID string) (domain models.DomainFields, apiErr error)
Create(domainName string, owningOrgGUID string) (createdDomain models.DomainFields, apiErr error)
CreateSharedDomain(domainName string, routerGroupGUID string) (apiErr error)
Delete(domainGUID string) (apiErr error)
DeleteSharedDomain(domainGUID string) (apiErr error)
FirstOrDefault(orgGUID string, name *string) (domain models.DomainFields, error error)
}
type CloudControllerDomainRepository struct {
config coreconfig.Reader
gateway net.Gateway
strategy strategy.EndpointStrategy
}
func NewCloudControllerDomainRepository(config coreconfig.Reader, gateway net.Gateway, strategy strategy.EndpointStrategy) CloudControllerDomainRepository {
return CloudControllerDomainRepository{
config: config,
gateway: gateway,
strategy: strategy,
}
}
func (repo CloudControllerDomainRepository) ListDomainsForOrg(orgGUID string, cb func(models.DomainFields) bool) error {
err := repo.listDomains(repo.strategy.PrivateDomainsByOrgURL(orgGUID), cb)
if err != nil {
return err
}
err = repo.listDomains(repo.strategy.SharedDomainsURL(), cb)
return err
}
func (repo CloudControllerDomainRepository) listDomains(path string, cb func(models.DomainFields) bool) (apiErr error) {
return repo.gateway.ListPaginatedResources(
repo.config.APIEndpoint(),
path,
resources.DomainResource{},
func(resource interface{}) bool {
return cb(resource.(resources.DomainResource).ToFields())
})
}
func (repo CloudControllerDomainRepository) isOrgDomain(orgGUID string, domain models.DomainFields) bool {
return orgGUID == domain.OwningOrganizationGUID || domain.Shared
}
func (repo CloudControllerDomainRepository) FindSharedByName(name string) (domain models.DomainFields, apiErr error) {
return repo.findOneWithPath(repo.strategy.SharedDomainURL(name), name)
}
func (repo CloudControllerDomainRepository) FindPrivateByName(name string) (domain models.DomainFields, apiErr error) {
return repo.findOneWithPath(repo.strategy.PrivateDomainURL(name), name)
}
func (repo CloudControllerDomainRepository) FindByNameInOrg(name string, orgGUID string) (domain models.DomainFields, apiErr error) {
domain, apiErr = repo.findOneWithPath(repo.strategy.OrgDomainURL(orgGUID, name), name)
switch apiErr.(type) {
case *errors.ModelNotFoundError:
domain, apiErr = repo.FindSharedByName(name)
if !domain.Shared {
apiErr = errors.NewModelNotFoundError("Domain", name)
}
}
return
}
func (repo CloudControllerDomainRepository) findOneWithPath(path, name string) (domain models.DomainFields, apiErr error) {
foundDomain := false
apiErr = repo.listDomains(path, func(result models.DomainFields) bool {
domain = result
foundDomain = true
return false
})
if apiErr == nil && !foundDomain {
apiErr = errors.NewModelNotFoundError("Domain", name)
}
return
}
func (repo CloudControllerDomainRepository) Create(domainName string, owningOrgGUID string) (createdDomain models.DomainFields, err error) {
data, err := json.Marshal(resources.DomainEntity{
Name: domainName,
OwningOrganizationGUID: owningOrgGUID,
Wildcard: true,
})
if err != nil {
return
}
resource := new(resources.DomainResource)
err = repo.gateway.CreateResource(
repo.config.APIEndpoint(),
repo.strategy.PrivateDomainsURL(),
bytes.NewReader(data),
resource)
if err != nil {
return
}
createdDomain = resource.ToFields()
return
}
func (repo CloudControllerDomainRepository) CreateSharedDomain(domainName string, routerGroupGUID string) error {
data, err := json.Marshal(resources.DomainEntity{
Name: domainName,
RouterGroupGUID: routerGroupGUID,
Wildcard: true,
})
if err != nil {
return err
}
return repo.gateway.CreateResource(
repo.config.APIEndpoint(),
repo.strategy.SharedDomainsURL(),
bytes.NewReader(data),
)
}
func (repo CloudControllerDomainRepository) Delete(domainGUID string) error {
return repo.gateway.DeleteResource(
repo.config.APIEndpoint(),
repo.strategy.DeleteDomainURL(domainGUID))
}
func (repo CloudControllerDomainRepository) DeleteSharedDomain(domainGUID string) error {
return repo.gateway.DeleteResource(
repo.config.APIEndpoint(),
repo.strategy.DeleteSharedDomainURL(domainGUID))
}
func (repo CloudControllerDomainRepository) FirstOrDefault(orgGUID string, name *string) (domain models.DomainFields, error error) {
if name == nil {
domain, error = repo.defaultDomain(orgGUID)
} else {
domain, error = repo.FindByNameInOrg(*name, orgGUID)
}
return
}
func (repo CloudControllerDomainRepository) defaultDomain(orgGUID string) (models.DomainFields, error) {
var foundDomain *models.DomainFields
err := repo.ListDomainsForOrg(orgGUID, func(domain models.DomainFields) bool {
foundDomain = &domain
return !domain.Shared
})
if err != nil {
return models.DomainFields{}, err
}
if foundDomain == nil {
return models.DomainFields{}, errors.New(T("Could not find a default domain"))
}
return *foundDomain, nil
}