Bulk configure multiple owners for apps

Bulk configuration of app meta during on-boarding

While implementing Torii, you can bulk load app data you're currently managing in spreadsheets, such as IT app owners and contract owners, rather than manually inputting this information within each and every app.

This guide documents a Google Sheets example of how to bulk upload your application data into Torii using our APIs and embedded Apps Script code. This example is similar to Bulk configure app details, but is focused on helping you update multiple app owners.

Below you will find a step-by-step configuration guide

Step 1 - Make a copy of the Google Sheet Example and make changes as required.

  1. Make a copy of the Bulk Update - Multiple App Owners sheet.

📘

Tip:

The Google Sheet examples all have read only permissions and should be copied into your own account.

  1. Select the Extensions menu item and then the Apps Script dropdown menu option.
  1. Detailed documentation of the code can be found in the Bulk Update App Owners via API recipe below, followed by the Apps Script code.

📘

Tip

The below App Script code can be modified as needed to support your requirements.

let API_KEY;

function onOpen() {
  let ui = SpreadsheetApp.getUi();
  ui.createMenu("Torii")
    .addItem("Sync Apps", "syncNow")
    .addItem("Reconfigure", "readConfig")
    .addToUi();
}

function syncNow() {
  let docProperties = PropertiesService.getDocumentProperties();
  API_KEY = docProperties.getProperty("api_key");

  if (!API_KEY) {
    readConfig();
    docProperties = PropertiesService.getDocumentProperties();
    API_KEY = docProperties.getProperty("api_key");
  }

  updateAttributes();
}

function readConfig() {
  let ui = SpreadsheetApp.getUi();
  let result = ui.prompt("Please enter Torii API Key");
  API_KEY = result.getResponseText();

  let docProperties = PropertiesService.getDocumentProperties();
  docProperties.setProperty("api_key", API_KEY);
}



function updateAttributes() {
  let sheet = SpreadsheetApp.getActiveSheet();
  let data = sheet.getDataRange().getValues();
  const userMap = new Map();

  Logger.log(SpreadsheetApp.getActiveSheet().getName());
  Logger.log(data);
  data.shift();
  row = 1;


  data.forEach(function (col) {
    row++;
    let applicationID = col[1];
    let updatedInTorii = col[2];
    let appOwners = col[3];
    let allAppOwnerEmails = [];


    if (applicationID && updatedInTorii != "Yes") {

      //Application Owners CODE
      let allAppOwnerEmails = appOwners.split(',');
      let primaryOwnerID = null;
      let secondaryOwnerID = null;
      let allOwnersIDs = [];

      for (let i = 0; i < allAppOwnerEmails.length; i++) {
        if (i == 0) {
          primaryOwnerID = getUserEmailId(allAppOwnerEmails[0]);
          if (primaryOwnerID == null && allAppOwnerEmails[0] != "") {
            let jsonResponse = JSON.parse(getUser(allAppOwnerEmails[0]));
            let userCount = Object.keys(jsonResponse.users).length;

            if (userCount == 1) {
              primaryOwnerID = jsonResponse.users[0].id;
              allOwnersIDs.push(primaryOwnerID);
              storeUserData(allAppOwnerEmails[0], primaryOwnerID);
              console.log(primaryOwnerID);
            } else {
              console.log("User " + primaryOwnerID + " not found");
            }
          } else {
            allOwnersIDs.push(primaryOwnerID);
          }
        } else {
          secondaryOwnerID = getUserEmailId(allAppOwnerEmails[i]);
          if (secondaryOwnerID == null && allAppOwnerEmails[i] != "") {
            let jsonResponse = JSON.parse(getUser(allAppOwnerEmails[i]));
            let userCount = Object.keys(jsonResponse.users).length;

            if (userCount == 1) {
              secondaryOwnerID = jsonResponse.users[0].id;
              allOwnersIDs.push(secondaryOwnerID);
              storeUserData(allAppOwnerEmails[0], secondaryOwnerID);
              console.log(secondaryOwnerID);
            } else {
              console.log("User " + secondaryOwnerID + " not found");
            }
          } else {
            allOwnersIDs.push(primaryOwnerID);
          }


        }

      }
     if(primaryOwnerID){
      let url = "https://api.toriihq.com/v1.0/apps/" + applicationID;

      response = UrlFetchApp.fetch(url, {
        muteHttpExceptions: true,
        method: "put",
        contentType: "application/json",
        payload: JSON.stringify({
          primaryOwner: primaryOwnerID,
          appOwners: allOwnersIDs
        }),
        headers: {
          Authorization: `Bearer ${API_KEY}`,
        },
      });
      if (response.getResponseCode() == 200) {
        sheet.getRange(row, 3).setValue("Yes");
      } else {
        sheet.getRange(row, 3).setValue(JSON.parse(response).message);
      }
      console.log(response.getResponseCode());
      Logger.log(response);

     } else {
      sheet.getRange(row, 3).setValue("App Owner Not Found");
     }

    }
  });

  function storeUserData(email, id) {
    userMap.set(email, id);
  }

  function getUserEmailId(email) {
    return userMap.get(email) || null;
  }

  function getUser(Email) {
    let url = "https://api.toriihq.com/v1.0/users?email=" + Email;
    let response = UrlFetchApp.fetch(url, {
      muteHttpExceptions: false,
      method: "get",
      contentType: "application/json",
      headers: { Authorization: `Bearer ${API_KEY}` },
    });
    // Logger.log(response);

    return response;
  }

  function getAppFields() {
    let url = "https://api.toriihq.com/v1.0/apps/fields";
    let response = UrlFetchApp.fetch(url, {
      muteHttpExceptions: false,
      method: "get",
      contentType: "application/json",
      headers: { Authorization: `Bearer ${API_KEY}` },
    });
    // Logger.log(response);

    return response;
  }


}
  1. Update your Google Sheet to reflect the two required fields below:
    Update the Name column with the application names you'd like to important the information from.
    Then, update the Owner column with the email addresses of the application owners or IT owners.

📘

Tips

If additional fields are required they should be added and the code should be refactored based on the additional fields.

The recipe referenced in the previous step will provide detailed documentation of the code to help you make changes to suit your requirements.

Step 2 - Sync the data from the Google Sheet to Torii

  1. Select Torii from the lop line menu item and then Sync Apps to run the code and push the data into Torii using the API.

🚧

The first time you run Sync Now the embedded Apps Script will request permission for the code to run.

Authorization should be granted as shown below. You will need to re-select Sync Now once authorization has been granted.

  1. Enter your API Key when prompted. If you do not have one, please refer to the Getting Started with Torii API guide.

Step 3 - Check the Torii console to verify that the app data has been updated

  1. Access the Applications tab in the Torii platform, find the applications you've bulk uploaded data for, and verify the app owners match the ones you've listed in your Google Sheet.

👍

Congratulations! At this point, you should have successfully bulk loaded one or more app owners for each app's details in Torii.