Created
September 5, 2018 09:17
-
-
Save lolgolflol/33e15236ac5548fcbb1b4cd645cdf51c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider" | |
"github.com/aws/aws-sdk-go/service/dynamodb" | |
) | |
func main() { | |
userTable := os.Getenv("USER_TABLE_NAME") | |
sess, _ := session.NewSessionWithOptions(session.Options{ | |
Profile: os.Getenv("AWS_PROFILE"), | |
}) | |
dbDynamodb := dynamodb.New(sess) | |
congnitoProvider := cognitoidentityprovider.New(sess) | |
queryUser := &dynamodb.ScanInput{ | |
// ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{ | |
// ":email": { | |
// S: aws.String("email"), | |
// }, | |
// ":phone_number": { | |
// S: aws.String("phone_number"), | |
// }, | |
// }, | |
TableName: aws.String(userTable), | |
FilterExpression: aws.String("(attribute_not_exists(email) or attribute_not_exists(phone_number)) and attribute_exists(username) "), | |
} | |
users, err := dbDynamodb.Scan(queryUser) | |
if err != nil { | |
fmt.Println(err.Error()) | |
} | |
for _, item := range users.Items { | |
input := &cognitoidentityprovider.AdminGetUserInput{ | |
UserPoolId: aws.String(os.Getenv("USER_POOL")), | |
Username: item["username"].S, | |
} | |
result, _ := congnitoProvider.AdminGetUser(input) | |
fmt.Printf("%#v", result) | |
var phoneNumber, email, subID string | |
for _, element := range result.UserAttributes { | |
if *(element.Name) == "phone_number" { | |
phoneNumber = *element.Value | |
} | |
if *(element.Name) == "email" { | |
email = *element.Value | |
} | |
if *(element.Name) == "sub" { | |
subID = *element.Value | |
} | |
} | |
fmt.Printf("%#v %#v %s", phoneNumber, email, subID) | |
if email != "" { | |
fmt.Println("Update") | |
updateDynamo(dbDynamodb, subID, phoneNumber, email) | |
} | |
} | |
fmt.Println("Success") | |
} | |
func updateDynamo(dbDynamodb *dynamodb.DynamoDB, subID string, phoneNumber string, email string) { | |
input := &dynamodb.UpdateItemInput{ | |
ExpressionAttributeNames: map[string]*string{ | |
"#phone_number": aws.String("phone_number"), | |
"#email": aws.String("email"), | |
}, | |
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{ | |
":phone_number": { | |
S: aws.String(phoneNumber), | |
}, | |
":email": { | |
S: aws.String(email), | |
}, | |
}, | |
Key: map[string]*dynamodb.AttributeValue{ | |
"id": { | |
S: aws.String(subID), | |
}, | |
"data_scope": { | |
S: aws.String("root"), | |
}, | |
}, | |
ReturnValues: aws.String("NONE"), | |
TableName: aws.String(os.Getenv("USER_TABLE_NAME")), | |
UpdateExpression: aws.String("SET #phone_number = :phone_number,#email = :email"), | |
} | |
_, err := dbDynamodb.UpdateItem(input) | |
if err != nil { | |
fmt.Println("Error: ", err.Error()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment