/
pubsub-subscription.go
78 lines (61 loc) · 1.83 KB
/
pubsub-subscription.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
package resources
import (
"context"
"fmt"
"path"
"cloud.google.com/go/pubsub"
"github.com/dshelley66/gcp-nuke/pkg/gcputil"
"github.com/dshelley66/gcp-nuke/pkg/types"
"google.golang.org/api/iterator"
)
const ResourceTypePubSubSubscription = "PubSubSubscription"
type PubSubSubscription struct {
name string
labels map[string]string
}
func init() {
register(ResourceTypePubSubSubscription, GetPubSubClient, ListPubSubSubscriptions)
}
func ListPubSubSubscriptions(project *gcputil.Project, client gcputil.GCPClient) ([]Resource, error) {
pubsubClient := client.(*pubsub.Client)
resources := make([]Resource, 0)
it := pubsubClient.Subscriptions(project.GetContext())
for {
subscription, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, fmt.Errorf("failed to list pubsub subscriptions: %v", err)
}
// get the SubscriptionConfig
sc, err := subscription.Config(project.GetContext())
if err != nil {
return nil, fmt.Errorf("failed to get pubsub subscription config for '%s': %v", subscription.String(), err)
}
resources = append(resources, &PubSubSubscription{
name: path.Base(subscription.String()),
labels: sc.Labels,
})
}
return resources, nil
}
func (x *PubSubSubscription) Remove(project *gcputil.Project, client gcputil.GCPClient) error {
pubsubClient := client.(*pubsub.Client)
subscription := pubsubClient.Subscription(x.name)
return subscription.Delete(project.GetContext())
}
func (x *PubSubSubscription) GetOperationError(_ context.Context) error {
return nil
}
func (x *PubSubSubscription) String() string {
return x.name
}
func (x *PubSubSubscription) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("Name", x.name)
for labelKey, label := range x.labels {
properties.SetTag(labelKey, label)
}
return properties
}