|
| 1 | +/** |
| 2 | + * Copyright 2019 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +const { google } = require("googleapis"); |
| 18 | +const { auth } = google; |
| 19 | +const crm = google.cloudresourcemanager("v1"); |
| 20 | + |
| 21 | +/** |
| 22 | + * Authenticates with Google Cloud Platform. |
| 23 | + * |
| 24 | + * @param {!Function} callback A callback function to signal completion. |
| 25 | + */ |
| 26 | +authenticate = callback => { |
| 27 | + console.log("Authenticating"); |
| 28 | + auth.getApplicationDefault((error, authClient) => { |
| 29 | + if (error) { |
| 30 | + console.error("Error while authenticating"); |
| 31 | + |
| 32 | + return callback(error); |
| 33 | + } |
| 34 | + console.log("Authenticated"); |
| 35 | + |
| 36 | + return callback(null, authClient); |
| 37 | + }); |
| 38 | +}; |
| 39 | + |
| 40 | +/** |
| 41 | + * Fetches fields from a given project. |
| 42 | + * |
| 43 | + * @param {!Object} authClient An authenticated client for GCP. |
| 44 | + * label. |
| 45 | + * @param {!String} projectId The identity of the project to fetch fields for. |
| 46 | + * @param {!Function} callback A callback function to signal completion. |
| 47 | + */ |
| 48 | +fetchFields = ({ authClient, projectId }, callback) => { |
| 49 | + console.log("Fetching fields for " + projectId); |
| 50 | + crm.projects.get( |
| 51 | + { auth: authClient, projectId }, (error, response) => { |
| 52 | + if (error) { |
| 53 | + console.error("Error while fetching fields"); |
| 54 | + |
| 55 | + return callback(error); |
| 56 | + } |
| 57 | + |
| 58 | + const fields = response.data || {}; |
| 59 | + |
| 60 | + console.log("Fetched labels:", fields); |
| 61 | + |
| 62 | + return callback(null, fields); |
| 63 | + }); |
| 64 | +}; |
| 65 | + |
| 66 | +/** |
| 67 | + * Stores fields on a given project. |
| 68 | + * |
| 69 | + * @param {!Object} authClient An authenticated client for GCP. |
| 70 | + * @param {!Object} fields Fields to be stored on the project. |
| 71 | + * @param {!String} projectId The identity of the project to save fields to. |
| 72 | + * @param {!Function} callback A callback function to signal completion. |
| 73 | + */ |
| 74 | +storeFields = |
| 75 | + ({ authClient, fields, projectId }, |
| 76 | + callback) => { |
| 77 | + console.log("Storing fields for " + projectId); |
| 78 | + crm.projects.update( |
| 79 | + { |
| 80 | + auth: authClient, |
| 81 | + projectId, |
| 82 | + resource: fields |
| 83 | + }, |
| 84 | + error => { |
| 85 | + if (error) { |
| 86 | + console.error("Error while storing fields"); |
| 87 | + |
| 88 | + return callback(error); |
| 89 | + } |
| 90 | + console.log("Stored fields:", fields); |
| 91 | + |
| 92 | + return callback(null); |
| 93 | + }); |
| 94 | + }; |
| 95 | + |
| 96 | +/** |
| 97 | + * Triggered from a message on a Cloud Pub/Sub topic. |
| 98 | + * |
| 99 | + * @param {!Object} event Event payload and metadata. |
| 100 | + * @param {!Function} callback Callback function to signal completion. |
| 101 | + */ |
| 102 | +exports.labelResource = (data, context, callback)=> { |
| 103 | + const eventData = |
| 104 | + JSON.parse(Buffer.from(data.data, "base64").toString()); |
| 105 | + |
| 106 | + console.log("Received event"); |
| 107 | + console.log(eventData); |
| 108 | + authenticate((error, authClient) => { |
| 109 | + if (error) { |
| 110 | + return callback(error); |
| 111 | + } |
| 112 | + |
| 113 | + const projectId = eventData.resource.labels.project_id; |
| 114 | + |
| 115 | + fetchFields( |
| 116 | + { authClient, projectId }, |
| 117 | + (error, fields, labelFingerprint) => { |
| 118 | + if (error) { |
| 119 | + return callback(error); |
| 120 | + } |
| 121 | + |
| 122 | + const labelKey = process.env.LABEL_KEY; |
| 123 | + const labelValue = process.env.LABEL_VALUE; |
| 124 | + |
| 125 | + storeFields( |
| 126 | + { |
| 127 | + authClient, |
| 128 | + fields: { |
| 129 | + name: fields.name, |
| 130 | + parent: fields.parent, |
| 131 | + labels: |
| 132 | + Object.assign(fields.labels || {}, { [labelKey]: labelValue }) |
| 133 | + }, |
| 134 | + projectId |
| 135 | + }, |
| 136 | + callback); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}; |
0 commit comments