Skip to content

Instantly share code, notes, and snippets.

@guizmaii
Last active September 29, 2017 15:02
Show Gist options
  • Save guizmaii/8881c3c0989d3d5e9ce66eb4685638ec to your computer and use it in GitHub Desktop.
Save guizmaii/8881c3c0989d3d5e9ce66eb4685638ec to your computer and use it in GitHub Desktop.
Ammonite (http://ammonite.io/) script to remove all identities in a AWS Cognito Identity Pool
#!/usr/local/bin/amm
import sys.process._
import collection.mutable
import ammonite.ops._
import ammonite.ops.ImplicitWd._
import $ivy.`com.amazonaws:aws-java-sdk:1.11.204`
import com.amazonaws.services.cognitoidp._
import com.amazonaws.services.cognitoidp.model._
import collection.JavaConverters._
val usageText =
"""
| DESCRIPTION
| Remove all users in a Cognito Identity Pool
|
| USAGE
| $ amm empty_cognito_user_pool.sc <command> <pool-id> <region>
|
| COMMANDS
| help print this help message
| fetch fetch all identities without delete them
| delete delete all identities
|
""".stripMargin
def usage(): Unit = println(usageText)
def fetch(poolId: String, client: AWSCognitoIdentityProvider): List[UserType] = {
var users: List[UserType] = List.empty
var paginationToken: String = null
do {
val request = new ListUsersRequest()
.withUserPoolId(poolId)
.withLimit(60)
if (paginationToken != null) {
request.setPaginationToken(paginationToken)
}
val res = client.listUsers(request)
paginationToken = res.getPaginationToken
users = users ++ res.getUsers.asScala.toList
} while (paginationToken != null)
println(s"nb users: ${users.size}")
users
}
def deleteAll(poolId: String, client: AWSCognitoIdentityProvider) = {
def deleteUser(username: String): AdminDeleteUserResult = {
val deleteRequest = new AdminDeleteUserRequest()
deleteRequest.setUsername(username)
deleteRequest.setUserPoolId(poolId)
client.adminDeleteUser(deleteRequest)
}
fetch(poolId, client).map(_.getUsername).map(deleteUser)
}
def buildClient(region: String): AWSCognitoIdentityProvider =
AWSCognitoIdentityProviderClientBuilder
.standard()
.withRegion(region)
.build()
@main
def main(args: String*) = args.toList match {
case Nil | List("help") => usage()
case List("fetch", poolId, region) => fetch(poolId, buildClient(region)); println("DONE")
case List("delete", poolId, region) => deleteAll(poolId, buildClient(region)); println("DONE")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment