2022-03-24 04:44:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-04 11:23:48 +00:00
|
|
|
"flag"
|
2022-06-16 12:00:25 +00:00
|
|
|
"fmt"
|
2022-03-24 04:44:57 +00:00
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/config"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
|
2022-06-16 12:00:25 +00:00
|
|
|
"github.com/brianvoe/gofakeit/v6"
|
2023-04-16 22:31:03 +00:00
|
|
|
"github.com/lmika/dynamo-browse/internal/dynamo-browse/models"
|
|
|
|
"github.com/lmika/dynamo-browse/internal/dynamo-browse/providers/dynamo"
|
|
|
|
"github.com/lmika/dynamo-browse/internal/dynamo-browse/services/tables"
|
2022-03-24 04:44:57 +00:00
|
|
|
"github.com/lmika/gopkgs/cli"
|
2022-07-28 11:32:32 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-06-16 12:00:25 +00:00
|
|
|
"log"
|
2022-03-24 04:44:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2022-10-04 11:23:48 +00:00
|
|
|
var flagSeed = flag.Int64("seed", 0, "random seed to use")
|
|
|
|
var flagCount = flag.Int("count", 500, "number of items to produce")
|
|
|
|
flag.Parse()
|
|
|
|
|
2022-03-24 04:44:57 +00:00
|
|
|
ctx := context.Background()
|
2022-06-16 12:00:25 +00:00
|
|
|
tableName := "business-addresses"
|
2022-10-04 11:23:48 +00:00
|
|
|
totalItems := *flagCount
|
2022-03-24 04:44:57 +00:00
|
|
|
|
2022-03-28 10:07:11 +00:00
|
|
|
cfg, err := config.LoadDefaultConfig(ctx)
|
2022-03-24 04:44:57 +00:00
|
|
|
if err != nil {
|
|
|
|
cli.Fatalf("cannot load AWS config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dynamoClient := dynamodb.NewFromConfig(cfg,
|
2022-04-14 20:03:55 +00:00
|
|
|
dynamodb.WithEndpointResolver(dynamodb.EndpointResolverFromURL("http://localhost:4566")))
|
2022-03-24 04:44:57 +00:00
|
|
|
|
2022-07-28 11:32:32 +00:00
|
|
|
// Other tables
|
2023-01-23 10:23:46 +00:00
|
|
|
if err := createTable(ctx, dynamoClient, "user-accounts", false); err != nil {
|
2022-07-28 11:32:32 +00:00
|
|
|
log.Fatal(err)
|
2022-03-24 04:44:57 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 10:23:46 +00:00
|
|
|
if err := createTable(ctx, dynamoClient, "inventory", true); err != nil {
|
2022-07-28 11:32:32 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-01-23 10:23:46 +00:00
|
|
|
if err := createTable(ctx, dynamoClient, tableName, false); err != nil {
|
2022-07-28 11:32:32 +00:00
|
|
|
log.Fatal(err)
|
2022-03-24 04:44:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 21:17:52 +00:00
|
|
|
tableInfo := &models.TableInfo{
|
|
|
|
Name: tableName,
|
|
|
|
Keys: models.KeyAttribute{PartitionKey: "pk", SortKey: "sk"},
|
|
|
|
}
|
2023-01-23 10:23:46 +00:00
|
|
|
inventoryTableInfo := &models.TableInfo{
|
|
|
|
Name: "inventory",
|
|
|
|
Keys: models.KeyAttribute{PartitionKey: "pk", SortKey: "sk"},
|
|
|
|
}
|
2022-03-24 21:17:52 +00:00
|
|
|
|
2022-03-24 04:44:57 +00:00
|
|
|
dynamoProvider := dynamo.NewProvider(dynamoClient)
|
2022-09-29 12:10:18 +00:00
|
|
|
tableService := tables.NewService(dynamoProvider, notROService{})
|
2022-03-24 04:44:57 +00:00
|
|
|
|
2022-03-28 10:07:11 +00:00
|
|
|
_, _ = tableService, tableInfo
|
|
|
|
|
2022-10-04 11:23:48 +00:00
|
|
|
log.Printf("using seed: %v", *flagSeed)
|
|
|
|
gofakeit.Seed(*flagSeed)
|
|
|
|
|
2022-03-24 04:44:57 +00:00
|
|
|
for i := 0; i < totalItems; i++ {
|
2022-10-04 11:23:48 +00:00
|
|
|
key := gofakeit.UUID()
|
2022-03-24 21:17:52 +00:00
|
|
|
if err := tableService.Put(ctx, tableInfo, models.Item{
|
2022-06-16 12:00:25 +00:00
|
|
|
"pk": &types.AttributeValueMemberS{Value: key},
|
|
|
|
"sk": &types.AttributeValueMemberS{Value: key},
|
|
|
|
"name": &types.AttributeValueMemberS{Value: gofakeit.Name()},
|
|
|
|
"address": &types.AttributeValueMemberS{Value: gofakeit.Address().Address},
|
|
|
|
"city": &types.AttributeValueMemberS{Value: gofakeit.Address().City},
|
|
|
|
"phone": &types.AttributeValueMemberN{Value: gofakeit.Phone()},
|
|
|
|
"web": &types.AttributeValueMemberS{Value: gofakeit.URL()},
|
|
|
|
"officeOpened": &types.AttributeValueMemberBOOL{Value: gofakeit.Bool()},
|
2022-10-04 11:23:48 +00:00
|
|
|
"colors": &types.AttributeValueMemberM{
|
|
|
|
Value: map[string]types.AttributeValue{
|
|
|
|
"door": &types.AttributeValueMemberS{Value: gofakeit.Color()},
|
|
|
|
"front": &types.AttributeValueMemberS{Value: gofakeit.Color()},
|
|
|
|
},
|
|
|
|
},
|
2022-06-02 11:43:14 +00:00
|
|
|
"ratings": &types.AttributeValueMemberL{Value: []types.AttributeValue{
|
2022-06-16 12:00:25 +00:00
|
|
|
&types.AttributeValueMemberN{Value: fmt.Sprint(gofakeit.IntRange(0, 5))},
|
|
|
|
&types.AttributeValueMemberN{Value: fmt.Sprint(gofakeit.IntRange(0, 5))},
|
|
|
|
&types.AttributeValueMemberN{Value: fmt.Sprint(gofakeit.IntRange(0, 5))},
|
2022-06-02 11:43:14 +00:00
|
|
|
}},
|
2022-03-24 04:44:57 +00:00
|
|
|
}); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 05:35:43 +00:00
|
|
|
var key = gofakeit.UUID()
|
2023-01-23 10:23:46 +00:00
|
|
|
for i := 0; i < totalItems; i++ {
|
2023-04-14 05:35:43 +00:00
|
|
|
if i%50 == 0 {
|
|
|
|
key = gofakeit.UUID()
|
|
|
|
}
|
2023-01-23 10:23:46 +00:00
|
|
|
if err := tableService.Put(ctx, inventoryTableInfo, models.Item{
|
|
|
|
"pk": &types.AttributeValueMemberS{Value: key},
|
2023-04-14 05:35:43 +00:00
|
|
|
"sk": &types.AttributeValueMemberN{Value: fmt.Sprint(i % 50)},
|
2023-01-23 10:23:46 +00:00
|
|
|
"uuid": &types.AttributeValueMemberS{Value: gofakeit.UUID()},
|
|
|
|
}); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 04:44:57 +00:00
|
|
|
log.Printf("table '%v' created with %v items", tableName, totalItems)
|
|
|
|
}
|
2022-07-28 11:32:32 +00:00
|
|
|
|
2023-01-23 10:23:46 +00:00
|
|
|
func createTable(ctx context.Context, dynamoClient *dynamodb.Client, tableName string, skNumber bool) error {
|
2022-07-28 11:32:32 +00:00
|
|
|
if _, err := dynamoClient.DeleteTable(ctx, &dynamodb.DeleteTableInput{
|
|
|
|
TableName: aws.String(tableName),
|
|
|
|
}); err != nil {
|
|
|
|
log.Printf("warn: cannot delete table: %v: %v", tableName, err)
|
|
|
|
}
|
|
|
|
|
2023-01-23 10:23:46 +00:00
|
|
|
var skType = types.ScalarAttributeTypeS
|
|
|
|
if skNumber {
|
|
|
|
skType = types.ScalarAttributeTypeN
|
|
|
|
}
|
|
|
|
|
2022-07-28 11:32:32 +00:00
|
|
|
if _, err := dynamoClient.CreateTable(ctx, &dynamodb.CreateTableInput{
|
|
|
|
TableName: aws.String(tableName),
|
|
|
|
KeySchema: []types.KeySchemaElement{
|
|
|
|
{AttributeName: aws.String("pk"), KeyType: types.KeyTypeHash},
|
|
|
|
{AttributeName: aws.String("sk"), KeyType: types.KeyTypeRange},
|
|
|
|
},
|
|
|
|
AttributeDefinitions: []types.AttributeDefinition{
|
|
|
|
{AttributeName: aws.String("pk"), AttributeType: types.ScalarAttributeTypeS},
|
2023-01-23 10:23:46 +00:00
|
|
|
{AttributeName: aws.String("sk"), AttributeType: skType},
|
2022-07-28 11:32:32 +00:00
|
|
|
},
|
|
|
|
ProvisionedThroughput: &types.ProvisionedThroughput{
|
|
|
|
ReadCapacityUnits: aws.Int64(100),
|
|
|
|
WriteCapacityUnits: aws.Int64(100),
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
return errors.Wrapf(err, "cannot create table: %v", tableName)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-29 12:10:18 +00:00
|
|
|
|
|
|
|
type notROService struct{}
|
|
|
|
|
2022-09-30 12:28:59 +00:00
|
|
|
func (n notROService) DefaultLimit() int {
|
|
|
|
return 1000
|
|
|
|
}
|
|
|
|
|
2022-09-29 12:10:18 +00:00
|
|
|
func (n notROService) IsReadOnly() (bool, error) {
|
|
|
|
return false, nil
|
|
|
|
}
|