sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/virtualmachineimages/client.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package virtualmachineimages 18 19 import ( 20 "context" 21 22 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5" 23 "github.com/pkg/errors" 24 "sigs.k8s.io/cluster-api-provider-azure/azure" 25 "sigs.k8s.io/cluster-api-provider-azure/util/tele" 26 ) 27 28 // Client is an interface for listing VM images. 29 type Client interface { 30 List(ctx context.Context, location, publisher, offer, sku string) (armcompute.VirtualMachineImagesClientListResponse, error) 31 } 32 33 // AzureClient contains the Azure go-sdk Client. 34 type AzureClient struct { 35 images *armcompute.VirtualMachineImagesClient 36 } 37 38 var _ Client = (*AzureClient)(nil) 39 40 // NewClient creates an AzureClient from an Authorizer. 41 func NewClient(auth azure.Authorizer) (*AzureClient, error) { 42 opts, err := azure.ARMClientOptions(auth.CloudEnvironment()) 43 if err != nil { 44 return nil, errors.Wrap(err, "failed to create virtualmachineimages client options") 45 } 46 computeClientFactory, err := armcompute.NewClientFactory(auth.SubscriptionID(), auth.Token(), opts) 47 if err != nil { 48 return nil, errors.Wrap(err, "failed to create armcompute client factory") 49 } 50 return &AzureClient{computeClientFactory.NewVirtualMachineImagesClient()}, nil 51 } 52 53 // List returns a VM image list response. 54 func (ac *AzureClient) List(ctx context.Context, location, publisher, offer, sku string) (armcompute.VirtualMachineImagesClientListResponse, error) { 55 ctx, _, done := tele.StartSpanWithLogger(ctx, "virtualmachineimages.AzureClient.List") 56 defer done() 57 58 opts := &armcompute.VirtualMachineImagesClientListOptions{} 59 return ac.images.List(ctx, location, publisher, offer, sku, opts) 60 }