github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/common/get_instance_ip.go (about)

     1  package common
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/emc-advanced-dev/pkg/errors"
     7  	"github.com/layer-x/layerx-commons/lxhttpclient"
     8  )
     9  
    10  func GetInstanceIp(listenerIp string, listenerPort int, instanceId string) (string, error) {
    11  	_, body, err := lxhttpclient.Get(fmt.Sprintf("%s:%v", listenerIp, listenerPort), "/instances", nil)
    12  	if err != nil {
    13  		return "", errors.New("http GET on instance listener", err)
    14  	}
    15  	var instanceIpMap map[string]string
    16  	if err := json.Unmarshal(body, &instanceIpMap); err != nil {
    17  		return "", errors.New("unmarshalling response ("+string(body)+") to map", err)
    18  	}
    19  	ip, ok := instanceIpMap[instanceId]
    20  	if !ok {
    21  		return "", errors.New("instance "+instanceId+" not found in map: "+fmt.Sprintf("%v", instanceIpMap), err)
    22  	}
    23  	return ip, nil
    24  }