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

Update tq sample to reflect now obsolete limitation of 2nd Gen function #1068

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions 2nd-gen/taskqueues-backup-images/README.md
Original file line number Diff line number Diff line change
@@ -77,12 +77,12 @@ gcloud projects add-iam-policy-binding $PROJECT_ID \
Please follow Google Cloud IAM documentation to add App Engine default service account as user of App Engine default service account.
```

* Identity used to trigger the Task Queue function needs `cloudfunctions.functions.invoke` permission.
* Identity used to trigger the Task Queue function needs `run.routes.invoke` permission.
* In our sample, this is the [Compute Engine default service account](https://cloud.google.com/compute/docs/access/service-accounts).

```
gcloud functions add-iam-policy-binding backupapod \
--region=us-central1 \
--member=serviceAccount:${PROJECT_NUMBER}-compute@developer.gserviceaccount.com \
--role=roles/cloudfunctions.invoker
--role=roles/run.invoker
```
33 changes: 0 additions & 33 deletions 2nd-gen/taskqueues-backup-images/functions/index.js
Original file line number Diff line number Diff line change
@@ -22,7 +22,6 @@ const {onRequest} = require("firebase-functions/v2/https");
const {initializeApp} = require("firebase-admin/app");
const {getFunctions} = require("firebase-admin/functions");
const {getStorage} = require("firebase-admin/storage");
const {GoogleAuth} = require("google-auth-library");
const logger = functions.logger;
const HttpsError = functions.https.HttpsError;
initializeApp();
@@ -99,41 +98,10 @@ exports.backupapod = onTaskDispatched(
});


let auth;

// [START v2GetFunctionUri]
/**
* Get the URL of a given v2 cloud function.
*
* @param {string} name the function's name
* @param {string} location the function's location
* @return {Promise<string>} The URL of the function
*/
async function getFunctionUrl(name, location="us-central1") {
if (!auth) {
auth = new GoogleAuth({
scopes: "https://www.googleapis.com/auth/cloud-platform",
});
}
const projectId = await auth.getProjectId();
const url = "https://cloudfunctions.googleapis.com/v2beta/" +
`projects/${projectId}/locations/${location}/functions/${name}`;

const client = await auth.getClient();
const res = await client.request({url});
const uri = res.data?.serviceConfig?.uri;
if (!uri) {
throw new Error(`Unable to retreive uri for function at ${url}`);
}
return uri;
}
// [END v2GetFunctionUri]

// [START v2EnqueueTasks]
exports.enqueuebackuptasks = onRequest(
async (_request, response) => {
const queue = getFunctions().taskQueue("backupapod");
const targetUri = await getFunctionUrl("backupapod");

const enqueues = [];
for (let i = 0; i <= BACKUP_COUNT; i += 1) {
@@ -149,7 +117,6 @@ exports.enqueuebackuptasks = onRequest(
queue.enqueue({date}, {
scheduleDelaySeconds,
dispatchDeadlineSeconds: 60 * 5, // 5 minutes
uri: targetUri,
}),
);
}
1 change: 0 additions & 1 deletion 2nd-gen/taskqueues-backup-images/functions/package.json
Original file line number Diff line number Diff line change
@@ -18,7 +18,6 @@
"dependencies": {
"firebase-admin": "^11.5.0",
"firebase-functions": "^4.2.1",
"google-auth-library": "^8.6.0",
"node-fetch": "^2.6.7"
},
"devDependencies": {