func (m *MarathonService) CreateGroup(payload []byte, marathonEndpoint string) (deploymentId string, err error) {
	url := strings.Join([]string{"http://", marathonEndpoint, "/v2/groups"}, "")
	logrus.Debugf("start to post group json %b to marathon %v", string(payload), marathonEndpoint)
	resp, err := httpclient.Http_post(url, string(payload),
		httpclient.Header{"Content-Type", "application/json"})
	if err != nil {
		logrus.Errorf("post group to marathon failed, error is %v", err)
		return
	}
	defer resp.Body.Close()

	// if response status is greater than 400, means marathon returns error
	// else parse body, findout deploymentId, and return
	data, _ := ioutil.ReadAll(resp.Body)
	if resp.StatusCode >= 400 {
		logrus.Errorf("marathon returned error code is %v", resp.StatusCode)
		logrus.Errorf("detail is %v", string(data))
		err = errors.New(string(data))
		return
	}

	// Parse data: marathon json data
	jsondata := map[string]interface{}{}
	result := json.NewDecoder(strings.NewReader(string(data)))
	result.Decode(&jsondata)
	jq := jsonq.NewQuery(jsondata)
	deploymentId, err = jq.String("deploymentId")
	return
}
Esempio n. 2
0
//send request to dcos deployment module to add nodes
func SendAddNodesRequest(request *dcosentity.AddNodeRequest) (servers *[]dcosentity.Server, err error) {
	logrus.Infoln("Call deployment to add nodes")
	body, err := json.Marshal(request)
	deployUrl, err := common.UTIL.LbClient.GetDeployEndpoint()
	if err != nil {
		logrus.Errorf("get deploy endpoint err is %v", err)
		return nil, err
	}
	url := strings.Join([]string{"http://", deployUrl, "/v1/deploy/nodes"}, "")
	logrus.Debugln("get deploy url=" + url)
	resp, err := httpclient.Http_post(url, string(body),
		httpclient.Header{"Content-Type", "application/json"})
	if err != nil {
		logrus.Errorf("send http post to dcos deployment error %v", err)
		return nil, err
	}
	defer resp.Body.Close()

	data, _ := ioutil.ReadAll(resp.Body)
	if resp.StatusCode >= 400 {
		logrus.Errorf("http status code from dcos deployment failed %v", string(data))
		return nil, errors.New("http status code from dcos deployment failed")
	}

	success := isResponseSuccess(data)
	if !success {
		return nil, errors.New("add node deployment module not success")
	}

	servers = new([]dcosentity.Server)
	err = getRetFromResponse(data, servers)
	return servers, err
}