<commit_before>package main

import (
	"encoding/json"
	"errors"
	"github.com/parnurzeal/gorequest"
	"strconv"
)

/*
 * {
 *   "name": "howdy-lb",
 *   "major_version": 1,
 *   "datacenter": "us-east-1",
 *   "namespace": "dev"
 * }
 */
type LoadbalancerCreate struct {
	Name         string `json:"name"`
	MajorVersion int    `json:"major_version"`
	Datacenter   string `json:"datacenter"`
	Namespace    string `json:"namespace"`
}

/*
 * {
 *   "name": "howdy-lb--1--974u8r6v",
 *   "routes": [
 *     ...
 *   ],
 *   "guid": "b74b8209468b",
 *   "deploy_time": 1481065235649,
 *   "datacenter": "us-east-1",
 *   "namespace": "dev"
 * }
 */
type Loadbalancer struct {
	Name         string              `json:"name"`
	Routes       []LoadbalancerRoute `json:"routes"`
	Guid         string              `json:"guid"`
	DeployTime   int64               `json:"deploy_time"`
	Datacenter   string              `json:"datacenter"`
	NamespaceRef string              `json:"namespace"`
	Address      string              `json:"address"`
}

/*
 * {
 *   "backend_port_reference": "default",
 *   "backend_major_version": 1,
 *   "backend_name": "howdy-http",
 *   "lb_port": 8444
 * }
 */
type LoadbalancerRoute struct {
	BackendPortReference string `json:"backend_port_reference"`
	BackendMajorVersion  int    `json:"backend_major_version"`
	BackendName          string `json:"backend_name"`
	LBPort               int    `json:"lb_port"`
}

//////////////////////// LIST ////////////////////////

func ListLoadbalancers(delimitedDcs string, delimitedNamespaces string, delimitedStatuses string, http *gorequest.SuperAgent, cfg *Config) (list []Loadbalancer, err []error) {
	uri := "/v1/loadbalancers?"
	// set the datacenters if specified
	if isValidCommaDelimitedList(delimitedDcs) {
		uri = uri + "dc=" + delimitedDcs + "&"
	}
	if isValidCommaDelimitedList(delimitedNamespaces) {
		uri = uri + "ns=" + delimitedNamespaces
	} else {
		uri = uri + "ns=dev,qa,prod"
	}

	r, bytes, errs := AugmentRequest(
		http.Get(cfg.Endpoint+uri), cfg).EndBytes()

	if r != nil {
		if r.StatusCode/100 != 2 {
			errs = append(errs, errors.New("bad response from Nelson server"))
			return nil, errs
		} else {
			var list []Loadbalancer
			if err := json.Unmarshal(bytes, &list); err != nil {
				panic(err)
			}
			return list, errs
		}
	} else {
		errs = append(errs, errors.New("No response from the Nelson server, aborting."))
		return nil, errs
	}
}

func PrintListLoadbalancers(lb []Loadbalancer) {
	var tabulized = [][]string{}
	for _, l := range lb {
		routes := ""
		for i, r := range l.Routes {
			// 8443 ~> howdy-http@1->default
			routes = routes + strconv.Itoa(r.LBPort) + " ~> " + r.BackendName + "@" + strconv.Itoa(r.BackendMajorVersion) + "->" + r.BackendPortReference

			// if not the last element, lets bang on a comma
			if i == len(l.Routes) {
				routes = routes + ", "
			}
		}
		tabulized = append(tabulized, []string{l.Guid, l.Datacenter, l.NamespaceRef, l.Name, routes, l.Address})
	}

	RenderTableToStdout([]string{"GUID", "Datacenter", "Namespace", "Name", "Routes", "Address"}, tabulized)
}

//////////////////////// REMOVE ////////////////////////

func RemoveLoadBalancer(guid string, http *gorequest.SuperAgent, cfg *Config) (str string, err []error) {<commit_msg>update the lbs list <commit_after>package main

import (
	"encoding/json"
	"errors"
	"github.com/parnurzeal/gorequest"
	"strconv"
)

/*
 * {
 *   "name": "howdy-lb",
 *   "major_version": 1,
 *   "datacenter": "us-east-1",
 *   "namespace": "dev"
 * }
 */
type LoadbalancerCreate struct {
	Name         string `json:"name"`
	MajorVersion int    `json:"major_version"`
	Datacenter   string `json:"datacenter"`
	Namespace    string `json:"namespace"`
}

/*
 * {
 *   "name": "howdy-lb--1--974u8r6v",
 *   "routes": [
 *     ...
 *   ],
 *   "guid": "b74b8209468b",
 *   "deploy_time": 1481065235649,
 *   "datacenter": "us-east-1",
 *   "namespace": "dev"
 * }
 */
type Loadbalancer struct {
	Name         string              `json:"name"`
	Routes       []LoadbalancerRoute `json:"routes"`
	Guid         string              `json:"guid"`
	DeployTime   int64               `json:"deploy_time"`
	Datacenter   string              `json:"datacenter"`
	NamespaceRef string              `json:"namespace"`
	Address      string              `json:"address"`
}

/*
 * {
 *   "backend_port_reference": "default",
 *   "backend_major_version": 1,
 *   "backend_name": "howdy-http",
 *   "lb_port": 8444
 * }
 */
type LoadbalancerRoute struct {
	BackendPortReference string `json:"backend_port_reference"`
	BackendMajorVersion  int    `json:"backend_major_version"`
	BackendName          string `json:"backend_name"`
	LBPort               int    `json:"lb_port"`
}

//////////////////////// LIST ////////////////////////

func ListLoadbalancers(delimitedDcs string, delimitedNamespaces string, delimitedStatuses string, http *gorequest.SuperAgent, cfg *Config) (list []Loadbalancer, err []error) {
	uri := "/v1/loadbalancers?"
	// set the datacenters if specified
	if isValidCommaDelimitedList(delimitedDcs) {
		uri = uri + "dc=" + delimitedDcs + "&"
	}
	if isValidCommaDelimitedList(delimitedNamespaces) {
		uri = uri + "ns=" + delimitedNamespaces
	} else {
		uri = uri + "ns=dev,qa,prod"
	}

	r, bytes, errs := AugmentRequest(
		http.Get(cfg.Endpoint+uri), cfg).EndBytes()

	if r != nil {
		if r.StatusCode/100 != 2 {
			errs = append(errs, errors.New("bad response from Nelson server"))
			return nil, errs
		} else {
			var list []Loadbalancer
			if err := json.Unmarshal(bytes, &list); err != nil {
				panic(err)
			}
			return list, errs
		}
	} else {
		errs = append(errs, errors.New("No response from the Nelson server, aborting."))
		return nil, errs
	}
}

func PrintListLoadbalancers(lb []Loadbalancer) {
	var tabulized = [][]string{}
	for _, l := range lb {
		routes := ""
		for i, r := range l.Routes {
			// 8443 ~> howdy-http->default
			routes = routes + strconv.Itoa(r.LBPort) + " ~> " + r.BackendName + "->" + r.BackendPortReference

			// if not the last element, lets bang on a comma
			if i == len(l.Routes) {
				routes = routes + ", "
			}
		}
		tabulized = append(tabulized, []string{l.Guid, l.Datacenter, l.NamespaceRef, l.Name, routes, l.Address})
	}

	RenderTableToStdout([]string{"GUID", "Datacenter", "Namespace", "Name", "Routes", "Address"}, tabulized)
}

//////////////////////// REMOVE ////////////////////////

func RemoveLoadBalancer(guid string, http *gorequest.SuperAgent, cfg *Config) (str string, err []error) {
{'fraction_non_alphanumeric': 0.17841240595731614, 'fraction_numerical': 0.01458621219100261, 'mean_word_length': 2.8430678466076698, 'pattern_counts': {'":': 28, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5209409', 'n_tokens_mistral': 2397, 'n_tokens_neox': 2247, 'n_words': 487}