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