sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/publicips/client.go (about) 1 /* 2 Copyright 2019 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 publicips 18 19 import ( 20 "context" 21 "time" 22 23 "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" 24 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4" 25 "github.com/pkg/errors" 26 "sigs.k8s.io/cluster-api-provider-azure/azure" 27 "sigs.k8s.io/cluster-api-provider-azure/azure/services/async" 28 "sigs.k8s.io/cluster-api-provider-azure/util/tele" 29 ) 30 31 // AzureClient contains the Azure go-sdk Client. 32 type AzureClient struct { 33 publicips *armnetwork.PublicIPAddressesClient 34 apiCallTimeout time.Duration 35 } 36 37 // NewClient creates a new public IP client from an authorizer. 38 func NewClient(auth azure.Authorizer, apiCallTimeout time.Duration) (*AzureClient, error) { 39 opts, err := azure.ARMClientOptions(auth.CloudEnvironment()) 40 if err != nil { 41 return nil, errors.Wrap(err, "failed to create publicips client options") 42 } 43 44 factory, err := armnetwork.NewClientFactory(auth.SubscriptionID(), auth.Token(), opts) 45 if err != nil { 46 return nil, errors.Wrap(err, "failed to create publicips client factory") 47 } 48 return &AzureClient{factory.NewPublicIPAddressesClient(), apiCallTimeout}, nil 49 } 50 51 // Get gets the specified public IP address in a specified resource group. 52 func (ac *AzureClient) Get(ctx context.Context, spec azure.ResourceSpecGetter) (result interface{}, err error) { 53 ctx, _, done := tele.StartSpanWithLogger(ctx, "publicips.AzureClient.Get") 54 defer done() 55 56 resp, err := ac.publicips.Get(ctx, spec.ResourceGroupName(), spec.ResourceName(), nil) 57 if err != nil { 58 return nil, err 59 } 60 return resp.PublicIPAddress, nil 61 } 62 63 // CreateOrUpdateAsync creates or updates a static or dynamic public IP address. 64 // It sends a PUT request to Azure and if accepted without error, the func will return a Poller which can be used to track the ongoing 65 // progress of the operation. 66 func (ac *AzureClient) CreateOrUpdateAsync(ctx context.Context, spec azure.ResourceSpecGetter, resumeToken string, parameters interface{}) (result interface{}, poller *runtime.Poller[armnetwork.PublicIPAddressesClientCreateOrUpdateResponse], err error) { 67 ctx, _, done := tele.StartSpanWithLogger(ctx, "publicips.AzureClient.CreateOrUpdate") 68 defer done() 69 70 publicip, ok := parameters.(armnetwork.PublicIPAddress) 71 if !ok && parameters != nil { 72 return nil, nil, errors.Errorf("%T is not an armnetwork.PublicIPAddress", parameters) 73 } 74 75 opts := &armnetwork.PublicIPAddressesClientBeginCreateOrUpdateOptions{ResumeToken: resumeToken} 76 poller, err = ac.publicips.BeginCreateOrUpdate(ctx, spec.ResourceGroupName(), spec.ResourceName(), publicip, opts) 77 if err != nil { 78 return nil, nil, err 79 } 80 81 ctx, cancel := context.WithTimeout(ctx, ac.apiCallTimeout) 82 defer cancel() 83 84 pollOpts := &runtime.PollUntilDoneOptions{Frequency: async.DefaultPollerFrequency} 85 resp, err := poller.PollUntilDone(ctx, pollOpts) 86 if err != nil { 87 // if an error occurs, return the poller. 88 // this means the long-running operation didn't finish in the specified timeout. 89 return nil, poller, err 90 } 91 92 // if the operation completed, return a nil poller. 93 return resp.PublicIPAddress, nil, err 94 } 95 96 // DeleteAsync deletes the specified public IP address asynchronously. DeleteAsync sends a DELETE 97 // request to Azure and if accepted without error, the func will return a Poller which can be used to track the ongoing 98 // progress of the operation. 99 func (ac *AzureClient) DeleteAsync(ctx context.Context, spec azure.ResourceSpecGetter, resumeToken string) (poller *runtime.Poller[armnetwork.PublicIPAddressesClientDeleteResponse], err error) { 100 ctx, _, done := tele.StartSpanWithLogger(ctx, "publicips.AzureClient.DeleteAsync") 101 defer done() 102 103 opts := &armnetwork.PublicIPAddressesClientBeginDeleteOptions{ResumeToken: resumeToken} 104 poller, err = ac.publicips.BeginDelete(ctx, spec.ResourceGroupName(), spec.ResourceName(), opts) 105 if err != nil { 106 return nil, err 107 } 108 109 ctx, cancel := context.WithTimeout(ctx, ac.apiCallTimeout) 110 defer cancel() 111 112 pollOpts := &runtime.PollUntilDoneOptions{Frequency: async.DefaultPollerFrequency} 113 _, err = poller.PollUntilDone(ctx, pollOpts) 114 if err != nil { 115 // if an error occurs, return the poller. 116 // this means the long-running operation didn't finish in the specified timeout. 117 return poller, err 118 } 119 // if the operation completed, return a nil poller. 120 return nil, err 121 }