// TODO(djd): Delete this function when it's no longer used. func Context(scopes ...string) context.Context { ctx := oauth2.NoContext ts := TokenSource(ctx, scopes...) if ts == nil { return nil } return cloud.NewContext(ProjID(), oauth2.NewClient(ctx, ts)) }
// CreateGooglePubSubClient returns client communicate with google Pub/Sub service func CreateGooglePubSubClient() (*pubsub.Client, error) { envValue := os.Getenv(NDGooglePubSubURL) if envValue != "" { googlePubSubURL = envValue } envValue = os.Getenv(NDGoogleProjectID) if envValue != "" { googleProjectID = envValue } ctx := cloud.NewContext(googleProjectID, http.DefaultClient) o := []cloud.ClientOption{ cloud.WithBaseHTTP(http.DefaultClient), cloud.WithEndpoint(googleDatastoreURL), } client, _ := pubsub.NewClient(ctx, googleProjectID, o...) return client, nil }
// This example demonstrates calling the Cloud Pub/Sub API. // // Before running this example, be sure to enable Cloud Pub/Sub // service on your project in Developer Console at: // https://console.developers.google.com/ // // Unless you run this sample on Compute Engine instance, please // create a new service account and download a JSON key file for it at // the developer console: https://console.developers.google.com/ // // It has the following subcommands: // // create_topic <name> // delete_topic <name> // create_subscription <name> <linked_topic> // delete_subscription <name> // publish <topic> <message> // pull_messages <subscription> <numworkers> // publish_messages <topic> <numworkers> // // You can choose any names for topic and subscription as long as they // follow the naming rule described at: // https://cloud.google.com/pubsub/overview#names // // You can create/delete topics/subscriptions by self-explanatory // subcommands. // // The "publish" subcommand is for publishing a single message to a // specified Cloud Pub/Sub topic. // // The "pull_messages" subcommand is for continuously pulling messages // from a specified Cloud Pub/Sub subscription with specified number // of workers. // // The "publish_messages" subcommand is for continuously publishing // messages to a specified Cloud Pub/Sub topic with specified number // of workers. func main() { flag.Parse() argv := flag.Args() checkArgs(argv, 1) if *projID == "" { usageAndExit("Please specify Project ID.") } oldStyle := map[string]func(ctx context.Context, argv []string){ "publish": publish, "pull_messages": pullMessages, "publish_messages": publishMessages, } newStyle := map[string]func(client *pubsub.Client, argv []string){ "create_topic": createTopic, "delete_topic": deleteTopic, "list_topics": listTopics, "list_topic_subscriptions": listTopicSubscriptions, "topic_exists": checkTopicExists, "create_subscription": createSubscription, "delete_subscription": deleteSubscription, "subscription_exists": checkSubscriptionExists, "list_subscriptions": listSubscriptions, } subcommand := argv[0] if f, ok := oldStyle[subcommand]; ok { httpClient, err := newClient(*jsonFile) if err != nil { log.Fatalf("clientAndId failed, %v", err) } ctx := cloud.NewContext(*projID, httpClient) f(ctx, argv) } else if f, ok := newStyle[subcommand]; ok { client, err := pubsub.NewClient(context.Background(), *projID) if err != nil { log.Fatalf("creating pubsub client: %v", err) } f(client, argv) } else { usageAndExit(fmt.Sprintf("Function not found for %s", subcommand)) } }
// CreateGoogleDatastoreClient create client to communicate to google Datastore service func CreateGoogleDatastoreClient() (*datastore.Client, error) { envValue := os.Getenv(NDGoogleDatastoreURL) if envValue != "" { googleDatastoreURL = envValue } envValue = os.Getenv(NDGoogleProjectID) if envValue != "" { googleProjectID = envValue } ctx := cloud.NewContext(googleProjectID, http.DefaultClient) o := []cloud.ClientOption{ cloud.WithBaseHTTP(http.DefaultClient), cloud.WithEndpoint(googleDatastoreURL), } client, err := datastore.NewClient(ctx, googleProjectID, o...) if err != nil { return nil, err } return client, nil }