tvaultapi/src/main/java/com/tmobile/cso/vault/api/service/IAMServiceAccountsService.java
private ResponseEntity<String> rollBackIAMOnboardOnFailure(IAMServiceAccount iamServiceAccount,
				String iamSvcAccName, String onAction, UserDetails userDetails) {
		log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
				.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
				.put(LogMessage.ACTION, "rollBackIAMOnboardOnFailure")
				.put(LogMessage.MESSAGE,
						String.format("Trying to rollback IAM Service Account [%s]", iamServiceAccount.getUserName()))
				.put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build()));
		String selfSupportToken = tokenUtils.getSelfServiceToken();
		//Delete the IAM Service account policies
		deleteIAMServiceAccountPolicies(selfSupportToken, iamSvcAccName);

		// delete group association if exists
		if(!StringUtils.isEmpty(iamServiceAccount.getAdSelfSupportGroup())) {
			Map<String, String> groups = new HashedMap();
			groups.put(iamServiceAccount.getAdSelfSupportGroup(), IAMServiceAccountConstants.IAM_WRITE_PERMISSION_STRING);
			updateGroupPolicyAssociationOnIAMSvcaccDelete(iamServiceAccount.getAwsAccountId() + "_" + iamServiceAccount.getUserName(), groups, selfSupportToken, userDetails);
		}

		//Deleting the IAM service account metadata
		OnboardedIAMServiceAccount iamSvcAccToRevert = new OnboardedIAMServiceAccount(
				iamServiceAccount.getUserName(), iamServiceAccount.getAwsAccountId(),
				iamServiceAccount.getOwnerNtid());
		ResponseEntity<String> iamMetaDataDeletionResponse = deleteIAMSvcAccount(selfSupportToken, iamSvcAccToRevert);
		if (iamMetaDataDeletionResponse != null
				&& HttpStatus.OK.equals(iamMetaDataDeletionResponse.getStatusCode())) {
			if (onAction.equals("onPolicyFailure")) {
				return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
						"{\"errors\":[\"Failed to onboard IAM service account. Policy creation failed.\"]}");
			}
			else if (onAction.equals("onSecretMountCreationFailure")) {
				return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
						"{\"errors\":[\"Failed to onboard IAM service account. Updating legacy accesses key ids failed.\"]}");
			}
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
					"{\"errors\":[\"Failed to onboard IAM service account. Association of owner permission failed\"]}");
		} else {
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
					"{\"errors\":[\"Failed to create IAM Service Account policies. Reverting IAM service account creation also failed.\"]}");
		}
	}
/**
	 * Method to rollback IAM service account onboarding process on failure.
	 * @param iamServiceAccount
	 * @param iamSvcAccName
	 * @param onAction
	 * @return
	 */
Method to rollback IAM service account onboarding process on failure.
Method to rollback IAM service account onboarding process on failure .
private ResponseEntity<String> rollBackIAMOnboardOnFailure(IAMServiceAccount iamServiceAccount,
				String iamSvcAccName, String onAction, UserDetails userDetails) {
		log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
				.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
				.put(LogMessage.ACTION, "rollBackIAMOnboardOnFailure")
				.put(LogMessage.MESSAGE,
						String.format("Trying to rollback IAM Service Account [%s]", iamServiceAccount.getUserName()))
				.put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build()));
		String selfSupportToken = tokenUtils.getSelfServiceToken();
		deleteIAMServiceAccountPolicies(selfSupportToken, iamSvcAccName);
		if(!StringUtils.isEmpty(iamServiceAccount.getAdSelfSupportGroup())) {
			Map<String, String> groups = new HashedMap();
			groups.put(iamServiceAccount.getAdSelfSupportGroup(), IAMServiceAccountConstants.IAM_WRITE_PERMISSION_STRING);
			updateGroupPolicyAssociationOnIAMSvcaccDelete(iamServiceAccount.getAwsAccountId() + "_" + iamServiceAccount.getUserName(), groups, selfSupportToken, userDetails);
		}
		OnboardedIAMServiceAccount iamSvcAccToRevert = new OnboardedIAMServiceAccount(
				iamServiceAccount.getUserName(), iamServiceAccount.getAwsAccountId(),
				iamServiceAccount.getOwnerNtid());
		ResponseEntity<String> iamMetaDataDeletionResponse = deleteIAMSvcAccount(selfSupportToken, iamSvcAccToRevert);
		if (iamMetaDataDeletionResponse != null
				&& HttpStatus.OK.equals(iamMetaDataDeletionResponse.getStatusCode())) {
			if (onAction.equals("onPolicyFailure")) {
				return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
						"{\"errors\":[\"Failed to onboard IAM service account. Policy creation failed.\"]}");
			}
			else if (onAction.equals("onSecretMountCreationFailure")) {
				return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
						"{\"errors\":[\"Failed to onboard IAM service account. Updating legacy accesses key ids failed.\"]}");
			}
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
					"{\"errors\":[\"Failed to onboard IAM service account. Association of owner permission failed\"]}");
		} else {
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
					"{\"errors\":[\"Failed to create IAM Service Account policies. Reverting IAM service account creation also failed.\"]}");
		}
	}
private ResponseEntity < String > rollBackIAMOnboardOnFailure ( IAMServiceAccount iamServiceAccount , String iamSvcAccName , String onAction , UserDetails userDetails ) { log . debug ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , "rollBackIAMOnboardOnFailure" ) . put ( LogMessage . MESSAGE , String . format ( "Trying to rollback IAM Service Account [%s]" , iamServiceAccount . getUserName ( ) ) ) . put ( LogMessage . APIURL , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . APIURL ) ) . build ( ) ) ) ; String selfSupportToken = tokenUtils . getSelfServiceToken ( ) ; deleteIAMServiceAccountPolicies ( selfSupportToken , iamSvcAccName ) ; if ( ! StringUtils . isEmpty ( iamServiceAccount . getAdSelfSupportGroup ( ) ) ) { Map < String , String > groups = new HashedMap ( ) ; groups . put ( iamServiceAccount . getAdSelfSupportGroup ( ) , IAMServiceAccountConstants . IAM_WRITE_PERMISSION_STRING ) ; updateGroupPolicyAssociationOnIAMSvcaccDelete ( iamServiceAccount . getAwsAccountId ( ) + "_" + iamServiceAccount . getUserName ( ) , groups , selfSupportToken , userDetails ) ; } OnboardedIAMServiceAccount iamSvcAccToRevert = new OnboardedIAMServiceAccount ( iamServiceAccount . getUserName ( ) , iamServiceAccount . getAwsAccountId ( ) , iamServiceAccount . getOwnerNtid ( ) ) ; ResponseEntity < String > iamMetaDataDeletionResponse = deleteIAMSvcAccount ( selfSupportToken , iamSvcAccToRevert ) ; if ( iamMetaDataDeletionResponse != null && HttpStatus . OK . equals ( iamMetaDataDeletionResponse . getStatusCode ( ) ) ) { if ( onAction . equals ( "onPolicyFailure" ) ) { return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( "{\"errors\":[\"Failed to onboard IAM service account. Policy creation failed.\"]}" ) ; } else if ( onAction . equals ( "onSecretMountCreationFailure" ) ) { return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( "{\"errors\":[\"Failed to onboard IAM service account. Updating legacy accesses key ids failed.\"]}" ) ; } return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( "{\"errors\":[\"Failed to onboard IAM service account. Association of owner permission failed\"]}" ) ; } else { return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( "{\"errors\":[\"Failed to create IAM Service Account policies. Reverting IAM service account creation also failed.\"]}" ) ; } }
Method to rollback IAM service account onboarding process on failure.
Method to rollback IAM service account onboarding process on failure .
//Delete the IAM Service account policies // delete group association if exists //Deleting the IAM service account metadata