Skip to content

Commit

Permalink
Banish autorest, once and for all
Browse files Browse the repository at this point in the history
  • Loading branch information
JenGoldstrich committed Mar 27, 2024
1 parent 9b56a09 commit 5e0089e
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 124 deletions.
74 changes: 74 additions & 0 deletions builder/azure/common/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) HashiCorp, Inc.
// spdx-license-identifier: mpl-2.0

package cli

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/dimchansky/utfbom"
"github.com/mitchellh/go-homedir"
)

// Code copy pasted from https://github.com/Azure/go-autorest/blob/main/autorest/azure/cli/profile.go

// Profile represents a Profile from the Azure CLI
type Profile struct {
InstallationID string `json:"installationId"`
Subscriptions []Subscription `json:"subscriptions"`
}

// Subscription represents a Subscription from the Azure CLI
type Subscription struct {
EnvironmentName string `json:"environmentName"`
ID string `json:"id"`
IsDefault bool `json:"isDefault"`
Name string `json:"name"`
State string `json:"state"`
TenantID string `json:"tenantId"`
User *User `json:"user"`
}

// User represents a User from the Azure CLI
type User struct {
Name string `json:"name"`
Type string `json:"type"`
}

const azureProfileJSON = "azureProfile.json"

func configDir() string {
return os.Getenv("AZURE_CONFIG_DIR")
}

// ProfilePath returns the path where the Azure Profile is stored from the Azure CLI
func ProfilePath() (string, error) {
if cfgDir := configDir(); cfgDir != "" {
return filepath.Join(cfgDir, azureProfileJSON), nil
}
return homedir.Expand("~/.azure/" + azureProfileJSON)
}

// LoadProfile restores a Profile object from a file located at 'path'.
func LoadProfile(path string) (result Profile, err error) {
var contents []byte
contents, err = ioutil.ReadFile(path)
if err != nil {
err = fmt.Errorf("failed to open file (%s) while loading token: %v", path, err)
return
}
reader := utfbom.SkipOnly(bytes.NewReader(contents))

dec := json.NewDecoder(reader)
if err = dec.Decode(&result); err != nil {
err = fmt.Errorf("failed to decode contents of file (%s) into a Profile representation: %v", path, err)
return
}

return
}
9 changes: 1 addition & 8 deletions builder/azure/common/client/azure_client_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ package client
import (
"context"
"fmt"
"net/http"
"regexp"
"strings"
"time"

"github.com/hashicorp/packer-plugin-sdk/useragent"

"github.com/Azure/go-autorest/autorest"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-01/images"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-01/virtualmachineimages"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-01/virtualmachines"
Expand Down Expand Up @@ -47,7 +45,6 @@ type AzureClientSet interface {
var _ AzureClientSet = &azureClientSet{}

type azureClientSet struct {
sender autorest.Sender
authorizer auth.Authorizer
subscriptionID string
PollingDelay time.Duration
Expand Down Expand Up @@ -136,7 +133,6 @@ func new(c Config, say func(string)) (*azureClientSet, error) {
return &azureClientSet{
authorizer: authorizer,
subscriptionID: c.SubscriptionID,
sender: http.DefaultClient,
PollingDelay: time.Second,
imagesClient: *imagesClient,
galleryImagesClient: *galleryImagesClient,
Expand All @@ -159,10 +155,7 @@ func (s azureClientSet) PollingDuration() time.Duration {
}

func (s azureClientSet) MetadataClient() MetadataClientAPI {
return metadataClient{
s.sender,
useragent.String(version.AzurePluginVersion.FormattedVersion()),
}
return metadataClient{}
}

func (s azureClientSet) DisksClient() disks.DisksClient {
Expand Down
10 changes: 8 additions & 2 deletions builder/azure/common/client/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// spdx-license-identifier: mpl-2.0

//go:generate packer-sdc struct-markdown

Expand All @@ -14,9 +14,9 @@ import (
"regexp"
"strings"

"github.com/hashicorp/packer-plugin-azure/builder/azure/common/cli"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common/log"

"github.com/Azure/go-autorest/autorest/azure/cli"
jwt "github.com/golang-jwt/jwt"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2022-12-01/subscriptions"
Expand Down Expand Up @@ -287,6 +287,12 @@ func (c *Config) FillParameters() error {
return nil
}

const azureProfileJSON = "azureProfile.json"

func configDir() string {
return os.Getenv("AZURE_CONFIG_DIR")
}

// getIDsFromAzureCLI returns the TenantID and SubscriptionID from an active Azure CLI login session
func getIDsFromAzureCLI() (string, string, error) {
profilePath, err := cli.ProfilePath()
Expand Down
36 changes: 11 additions & 25 deletions builder/azure/common/client/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
package client

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
)

// DefaultMetadataClient is the default instance metadata client for Azure. Replace this variable for testing purposes only
Expand Down Expand Up @@ -42,8 +40,6 @@ type ComputeInfo struct {

// metadataClient implements MetadataClient
type metadataClient struct {
autorest.Sender
UserAgent string
}

var _ MetadataClientAPI = metadataClient{}
Expand All @@ -52,31 +48,23 @@ const imdsURL = "http://169.254.169.254/metadata/instance?api-version=2021-02-01

// VMResourceID returns the resource ID of the current VM
func (client metadataClient) GetComputeInfo() (*ComputeInfo, error) {
req, err := autorest.CreatePreparer(
autorest.AsGet(),
autorest.WithHeader("Metadata", "true"),
autorest.WithUserAgent(client.UserAgent),
autorest.WithBaseURL(imdsURL),
).Prepare((&http.Request{}))
httpClient := &http.Client{}
req, err := http.NewRequest("GET", imdsURL, nil)
req.Header.Add("Metadata", "true")
httpClient.Get(imdsURL)
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}

res, err := autorest.SendWithSender(client, req,
autorest.DoRetryForDuration(1*time.Minute, 5*time.Second))
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var vminfo struct {
ComputeInfo `json:"compute"`
}

err = autorest.Respond(
res,
azure.WithErrorUnlessStatusCode(http.StatusOK),
autorest.ByUnmarshallingJSON(&vminfo),
autorest.ByClosing())
err = json.Unmarshal(body, vminfo)
if err != nil {
return nil, err
}
Expand All @@ -89,7 +77,5 @@ func (ci ComputeInfo) GetResourceID() string {

// NewMetadataClient creates a new instance metadata client
func NewMetadataClient() MetadataClientAPI {
return metadataClient{
Sender: autorest.CreateSender(),
}
return metadataClient{}
}
35 changes: 0 additions & 35 deletions builder/azure/common/client/metadata_test.go

This file was deleted.

16 changes: 3 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
module github.com/hashicorp/packer-plugin-azure

go 1.21

toolchain go1.22.1
go 1.19

require (
github.com/Azure/go-autorest/autorest v0.11.29
github.com/Azure/go-autorest/autorest/adal v0.9.23 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/approvals/go-approval-tests v0.0.0-20210131072903-38d0b0ec12b1
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/google/go-cmp v0.5.9
Expand All @@ -23,9 +18,10 @@ require (
)

require (
github.com/Azure/go-autorest/autorest/azure/cli v0.4.6
github.com/dimchansky/utfbom v1.1.1
github.com/hashicorp/go-azure-sdk/resource-manager v0.20240327.1161949
github.com/hashicorp/go-azure-sdk/sdk v0.20240327.1161949
github.com/mitchellh/go-homedir v1.1.0
github.com/tombuildsstuff/giovanni v0.25.3
)

Expand All @@ -35,9 +31,6 @@ require (
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v0.13.0 // indirect
cloud.google.com/go/storage v1.29.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c // indirect
github.com/ChrisTrenkamp/goxpath v0.0.0-20210404020558-97928f7e12b6 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
Expand All @@ -49,13 +42,11 @@ require (
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/dylanmei/iso8601 v0.1.0 // indirect
github.com/fatih/color v1.14.1 // indirect
github.com/go-jose/go-jose/v3 v3.0.0 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gofrs/uuid v4.0.0+incompatible // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/s2a-go v0.1.4 // indirect
Expand Down Expand Up @@ -99,7 +90,6 @@ require (
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-fs v0.0.0-20180402235330-b7b9ca407fff // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/iochan v1.0.0 // indirect
Expand Down
Loading

0 comments on commit 5e0089e

Please sign in to comment.