github.com/sleungcy/cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/route_mapping.go (about) 1 package ccv2 2 3 import ( 4 "code.cloudfoundry.org/cli/api/cloudcontroller" 5 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 6 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal" 7 ) 8 9 // RouteMapping represents a Cloud Controller map between an application and route. 10 type RouteMapping struct { 11 // GUID is the unique route mapping identifier. 12 GUID string 13 14 // AppGUID is the unique application identifier. 15 AppGUID string 16 17 // RouteGUID is the unique route identifier. 18 RouteGUID string 19 } 20 21 // UnmarshalJSON helps unmarshal a Cloud Controller Route Mapping 22 func (routeMapping *RouteMapping) UnmarshalJSON(data []byte) error { 23 var ccRouteMapping struct { 24 Metadata internal.Metadata `json:"metadata"` 25 Entity struct { 26 AppGUID string `json:"app_guid"` 27 RouteGUID string `json:"route_guid"` 28 } `json:"entity"` 29 } 30 31 err := cloudcontroller.DecodeJSON(data, &ccRouteMapping) 32 if err != nil { 33 return err 34 } 35 36 routeMapping.GUID = ccRouteMapping.Metadata.GUID 37 routeMapping.AppGUID = ccRouteMapping.Entity.AppGUID 38 routeMapping.RouteGUID = ccRouteMapping.Entity.RouteGUID 39 return nil 40 } 41 42 // GetRouteMapping returns a route mapping with the provided guid. 43 func (client *Client) GetRouteMapping(guid string) (RouteMapping, Warnings, error) { 44 request, err := client.newHTTPRequest(requestOptions{ 45 RequestName: internal.GetRouteMappingRequest, 46 URIParams: Params{"route_mapping_guid": guid}, 47 }) 48 if err != nil { 49 return RouteMapping{}, nil, err 50 } 51 52 var routeMapping RouteMapping 53 response := cloudcontroller.Response{ 54 DecodeJSONResponseInto: &routeMapping, 55 } 56 57 err = client.connection.Make(request, &response) 58 return routeMapping, response.Warnings, err 59 } 60 61 // GetRouteMappings returns a list of RouteMappings based off of the provided queries. 62 func (client *Client) GetRouteMappings(filters ...Filter) ([]RouteMapping, Warnings, error) { 63 request, err := client.newHTTPRequest(requestOptions{ 64 RequestName: internal.GetRouteMappingsRequest, 65 Query: ConvertFilterParameters(filters), 66 }) 67 if err != nil { 68 return nil, nil, err 69 } 70 71 var fullRouteMappingsList []RouteMapping 72 warnings, err := client.paginate(request, RouteMapping{}, func(item interface{}) error { 73 if routeMapping, ok := item.(RouteMapping); ok { 74 fullRouteMappingsList = append(fullRouteMappingsList, routeMapping) 75 } else { 76 return ccerror.UnknownObjectInListError{ 77 Expected: RouteMapping{}, 78 Unexpected: item, 79 } 80 } 81 return nil 82 }) 83 84 return fullRouteMappingsList, warnings, err 85 }