コード例 #1
0
ファイル: legacy.go プロジェクト: tav/gcloud-golang
func rawService(ctx context.Context) *raw.Service {
	return internal.Service(ctx, "pubsub", func(hc *http.Client) interface{} {
		svc, _ := raw.New(hc)
		svc.BasePath = baseAddr()
		return svc
	}).(*raw.Service)
}
コード例 #2
0
ファイル: service.go プロジェクト: Celluliodio/flannel
func newPubSubService(client *http.Client, endpoint string) (*apiService, error) {
	s, err := raw.New(client)
	if err != nil {
		return nil, err
	}
	s.BasePath = endpoint

	return &apiService{s: s}, nil
}
コード例 #3
0
ファイル: legacy.go プロジェクト: Ropes/pubbing
func rawService(ctx context.Context) *raw.Service {
	baseAddr := func() string {
		// Environment variables for gcloud emulator:
		// https://cloud.google.com/sdk/gcloud/reference/beta/emulators/pubsub/
		if host := os.Getenv("PUBSUB_EMULATOR_HOST"); host != "" {
			return "http://" + host + "/"
		}
		return prodAddr
	}
	return internal.Service(ctx, "pubsub", func(hc *http.Client) interface{} {
		svc, _ := raw.New(hc)
		svc.BasePath = baseAddr()
		return svc
	}).(*raw.Service)
}
コード例 #4
0
// NewClient create a new PubSub client.
func NewClient(ctx context.Context, projectID string, opts ...cloud.ClientOption) (*Client, error) {
	o := []cloud.ClientOption{
		cloud.WithEndpoint(prodAddr),
		cloud.WithScopes(raw.PubsubScope, raw.CloudPlatformScope),
		cloud.WithUserAgent(userAgent),
	}
	o = append(o, opts...)
	httpClient, endpoint, err := transport.NewHTTPClient(ctx, o...)
	if err != nil {
		return nil, fmt.Errorf("dialing: %v", err)
	}
	s, err := raw.New(httpClient)
	if err != nil {
		return nil, err
	}

	s.BasePath = endpoint
	c := &Client{
		projectID: projectID,
		s:         s,
	}

	return c, nil
}
コード例 #5
0
ファイル: main.go プロジェクト: chrusty/tail-pubsub
func main() {
	// Make sure we flush the log before quitting:
	defer log.Flush()

	// Connect to GCE (either from GCE permissions, JSON file, or ENV-vars):
	client, err := google.DefaultClient(context.Background(), pubsub.PubsubScope)
	if err != nil {
		log.Criticalf("Unable to authenticate to GCE! (%s)", err)
		os.Exit(2)
	}

	// Get a DNS service-object:
	pubsubService, err := pubsub.New(client)
	if err != nil {
		log.Criticalf("Failed to connect to PUB/SUB! %v", err)
		os.Exit(2)
	}

	// Build the topic and subscription names:
	fullTopicName := fmt.Sprintf("projects/%v/topics/%v", *project, *topic)
	fullSubscriptionName := fmt.Sprintf("projects/%v/subscriptions/%v", *project, "tail-pubsub")

	// Create a subscription:
	log.Debugf("Making subscription to topic (%v) ...", fullTopicName)
	sub := &pubsub.Subscription{Topic: fullTopicName}
	subscription, err := pubsubService.Projects.Subscriptions.Create(fullSubscriptionName, sub).Do()
	if err != nil {
		log.Warnf("Failed to create subscription to topic (%v): %v", fullTopicName, err)
		log.Debugf("Getting existing subscription (%v) to topic (%v) ...", fullSubscriptionName, fullTopicName)
		subscription, err = pubsubService.Projects.Subscriptions.Get(fullSubscriptionName).Do()
		if err != nil {
			log.Criticalf("Couldn't even get existing subscription (%v): %v", fullSubscriptionName, err)
			os.Exit(2)
		}
	}
	log.Debugf("Subscription (%s) was created", subscription.Name)

	// Prepare a pull-request:
	pullRequest := &pubsub.PullRequest{
		ReturnImmediately: true,
		MaxMessages:       *batchsize,
	}

	// Now attack the pub/sub API:
	for {
		log.Debugf("Polling for messages ...")
		pullResponse, err := pubsubService.Projects.Subscriptions.Pull(fullSubscriptionName, pullRequest).Do()
		if err != nil {
			log.Errorf("Failed to pull messages from subscription (%v): %v", fullSubscriptionName, err)
		} else {
			for _, receivedMessage := range pullResponse.ReceivedMessages {
				messageData, err := base64.StdEncoding.DecodeString(receivedMessage.Message.Data)
				if err != nil {
					log.Warnf("Failed to decode message: %v", err)
				} else {
					log.Infof("Message: %v", string(messageData))
					ackRequest := &pubsub.AcknowledgeRequest{
						AckIds: []string{receivedMessage.AckId},
					}
					if _, err = pubsubService.Projects.Subscriptions.Acknowledge(fullSubscriptionName, ackRequest).Do(); err != nil {
						log.Warnf("Failed to acknowledge messages: %v", err)
					}
				}
			}
		}

		// Sleep until trying again:
		time.Sleep(1 * time.Second)

	}

}