clearPrincipals: function()

in backend/src/cleanup/detachPrincipals.js [8:52]


  clearPrincipals: function (event, context, cb) {
    console.log("Event=", event);
    console.log("Context=", context);
    if (event.RequestType === 'Delete')  {
      var policyName = event.ResourceProperties.PolicyName;
      var failure = 0;

      var params = {
          policyName: policyName
      };
      iot.listPolicyPrincipals(params, function(err, data) {
          if (err) { 
              console.log("ERROR: listPolicyPrincipals API Call failed!");
              console.log(err, err.stack);
              sendResponse(event, context, "FAILED");
              failure = 1;
          } else {
              data.principals.forEach(function(principal){
                  var detachParams = {
                      policyName: policyName,
                      principal: principal
                  };
                  iot.detachPrincipalPolicy(detachParams, function(err2, data2){
                      if (err2) {
                         console.log("ERROR: detachPrincipalPolicy API Call failed!");
                         console.log(err2, err2.stack);
                         sendResponse(event, context, "FAILED");
                         failure = 1;
                      } else {
                          console.log(`Successfully detached ${principal} from ${policyName}`);
                      }
                  });
              });
          }
      });
      
      if ( failure === 0 ) {
         sendResponse(event, context, "SUCCESS");
      }
    } else {
      console.log("Detach not requested.");
      sendResponse(event, context, "SUCCESS");
    }

  }