forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domains.go
170 lines (140 loc) · 5.11 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
package api
import (
"encoding/json"
. "github.com/cloudfoundry/cli/cf/i18n"
"strings"
"github.com/cloudfoundry/cli/cf/api/resources"
"github.com/cloudfoundry/cli/cf/api/strategy"
"github.com/cloudfoundry/cli/cf/configuration"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/net"
)
type DomainRepository interface {
ListDomainsForOrg(orgGuid string, cb func(models.DomainFields) bool) error
FindByName(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) (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 configuration.Reader
gateway net.Gateway
strategy strategy.EndpointStrategy
}
func NewCloudControllerDomainRepository(config configuration.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) FindByName(name string) (domain models.DomainFields, apiErr error) {
return repo.findOneWithPath(repo.strategy.DomainURL(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.FindByName(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(),
strings.NewReader(string(data)),
resource)
if err != nil {
return
}
createdDomain = resource.ToFields()
return
}
func (repo CloudControllerDomainRepository) CreateSharedDomain(domainName string) (apiErr error) {
data, err := json.Marshal(resources.DomainEntity{
Name: domainName,
Wildcard: true,
})
if err != nil {
return
}
apiErr = repo.gateway.CreateResource(
repo.config.ApiEndpoint()+repo.strategy.SharedDomainsURL(),
strings.NewReader(string(data)))
return
}
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
repo.ListDomainsForOrg(orgGuid, func(domain models.DomainFields) bool {
foundDomain = &domain
return !domain.Shared
})
if foundDomain == nil {
return models.DomainFields{}, errors.New(T("Could not find a default domain"))
}
return *foundDomain, nil
}