/
resource-group.go
81 lines (60 loc) · 1.61 KB
/
resource-group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package resources
import (
"context"
"time"
"github.com/sirupsen/logrus"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-05-01/resources"
"github.com/ekristen/azure-nuke/pkg/resource"
"github.com/ekristen/azure-nuke/pkg/types"
)
type ResourceGroup struct {
client resources.GroupsClient
name *string
}
func init() {
resource.RegisterV2(resource.Registration{
Name: "ResourceGroup",
Lister: ListResourceGroup,
Scope: resource.Subscription,
})
}
func ListResourceGroup(opts resource.ListerOpts) ([]resource.Resource, error) {
logrus.Tracef("subscription id: %s", opts.SubscriptionId)
client := resources.NewGroupsClient(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 groups")
ctx := context.Background()
list, err := client.List(ctx, "", nil)
if err != nil {
return nil, err
}
logrus.Trace("listing ....")
for list.NotDone() {
logrus.Trace("list not done")
for _, entity := range list.Values() {
resources = append(resources, &ResourceGroup{
client: client,
name: entity.Name,
})
}
if err := list.NextWithContext(ctx); err != nil {
return nil, err
}
}
return resources, nil
}
func (r *ResourceGroup) Remove() error {
_, err := r.client.Delete(context.TODO(), *r.name)
return err
}
func (r *ResourceGroup) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("Name", *r.name)
return properties
}
func (r *ResourceGroup) String() string {
return *r.name
}