|
| 1 | +/* |
| 2 | +Copyright 2020 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 utils |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "log" |
| 22 | + |
| 23 | + "sigs.k8s.io/cluster-api-provider-azure/test/e2e/auth" |
| 24 | + |
| 25 | + "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-05-01/resources" |
| 26 | + "github.com/Azure/go-autorest/autorest" |
| 27 | + azureAuth "github.com/Azure/go-autorest/autorest/azure/auth" |
| 28 | +) |
| 29 | + |
| 30 | +// GetGroup gets the resource group if exist |
| 31 | +func GetGroup(ctx context.Context, creds auth.Creds, resourceName string) (resources.Group, error) { |
| 32 | + groupClient, err := getGroupsClient(creds) |
| 33 | + if err != nil { |
| 34 | + return resources.Group{}, err |
| 35 | + } |
| 36 | + return groupClient.Get(ctx, resourceName) |
| 37 | +} |
| 38 | + |
| 39 | +// CleanupE2EResources deletes the resource group created during the testing |
| 40 | +func CleanupE2EResources(ctx context.Context, creds auth.Creds, resourceName string) error { |
| 41 | + groupClient, err := getGroupsClient(creds) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + deleteGroupFuture, err := groupClient.Delete(ctx, resourceName) |
| 47 | + if err != nil { |
| 48 | + log.Printf("failed to delete group %s: %s\n", resourceName, err.Error()) |
| 49 | + } |
| 50 | + |
| 51 | + autorestClient := autorest.Client{ |
| 52 | + PollingDelay: autorest.DefaultPollingDelay, |
| 53 | + PollingDuration: autorest.DefaultPollingDuration, |
| 54 | + RetryAttempts: autorest.DefaultRetryAttempts, |
| 55 | + RetryDuration: autorest.DefaultRetryDuration, |
| 56 | + Authorizer: groupClient.Authorizer, |
| 57 | + } |
| 58 | + |
| 59 | + log.Println("waiting for the group deletion") |
| 60 | + |
| 61 | + err = deleteGroupFuture.WaitForCompletionRef(context.Background(), autorestClient) |
| 62 | + if err != nil { |
| 63 | + log.Printf("failed to wait for deletion %s\n", err.Error()) |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + log.Printf("group %s deleted\n", resourceName) |
| 68 | + return nil |
| 69 | +} |
| 70 | + |
| 71 | +func getGroupsClient(creds auth.Creds) (resources.GroupsClient, error) { |
| 72 | + groupsClient := resources.NewGroupsClient(creds.SubscriptionID) |
| 73 | + a, err := getAuthorizer(creds) |
| 74 | + if err != nil { |
| 75 | + return resources.GroupsClient{}, err |
| 76 | + } |
| 77 | + groupsClient.Authorizer = a |
| 78 | + return groupsClient, nil |
| 79 | +} |
| 80 | + |
| 81 | +func getAuthorizer(creds auth.Creds) (autorest.Authorizer, error) { |
| 82 | + credsConfig := azureAuth.NewClientCredentialsConfig(creds.ClientID, creds.ClientSecret, creds.TenantID) |
| 83 | + return credsConfig.Authorizer() |
| 84 | +} |
0 commit comments