Skip to content

Commit

Permalink
fix: interface break for 2021-12-01 api (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeldeib committed Aug 3, 2022
1 parent 7b622b6 commit f2a784d
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 38 deletions.
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,18 @@ linters:
- gocyclo
- gofmt
- goimports
- golint
- gomnd
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- lll
- misspell
- nakedret
- noctx
- nolintlint
- rowserrcheck
- scopelint
- staticcheck
- structcheck
- stylecheck
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"fmt"

"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute" //nolint:staticcheck

"github.com/Azure/skewer"
)
Expand Down
17 changes: 13 additions & 4 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (

// Config contains configuration options for a cache.
type Config struct {
location string
filter string
client client
location string
includeExtendedLocations string
filter string
client client
}

// Cache stores a list of known skus, possibly fetched with a provided client
Expand All @@ -31,6 +32,14 @@ func WithLocation(location string) Option {
}
}

// WithExtendedLocations is a functional option to include extended locations
func WithExtendedLocations() Option {
return func(c *Config) (*Config, error) {
c.includeExtendedLocations = "true"
return c, nil
}
}

// ErrClientNil will be returned when a user attempts to create a cache
// without a client and use it.
type ErrClientNil struct {
Expand Down Expand Up @@ -139,7 +148,7 @@ func NewStaticCache(data []SKU, opts ...Option) (*Cache, error) {
}

func (c *Cache) refresh(ctx context.Context) error {
data, err := c.config.client.List(ctx, c.config.filter)
data, err := c.config.client.List(ctx, c.config.filter, c.config.includeExtendedLocations)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"testing"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute" //nolint:staticcheck
"github.com/Azure/go-autorest/autorest/to"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand Down
17 changes: 9 additions & 8 deletions clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package skewer
import (
"context"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute" //nolint:staticcheck
"github.com/pkg/errors"
)

Expand All @@ -18,8 +18,8 @@ func newWrappedResourceClient(client ResourceClient) *wrappedResourceClient {
}

// List greedily traverses all returned sku pages
func (w *wrappedResourceClient) List(ctx context.Context, filter string) ([]compute.ResourceSku, error) {
return iterate(ctx, filter, w.client.ListComplete)
func (w *wrappedResourceClient) List(ctx context.Context, filter, includeExtendedLocations string) ([]compute.ResourceSku, error) {
return iterate(ctx, filter, includeExtendedLocations, w.client.ListComplete)
}

// wrappedResourceProviderClient defines a wrapper for the typical Azure client
Expand All @@ -33,19 +33,20 @@ func newWrappedResourceProviderClient(client ResourceProviderClient) *wrappedRes
return &wrappedResourceProviderClient{client}
}

func (w *wrappedResourceProviderClient) ListComplete(ctx context.Context, filter string) (compute.ResourceSkusResultIterator, error) {
page, err := w.client.List(ctx, filter)
//nolint:lll
func (w *wrappedResourceProviderClient) ListComplete(ctx context.Context, filter, includeExtendedLocations string) (compute.ResourceSkusResultIterator, error) {
page, err := w.client.List(ctx, filter, includeExtendedLocations)
if err != nil {
return compute.ResourceSkusResultIterator{}, nil
}
return compute.NewResourceSkusResultIterator(page), nil
}

type iterFunc func(context.Context, string) (compute.ResourceSkusResultIterator, error)
type iterFunc func(context.Context, string, string) (compute.ResourceSkusResultIterator, error)

// iterate invokes fn to get an iterator, then drains it into an array.
func iterate(ctx context.Context, filter string, fn iterFunc) ([]compute.ResourceSku, error) {
iter, err := fn(ctx, filter)
func iterate(ctx context.Context, filter, includeExtendedLocations string, fn iterFunc) ([]compute.ResourceSku, error) {
iter, err := fn(ctx, filter, includeExtendedLocations)
if err != nil {
return nil, errors.Wrap(err, "could not list resource skus")
}
Expand Down
5 changes: 5 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ const (
// Generation 2.
HyperVGeneration2 = "V2"
)

const (
ten = 10
sixtyFour = 64
)
2 changes: 1 addition & 1 deletion data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"testing"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute" //nolint:staticcheck
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
Expand Down
14 changes: 8 additions & 6 deletions fakes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package skewer
import (
"context"
"encoding/json"
"io/ioutil"
"os"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute" //nolint:staticcheck
)

// dataWrapper is a convenience wrapper for deserializing json testdata
Expand All @@ -16,7 +16,7 @@ type dataWrapper struct {
// newDataWrapper takes a path to a list of compute skus and parses them
// to a dataWrapper for use in fake clients
func newDataWrapper(path string) (*dataWrapper, error) {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand All @@ -36,7 +36,7 @@ type fakeClient struct {
err error
}

func (f *fakeClient) List(ctx context.Context, filter string) ([]compute.ResourceSku, error) {
func (f *fakeClient) List(ctx context.Context, filter, includeExtendedLocations string) ([]compute.ResourceSku, error) {
if f.err != nil {
return nil, f.err
}
Expand All @@ -51,7 +51,8 @@ type fakeResourceClient struct {
err error
}

func (f *fakeResourceClient) ListComplete(ctx context.Context, filter string) (compute.ResourceSkusResultIterator, error) {
//nolint:lll
func (f *fakeResourceClient) ListComplete(ctx context.Context, filter, includeExtendedLocations string) (compute.ResourceSkusResultIterator, error) {
if f.err != nil {
return compute.ResourceSkusResultIterator{}, f.err
}
Expand Down Expand Up @@ -90,7 +91,8 @@ type fakeResourceProviderClient struct {
err error
}

func (f *fakeResourceProviderClient) List(ctx context.Context, filter string) (compute.ResourceSkusResultPage, error) {
//nolint:lll
func (f *fakeResourceProviderClient) List(ctx context.Context, filter, includeExtendedLocations string) (compute.ResourceSkusResultPage, error) {
if f.err != nil {
return compute.ResourceSkusResultPage{}, f.err
}
Expand Down
14 changes: 12 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/Azure/skewer

go 1.13
go 1.17

require (
github.com/Azure/azure-sdk-for-go v66.0.0+incompatible
Expand All @@ -10,4 +10,14 @@ require (
github.com/pkg/errors v0.9.1
)

require github.com/Azure/go-autorest/autorest/validation v0.3.0 // indirect
require (
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.18 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/autorest/validation v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
)
8 changes: 4 additions & 4 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ package skewer
import (
"context"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute" //nolint:staticcheck
)

// ResourceClient is the required Azure client interface used to populate skewer's data.
type ResourceClient interface {
ListComplete(ctx context.Context, filter string) (compute.ResourceSkusResultIterator, error)
ListComplete(ctx context.Context, filter, includeExtendedLocations string) (compute.ResourceSkusResultIterator, error)
}

// ResourceProviderClient is a convenience interface for uses cases
// specific to Azure resource providers.
type ResourceProviderClient interface {
List(ctx context.Context, filter string) (compute.ResourceSkusResultPage, error)
List(ctx context.Context, filter, includeExtendedLocations string) (compute.ResourceSkusResultPage, error)
}

// client defines the internal interface required by the skewer Cache.
// TODO(ace): implement a lazy iterator with caching (and a cursor?)
type client interface {
List(ctx context.Context, filter string) ([]compute.ResourceSku, error)
List(ctx context.Context, filter, includeExtendedLocations string) ([]compute.ResourceSku, error)
}
12 changes: 6 additions & 6 deletions sku.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strconv"
"strings"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute" //nolint:staticcheck
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -94,7 +94,7 @@ func (s *SKU) IsUltraSSDAvailableInAvailabilityZone(zone string) bool {
// IsUltraSSDAvailable returns true when a VM size has ultra SSD enabled
// in at least 1 unrestricted zone.
//
// Deprecated. Use either IsUltraSSDAvailableWithoutAvailabilityZone or IsUltraSSDAvailableInAvailabilityZone
// Deprecated: use either IsUltraSSDAvailableWithoutAvailabilityZone or IsUltraSSDAvailableInAvailabilityZone
func (s *SKU) IsUltraSSDAvailable() bool {
return s.HasZonalCapability(UltraSSDAvailable)
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func (s *SKU) GetCapabilityIntegerQuantity(name string) (int64, error) {
for _, capability := range *s.Capabilities {
if capability.Name != nil && *capability.Name == name {
if capability.Value != nil {
intVal, err := strconv.ParseInt(*capability.Value, 10, 64)
intVal, err := strconv.ParseInt(*capability.Value, ten, sixtyFour)
if err != nil {
return -1, &ErrCapabilityValueParse{name, *capability.Value, err}
}
Expand All @@ -151,7 +151,7 @@ func (s *SKU) GetCapabilityFloatQuantity(name string) (float64, error) {
for _, capability := range *s.Capabilities {
if capability.Name != nil && *capability.Name == name {
if capability.Value != nil {
intVal, err := strconv.ParseFloat(*capability.Value, 64)
intVal, err := strconv.ParseFloat(*capability.Value, sixtyFour)
if err != nil {
return -1, &ErrCapabilityValueParse{name, *capability.Value, err}
}
Expand Down Expand Up @@ -213,7 +213,7 @@ func (s *SKU) HasZonalCapability(name string) bool {

// HasCapabilityInZone return true if the specified capability name is supported in the
// specified zone.
func (s *SKU) HasCapabilityInZone(name string, zone string) bool {
func (s *SKU) HasCapabilityInZone(name, zone string) bool {
if s.LocationInfo == nil {
return false
}
Expand Down Expand Up @@ -281,7 +281,7 @@ func (s *SKU) HasCapabilityWithMinCapacity(name string, value int64) (bool, erro
for _, capability := range *s.Capabilities {
if capability.Name != nil && strings.EqualFold(*capability.Name, name) {
if capability.Value != nil {
intVal, err := strconv.ParseInt(*capability.Value, 10, 64)
intVal, err := strconv.ParseInt(*capability.Value, ten, sixtyFour)
if err != nil {
return false, errors.Wrapf(err, "failed to parse string '%s' as int64", *capability.Value)
}
Expand Down
3 changes: 2 additions & 1 deletion sku_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute" //nolint:staticcheck
"github.com/Azure/go-autorest/autorest/to"
"github.com/google/go-cmp/cmp"
)
Expand Down Expand Up @@ -396,6 +396,7 @@ func Test_SKU_GetLocation(t *testing.T) {

func Test_SKU_AvailabilityZones(t *testing.T) {}

//nolint:funlen
func Test_SKU_HasCapabilityInZone(t *testing.T) {
cases := map[string]struct {
sku compute.ResourceSku
Expand Down
2 changes: 1 addition & 1 deletion wrap.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package skewer

import "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute"
import "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-12-01/compute" //nolint:staticcheck

// Wrap takes an array of compute resource skus and wraps them into an
// array of our richer type.
Expand Down

0 comments on commit f2a784d

Please sign in to comment.