github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/api/cloudcontroller/ccv2/domain.go (about) 1 package ccv2 2 3 import ( 4 "bytes" 5 "encoding/json" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal" 11 ) 12 13 // Domain represents a Cloud Controller Domain. 14 type Domain struct { 15 // GUID is the unique domain identifier. 16 GUID string 17 18 // Internal indicates whether the domain is an internal domain. 19 Internal bool 20 21 // Name is the name given to the domain. 22 Name string 23 24 // RouterGroupGUID is the unique identier of the router group this domain is 25 // assigned to. 26 RouterGroupGUID string 27 28 // RouterGroupType is the type of router group this domain is assigned to. It 29 // can be of type `tcp` or `http`. 30 RouterGroupType constant.RouterGroupType 31 32 // DomainType is the access type of the domain. It can be either a domain 33 // private to a single org or it can be a domain shared to all orgs. 34 Type constant.DomainType 35 36 // Organization owning the domain 37 OwningOrganizationGUID string 38 } 39 40 // UnmarshalJSON helps unmarshal a Cloud Controller Domain response. 41 func (domain *Domain) UnmarshalJSON(data []byte) error { 42 var ccDomain struct { 43 Metadata internal.Metadata `json:"metadata"` 44 Entity struct { 45 Name string `json:"name"` 46 RouterGroupGUID string `json:"router_group_guid"` 47 RouterGroupType string `json:"router_group_type"` 48 Internal bool `json:"internal"` 49 OwningOrganizationGUID string `json:"owning_organization_guid"` 50 } `json:"entity"` 51 } 52 err := cloudcontroller.DecodeJSON(data, &ccDomain) 53 if err != nil { 54 return err 55 } 56 57 domain.GUID = ccDomain.Metadata.GUID 58 domain.Name = ccDomain.Entity.Name 59 domain.RouterGroupGUID = ccDomain.Entity.RouterGroupGUID 60 domain.RouterGroupType = constant.RouterGroupType(ccDomain.Entity.RouterGroupType) 61 domain.Internal = ccDomain.Entity.Internal 62 domain.OwningOrganizationGUID = ccDomain.Entity.OwningOrganizationGUID 63 return nil 64 } 65 66 type createSharedDomainBody struct { 67 Name string `json:"name"` 68 RouterGroupGUID string `json:"router_group_guid,omitempty"` 69 Internal bool `json:"internal"` 70 } 71 72 type createPrivateDomainBody struct { 73 Name string `json:"name"` 74 OwningOrganizationGuid string `json:"owning_organization_guid"` 75 } 76 77 func (client *Client) CreateSharedDomain(domainName string, routerGroupdGUID string, isInternal bool) (Domain, Warnings, error) { 78 body := createSharedDomainBody{ 79 Name: domainName, 80 RouterGroupGUID: routerGroupdGUID, 81 Internal: isInternal, 82 } 83 bodyBytes, err := json.Marshal(body) 84 request, err := client.newHTTPRequest(requestOptions{ 85 RequestName: internal.PostSharedDomainRequest, 86 Body: bytes.NewReader(bodyBytes), 87 }) 88 89 if err != nil { 90 return Domain{}, nil, err 91 } 92 var updatedObj Domain 93 response := cloudcontroller.Response{ 94 DecodeJSONResponseInto: &updatedObj, 95 } 96 97 err = client.connection.Make(request, &response) 98 return updatedObj, response.Warnings, err 99 } 100 101 // GetOrganizationPrivateDomains returns the private domains associated with an organization. 102 func (client *Client) GetOrganizationPrivateDomains(orgGUID string, filters ...Filter) ([]Domain, Warnings, error) { 103 request, err := client.newHTTPRequest(requestOptions{ 104 RequestName: internal.GetOrganizationPrivateDomainsRequest, 105 Query: ConvertFilterParameters(filters), 106 URIParams: map[string]string{"organization_guid": orgGUID}, 107 }) 108 if err != nil { 109 return []Domain{}, nil, err 110 } 111 112 fullDomainsList := []Domain{} 113 warnings, err := client.paginate(request, Domain{}, func(item interface{}) error { 114 if domain, ok := item.(Domain); ok { 115 domain.Type = constant.PrivateDomain 116 fullDomainsList = append(fullDomainsList, domain) 117 } else { 118 return ccerror.UnknownObjectInListError{ 119 Expected: Domain{}, 120 Unexpected: item, 121 } 122 } 123 return nil 124 }) 125 126 return fullDomainsList, warnings, err 127 } 128 129 // GetPrivateDomain returns the Private Domain associated with the provided 130 // Domain GUID. 131 func (client *Client) GetPrivateDomain(domainGUID string) (Domain, Warnings, error) { 132 request, err := client.newHTTPRequest(requestOptions{ 133 RequestName: internal.GetPrivateDomainRequest, 134 URIParams: map[string]string{"private_domain_guid": domainGUID}, 135 }) 136 if err != nil { 137 return Domain{}, nil, err 138 } 139 140 var domain Domain 141 response := cloudcontroller.Response{ 142 DecodeJSONResponseInto: &domain, 143 } 144 145 err = client.connection.Make(request, &response) 146 if err != nil { 147 return Domain{}, response.Warnings, err 148 } 149 150 domain.Type = constant.PrivateDomain 151 return domain, response.Warnings, nil 152 } 153 154 // GetPrivateDomains returns the private domains this client has access to. 155 func (client *Client) GetPrivateDomains(filters ...Filter) ([]Domain, Warnings, error) { 156 request, err := client.newHTTPRequest(requestOptions{ 157 RequestName: internal.GetPrivateDomainsRequest, 158 Query: ConvertFilterParameters(filters), 159 }) 160 if err != nil { 161 return []Domain{}, nil, err 162 } 163 164 fullDomainsList := []Domain{} 165 warnings, err := client.paginate(request, Domain{}, func(item interface{}) error { 166 if domain, ok := item.(Domain); ok { 167 domain.Type = constant.PrivateDomain 168 fullDomainsList = append(fullDomainsList, domain) 169 } else { 170 return ccerror.UnknownObjectInListError{ 171 Expected: Domain{}, 172 Unexpected: item, 173 } 174 } 175 return nil 176 }) 177 178 return fullDomainsList, warnings, err 179 } 180 181 // DeletePrivateDomain delete a private domain 182 func (client *Client) DeletePrivateDomain(guid string) (Warnings, error) { 183 request, err := client.newHTTPRequest(requestOptions{ 184 RequestName: internal.DeletePrivateDomainRequest, 185 URIParams: map[string]string{"private_domain_guid": guid}, 186 }) 187 if err != nil { 188 return nil, err 189 } 190 191 response := cloudcontroller.Response{} 192 193 err = client.connection.Make(request, &response) 194 return response.Warnings, err 195 } 196 197 // GetSharedDomain returns the Shared Domain associated with the provided 198 // Domain GUID. 199 func (client *Client) GetSharedDomain(domainGUID string) (Domain, Warnings, error) { 200 request, err := client.newHTTPRequest(requestOptions{ 201 RequestName: internal.GetSharedDomainRequest, 202 URIParams: map[string]string{"shared_domain_guid": domainGUID}, 203 }) 204 if err != nil { 205 return Domain{}, nil, err 206 } 207 208 var domain Domain 209 response := cloudcontroller.Response{ 210 DecodeJSONResponseInto: &domain, 211 } 212 213 err = client.connection.Make(request, &response) 214 if err != nil { 215 return Domain{}, response.Warnings, err 216 } 217 218 domain.Type = constant.SharedDomain 219 return domain, response.Warnings, nil 220 } 221 222 // DeleteSharedDomain delete a shared domain 223 func (client *Client) DeleteSharedDomain(guid string) (Warnings, error) { 224 request, err := client.newHTTPRequest(requestOptions{ 225 RequestName: internal.DeleteSharedDomainRequest, 226 URIParams: map[string]string{"shared_domain_guid": guid}, 227 }) 228 if err != nil { 229 return nil, err 230 } 231 232 response := cloudcontroller.Response{} 233 234 err = client.connection.Make(request, &response) 235 return response.Warnings, err 236 } 237 238 // GetSharedDomains returns the global shared domains. 239 func (client *Client) GetSharedDomains(filters ...Filter) ([]Domain, Warnings, error) { 240 request, err := client.newHTTPRequest(requestOptions{ 241 RequestName: internal.GetSharedDomainsRequest, 242 Query: ConvertFilterParameters(filters), 243 }) 244 if err != nil { 245 return []Domain{}, nil, err 246 } 247 248 fullDomainsList := []Domain{} 249 warnings, err := client.paginate(request, Domain{}, func(item interface{}) error { 250 if domain, ok := item.(Domain); ok { 251 domain.Type = constant.SharedDomain 252 fullDomainsList = append(fullDomainsList, domain) 253 } else { 254 return ccerror.UnknownObjectInListError{ 255 Expected: Domain{}, 256 Unexpected: item, 257 } 258 } 259 return nil 260 }) 261 262 return fullDomainsList, warnings, err 263 } 264 265 // DeleteOrganizationPrivateDomain delete a organization private domain 266 func (client *Client) DeleteOrganizationPrivateDomain(organizationGuid, privateDomainGuid string) (Warnings, error) { 267 request, err := client.newHTTPRequest(requestOptions{ 268 RequestName: internal.DeleteOrganizationPrivateDomainRequest, 269 URIParams: Params{ 270 "organization_guid": organizationGuid, 271 "private_domain_guid": privateDomainGuid, 272 }, 273 }) 274 if err != nil { 275 return nil, err 276 } 277 278 response := cloudcontroller.Response{} 279 280 err = client.connection.Make(request, &response) 281 return response.Warnings, err 282 } 283 284 // CreateDomain creates a cloud controller domain in with the given settings. 285 func (client *Client) CreatePrivateDomain(domainName string, organizationGUID string) (Domain, Warnings, error) { 286 body := createPrivateDomainBody{ 287 Name: domainName, 288 OwningOrganizationGuid: organizationGUID, 289 } 290 bodyBytes, err := json.Marshal(body) 291 request, err := client.newHTTPRequest(requestOptions{ 292 RequestName: internal.PostPrivateDomainRequest, 293 Body: bytes.NewReader(bodyBytes), 294 }) 295 296 if err != nil { 297 return Domain{}, nil, err 298 } 299 var updatedObj Domain 300 response := cloudcontroller.Response{ 301 DecodeJSONResponseInto: &updatedObj, 302 } 303 304 err = client.connection.Make(request, &response) 305 return updatedObj, response.Warnings, err 306 } 307 308 // CreateDomain creates a cloud controller domain in with the given settings. 309 func (client *Client) SetOrganizationPrivateDomain(domainGUID string, organizationGUID string) (Warnings, error) { 310 request, err := client.newHTTPRequest(requestOptions{ 311 RequestName: internal.PutOrganizationPrivateDomainRequest, 312 URIParams: Params{ 313 "organization_guid": organizationGUID, 314 "private_domain_guid": domainGUID, 315 }, 316 }) 317 if err != nil { 318 return nil, err 319 } 320 var response cloudcontroller.Response 321 322 err = client.connection.Make(request, &response) 323 return response.Warnings, err 324 }