github.com/arunkumar7540/cli@v6.45.0+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 } 19 20 func (r Route) MarshalJSON() ([]byte, error) { 21 type Data struct { 22 GUID string `json:"guid,omitempty"` 23 } 24 25 type RelationshipData struct { 26 Data Data `json:"data,omitempty"` 27 } 28 29 type Relationships struct { 30 Space RelationshipData `json:"space,omitempty"` 31 Domain RelationshipData `json:"domain,omitempty"` 32 } 33 34 // Building up the request body in ccRoute 35 type ccRoute struct { 36 GUID string `json:"guid,omitempty"` 37 Host string `json:"host,omitempty"` 38 Path string `json:"path,omitempty"` 39 Relationships *Relationships `json:"relationships,omitempty"` 40 } 41 42 ccR := ccRoute{ 43 GUID: r.GUID, 44 Host: r.Host, 45 Path: r.Path, 46 } 47 48 if r.SpaceGUID != "" { 49 ccR.Relationships = &Relationships{RelationshipData{Data{GUID: r.SpaceGUID}}, 50 RelationshipData{Data{GUID: r.DomainGUID}}} 51 } 52 return json.Marshal(ccR) 53 } 54 55 func (r *Route) UnmarshalJSON(data []byte) error { 56 var alias struct { 57 GUID string `json:"guid,omitempty"` 58 Host string `json:"host,omitempty"` 59 Path string `json:"path,omitempty"` 60 61 Relationships struct { 62 Space struct { 63 Data struct { 64 GUID string `json:"guid,omitempty"` 65 } `json:"data,omitempty"` 66 } `json:"space,omitempty"` 67 Domain struct { 68 Data struct { 69 GUID string `json:"guid,omitempty"` 70 } `json:"data,omitempty"` 71 } `json:"domain,omitempty"` 72 } `json:"relationships,omitempty"` 73 } 74 75 err := cloudcontroller.DecodeJSON(data, &alias) 76 if err != nil { 77 return err 78 } 79 80 r.GUID = alias.GUID 81 r.Host = alias.Host 82 r.SpaceGUID = alias.Relationships.Space.Data.GUID 83 r.DomainGUID = alias.Relationships.Domain.Data.GUID 84 r.Path = alias.Path 85 86 return nil 87 } 88 89 func (client Client) CreateRoute(route Route) (Route, Warnings, error) { 90 bodyBytes, err := json.Marshal(route) 91 if err != nil { 92 return Route{}, nil, err 93 } 94 95 request, err := client.newHTTPRequest(requestOptions{ 96 RequestName: internal.PostRouteRequest, 97 Body: bytes.NewReader(bodyBytes), 98 }) 99 100 if err != nil { 101 return Route{}, nil, err 102 } 103 104 var ccRoute Route 105 response := cloudcontroller.Response{ 106 DecodeJSONResponseInto: &ccRoute, 107 } 108 109 err = client.connection.Make(request, &response) 110 111 return ccRoute, response.Warnings, err 112 } 113 114 func (client Client) DeleteRoute(routeGUID string) (JobURL, Warnings, error) { 115 request, err := client.newHTTPRequest(requestOptions{ 116 URIParams: map[string]string{ 117 "route_guid": routeGUID, 118 }, 119 RequestName: internal.DeleteRouteRequest, 120 }) 121 if err != nil { 122 return "", nil, err 123 } 124 125 response := cloudcontroller.Response{} 126 err = client.connection.Make(request, &response) 127 128 return JobURL(response.ResourceLocationURL), response.Warnings, err 129 } 130 131 func (client Client) GetRoutes(query ...Query) ([]Route, Warnings, error) { 132 request, err := client.newHTTPRequest(requestOptions{ 133 RequestName: internal.GetRoutesRequest, 134 Query: query, 135 }) 136 if err != nil { 137 return nil, nil, err 138 } 139 140 var fullRoutesList []Route 141 warnings, err := client.paginate(request, Route{}, func(item interface{}) error { 142 if route, ok := item.(Route); ok { 143 fullRoutesList = append(fullRoutesList, route) 144 } else { 145 return ccerror.UnknownObjectInListError{ 146 Expected: Route{}, 147 Unexpected: item, 148 } 149 } 150 return nil 151 }) 152 153 return fullRoutesList, warnings, err 154 }