sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/resourcehealth/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 resourcehealth 18 19 import ( 20 "context" 21 22 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcehealth/armresourcehealth" 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 wraps go-sdk. 29 type client interface { 30 GetByResource(context.Context, string) (armresourcehealth.AvailabilityStatus, error) 31 } 32 33 // azureClient contains the Azure go-sdk Client. 34 type azureClient struct { 35 availabilityStatuses *armresourcehealth.AvailabilityStatusesClient 36 } 37 38 // newClient creates a new resource health client from an authorizer. 39 func newClient(auth azure.Authorizer) (*azureClient, error) { 40 opts, err := azure.ARMClientOptions(auth.CloudEnvironment()) 41 if err != nil { 42 return nil, errors.Wrap(err, "failed to create resourcehealth client options") 43 } 44 factory, err := armresourcehealth.NewClientFactory(auth.SubscriptionID(), auth.Token(), opts) 45 if err != nil { 46 return nil, errors.Wrap(err, "failed to create armresourcehealth client factory") 47 } 48 return &azureClient{factory.NewAvailabilityStatusesClient()}, nil 49 } 50 51 // GetByResource gets the availability status for the specified resource. 52 func (ac *azureClient) GetByResource(ctx context.Context, resourceURI string) (armresourcehealth.AvailabilityStatus, error) { 53 ctx, _, done := tele.StartSpanWithLogger(ctx, "resourcehealth.AzureClient.GetByResource") 54 defer done() 55 56 opts := &armresourcehealth.AvailabilityStatusesClientGetByResourceOptions{} 57 resp, err := ac.availabilityStatuses.GetByResource(ctx, resourceURI, opts) 58 if err != nil { 59 return armresourcehealth.AvailabilityStatus{}, err 60 } 61 return resp.AvailabilityStatus, nil 62 }