예제 #1
0
func (n NitroApi) executeRequest(method string, url string, contentType string, jsonContent string) (interface{}, error) {
	log.Debugf("HTTP %s to %s, contents=%s", method, url, jsonContent)
	client := &http.Client{}
	req, err := http.NewRequest(method, "http://"+n.nitroBaseUrl+url, strings.NewReader(jsonContent))
	if err != nil {
		return nil, err
	}
	req.Header.Add("X-NITRO-USER", "root")
	req.Header.Add("X-NITRO-PASS", "linux")
	req.Header.Add("Content-Type", contentType)
	var attempts float64 = 1
	for {
		resp, err := client.Do(req)
		if err != nil {
			log.Errorf("%v", err)
		} else {
			defer resp.Body.Close()
			log.Debugf("response StatusCode: %v", resp.StatusCode)
			body, _ := ioutil.ReadAll(resp.Body)
			log.Debugf("response Body: %v", string(body))
			if resp.StatusCode == 200 || resp.StatusCode == 201 || resp.StatusCode == 409 {
				return body, nil
			}
		}
		millis := math.Min(60000, math.Pow(2, attempts)*100)
		log.Debugf("attempt %v, waiting %v millis", attempts, millis)
		time.Sleep(time.Duration(millis) * time.Millisecond)
		attempts++
	}

	return nil, err
}
예제 #2
0
func (n NmeHandler) LoadConfigs() (map[string]Lbvserver, error) {
	lbvservers, err := n.apiHandler.GetLbvservers()
	if err != nil {
		return nil, err
	}
	lbConfigs := make(map[string]Lbvserver)
	for lbvserverName, lbvserverMap := range lbvservers {
		vip := lbvserverMap["ipaddress"]
		port := lbvserverMap["port"]

		lbvServerBindings := make(map[string]Service)
		bindings, err := n.apiHandler.GetLbvserverBindings(lbvserverName)
		if err != nil {
			log.Errorf("Error getting service bindings for %s: %v", lbvserverName, err)
			continue
		}
		for serviceName, serviceDetailsMap := range bindings {
			serviceBinding := Service{
				Name:      serviceName,
				IpAddress: serviceDetailsMap["ipaddress"],
			}
			lbvServerBindings[serviceName] = serviceBinding
		}

		lbConfig := Lbvserver{
			Name:      lbvserverName,
			IpAddress: vip,
			Port:      port,
			Bindings:  lbvServerBindings,
		}
		lbConfigs[lbvserverName] = lbConfig
	}
	return lbConfigs, nil
}