forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domain.go
131 lines (108 loc) · 3.67 KB
/
domain.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
package v2action
import (
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
log "github.com/Sirupsen/logrus"
)
// Domain represents a CLI Domain.
type Domain ccv2.Domain
// DomainNotFoundError is an error wrapper that represents the case
// when the domain is not found.
type DomainNotFoundError struct{}
// Error method to display the error message.
func (_ DomainNotFoundError) Error() string {
return "Domain not found."
}
// TODO: Move into own file or add function to CCV2/3
func isResourceNotFoundError(err error) bool {
_, isResourceNotFound := err.(ccerror.ResourceNotFoundError)
return isResourceNotFound
}
// GetDomain returns the shared or private domain associated with the provided
// Domain GUID.
func (actor Actor) GetDomain(domainGUID string) (Domain, Warnings, error) {
var allWarnings Warnings
domain, warnings, err := actor.GetSharedDomain(domainGUID)
allWarnings = append(allWarnings, warnings...)
switch err.(type) {
case nil:
return domain, allWarnings, nil
case DomainNotFoundError:
default:
return Domain{}, allWarnings, err
}
domain, warnings, err = actor.GetPrivateDomain(domainGUID)
allWarnings = append(allWarnings, warnings...)
switch err.(type) {
case nil:
return domain, allWarnings, nil
default:
return Domain{}, allWarnings, err
}
}
// GetSharedDomain returns the shared domain associated with the provided
// Domain GUID.
func (actor Actor) GetSharedDomain(domainGUID string) (Domain, Warnings, error) {
if domain, found := actor.loadDomain(domainGUID); found {
log.WithFields(log.Fields{
"domain": domain.Name,
"GUID": domain.GUID,
}).Debug("using domain from cache")
return domain, nil, nil
}
domain, warnings, err := actor.CloudControllerClient.GetSharedDomain(domainGUID)
if isResourceNotFoundError(err) {
return Domain{}, Warnings(warnings), DomainNotFoundError{}
}
actor.saveDomain(domain)
return Domain(domain), Warnings(warnings), err
}
// GetPrivateDomain returns the private domain associated with the provided
// Domain GUID.
func (actor Actor) GetPrivateDomain(domainGUID string) (Domain, Warnings, error) {
if domain, found := actor.loadDomain(domainGUID); found {
log.WithFields(log.Fields{
"domain": domain.Name,
"GUID": domain.GUID,
}).Debug("using domain from cache")
return domain, nil, nil
}
domain, warnings, err := actor.CloudControllerClient.GetPrivateDomain(domainGUID)
if isResourceNotFoundError(err) {
return Domain{}, Warnings(warnings), DomainNotFoundError{}
}
actor.saveDomain(domain)
return Domain(domain), Warnings(warnings), err
}
// GetOrganizationDomains returns the shared and private domains associated
// with an organization.
func (actor Actor) GetOrganizationDomains(orgGUID string) ([]Domain, Warnings, error) {
var allWarnings Warnings
var allDomains []Domain
domains, warnings, err := actor.CloudControllerClient.GetSharedDomains()
allWarnings = append(allWarnings, warnings...)
if err != nil {
return []Domain{}, allWarnings, err
}
for _, domain := range domains {
allDomains = append(allDomains, Domain(domain))
}
domains, warnings, err = actor.CloudControllerClient.GetOrganizationPrivateDomains(orgGUID, nil)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return []Domain{}, allWarnings, err
}
for _, domain := range domains {
allDomains = append(allDomains, Domain(domain))
}
return allDomains, allWarnings, nil
}
func (actor Actor) saveDomain(domain ccv2.Domain) {
if domain.GUID != "" {
actor.domainCache[domain.GUID] = Domain(domain)
}
}
func (actor Actor) loadDomain(domainGUID string) (Domain, bool) {
domain, found := actor.domainCache[domainGUID]
return domain, found
}