github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/api/cloudcontroller/ccv3/route.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 ) 11 12 type Route struct { 13 GUID string 14 SpaceGUID string 15 DomainGUID string 16 Host string 17 Path string 18 URL string 19 Destinations []RouteDestination 20 Metadata *Metadata 21 } 22 23 func (r Route) MarshalJSON() ([]byte, error) { 24 type Data struct { 25 GUID string `json:"guid,omitempty"` 26 } 27 28 type RelationshipData struct { 29 Data Data `json:"data,omitempty"` 30 } 31 32 type Relationships struct { 33 Space RelationshipData `json:"space,omitempty"` 34 Domain RelationshipData `json:"domain,omitempty"` 35 } 36 37 // Building up the request body in ccRoute 38 type ccRoute struct { 39 GUID string `json:"guid,omitempty"` 40 Host string `json:"host,omitempty"` 41 Path string `json:"path,omitempty"` 42 Relationships *Relationships `json:"relationships,omitempty"` 43 } 44 45 ccR := ccRoute{ 46 GUID: r.GUID, 47 Host: r.Host, 48 Path: r.Path, 49 } 50 51 if r.SpaceGUID != "" { 52 ccR.Relationships = &Relationships{ 53 Space: RelationshipData{Data{GUID: r.SpaceGUID}}, 54 Domain: RelationshipData{Data{GUID: r.DomainGUID}}, 55 } 56 } 57 58 return json.Marshal(ccR) 59 } 60 61 func (r *Route) UnmarshalJSON(data []byte) error { 62 var alias struct { 63 GUID string `json:"guid,omitempty"` 64 Host string `json:"host,omitempty"` 65 Path string `json:"path,omitempty"` 66 URL string `json:"url,omitempty"` 67 Destinations []RouteDestination `json:"destinations,omitempty"` 68 Metadata *Metadata `json:"metadata,omitempty"` 69 70 Relationships struct { 71 Space struct { 72 Data struct { 73 GUID string `json:"guid,omitempty"` 74 } `json:"data,omitempty"` 75 } `json:"space,omitempty"` 76 Domain struct { 77 Data struct { 78 GUID string `json:"guid,omitempty"` 79 } `json:"data,omitempty"` 80 } `json:"domain,omitempty"` 81 } `json:"relationships,omitempty"` 82 } 83 84 err := cloudcontroller.DecodeJSON(data, &alias) 85 if err != nil { 86 return err 87 } 88 89 r.GUID = alias.GUID 90 r.Host = alias.Host 91 r.SpaceGUID = alias.Relationships.Space.Data.GUID 92 r.DomainGUID = alias.Relationships.Domain.Data.GUID 93 r.Path = alias.Path 94 r.URL = alias.URL 95 r.Destinations = alias.Destinations 96 r.Metadata = alias.Metadata 97 98 return nil 99 } 100 101 type RouteDestinationApp struct { 102 GUID string 103 Process struct { 104 Type string 105 } 106 } 107 108 type RouteDestination struct { 109 GUID string 110 App RouteDestinationApp 111 } 112 113 func (client Client) CreateRoute(route Route) (Route, Warnings, error) { 114 bodyBytes, err := json.Marshal(route) 115 if err != nil { 116 return Route{}, nil, err 117 } 118 119 request, err := client.newHTTPRequest(requestOptions{ 120 RequestName: internal.PostRouteRequest, 121 Body: bytes.NewReader(bodyBytes), 122 }) 123 124 if err != nil { 125 return Route{}, nil, err 126 } 127 128 var ccRoute Route 129 response := cloudcontroller.Response{ 130 DecodeJSONResponseInto: &ccRoute, 131 } 132 133 err = client.connection.Make(request, &response) 134 135 return ccRoute, response.Warnings, err 136 } 137 138 func (client Client) DeleteOrphanedRoutes(spaceGUID string) (JobURL, Warnings, error) { 139 request, err := client.newHTTPRequest(requestOptions{ 140 RequestName: internal.DeleteOrphanedRoutesRequest, 141 URIParams: map[string]string{ 142 "space_guid": spaceGUID, 143 }, 144 Query: []Query{{Key: UnmappedFilter, Values: []string{"true"}}}, 145 }) 146 if err != nil { 147 return "", nil, err 148 } 149 150 response := cloudcontroller.Response{} 151 err = client.connection.Make(request, &response) 152 153 return JobURL(response.ResourceLocationURL), response.Warnings, err 154 } 155 156 func (client Client) DeleteRoute(routeGUID string) (JobURL, Warnings, error) { 157 request, err := client.newHTTPRequest(requestOptions{ 158 URIParams: map[string]string{ 159 "route_guid": routeGUID, 160 }, 161 RequestName: internal.DeleteRouteRequest, 162 }) 163 if err != nil { 164 return "", nil, err 165 } 166 167 response := cloudcontroller.Response{} 168 err = client.connection.Make(request, &response) 169 170 return JobURL(response.ResourceLocationURL), response.Warnings, err 171 } 172 173 func (client Client) GetApplicationRoutes(appGUID string) ([]Route, Warnings, error) { 174 request, err := client.newHTTPRequest(requestOptions{ 175 RequestName: internal.GetApplicationRoutesRequest, 176 URIParams: internal.Params{"app_guid": appGUID}, 177 }) 178 if err != nil { 179 return nil, nil, err 180 } 181 182 var fullRoutesList []Route 183 warnings, err := client.paginate(request, Route{}, func(item interface{}) error { 184 if route, ok := item.(Route); ok { 185 fullRoutesList = append(fullRoutesList, route) 186 } else { 187 return ccerror.UnknownObjectInListError{ 188 Expected: Route{}, 189 Unexpected: item, 190 } 191 } 192 return nil 193 }) 194 195 return fullRoutesList, warnings, err 196 } 197 198 func (client Client) GetRouteDestinations(routeGUID string) ([]RouteDestination, Warnings, error) { 199 request, err := client.newHTTPRequest(requestOptions{ 200 RequestName: internal.GetRouteDestinationsRequest, 201 URIParams: internal.Params{"route_guid": routeGUID}, 202 }) 203 204 if err != nil { 205 return nil, nil, err 206 } 207 208 var destinationResponse struct { 209 Destinations []RouteDestination `json:"destinations"` 210 } 211 212 response := cloudcontroller.Response{ 213 DecodeJSONResponseInto: &destinationResponse, 214 } 215 216 err = client.connection.Make(request, &response) 217 return destinationResponse.Destinations, response.Warnings, err 218 } 219 220 func (client Client) GetRoutes(query ...Query) ([]Route, Warnings, error) { 221 request, err := client.newHTTPRequest(requestOptions{ 222 RequestName: internal.GetRoutesRequest, 223 Query: query, 224 }) 225 if err != nil { 226 return nil, nil, err 227 } 228 229 var fullRoutesList []Route 230 warnings, err := client.paginate(request, Route{}, func(item interface{}) error { 231 if route, ok := item.(Route); ok { 232 fullRoutesList = append(fullRoutesList, route) 233 } else { 234 return ccerror.UnknownObjectInListError{ 235 Expected: Route{}, 236 Unexpected: item, 237 } 238 } 239 return nil 240 }) 241 242 return fullRoutesList, warnings, err 243 } 244 245 func (client Client) MapRoute(routeGUID string, appGUID string) (Warnings, error) { 246 type destinationProcess struct { 247 ProcessType string `json:"process_type"` 248 } 249 250 type destinationApp struct { 251 GUID string `json:"guid"` 252 Process *destinationProcess `json:"process,omitempty"` 253 } 254 type destination struct { 255 App destinationApp `json:"app"` 256 } 257 258 type body struct { 259 Destinations []destination `json:"destinations"` 260 } 261 262 requestBody := body{ 263 Destinations: []destination{ 264 {App: destinationApp{GUID: appGUID}}, 265 }, 266 } 267 268 bodyBytes, err := json.Marshal(requestBody) 269 if err != nil { 270 return nil, err 271 } 272 273 request, err := client.newHTTPRequest(requestOptions{ 274 RequestName: internal.MapRouteRequest, 275 URIParams: map[string]string{ 276 "route_guid": routeGUID, 277 }, 278 Body: bytes.NewReader(bodyBytes), 279 }) 280 if err != nil { 281 return nil, err 282 } 283 284 response := cloudcontroller.Response{} 285 err = client.connection.Make(request, &response) 286 287 return response.Warnings, err 288 } 289 290 func (client Client) UnmapRoute(routeGUID string, destinationGUID string) (Warnings, error) { 291 request, err := client.newHTTPRequest(requestOptions{ 292 RequestName: internal.UnmapRouteRequest, 293 URIParams: map[string]string{ 294 "route_guid": routeGUID, 295 "destination_guid": destinationGUID, 296 }, 297 }) 298 if err != nil { 299 return nil, err 300 } 301 302 response := cloudcontroller.Response{} 303 err = client.connection.Make(request, &response) 304 305 return response.Warnings, err 306 }