/
remove_dummy_data.go
115 lines (102 loc) · 3.3 KB
/
remove_dummy_data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/ONSdigital/dp-identity-api/cognito"
"github.com/ONSdigital/dp-identity-api/config"
"github.com/ONSdigital/dp-identity-api/models"
"github.com/ONSdigital/dp-identity-api/service"
"github.com/ONSdigital/log.go/v2/log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
"github.com/pkg/errors"
)
const RemovalLocalUserPoolName = "local-florence-users"
const UserRemovalCount = 2000
const GroupRemovalCount = 200
func main() {
ctx := context.Background()
if err := runUserAndGroupsRemove(ctx); err != nil {
log.Fatal(ctx, "fatal runtime error", err)
os.Exit(1)
}
}
func runUserAndGroupsRemove(ctx context.Context) error {
svcList := service.NewServiceList(&service.Init{})
cfg, err := config.Get()
if err != nil {
return errors.Wrap(err, "error getting configuration")
}
cognitoClient := svcList.GetCognitoClient(cfg.AWSRegion)
err = checkPoolExistsAndIsLocalForRemove(ctx, cognitoClient, cfg.AWSCognitoUserPoolID)
if err != nil {
return errors.Wrap(err, "error checking user pool details")
}
backoffSchedule := []time.Duration{
1 * time.Second,
3 * time.Second,
10 * time.Second,
}
deleteUsers(ctx, cognitoClient, cfg.AWSCognitoUserPoolID, backoffSchedule)
deleteGroups(ctx, cognitoClient, cfg.AWSCognitoUserPoolID, backoffSchedule)
return nil
}
func checkPoolExistsAndIsLocalForRemove(ctx context.Context, client cognito.Client, userPoolId string) error {
input := cognitoidentityprovider.DescribeUserPoolInput{
UserPoolId: aws.String(userPoolId),
}
userPoolDetails, err := client.DescribeUserPool(&input)
if err != nil {
return models.NewCognitoError(ctx, err, "loading User Pool details for dummy data population")
}
if *userPoolDetails.UserPool.Name != RemovalLocalUserPoolName {
return models.NewValidationError(ctx, models.InvalidUserPoolError, models.InvalidUserPoolDescription)
}
return nil
}
func deleteUsers(ctx context.Context, client cognito.Client, userPoolId string, backoffSchedule []time.Duration) {
baseUsername := "test-user-"
for i := range [UserRemovalCount]int{} {
for _, backoff := range backoffSchedule {
username := baseUsername + fmt.Sprint(i)
userDeletionInput := cognitoidentityprovider.AdminDeleteUserInput{
UserPoolId: &userPoolId,
Username: &username,
}
_, awsErr := client.AdminDeleteUser(&userDeletionInput)
if awsErr != nil {
err := models.NewCognitoError(ctx, awsErr, "AdminDeleteUser during dummy data creation")
if err.Code != models.TooManyRequestsError {
break
}
} else {
break
}
time.Sleep(backoff)
}
}
}
func deleteGroups(ctx context.Context, client cognito.Client, userPoolId string, backoffSchedule []time.Duration) {
baseGroupName := "test-group-"
for i := range [GroupRemovalCount]int{} {
for _, backoff := range backoffSchedule {
groupName := baseGroupName + fmt.Sprint(i)
groupDeletionInput := cognitoidentityprovider.DeleteGroupInput{
GroupName: &groupName,
UserPoolId: &userPoolId,
}
_, awsErr := client.DeleteGroup(&groupDeletionInput)
if awsErr != nil {
err := models.NewCognitoError(ctx, awsErr, "AdminDeleteUser during dummy data creation")
if err.Code != models.TooManyRequestsError {
break
}
} else {
break
}
time.Sleep(backoff)
}
}
}