Skip to content

Commit

Permalink
feat: adding key-vault resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Apr 12, 2022
1 parent b5b11af commit 9143367
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions resources/key-vault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package resources

import (
"context"
"time"

"github.com/sirupsen/logrus"

"github.com/Azure/azure-sdk-for-go/services/keyvault/mgmt/2019-09-01/keyvault"

"github.com/ekristen/azure-nuke/pkg/resource"
"github.com/ekristen/azure-nuke/pkg/types"
)

type KeyVault struct {
client keyvault.VaultsClient
name string
rg string
}

func init() {
resource.RegisterV2(resource.Registration{
Name: "KeyVault",
Scope: resource.ResourceGroup,
Lister: ListKeyVault,
})
}

func ListKeyVault(opts resource.ListerOpts) ([]resource.Resource, error) {
logrus.Tracef("subscription id: %s", opts.SubscriptionId)

client := keyvault.NewVaultsClient(opts.SubscriptionId)
client.Authorizer = opts.Authorizers.Management
client.RetryAttempts = 1
client.RetryDuration = time.Second * 2

resources := make([]resource.Resource, 0)

logrus.Trace("attempting to list ssh key")

ctx := context.Background()
list, err := client.ListByResourceGroup(ctx, opts.ResourceGroup, nil)
if err != nil {
return nil, err
}

logrus.Trace("listing ....")

for list.NotDone() {
logrus.Trace("list not done")
for _, g := range list.Values() {
resources = append(resources, &KeyVault{
client: client,
name: *g.Name,
rg: opts.ResourceGroup,
})
}

if err := list.NextWithContext(ctx); err != nil {
return nil, err
}
}

return resources, nil
}

func (r *KeyVault) Remove() error {
_, err := r.client.Delete(context.TODO(), r.rg, r.name)
return err
}

func (r *KeyVault) Properties() types.Properties {
properties := types.NewProperties()

properties.Set("Name", r.name)

return properties
}

func (r *KeyVault) String() string {
return r.name
}

0 comments on commit 9143367

Please sign in to comment.