-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare.go
164 lines (139 loc) · 5.01 KB
/
prepare.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package fxgcppubsub
import (
"context"
"fmt"
"cloud.google.com/go/pubsub"
"go.uber.org/fx"
)
// PrepareSchemaParams represents the parameters used in PrepareSchema.
type PrepareSchemaParams struct {
SchemaID string
SchemaConfig pubsub.SchemaConfig
}
// PrepareSchema prepares a pub/sub schema.
func PrepareSchema(params PrepareSchemaParams) fx.Option {
return fx.Invoke(
func(ctx context.Context, schemaClient *pubsub.SchemaClient) error {
_, err := schemaClient.CreateSchema(ctx, params.SchemaID, params.SchemaConfig)
if err != nil {
return fmt.Errorf("failed to prepare schema %q: %w", params.SchemaID, err)
}
return nil
},
)
}
// PrepareTopicParams represents the parameters used in PrepareTopic.
type PrepareTopicParams struct {
TopicID string
TopicConfig pubsub.TopicConfig
}
// PrepareTopic prepares a pub/sub topic.
func PrepareTopic(params PrepareTopicParams) fx.Option {
return fx.Invoke(
func(ctx context.Context, client *pubsub.Client) error {
_, err := client.CreateTopicWithConfig(ctx, params.TopicID, ¶ms.TopicConfig)
if err != nil {
return fmt.Errorf("failed to prepare topic %q: %w", params.TopicID, err)
}
return nil
},
)
}
// PrepareTopicWithSchemaParams represents the parameters used in PrepareTopicWithSchema.
type PrepareTopicWithSchemaParams struct {
TopicID string
TopicConfig pubsub.TopicConfig
SchemaID string
SchemaConfig pubsub.SchemaConfig
SchemaEncoding pubsub.SchemaEncoding
}
// PrepareTopicWithSchema prepares a pub/sub topic with a schema.
func PrepareTopicWithSchema(params PrepareTopicWithSchemaParams) fx.Option {
return fx.Invoke(
func(ctx context.Context, schemaClient *pubsub.SchemaClient, client *pubsub.Client) error {
// prepare schema
schema, err := schemaClient.CreateSchema(ctx, params.SchemaID, params.SchemaConfig)
if err != nil {
return fmt.Errorf("failed to prepare schema %q: %w", params.SchemaID, err)
}
// prepare topic
topicConfig := params.TopicConfig
topicConfig.SchemaSettings = &pubsub.SchemaSettings{
Schema: schema.Name,
Encoding: params.SchemaEncoding,
}
_, err = client.CreateTopicWithConfig(ctx, params.TopicID, &topicConfig)
if err != nil {
return fmt.Errorf("failed to prepare topic %q: %w", params.TopicID, err)
}
return nil
},
)
}
// PrepareTopicAndSubscriptionParams represents the parameters used in PrepareTopicAndSubscription.
type PrepareTopicAndSubscriptionParams struct {
TopicID string
TopicConfig pubsub.TopicConfig
SubscriptionID string
SubscriptionConfig pubsub.SubscriptionConfig
}
// PrepareTopicAndSubscription prepares a pub/sub topic and an associated subscription.
func PrepareTopicAndSubscription(params PrepareTopicAndSubscriptionParams) fx.Option {
return fx.Invoke(
func(ctx context.Context, client *pubsub.Client) error {
// prepare topic
topic, err := client.CreateTopicWithConfig(ctx, params.TopicID, ¶ms.TopicConfig)
if err != nil {
return fmt.Errorf("failed to prepare topic %q: %w", params.TopicID, err)
}
// prepare subscription
subscriptionConfig := params.SubscriptionConfig
subscriptionConfig.Topic = topic
_, err = client.CreateSubscription(ctx, params.SubscriptionID, subscriptionConfig)
if err != nil {
return fmt.Errorf("failed to prepare subscription %q: %w", params.SubscriptionID, err)
}
return nil
},
)
}
// PrepareTopicAndSubscriptionWithSchemaParams represents the parameters used in PrepareTopicAndSubscriptionWithSchema.
type PrepareTopicAndSubscriptionWithSchemaParams struct {
TopicID string
TopicConfig pubsub.TopicConfig
SubscriptionID string
SubscriptionConfig pubsub.SubscriptionConfig
SchemaID string
SchemaConfig pubsub.SchemaConfig
SchemaEncoding pubsub.SchemaEncoding
}
// PrepareTopicAndSubscriptionWithSchema prepares a pub/sub topic and an associated subscription with a schema.
func PrepareTopicAndSubscriptionWithSchema(params PrepareTopicAndSubscriptionWithSchemaParams) fx.Option {
return fx.Invoke(
func(ctx context.Context, schemaClient *pubsub.SchemaClient, client *pubsub.Client) error {
// prepare schema
schema, err := schemaClient.CreateSchema(ctx, params.SchemaID, params.SchemaConfig)
if err != nil {
return fmt.Errorf("failed to prepare schema %q: %w", params.SchemaID, err)
}
// prepare topic
topicConfig := params.TopicConfig
topicConfig.SchemaSettings = &pubsub.SchemaSettings{
Schema: schema.Name,
Encoding: params.SchemaEncoding,
}
topic, err := client.CreateTopicWithConfig(ctx, params.TopicID, &topicConfig)
if err != nil {
return fmt.Errorf("failed to prepare topic %q: %w", params.TopicID, err)
}
// prepare subscription
subscriptionConfig := params.SubscriptionConfig
subscriptionConfig.Topic = topic
_, err = client.CreateSubscription(ctx, params.SubscriptionID, subscriptionConfig)
if err != nil {
return fmt.Errorf("failed to prepare subscription %q: %w", params.SubscriptionID, err)
}
return nil
},
)
}