123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package hds
- import (
- "encoding/json"
- "errors"
- "io/ioutil"
- "net"
- "net/http"
- "strconv"
- "sync"
- "time"
- )
- func isJSON(s string) bool {
- var js map[string]interface{}
- return json.Unmarshal([]byte(s), &js) == nil
- }
- func tryGet(url string) (string, error) {
- client := http.Client{
- Timeout: 5 * time.Second,
- }
- resp, err := client.Get(url)
- if err != nil {
- return "", err
- }
- if resp.StatusCode != 200 {
- return "", errors.New("Server side return status code " + strconv.Itoa(resp.StatusCode))
- }
- content, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return "", err
- }
- return string(content), nil
- }
- //Check if the given ip address is HDS device, return its UUID if true
- func tryGetHDSUUID(wg *sync.WaitGroup, ip string) (string, error) {
- defer wg.Done()
- uuid, err := tryGet("http://" + ip + "/uuid")
- if err != nil {
- return "", err
- }
- return uuid, nil
- }
- func getLocalIP() string {
- addrs, err := net.InterfaceAddrs()
- if err != nil {
- return ""
- }
- for _, address := range addrs {
- // check the address type and if it is not a loopback the display it
- if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
- if ipnet.IP.To4() != nil {
- return ipnet.IP.String()
- }
- }
- }
- return ""
- }
|