Skip to content

Commit

Permalink
update azure sdk will solve genevieve#106
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonskie committed May 11, 2023
1 parent b09f7a8 commit 00a3e80
Show file tree
Hide file tree
Showing 3,269 changed files with 1,741,721 additions and 39 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
51 changes: 27 additions & 24 deletions azure/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@ import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-04-01/network"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
)

type client struct {
rgClient resources.GroupsClient
sgClient network.ApplicationSecurityGroupsClient
autorestClient autorest.Client
rgClient armresources.ResourceGroupsClient
sgClient armnetwork.ApplicationSecurityGroupsClient
}

func NewClient(rg resources.GroupsClient, sg network.ApplicationSecurityGroupsClient) client {
func NewClient(rg armresources.ResourceGroupsClient, sg armnetwork.ApplicationSecurityGroupsClient) client {
return client{
rgClient: rg,
sgClient: sg,
autorestClient: rg.Client,
rgClient: rg,
sgClient: sg,
}
}

func (c client) ListGroups() ([]string, error) {
ctx := context.Background()
groups := []string{}

for list, err := c.rgClient.ListComplete(ctx, "", nil); list.NotDone(); err = list.Next() {
pager := c.rgClient.NewListPager(nil)
for pager.More() {
nextResult, err := pager.NextPage(ctx)
if err != nil {
return []string{}, fmt.Errorf("List Complete Resource Groups: %s", err)
return groups, fmt.Errorf("failed to advance page: %s", err)
}
for _, group := range nextResult.Value {
groups = append(groups, *group.Name)
}

groups = append(groups, *list.Value().Name)
}

return groups, nil
Expand All @@ -41,14 +41,14 @@ func (c client) ListGroups() ([]string, error) {
func (c client) DeleteGroup(name string) error {
ctx := context.Background()

future, err := c.rgClient.Delete(ctx, name)
poller, err := c.rgClient.BeginDelete(ctx, name, nil)
if err != nil {
return err
}

err = future.WaitForCompletionRef(ctx, c.autorestClient)
_, err = poller.PollUntilDone(ctx, nil)
if err != nil {
return fmt.Errorf("Waiting for completion: %s", err)
return err
}

return nil
Expand All @@ -58,12 +58,15 @@ func (c client) ListAppSecurityGroups(rgName string) ([]string, error) {
ctx := context.Background()
groups := []string{}

for list, err := c.sgClient.ListComplete(ctx, rgName); list.NotDone(); err = list.Next() {
pager := c.sgClient.NewListAllPager(nil)
for pager.More() {
nextResult, err := pager.NextPage(ctx)
if err != nil {
return groups, fmt.Errorf("List Complete App Security Groups: %s", err)
return groups, fmt.Errorf("failed to advance page: %s", err)
}
for _, group := range nextResult.Value {
groups = append(groups, *group.Name)
}

groups = append(groups, *list.Value().Name)
}

return groups, nil
Expand All @@ -72,14 +75,14 @@ func (c client) ListAppSecurityGroups(rgName string) ([]string, error) {
func (c client) DeleteAppSecurityGroup(rgName, name string) error {
ctx := context.Background()

future, err := c.sgClient.Delete(ctx, rgName, name)
poller, err := c.sgClient.BeginDelete(ctx, rgName, name, nil)
if err != nil {
return err
}

err = future.WaitForCompletionRef(ctx, c.autorestClient)
_, err = poller.PollUntilDone(ctx, nil)
if err != nil {
return fmt.Errorf("Waiting for completion: %s", err)
return fmt.Errorf("failed to pull the result %s", err)
}

return nil
Expand Down
27 changes: 12 additions & 15 deletions azure/leftovers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"errors"
"fmt"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-04-01/network"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
azurelib "github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/fatih/color"
"github.com/genevieve/leftovers/common"
multierror "github.com/hashicorp/go-multierror"
Expand Down Expand Up @@ -110,23 +108,22 @@ func NewLeftovers(logger logger, clientId, clientSecret, subscriptionId, tenantI
return Leftovers{}, errors.New("Missing tenant id.")
}

oauthConfig, err := adal.NewOAuthConfig(azurelib.PublicCloud.ActiveDirectoryEndpoint, tenantId)
credential, err := azidentity.NewClientSecretCredential(tenantId, clientId, clientSecret, nil)
if err != nil {
return Leftovers{}, fmt.Errorf("Creating oauth config: %s\n", err)
return Leftovers{}, fmt.Errorf("Creating service principal token: %s\n", err)
}

token, err := adal.NewServicePrincipalToken(*oauthConfig, clientId, clientSecret, azurelib.PublicCloud.ResourceManagerEndpoint)
rg, err := armresources.NewResourceGroupsClient(subscriptionId, credential, nil)
if err != nil {
return Leftovers{}, fmt.Errorf("Creating service principal token: %s\n", err)
return Leftovers{}, fmt.Errorf("Creating resource groups client: %s\n", err)
}

rg := resources.NewGroupsClient(subscriptionId)
rg.Authorizer = autorest.NewBearerAuthorizer(token)

sg := network.NewApplicationSecurityGroupsClient(subscriptionId)
sg.Authorizer = autorest.NewBearerAuthorizer(token)
sg, err := armnetwork.NewApplicationSecurityGroupsClient(subscriptionId, credential, nil)
if err != nil {
return Leftovers{}, fmt.Errorf("Creating application security groups client: %s\n", err)
}

client := NewClient(rg, sg)
client := NewClient(*rg, *sg)

return Leftovers{
logger: logger,
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ go 1.13
require (
cloud.google.com/go/compute v1.19.2 // indirect
github.com/Azure/azure-sdk-for-go v33.2.0+incompatible
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork v1.1.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.1
github.com/Azure/go-autorest/autorest v0.9.1
github.com/Azure/go-autorest/autorest/adal v0.6.0
github.com/Azure/go-autorest/autorest/to v0.3.0 // indirect
Expand Down
Loading

0 comments on commit 00a3e80

Please sign in to comment.