mirror of https://github.com/hashicorp/packer
parent
113dc12346
commit
3c33aa4fc5
@ -0,0 +1,71 @@
|
||||
package chroot
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/hashicorp/go-retryablehttp"
|
||||
)
|
||||
|
||||
// DefaultMetadataClient is the default instance metadata client for Azure. Replace this variable for testing purposes only
|
||||
var DefaultMetadataClient = NewMetadataClient()
|
||||
|
||||
// MetadataClient holds methods that Packer uses to get information about the current VM
|
||||
type MetadataClient interface {
|
||||
VMResourceID() (string, error)
|
||||
}
|
||||
|
||||
// metadataClient implements MetadataClient
|
||||
type metadataClient struct{}
|
||||
|
||||
const imdsURL = "http://169.254.169.254/metadata/instance?api-version=2017-08-01"
|
||||
|
||||
// VMResourceID returns the resource ID of the current VM
|
||||
func (metadataClient) VMResourceID() (string, error) {
|
||||
wc := retryablehttp.NewClient()
|
||||
wc.RetryMax = 5
|
||||
|
||||
req, err := retryablehttp.NewRequest(http.MethodGet, imdsURL, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Add("Metadata", "true")
|
||||
|
||||
res, err := wc.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
d, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var vminfo struct {
|
||||
Compute struct {
|
||||
Name string
|
||||
ResourceGroupName string
|
||||
SubscriptionID string
|
||||
}
|
||||
}
|
||||
|
||||
err = json.Unmarshal(d, &vminfo)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachines/%s",
|
||||
vminfo.Compute.Name,
|
||||
vminfo.Compute.ResourceGroupName,
|
||||
vminfo.Compute.SubscriptionID,
|
||||
), nil
|
||||
|
||||
}
|
||||
|
||||
// NewMetadataClient creates a new instance metadata client
|
||||
func NewMetadataClient() MetadataClient {
|
||||
return metadataClient{}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package chroot
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Azure/go-autorest/autorest/azure"
|
||||
|
||||
"github.com/hashicorp/packer/builder/azure/common"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_MetadataReturnsVMResourceID(t *testing.T) {
|
||||
if !common.IsAzure() {
|
||||
t.Skipf("Not running on Azure, skipping live IMDS test")
|
||||
}
|
||||
mdc := NewMetadataClient()
|
||||
id, err := mdc.VMResourceID()
|
||||
assert.Nil(t, err)
|
||||
assert.NotEqual(t, id, "", "Expected VMResourceID to return non-empty string because we are running on Azure")
|
||||
|
||||
vm, err := azure.ParseResourceID(id)
|
||||
assert.Nil(t, err, "%q is not parsable as an Azure resource id", id)
|
||||
t.Logf("VM: %+v", vm)
|
||||
}
|
||||
Loading…
Reference in new issue