github.com/arunkumar7540/cli@v6.45.0+incompatible/api/cloudcontroller/ccv3/domain.go (about) 1 package ccv3 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/ccv3/internal" 10 "code.cloudfoundry.org/cli/types" 11 ) 12 13 type Domain struct { 14 GUID string `json:"guid,omitempty"` 15 Name string `json:"name"` 16 Internal types.NullBool `json:"internal,omitempty"` 17 OrganizationGUID string `json:"orgguid,omitempty"` 18 } 19 20 func (d Domain) MarshalJSON() ([]byte, error) { 21 type Data struct { 22 GUID string `json:"guid,omitempty"` 23 } 24 25 type OrgData struct { 26 Data Data `json:"data,omitempty"` 27 } 28 29 type OrgRelationship struct { 30 Org OrgData `json:"organization,omitempty"` 31 } 32 33 type ccDomain struct { 34 GUID string `json:"guid,omitempty"` 35 Name string `json:"name"` 36 Internal *bool `json:"internal,omitempty"` 37 Relationships *OrgRelationship `json:"relationships,omitempty"` 38 } 39 40 ccDom := ccDomain{ 41 Name: d.Name, 42 } 43 44 if d.Internal.IsSet { 45 ccDom.Internal = &d.Internal.Value 46 } 47 48 if d.GUID != "" { 49 ccDom.GUID = d.GUID 50 } 51 52 if d.OrganizationGUID != "" { 53 ccDom.Relationships = &OrgRelationship{OrgData{Data{GUID: d.OrganizationGUID}}} 54 } 55 return json.Marshal(ccDom) 56 } 57 58 func (d *Domain) UnmarshalJSON(data []byte) error { 59 var ccRouteStruct struct { 60 GUID string `json:"guid,omitempty"` 61 Name string `json:"name"` 62 Internal types.NullBool `json:"internal,omitempty"` 63 Relationships struct { 64 Organization struct { 65 Data struct { 66 GUID string `json:"guid,omitempty"` 67 } `json:"data,omitempty"` 68 } `json:"organization,omitempty"` 69 } `json:"relationships,omitempty"` 70 } 71 72 err := cloudcontroller.DecodeJSON(data, &ccRouteStruct) 73 if err != nil { 74 return err 75 } 76 77 d.GUID = ccRouteStruct.GUID 78 d.Name = ccRouteStruct.Name 79 d.Internal = ccRouteStruct.Internal 80 d.OrganizationGUID = ccRouteStruct.Relationships.Organization.Data.GUID 81 82 return nil 83 } 84 85 type SharedOrgs struct { 86 GUIDs []string 87 } 88 89 func (sharedOrgs SharedOrgs) MarshalJSON() ([]byte, error) { 90 type Org struct { 91 GUID string `json:"guid,omitempty"` 92 } 93 94 type Data = []Org 95 96 type sharedOrgsRelationship struct { 97 Data Data `json:"data"` 98 } 99 100 var orgs []Org 101 for _, sharedOrgGUID := range sharedOrgs.GUIDs { 102 orgs = append(orgs, Org{GUID: sharedOrgGUID}) 103 } 104 105 relationship := sharedOrgsRelationship{ 106 Data: orgs, 107 } 108 109 return json.Marshal(relationship) 110 } 111 112 func (sharedOrgs *SharedOrgs) UnmarshalJSON(data []byte) error { 113 var alias struct { 114 Data []struct { 115 GUID string `json:"guid,omitempty"` 116 } `json:"data,omitempty"` 117 } 118 119 err := cloudcontroller.DecodeJSON(data, &alias) 120 if err != nil { 121 return err 122 } 123 124 var guids []string 125 for _, org := range alias.Data { 126 guids = append(guids, org.GUID) 127 } 128 129 sharedOrgs.GUIDs = guids 130 return nil 131 } 132 133 func (client Client) CreateDomain(domain Domain) (Domain, Warnings, error) { 134 bodyBytes, err := json.Marshal(domain) 135 if err != nil { 136 return Domain{}, nil, err 137 } 138 139 request, err := client.newHTTPRequest(requestOptions{ 140 RequestName: internal.PostDomainRequest, 141 Body: bytes.NewReader(bodyBytes), 142 }) 143 144 if err != nil { 145 return Domain{}, nil, err 146 } 147 148 var ccDomain Domain 149 response := cloudcontroller.Response{ 150 DecodeJSONResponseInto: &ccDomain, 151 } 152 153 err = client.connection.Make(request, &response) 154 155 return ccDomain, response.Warnings, err 156 } 157 158 func (client Client) DeleteDomain(domainGUID string) (JobURL, Warnings, error) { 159 request, err := client.newHTTPRequest(requestOptions{ 160 URIParams: map[string]string{ 161 "domain_guid": domainGUID, 162 }, 163 RequestName: internal.DeleteDomainRequest, 164 }) 165 if err != nil { 166 return "", nil, err 167 } 168 169 response := cloudcontroller.Response{} 170 err = client.connection.Make(request, &response) 171 172 return JobURL(response.ResourceLocationURL), response.Warnings, err 173 } 174 175 // GetDomain returns a domain with the given GUID. 176 func (client *Client) GetDomain(domainGUID string) (Domain, Warnings, error) { 177 request, err := client.newHTTPRequest(requestOptions{ 178 RequestName: internal.GetDomainRequest, 179 URIParams: map[string]string{"domain_guid": domainGUID}, 180 }) 181 if err != nil { 182 return Domain{}, nil, err 183 } 184 185 var responseDomain Domain 186 response := cloudcontroller.Response{ 187 DecodeJSONResponseInto: &responseDomain, 188 } 189 err = client.connection.Make(request, &response) 190 191 return responseDomain, response.Warnings, err 192 } 193 194 func (client Client) GetDomains(query ...Query) ([]Domain, Warnings, error) { 195 request, err := client.newHTTPRequest(requestOptions{ 196 RequestName: internal.GetDomainsRequest, 197 Query: query, 198 }) 199 if err != nil { 200 return nil, nil, err 201 } 202 203 var fullDomainsList []Domain 204 warnings, err := client.paginate(request, Domain{}, func(item interface{}) error { 205 if domain, ok := item.(Domain); ok { 206 fullDomainsList = append(fullDomainsList, domain) 207 } else { 208 return ccerror.UnknownObjectInListError{ 209 Expected: Domain{}, 210 Unexpected: item, 211 } 212 } 213 return nil 214 }) 215 216 return fullDomainsList, warnings, err 217 } 218 219 func (client Client) GetOrganizationDomains(orgGUID string, query ...Query) ([]Domain, Warnings, error) { 220 request, err := client.newHTTPRequest(requestOptions{ 221 URIParams: internal.Params{"organization_guid": orgGUID}, 222 RequestName: internal.GetOrganizationDomainsRequest, 223 Query: query, 224 }) 225 if err != nil { 226 return nil, nil, err 227 } 228 229 var fullDomainsList []Domain 230 warnings, err := client.paginate(request, Domain{}, func(item interface{}) error { 231 if domain, ok := item.(Domain); ok { 232 fullDomainsList = append(fullDomainsList, domain) 233 } else { 234 return ccerror.UnknownObjectInListError{ 235 Expected: Domain{}, 236 Unexpected: item, 237 } 238 } 239 return nil 240 }) 241 242 return fullDomainsList, warnings, err 243 } 244 245 func (client Client) SharePrivateDomainToOrgs(domainGuid string, sharedOrgs SharedOrgs) (Warnings, error) { 246 bodyBytes, err := json.Marshal(sharedOrgs) 247 248 if err != nil { 249 return nil, err 250 } 251 252 request, err := client.newHTTPRequest(requestOptions{ 253 URIParams: internal.Params{"domain_guid": domainGuid}, 254 RequestName: internal.SharePrivateDomainRequest, 255 Body: bytes.NewReader(bodyBytes), 256 }) 257 258 if err != nil { 259 return nil, err 260 } 261 262 var ccSharedOrgs SharedOrgs 263 response := cloudcontroller.Response{ 264 DecodeJSONResponseInto: &ccSharedOrgs, 265 } 266 267 err = client.connection.Make(request, &response) 268 269 return response.Warnings, err 270 } 271 272 func (client Client) UnsharePrivateDomainFromOrg(domainGuid string, orgGUID string) (Warnings, error) { 273 request, err := client.newHTTPRequest(requestOptions{ 274 URIParams: internal.Params{"domain_guid": domainGuid, "org_guid": orgGUID}, 275 RequestName: internal.DeleteSharedOrgFromDomainRequest, 276 }) 277 278 if err != nil { 279 return nil, err 280 } 281 282 var response cloudcontroller.Response 283 284 err = client.connection.Make(request, &response) 285 return response.Warnings, err 286 }