-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscription.go
60 lines (50 loc) · 1.87 KB
/
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
package healthcheck
import (
"context"
"fmt"
"strings"
"cloud.google.com/go/pubsub"
"github.com/ankorstore/yokai/config"
"github.com/ankorstore/yokai/healthcheck"
)
// SubscriptionsProbeName is the name of the GCP pub/sub subscriptions probe.
const SubscriptionsProbeName = "gcppubsub-subscriptions"
// GcpPubSubSubscriptionsProbe is a probe compatible with the [healthcheck] module.
//
// [healthcheck]: https://github.com/ankorstore/yokai/tree/main/healthcheck
type GcpPubSubSubscriptionsProbe struct {
config *config.Config
client *pubsub.Client
}
// NewGcpPubSubSubscriptionsProbe returns a new [GcpPubSubSubscriptionsProbe].
func NewGcpPubSubSubscriptionsProbe(config *config.Config, client *pubsub.Client) *GcpPubSubSubscriptionsProbe {
return &GcpPubSubSubscriptionsProbe{
config: config,
client: client,
}
}
// Name returns the name of the [GcpPubSubSubscriptionsProbe].
func (p *GcpPubSubSubscriptionsProbe) Name() string {
return SubscriptionsProbeName
}
// Check returns a successful [healthcheck.CheckerProbeResult] if all configured subscriptions exist.
func (p *GcpPubSubSubscriptionsProbe) Check(ctx context.Context) *healthcheck.CheckerProbeResult {
success := true
var messages []string
for _, subscriptionName := range p.config.GetStringSlice("modules.gcppubsub.healthcheck.subscriptions") {
subscription := p.client.Subscription(subscriptionName)
exists, err := subscription.Exists(ctx)
if err != nil {
success = false
messages = append(messages, fmt.Sprintf("subscription %s error: %v", subscriptionName, err))
} else {
if !exists {
success = false
messages = append(messages, fmt.Sprintf("subscription %s does not exist", subscriptionName))
} else {
messages = append(messages, fmt.Sprintf("subscription %s exists", subscriptionName))
}
}
}
return healthcheck.NewCheckerProbeResult(success, strings.Join(messages, ", "))
}