forked from rexray/rexray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
54 lines (47 loc) · 1.27 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package utils
import (
"net"
"net/http"
"os"
"time"
"github.com/rexray/rexray/libstorage/api/types"
"github.com/rexray/rexray/libstorage/drivers/storage/azureud"
)
const (
raddr = "169.254.169.254"
maintURL = "http://" + raddr + "/metadata/v1/maintenance"
)
// IsAzureInstance returns a flag indicating whether the executing host
// is an Azure instance .
func IsAzureInstance(ctx types.Context) (bool, error) {
client := &http.Client{Timeout: time.Duration(1 * time.Second)}
req, err := http.NewRequest(http.MethodGet, maintURL, nil)
if err != nil {
return false, err
}
res, err := doRequestWithClient(ctx, client, req)
if err != nil {
if terr, ok := err.(net.Error); ok && terr.Timeout() {
return false, nil
}
return false, err
}
if res.StatusCode >= 200 && res.StatusCode <= 299 {
return true, nil
}
return false, nil
}
// InstanceID returns the instance ID for the local host.
func InstanceID(ctx types.Context) (*types.InstanceID, error) {
// UUID can be obtained as descried in
// https://azure.microsoft.com/en-us/blog/accessing-and-using-azure-vm-unique-id/
// but this code will use hostname as ID
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
return &types.InstanceID{
ID: hostname,
Driver: azureud.Name,
}, nil
}