github.com/sleungcy-sap/cli@v7.1.0+incompatible/actor/v2action/domain.go (about) 1 package v2action 2 3 import ( 4 "code.cloudfoundry.org/cli/actor/actionerror" 5 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 6 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 8 9 log "github.com/sirupsen/logrus" 10 ) 11 12 // Domain represents a CLI Domain. 13 type Domain ccv2.Domain 14 15 // IsHTTP returns true for any router group type that is not 'tcp'. 16 func (domain Domain) IsHTTP() bool { 17 // The default state of a domain is an HTTP domain; so if it is anything 18 // other than TCP, it is HTTP. 19 return !domain.IsTCP() 20 } 21 22 // IsPrivate returns true when the domain is a private domain. 23 func (domain Domain) IsPrivate() bool { 24 return domain.Type == constant.PrivateDomain 25 } 26 27 // IsShared returns true when the domain is a shared domain. 28 func (domain Domain) IsShared() bool { 29 return domain.Type == constant.SharedDomain 30 } 31 32 // IsTCP returns true only when the router group type equals 'tcp'. 33 func (domain Domain) IsTCP() bool { 34 return domain.RouterGroupType == constant.TCPRouterGroup 35 } 36 37 // TODO: Move into own file or add function to CCV2/3 38 func isResourceNotFoundError(err error) bool { 39 _, isResourceNotFound := err.(ccerror.ResourceNotFoundError) 40 return isResourceNotFound 41 } 42 43 // GetDomain returns the shared or private domain associated with the provided 44 // Domain GUID. 45 func (actor Actor) GetDomain(domainGUID string) (Domain, Warnings, error) { 46 var allWarnings Warnings 47 48 domain, warnings, err := actor.GetSharedDomain(domainGUID) 49 allWarnings = append(allWarnings, warnings...) 50 switch err.(type) { 51 case nil: 52 return domain, allWarnings, nil 53 case actionerror.DomainNotFoundError: 54 default: 55 return Domain{}, allWarnings, err 56 } 57 58 domain, warnings, err = actor.GetPrivateDomain(domainGUID) 59 allWarnings = append(allWarnings, warnings...) 60 switch err.(type) { 61 case nil: 62 return domain, allWarnings, nil 63 default: 64 return Domain{}, allWarnings, err 65 } 66 } 67 68 // GetDomainsByNameAndOrganization returns back a list of domains given a list 69 // of domains names and the organization GUID. If no domains are given, than this 70 // command will not lookup any domains. 71 func (actor Actor) GetDomainsByNameAndOrganization(domainNames []string, orgGUID string) ([]Domain, Warnings, error) { 72 if len(domainNames) == 0 { 73 return nil, nil, nil 74 } 75 76 var domains []Domain 77 var allWarnings Warnings 78 79 // TODO: If the following causes URI length problems, break domainNames into 80 // batched (based on character length?) and loop over them. 81 82 sharedDomains, warnings, err := actor.CloudControllerClient.GetSharedDomains(ccv2.Filter{ 83 Type: constant.NameFilter, 84 Operator: constant.InOperator, 85 Values: domainNames, 86 }) 87 allWarnings = append(allWarnings, warnings...) 88 if err != nil { 89 return nil, allWarnings, err 90 } 91 92 for _, domain := range sharedDomains { 93 domains = append(domains, Domain(domain)) 94 actor.saveDomain(domain) 95 } 96 97 privateDomains, warnings, err := actor.CloudControllerClient.GetOrganizationPrivateDomains( 98 orgGUID, 99 ccv2.Filter{ 100 Type: constant.NameFilter, 101 Operator: constant.InOperator, 102 Values: domainNames, 103 }) 104 allWarnings = append(allWarnings, warnings...) 105 if err != nil { 106 return nil, allWarnings, err 107 } 108 109 for _, domain := range privateDomains { 110 domains = append(domains, Domain(domain)) 111 actor.saveDomain(domain) 112 } 113 114 return domains, allWarnings, err 115 } 116 117 func (actor Actor) GetDomains(orgGUID string) ([]Domain, Warnings, error) { 118 var ( 119 allDomains []Domain 120 allWarnings Warnings 121 ) 122 123 sharedDomains, warnings, err := actor.CloudControllerClient.GetSharedDomains() 124 allWarnings = append(allWarnings, warnings...) 125 if err != nil { 126 return nil, allWarnings, err 127 } 128 for _, domain := range sharedDomains { 129 allDomains = append(allDomains, Domain(domain)) 130 } 131 132 privateDomains, warnings, err := actor.CloudControllerClient.GetOrganizationPrivateDomains(orgGUID) 133 allWarnings = append(allWarnings, warnings...) 134 if err != nil { 135 return nil, allWarnings, err 136 } 137 138 for _, domain := range privateDomains { 139 allDomains = append(allDomains, Domain(domain)) 140 } 141 return allDomains, allWarnings, err 142 } 143 144 // GetSharedDomain returns the shared domain associated with the provided 145 // Domain GUID. 146 func (actor Actor) GetSharedDomain(domainGUID string) (Domain, Warnings, error) { 147 if domain, found := actor.loadDomain(domainGUID); found { 148 log.WithFields(log.Fields{ 149 "domain": domain.Name, 150 "GUID": domain.GUID, 151 }).Debug("using domain from cache") 152 return domain, nil, nil 153 } 154 155 domain, warnings, err := actor.CloudControllerClient.GetSharedDomain(domainGUID) 156 if isResourceNotFoundError(err) { 157 return Domain{}, Warnings(warnings), actionerror.DomainNotFoundError{GUID: domainGUID} 158 } 159 160 actor.saveDomain(domain) 161 return Domain(domain), Warnings(warnings), err 162 } 163 164 // GetPrivateDomain returns the private domain associated with the provided 165 // Domain GUID. 166 func (actor Actor) GetPrivateDomain(domainGUID string) (Domain, Warnings, error) { 167 if domain, found := actor.loadDomain(domainGUID); found { 168 log.WithFields(log.Fields{ 169 "domain": domain.Name, 170 "GUID": domain.GUID, 171 }).Debug("using domain from cache") 172 return domain, nil, nil 173 } 174 175 domain, warnings, err := actor.CloudControllerClient.GetPrivateDomain(domainGUID) 176 if isResourceNotFoundError(err) { 177 return Domain{}, Warnings(warnings), actionerror.DomainNotFoundError{GUID: domainGUID} 178 } 179 180 actor.saveDomain(domain) 181 return Domain(domain), Warnings(warnings), err 182 } 183 184 // GetOrganizationDomains returns the shared and private domains associated 185 // with an organization. 186 func (actor Actor) GetOrganizationDomains(orgGUID string) ([]Domain, Warnings, error) { 187 var ( 188 allWarnings Warnings 189 allDomains []Domain 190 ) 191 192 domains, warnings, err := actor.CloudControllerClient.GetSharedDomains() 193 allWarnings = append(allWarnings, warnings...) 194 195 if err != nil { 196 return []Domain{}, allWarnings, err 197 } 198 for _, domain := range domains { 199 allDomains = append(allDomains, Domain(domain)) 200 } 201 202 domains, warnings, err = actor.CloudControllerClient.GetOrganizationPrivateDomains(orgGUID) 203 allWarnings = append(allWarnings, warnings...) 204 205 if err != nil { 206 return []Domain{}, allWarnings, err 207 } 208 for _, domain := range domains { 209 allDomains = append(allDomains, Domain(domain)) 210 } 211 212 return allDomains, allWarnings, nil 213 } 214 215 func (actor Actor) saveDomain(domain ccv2.Domain) { 216 if domain.GUID != "" { 217 actor.domainCache[domain.GUID] = Domain(domain) 218 } 219 } 220 221 func (actor Actor) loadDomain(domainGUID string) (Domain, bool) { 222 domain, found := actor.domainCache[domainGUID] 223 return domain, found 224 }