This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
broker.go
154 lines (126 loc) · 4.97 KB
/
broker.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
// Copyright 2018 the Service Broker Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pubsub
import (
"errors"
googlepubsub "cloud.google.com/go/pubsub"
"github.com/GoogleCloudPlatform/gcp-service-broker/db_service/models"
"github.com/GoogleCloudPlatform/gcp-service-broker/pkg/varcontext"
"github.com/GoogleCloudPlatform/gcp-service-broker/utils"
"github.com/pivotal-cf/brokerapi"
"golang.org/x/net/context"
"fmt"
"time"
"github.com/GoogleCloudPlatform/gcp-service-broker/pkg/providers/builtin/base"
"google.golang.org/api/option"
)
// PubSubBroker is the service-broker back-end for creating Google Pub/Sub
// topics, subscriptions, and accounts.
type PubSubBroker struct {
base.BrokerBase
}
// InstanceInformation holds the details needed to connect to a PubSub instance
// after it has been provisioned.
type InstanceInformation struct {
TopicName string `json:"topic_name"`
// SubscriptionName is optional, if non-empty then a susbcription was created.
SubscriptionName string `json:"subscription_name"`
}
// Provision creates a new Pub/Sub topic from the settings in the user-provided details and service plan.
// If a subscription name is supplied, the function will also create a subscription for the topic.
func (b *PubSubBroker) Provision(ctx context.Context, provisionContext *varcontext.VarContext) (models.ServiceInstanceDetails, error) {
// Extract and validate all the params exist and are the right types
defaultLabels := provisionContext.GetStringMapString("labels")
topicName := provisionContext.GetString("topic_name")
subscriptionName := provisionContext.GetString("subscription_name")
endpoint := ""
if provisionContext.GetBool("is_push") {
endpoint = provisionContext.GetString("endpoint")
}
subscriptionConfig := googlepubsub.SubscriptionConfig{
PushConfig: googlepubsub.PushConfig{
Endpoint: endpoint,
},
AckDeadline: time.Duration(provisionContext.GetInt("ack_deadline")) * time.Second,
Labels: defaultLabels,
}
if err := provisionContext.Error(); err != nil {
return models.ServiceInstanceDetails{}, err
}
// Check special-cases
if topicName == "" {
return models.ServiceInstanceDetails{}, errors.New("topic_name must not be blank")
}
// Create
pubsubClient, err := b.createClient(ctx)
if err != nil {
return models.ServiceInstanceDetails{}, err
}
topic, err := pubsubClient.CreateTopic(ctx, topicName)
if err != nil {
return models.ServiceInstanceDetails{}, fmt.Errorf("Error creating new Pub/Sub topic: %s", err)
}
// This service needs labels to be set after creation
if _, err := topic.Update(ctx, googlepubsub.TopicConfigToUpdate{Labels: defaultLabels}); err != nil {
return models.ServiceInstanceDetails{}, fmt.Errorf("Error setting labels on new Pub/Sub topic: %s", err)
}
if subscriptionName != "" {
subscriptionConfig.Topic = topic
if _, err := pubsubClient.CreateSubscription(ctx, subscriptionName, subscriptionConfig); err != nil {
return models.ServiceInstanceDetails{}, fmt.Errorf("Error creating subscription: %s", err)
}
}
ii := InstanceInformation{
TopicName: topicName,
SubscriptionName: subscriptionName,
}
id := models.ServiceInstanceDetails{
Name: topicName,
Url: "",
Location: "",
}
if err := id.SetOtherDetails(ii); err != nil {
return models.ServiceInstanceDetails{}, err
}
return id, err
}
// Deprovision deletes the topic and subscription associated with the given instance.
func (b *PubSubBroker) Deprovision(ctx context.Context, topic models.ServiceInstanceDetails, details brokerapi.DeprovisionDetails) (*string, error) {
service, err := b.createClient(ctx)
if err != nil {
return nil, err
}
if err := service.Topic(topic.Name).Delete(ctx); err != nil {
return nil, fmt.Errorf("Error deleting pubsub topic: %s", err)
}
otherDetails := InstanceInformation{}
if err := topic.GetOtherDetails(&otherDetails); err != nil {
return nil, err
}
if otherDetails.SubscriptionName != "" {
if err := service.Subscription(otherDetails.SubscriptionName).Delete(ctx); err != nil {
return nil, fmt.Errorf("Error deleting subscription: %s", err)
}
}
return nil, nil
}
func (b *PubSubBroker) createClient(ctx context.Context) (*googlepubsub.Client, error) {
co := option.WithUserAgent(utils.CustomUserAgent)
ct := option.WithTokenSource(b.HttpConfig.TokenSource(ctx))
client, err := googlepubsub.NewClient(ctx, b.ProjectId, co, ct)
if err != nil {
return nil, fmt.Errorf("Couldn't instantiate Pub/Sub API client: %s", err)
}
return client, nil
}