github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/actor/pushaction/route.go (about)

     1  package pushaction
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/v2action"
     5  	log "github.com/sirupsen/logrus"
     6  )
     7  
     8  func (actor Actor) BindRoutes(config ApplicationConfig) (ApplicationConfig, bool, Warnings, error) {
     9  	log.Info("binding routes")
    10  
    11  	var boundRoutes bool
    12  	var allWarnings Warnings
    13  
    14  	for _, route := range config.DesiredRoutes {
    15  		if !actor.routeInListByGUID(route, config.CurrentRoutes) {
    16  			log.Debugf("binding route: %#v", route)
    17  			warnings, err := actor.BindRouteToApp(route, config.DesiredApplication.GUID)
    18  			allWarnings = append(allWarnings, warnings...)
    19  			if err != nil {
    20  				log.Errorln("binding route:", err)
    21  				return ApplicationConfig{}, false, allWarnings, err
    22  			}
    23  			boundRoutes = true
    24  		} else {
    25  			log.Debugf("route %s already bound to app", route)
    26  		}
    27  	}
    28  	log.Debug("binding routes complete")
    29  	config.CurrentRoutes = config.DesiredRoutes
    30  
    31  	return config, boundRoutes, allWarnings, nil
    32  }
    33  
    34  func (actor Actor) getDefaultRoute(orgGUID string, spaceGUID string, appName string) (v2action.Route, Warnings, error) {
    35  	defaultDomain, domainWarnings, err := actor.DefaultDomain(orgGUID)
    36  	if err != nil {
    37  		return v2action.Route{}, domainWarnings, err
    38  	}
    39  
    40  	return v2action.Route{
    41  		Host:      appName,
    42  		Domain:    defaultDomain,
    43  		SpaceGUID: spaceGUID,
    44  	}, domainWarnings, nil
    45  
    46  }
    47  
    48  func (actor Actor) CreateAndBindApplicationRoutes(orgGUID string, spaceGUID string, app v2action.Application) (Warnings, error) {
    49  	var warnings Warnings
    50  	defaultRoute, domainWarnings, err := actor.getDefaultRoute(orgGUID, spaceGUID, app.Name)
    51  	warnings = append(warnings, domainWarnings...)
    52  	if err != nil {
    53  		return warnings, err
    54  	}
    55  
    56  	boundRoutes, appRouteWarnings, err := actor.V2Actor.GetApplicationRoutes(app.GUID)
    57  	warnings = append(warnings, appRouteWarnings...)
    58  	if err != nil {
    59  		return Warnings(warnings), err
    60  	}
    61  
    62  	_, routeAlreadyBound := actor.routeInListBySettings(defaultRoute, boundRoutes)
    63  	if routeAlreadyBound {
    64  		return Warnings(warnings), nil
    65  	}
    66  
    67  	spaceRoute, spaceRouteWarnings, err := actor.V2Actor.FindRouteBoundToSpaceWithSettings(defaultRoute)
    68  	warnings = append(warnings, spaceRouteWarnings...)
    69  	routeAlreadyExists := true
    70  	if _, ok := err.(v2action.RouteNotFoundError); ok {
    71  		routeAlreadyExists = false
    72  	} else if err != nil {
    73  		return Warnings(warnings), err
    74  	}
    75  
    76  	if !routeAlreadyExists {
    77  		var createRouteWarning v2action.Warnings
    78  		spaceRoute, createRouteWarning, err = actor.V2Actor.CreateRoute(defaultRoute, false)
    79  		warnings = append(warnings, createRouteWarning...)
    80  		if err != nil {
    81  			return Warnings(warnings), err
    82  		}
    83  	}
    84  
    85  	bindWarnings, err := actor.V2Actor.BindRouteToApplication(spaceRoute.GUID, app.GUID)
    86  	warnings = append(warnings, bindWarnings...)
    87  	if err != nil {
    88  		return Warnings(warnings), err
    89  	}
    90  
    91  	return Warnings(warnings), nil
    92  }
    93  
    94  func (actor Actor) CreateRoutes(config ApplicationConfig) (ApplicationConfig, bool, Warnings, error) {
    95  	log.Info("creating routes")
    96  
    97  	var routes []v2action.Route
    98  	var createdRoutes bool
    99  	var allWarnings Warnings
   100  
   101  	for _, route := range config.DesiredRoutes {
   102  		if route.GUID == "" {
   103  			log.Debugf("creating route: %#v", route)
   104  
   105  			createdRoute, warnings, err := actor.V2Actor.CreateRoute(route, false)
   106  			allWarnings = append(allWarnings, warnings...)
   107  			if err != nil {
   108  				log.Errorln("creating route:", err)
   109  				return ApplicationConfig{}, true, allWarnings, err
   110  			}
   111  			routes = append(routes, createdRoute)
   112  
   113  			createdRoutes = true
   114  		} else {
   115  			log.WithField("route", route).Debug("already exists, skipping")
   116  			routes = append(routes, route)
   117  		}
   118  	}
   119  	config.DesiredRoutes = routes
   120  
   121  	return config, createdRoutes, allWarnings, nil
   122  }
   123  
   124  // GetRouteWithDefaultDomain returns a route with the host and the default org
   125  // domain. This may be a partial route (ie no GUID) if the route does not
   126  // exist.
   127  func (actor Actor) GetRouteWithDefaultDomain(host string, orgGUID string, spaceGUID string, knownRoutes []v2action.Route) (v2action.Route, Warnings, error) {
   128  	defaultDomain, warnings, err := actor.DefaultDomain(orgGUID)
   129  	if err != nil {
   130  		log.Errorln("could not find default domains:", err.Error())
   131  		return v2action.Route{}, Warnings(warnings), err
   132  	}
   133  
   134  	defaultRoute := v2action.Route{
   135  		Domain:    defaultDomain,
   136  		Host:      host,
   137  		SpaceGUID: spaceGUID,
   138  	}
   139  
   140  	if cachedRoute, found := actor.routeInListBySettings(defaultRoute, knownRoutes); !found {
   141  		route, routeWarnings, err := actor.V2Actor.FindRouteBoundToSpaceWithSettings(defaultRoute)
   142  		if _, ok := err.(v2action.RouteNotFoundError); ok {
   143  			return defaultRoute, append(Warnings(warnings), routeWarnings...), nil
   144  		}
   145  		return route, append(Warnings(warnings), routeWarnings...), err
   146  	} else {
   147  		return cachedRoute, Warnings(warnings), nil
   148  	}
   149  }
   150  
   151  func (actor Actor) BindRouteToApp(route v2action.Route, appGUID string) (v2action.Warnings, error) {
   152  	warnings, err := actor.V2Actor.BindRouteToApplication(route.GUID, appGUID)
   153  	if _, ok := err.(v2action.RouteInDifferentSpaceError); ok {
   154  		return warnings, v2action.RouteInDifferentSpaceError{Route: route.String()}
   155  	}
   156  	return warnings, err
   157  }
   158  
   159  func (_ Actor) routeInListByGUID(route v2action.Route, routes []v2action.Route) bool {
   160  	for _, r := range routes {
   161  		if r.GUID == route.GUID {
   162  			return true
   163  		}
   164  	}
   165  
   166  	return false
   167  }
   168  
   169  func (_ Actor) routeInListBySettings(route v2action.Route, routes []v2action.Route) (v2action.Route, bool) {
   170  	for _, r := range routes {
   171  		if r.Host == route.Host && r.Path == route.Path && r.Port == route.Port &&
   172  			r.SpaceGUID == route.SpaceGUID && r.Domain.GUID == route.Domain.GUID {
   173  			return r, true
   174  		}
   175  	}
   176  
   177  	return v2action.Route{}, false
   178  }