Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify PubSub samples. #202

Merged
merged 1 commit into from
Sep 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions pubsub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,19 @@ __Usage:__ `node topics --help`

```
Commands:
create <name> Create a new topic.
list List topics.
publish <topic> <message> Publish a message to the specified topic.
delete <name> Delete the specified topic.
create <topicName> Creates a new topic.
list Lists topics.
publish <topicName> <message> Publish a message to the specified topic.
delete <topicName> Deletes the specified topic.

Options:
--help Show help [boolean]
--help Show help [boolean]

Examples:
node topics create my-topic Create a new topic named "my-topic".
node topics list List topics.
node topics publish my-topic Publish a message to "my-topic".
'{"data":"Hello world!"}'
node topics delete my-topic Delete a topic named "my-topic".
node topics create greetings Creates a new topic named "greetings".
node topics list Lists all topics.
node topics publish greetings '{"data":"Hello world!"}' Publishes a message to "greetings".
node topics delete greetings Deletes a topic named "greetings".

For more information, see https://cloud.google.com/pubsub/docs
```
Expand All @@ -64,20 +63,22 @@ __Usage:__ `node subscriptions --help`

```
Commands:
create <topic> <name> Create a new subscription.
list [topic] List subscriptions.
pull <subscription> Pull messages from the specified subscription.
delete <subscription> Delete the specified subscription.
create <topicName> <subscriptionName> Creates a new subscription.
list [topicName] Lists subscriptions, optionally filtering by a topic.
get <subscriptionName> Gets the metadata the metadata for the specified subscription.
pull <subscriptionName> Pulls messages for the specified subscription.
delete <subscriptionName> Deletes the specified subscription.

Options:
--help Show help [boolean]
--help Show help [boolean]

Examples:
node subscriptions create my-topic my-subscription Create a new subscription.
node subscriptions delete my-subscription Delete a subscription.
node subscriptions pull my-subscription Pull messages from "my-subscription".
node subscriptions list List all subscriptions.
node subscriptions list my-topic List subscriptions to topic "my-topic".
node subscriptions create greetings greetings-worker-1 Creates a subscription named "greetings-worker-1" to a topic
named "greetings".
node subscriptions delete greetings-worker-1 Deletes a subscription named "greetings-worker-1".
node subscriptions pull greetings-worker-1 Pulls messages for a subscription named "greetings-worker-1".
node subscriptions list Lists all subscriptions.
node subscriptions list greetings Lists subscriptions for a topic named "greetings".

For more information, see https://cloud.google.com/pubsub/docs
```
Expand Down
124 changes: 28 additions & 96 deletions pubsub/iam.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,84 +13,48 @@

'use strict';

// [START auth]
// [START setup]
// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// the project specified by the GCLOUD_PROJECT environment variable. See
// https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication
var PubSub = require('@google-cloud/pubsub');
// [END setup]

// Instantiate a pubsub client
var pubsub = PubSub();
// [END auth]

// [START get_topic_policy]
/**
* Retrieve a topic's IAM policy.
*
* @param {string} topicName The name of the topic.
* @param {function} callback The callback function.
*/
function getTopicPolicy (topicName, callback) {
if (!topicName) {
return callback(new Error('"topicName" is required!'));
}

// Grab a reference to an existing topic
var pubsub = PubSub();
var topic = pubsub.topic(topicName);

// Retrieve the IAM policy for the topic
topic.iam.getPolicy(function (err, policy) {
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/latest/pubsub/topic?method=iam.getPolicy
topic.iam.getPolicy(function (err, policy, apiResponse) {
if (err) {
return callback(err);
}

console.log('Got topic policy:', policy);
return callback(null, policy);
console.log('Got policy for topic: %s', topicName);
return callback(null, policy, apiResponse);
});
}
// [END get_topic_policy]

// [START get_subscription_policy]
/**
* Retrieve a subcription's IAM policy.
*
* @param {string} subscriptionName The name of the subscription.
* @param {function} callback The callback function.
*/
function getSubscriptionPolicy (subscriptionName, callback) {
if (!subscriptionName) {
return callback(new Error('"subscriptionName" is required!'));
}

// Grab a reference to an existing subscription
var pubsub = PubSub();
var subscription = pubsub.subscription(subscriptionName);

// Retrieve the IAM policy for the subscription
subscription.iam.getPolicy(function (err, policy) {
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/latest/pubsub/subscription?method=iam.getPolicy
subscription.iam.getPolicy(function (err, policy, apiResponse) {
if (err) {
return callback(err);
}

console.log('Got subscription policy:', policy);
return callback(null, policy);
console.log('Got policy for subscription: %s', subscriptionName);
return callback(null, policy, apiResponse);
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// [END get_subscription_policy]

// [START set_topic_policy]
/**
* Set a topic's IAM policy.
*
* @param {string} topicName The name of the topic.
* @param {function} callback The callback function.
*/
function setTopicPolicy (topicName, callback) {
if (!topicName) {
return callback(new Error('"topicName" is required!'));
}

// Grab a reference to an existing topic
var pubsub = PubSub();
var topic = pubsub.topic(topicName);

// Policy update
Expand All @@ -104,29 +68,19 @@ function setTopicPolicy (topicName, callback) {
};

// Set the IAM policy for the specified topic
topic.iam.setPolicy(newPolicy, function (err, policy) {
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/latest/pubsub/topic?method=iam.setPolicy
topic.iam.setPolicy(newPolicy, function (err, updatedPolicy, apiResponse) {
if (err) {
return callback(err);
}

console.log('Updated policy for topic: %s', topicName);
return callback(null, policy);
return callback(null, updatedPolicy, apiResponse);
});
}
// [END set_topic_policy]

// [START set_subscription_policy]
/**
* @param {string} subscriptionName Name of the subscription whose policy is to
* be updated.
* @param {function} callback The callback function.
*/
function setSubscriptionPolicy (subscriptionName, callback) {
if (!subscriptionName) {
return callback(new Error('"subscriptionName" is required!'));
}

// Grab a reference to an existing subscription
var pubsub = PubSub();
var subscription = pubsub.subscription(subscriptionName);

// Policy update
Expand All @@ -140,30 +94,19 @@ function setSubscriptionPolicy (subscriptionName, callback) {
};

// Set the IAM policy for the specified subscription
subscription.iam.setPolicy(newPolicy, function (err, policy) {
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/latest/pubsub/subscription?method=iam.setPolicy
subscription.iam.setPolicy(newPolicy, function (err, updatedPolicy, apiResponse) {
if (err) {
return callback(err);
}

console.log('Updated policy for subscription: %s', subscriptionName);
return callback(null, policy);
return callback(null, updatedPolicy, apiResponse);
});
}
// [END set_subscription_policy]

// [START test_topic_permissions]
/**
* Test a topic's IAM permissions.
*
* @param {string} topicName The name of the topic.
* @param {function} callback The callback function.
*/
function testTopicPermissions (topicName, callback) {
if (!topicName) {
return callback(new Error('"topicName" is required!'));
}

// Grab a reference to an existing topic
var pubsub = PubSub();
var topic = pubsub.topic(topicName);

var permissionsToTest = [
Expand All @@ -173,30 +116,19 @@ function testTopicPermissions (topicName, callback) {
];

// Test the IAM policy for the specified topic
topic.iam.testPermissions(permissionsToTest, function (err, permissions) {
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/latest/pubsub/topic?method=iam.testPermissions
topic.iam.testPermissions(permissionsToTest, function (err, permissions, apiResponse) {
if (err) {
return callback(err);
}

console.log('Tested permissions for topic: %s', topicName);
return callback(null, permissions);
return callback(null, permissions, apiResponse);
});
}
// [END test_topic_permissions]

// [START test_subscription_permissions]
/**
* Test a subcription's IAM permissions.
*
* @param {string} subscriptionName The name of the subscription.
* @param {function} callback The callback function.
*/
function testSubscriptionPermissions (subscriptionName, callback) {
if (!subscriptionName) {
return callback(new Error('"subscriptionName" is required!'));
}

// Grab a reference to an existing subscription
var pubsub = PubSub();
var subscription = pubsub.subscription(subscriptionName);

var permissionsToTest = [
Expand All @@ -205,16 +137,16 @@ function testSubscriptionPermissions (subscriptionName, callback) {
];

// Test the IAM policy for the specified subscription
subscription.iam.testPermissions(permissionsToTest, function (err, permissions) {
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/latest/pubsub/subscription?method=iam.testPermissions
subscription.iam.testPermissions(permissionsToTest, function (err, permissions, apiResponse) {
if (err) {
return callback(err);
}

console.log('Tested permissions for subscription: %s', subscriptionName);
return callback(null, permissions);
return callback(null, permissions, apiResponse);
});
}
// [END test_subscription_permissions]

// [START usage]
function printUsage () {
Expand Down
Loading