forked from devops-rob/demo-app-vault-azure-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
58 lines (49 loc) · 1.26 KB
/
metadata.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
55
56
57
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
)
func metaData() (Metadata metadataJson){
// Create HTTP requests for a managed services for Azure resources token to access Azure Resource Manager
var metaEndpoint *url.URL
metaEndpoint, err := url.Parse("http://169.254.169.254/metadata/instance?api-version=2021-02-01")
if err != nil {
fmt.Println("Error creating URL: ", err)
return
}
req, err := http.NewRequest("GET", metaEndpoint.String(), nil)
if err != nil {
fmt.Println("Error creating HTTP request: ", err)
return
}
req.Header.Add("Metadata", "true")
// Call managed services for Azure resources token endpoint
resp, err := Client.Do(req)
if err != nil{
fmt.Println("Error calling Metadata endpoint: ", err)
return
}
// Pull out response body
responseBytes,err := ioutil.ReadAll(resp.Body)
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(resp.Body)
if err != nil {
fmt.Println("Error reading response body : ", err)
return
}
// Unmarshall response body into struct
err = json.Unmarshal(responseBytes, &Metadata)
if err != nil {
fmt.Println("Error unmarshalling the response:", err)
return
}
//fmt.Printf(Metadata.Compute.SubscriptionId)
return Metadata
}