Inserting ACL after creating Google Room Resource frequently throws error

Viewed 35

I have a Google Cloud function which first creates a Google Room Resource using resources.calendars.insert method from the Google admin sdk, and right after I try to insert an ACL using Acl: insert method from the google calendar api.

Similar to the following code:

const AdminService = google.admin({version: 'directory_v1'});
 try {
    const response = await AdminService.resources.calendars.insert(options); // options omitted
  } catch (error) {
    console.log(`Google Room Resource FAIL`);
    console.error(error.message);
  }

await new Promise((r) => setTimeout(r, 10000));

const CalendarService = google.calendar({version: 'v3'});
 try {
    const res = await CalendarService.acl.insert(option); // options omitted
    console.log(res);
  } catch (error) {
    console.log(error);
    throw new Error(error.message);
  }

As for the authentication, I am using a service account with the correct scopes which impersionates an admin user with the correct permissions. This is how I generate the required JWT token:

const generateJWT = async (scope:string[])=>{
const jwtClient = new google.auth.JWT(
      client_email, // service account
      undefined,
      private_key,
      scope,
      subject // admin user
  );
return jwtClient;
}

In the options parameter for each api call I directly acquire the token for the auth attribute like this:

const option = {
    'calendarId': acl.calendarId,
    'auth': await generateJWT('https://www.googleapis.com/auth/calendar'),
    'resource': {
      'role': acl.role,
      'scope': {
        'type': acl.scopeType,
        'value': acl.scopeValue,
      },
    },
  };

Since I await all api calls, I thought that I will only get the response back when everything is already propagated in Google Workspace but when I do not use the setTimeout in between I always get an Error: Not Found back.

First I had the timeout set to 5 seconds which worked until it didn't so I moved it up to 10 seconds. This worked quite long but now I get again sometimes the Not Found error back...

I don't like the setTimeout hack...and even less if it does not work reliable, so how should I deal with this asynchronous behavior without spinning up any other infrastructure like queues or similar?

0 Answers
Related