|
| 1 | +// Copyright 2016, Google, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +// [START setup] |
| 17 | +var projectId = process.env.TEST_PROJECT_ID; |
| 18 | +var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS; |
| 19 | + |
| 20 | +projectId = projectId || '<your-project-id>'; |
| 21 | +keyFilename = keyFilename || '/path/to/keyfile.json'; |
| 22 | + |
| 23 | +var gcloud = require('gcloud')({ |
| 24 | + projectId: projectId, |
| 25 | + keyFilename: keyFilename |
| 26 | +}); |
| 27 | + |
| 28 | +var logging = gcloud.logging(); |
| 29 | +// [END setup] |
| 30 | + |
| 31 | +// [START listSinks] |
| 32 | +function listSinks(callback) { |
| 33 | + // list all sinks in the project |
| 34 | + logging.getSinks(callback); |
| 35 | +} |
| 36 | +// [END listSinks] |
| 37 | + |
| 38 | +// [START createSink] |
| 39 | +function createSink(callback) { |
| 40 | + var gcs = gcloud.storage(); |
| 41 | + |
| 42 | + // create a new sink |
| 43 | + // |
| 44 | + // This method only works if you are authenticated as yourself, e.g. using the |
| 45 | + // gcloud SDK. |
| 46 | + logging.createSink('mySink', { |
| 47 | + destination: gcs.bucket('logging-bucket') |
| 48 | + }, callback); |
| 49 | +} |
| 50 | +// [END createSink] |
| 51 | + |
| 52 | +// [START updateSink] |
| 53 | +function updateSink(callback) { |
| 54 | + var gcs = gcloud.storage(); |
| 55 | + var sink = logging.sink('mySink'); |
| 56 | + |
| 57 | + // update a sink |
| 58 | + // |
| 59 | + // This method only works if you are authenticated as yourself, e.g. using the |
| 60 | + // gcloud SDK. |
| 61 | + sink.setMetadata({ |
| 62 | + // change destination to something else |
| 63 | + destination: gcs.bucket('other-logging-bucket') |
| 64 | + }, callback); |
| 65 | +} |
| 66 | +// [END updateSink] |
| 67 | + |
| 68 | +// [START deleteSink] |
| 69 | +function deleteSink(callback) { |
| 70 | + var sink = logging.sink('mySink'); |
| 71 | + |
| 72 | + // delete a sink |
| 73 | + // |
| 74 | + // This method only works if you are authenticated as yourself, e.g. using the |
| 75 | + // gcloud SDK. |
| 76 | + sink.delete(callback); |
| 77 | +} |
| 78 | +// [END deleteSink] |
| 79 | + |
| 80 | +exports.listSinks = listSinks; |
| 81 | +exports.createSink = createSink; |
| 82 | +exports.updateSink = updateSink; |
| 83 | +exports.deleteSink = deleteSink; |
| 84 | + |
| 85 | +if (module === require.main) { |
| 86 | + listSinks(function (err, sinks, apiResponse) { |
| 87 | + console.log(err, sinks, apiResponse); |
| 88 | + if (err) { |
| 89 | + return; |
| 90 | + } |
| 91 | + }); |
| 92 | +} |
0 commit comments