github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/mccp/mccpv2/routes.go (about)

     1  package mccpv2
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/IBM-Cloud/bluemix-go/client"
     7  	"github.com/IBM-Cloud/bluemix-go/rest"
     8  )
     9  
    10  //ErrCodeRouteDoesnotExist ...
    11  var ErrCodeRouteDoesnotExist = "RouteDoesnotExist"
    12  
    13  //RouteRequest ...
    14  type RouteRequest struct {
    15  	Host       string `json:"host,omitempty"`
    16  	SpaceGUID  string `json:"space_guid"`
    17  	DomainGUID string `json:"domain_guid,omitempty"`
    18  	Path       string `json:"path,omitempty"`
    19  	Port       *int   `json:"port,omitempty"`
    20  }
    21  
    22  //RouteUpdateRequest ...
    23  type RouteUpdateRequest struct {
    24  	Host *string `json:"host,omitempty"`
    25  	Path *string `json:"path,omitempty"`
    26  	Port *int    `json:"port,omitempty"`
    27  }
    28  
    29  //RouteMetadata ...
    30  type RouteMetadata struct {
    31  	GUID string `json:"guid"`
    32  	URL  string `json:"url"`
    33  }
    34  
    35  //RouteEntity ...
    36  type RouteEntity struct {
    37  	Host                string `json:"host"`
    38  	Path                string `json:"path"`
    39  	DomainGUID          string `json:"domain_guid"`
    40  	SpaceGUID           string `json:"space_guid"`
    41  	ServiceInstanceGUID string `json:"service_instance_guid"`
    42  	Port                *int   `json:"port"`
    43  	DomainURL           string `json:"domain_url"`
    44  	SpaceURL            string `json:"space_url"`
    45  	AppsURL             string `json:"apps_url"`
    46  	RouteMappingURL     string `json:"route_mapping_url"`
    47  }
    48  
    49  //RouteResource ...
    50  type RouteResource struct {
    51  	Resource
    52  	Entity RouteEntity
    53  }
    54  
    55  //RouteFields ...
    56  type RouteFields struct {
    57  	Metadata RouteMetadata
    58  	Entity   RouteEntity
    59  }
    60  
    61  //ToFields ..
    62  func (resource RouteResource) ToFields() Route {
    63  	entity := resource.Entity
    64  
    65  	return Route{
    66  		GUID:                resource.Metadata.GUID,
    67  		Host:                entity.Host,
    68  		Path:                entity.Path,
    69  		DomainGUID:          entity.DomainGUID,
    70  		SpaceGUID:           entity.SpaceGUID,
    71  		ServiceInstanceGUID: entity.ServiceInstanceGUID,
    72  		Port:                entity.Port,
    73  		DomainURL:           entity.DomainURL,
    74  		SpaceURL:            entity.SpaceURL,
    75  		AppsURL:             entity.AppsURL,
    76  		RouteMappingURL:     entity.RouteMappingURL,
    77  	}
    78  }
    79  
    80  //Route model
    81  type Route struct {
    82  	GUID                string
    83  	Host                string
    84  	Path                string
    85  	DomainGUID          string
    86  	SpaceGUID           string
    87  	ServiceInstanceGUID string
    88  	Port                *int
    89  	DomainURL           string
    90  	SpaceURL            string
    91  	AppsURL             string
    92  	RouteMappingURL     string
    93  }
    94  
    95  //Routes ...
    96  type Routes interface {
    97  	Find(hostname, domainGUID string) ([]Route, error)
    98  	Create(req RouteRequest, opts ...bool) (*RouteFields, error)
    99  	Get(routeGUID string) (*RouteFields, error)
   100  	Update(routeGUID string, req RouteUpdateRequest, opts ...bool) (*RouteFields, error)
   101  	Delete(routeGUID string, opts ...bool) error
   102  }
   103  
   104  type route struct {
   105  	client *client.Client
   106  }
   107  
   108  func newRouteAPI(c *client.Client) Routes {
   109  	return &route{
   110  		client: c,
   111  	}
   112  }
   113  
   114  func (r *route) Get(routeGUID string) (*RouteFields, error) {
   115  	rawURL := fmt.Sprintf("/v2/routes/%s", routeGUID)
   116  	routeFields := RouteFields{}
   117  	_, err := r.client.Get(rawURL, &routeFields, nil)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  	return &routeFields, nil
   122  }
   123  
   124  func (r *route) Find(hostname, domainGUID string) ([]Route, error) {
   125  	rawURL := "/v2/routes?inline-relations-depth=1"
   126  	req := rest.GetRequest(rawURL).Query("q", "host:"+hostname+";domain_guid:"+domainGUID)
   127  	httpReq, err := req.Build()
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  	path := httpReq.URL.String()
   132  	route, err := listRouteWithPath(r.client, path)
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  	return route, nil
   137  }
   138  
   139  // opts is list of boolean parametes
   140  // opts[0] - async - Will run the create request in a background job. Recommended: 'true'. Default to 'true'.
   141  
   142  func (r *route) Create(req RouteRequest, opts ...bool) (*RouteFields, error) {
   143  	async := true
   144  	if len(opts) > 0 {
   145  		async = opts[0]
   146  	}
   147  	rawURL := fmt.Sprintf("/v2/routes?async=%t&inline-relations-depth=1", async)
   148  	routeFields := RouteFields{}
   149  	_, err := r.client.Post(rawURL, req, &routeFields)
   150  	if err != nil {
   151  		return nil, err
   152  	}
   153  	return &routeFields, nil
   154  }
   155  
   156  // opts is list of boolean parametes
   157  // opts[0] - async - Will run the update request in a background job. Recommended: 'true'. Default to 'true'.
   158  
   159  func (r *route) Update(routeGUID string, req RouteUpdateRequest, opts ...bool) (*RouteFields, error) {
   160  	async := true
   161  	if len(opts) > 0 {
   162  		async = opts[0]
   163  	}
   164  	rawURL := fmt.Sprintf("/v2/routes/%s?async=%t", routeGUID, async)
   165  	routeFields := RouteFields{}
   166  	_, err := r.client.Put(rawURL, req, &routeFields)
   167  	if err != nil {
   168  		return nil, err
   169  	}
   170  	return &routeFields, nil
   171  }
   172  
   173  // opts is list of boolean parametes
   174  // opts[0] - async - Will run the delete request in a background job. Recommended: 'true'. Default to 'true'.
   175  // opts[1] - recursive - Will delete route service bindings and route mappings associated with the route. Default to 'false'.
   176  
   177  func (r *route) Delete(routeGUID string, opts ...bool) error {
   178  	async := true
   179  	recursive := false
   180  	if len(opts) > 0 {
   181  		async = opts[0]
   182  	}
   183  	if len(opts) > 1 {
   184  		recursive = opts[1]
   185  	}
   186  	rawURL := fmt.Sprintf("/v2/routes/%s?async=%t&recursive=%t", routeGUID, async, recursive)
   187  	_, err := r.client.Delete(rawURL)
   188  	return err
   189  }
   190  
   191  func listRouteWithPath(c *client.Client, path string) ([]Route, error) {
   192  	var route []Route
   193  	_, err := c.GetPaginated(path, NewCCPaginatedResources(RouteResource{}), func(resource interface{}) bool {
   194  		if routeResource, ok := resource.(RouteResource); ok {
   195  			route = append(route, routeResource.ToFields())
   196  			return true
   197  		}
   198  		return false
   199  	})
   200  	return route, err
   201  }