Esempio n. 1
1
func GetItemFromDynamoDb(svc *dynamodb.DynamoDB, productId string) string {
	params := &dynamodb.GetItemInput{
		Key: map[string]*dynamodb.AttributeValue{ // Required
			"productId": { // Required
				S: aws.String(productId),
			},
			// More values...
		},
		TableName: aws.String("ProductRecommendation"), // Required
		AttributesToGet: []*string{
			aws.String("boughtTogether"), // Required
			// More values...
		},
		ConsistentRead: aws.Bool(true),
		/*
			ExpressionAttributeNames: map[string]*string{
				"Key": aws.String("AttributeName"), // Required
				// More values...
			},
			ProjectionExpression:   aws.String("ProjectionExpression"),
			ReturnConsumedCapacity: aws.String("ReturnConsumedCapacity"),
		*/
	}
	resp, err := svc.GetItem(params)

	if err != nil {
		// Print the error, cast err to awserr.Error to get the Code and
		// Message from an error.
		glog.Errorln(err.Error())
		return "failed to query DynamoDb"
	}

	// Pretty-print the response data.
	return *resp.Item["boughtTogether"].S
}
Esempio n. 2
1
func getServices(containerID string, svc *dynamodb.DynamoDB) ([]*Service, error) {
	context := Context{}
	params := &dynamodb.QueryInput{
		TableName: aws.String("Services"),
		KeyConditions: map[string]*dynamodb.Condition{
			"ContainerID": {
				ComparisonOperator: aws.String("EQ"), // Required
				AttributeValueList: []*dynamodb.AttributeValue{
					{
						S: aws.String(containerID),
					},
				},
			},
		},
	}
	resp, err := svc.Query(params)

	if err != nil {
		return context, err
	}
	// loop through the items and convert back to struct
	for _, item := range resp.Items {
		service := Service{}
		dynamodbattribute.ConvertFromMap(item, &service)
		context = append(context, &service)
	}
	return context, err
}
Esempio n. 3
1
// CheckRowCount returns the number of records in a table.
func CheckRowCount(db *dynamodb.DynamoDB, tableName string) int {
	resp, err := db.Scan(&dynamodb.ScanInput{
		TableName: aws.String(tableName),
		Select:    aws.String(dynamodb.SelectCount),
	})
	if err != nil {
		return -1
	}
	return int(*resp.Count)
}
func CreateThreadTable(svc *dynamodb.DynamoDB) (*dynamodb.CreateTableOutput, error) {

	params := &dynamodb.CreateTableInput{
		TableName: aws.String("Thread"),
		AttributeDefinitions: []*dynamodb.AttributeDefinition{
			{
				AttributeName: aws.String("ForumName"),
				AttributeType: aws.String("S"),
			},
			{
				AttributeName: aws.String("Subject"),
				AttributeType: aws.String("S"),
			},
		},
		KeySchema: []*dynamodb.KeySchemaElement{
			{
				AttributeName: aws.String("ForumName"),
				KeyType:       aws.String("HASH"),
			},
			{
				AttributeName: aws.String("Subject"),
				KeyType:       aws.String("RANGE"),
			},
		},
		ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
			ReadCapacityUnits:  aws.Int64(10),
			WriteCapacityUnits: aws.Int64(5),
		},
	}

	return svc.CreateTable(params)
}
Esempio n. 5
0
func CreateAdminTable() {
	var svc *dynamodb.DynamoDB = dbutil.CreateDynamoDBClient()

	params := &dynamodb.CreateTableInput{
		AttributeDefinitions: []*dynamodb.AttributeDefinition{
			{
				AttributeName: aws.String("AdminID"),
				AttributeType: aws.String("S"),
			},
		},
		KeySchema: []*dynamodb.KeySchemaElement{
			{
				AttributeName: aws.String("AdminID"),
				KeyType:       aws.String("HASH"),
			},
		},
		ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
			ReadCapacityUnits:  aws.Int64(1),
			WriteCapacityUnits: aws.Int64(1),
		},
		TableName: aws.String(AdminTableName),
	}

	resp, err := svc.CreateTable(params)
	if err != nil {
		log.Fatal(err)
	}

	log.Info(resp)
}
Esempio n. 6
0
func fixturePost(db *dynamodb.DynamoDB) error {
	params := &dynamodb.PutItemInput{
		Item: map[string]*dynamodb.AttributeValue{
			"id": {
				S: aws.String("pid123"),
			},
			"uid": {
				S: aws.String("uid123"),
			},
			"wall_id": {
				S: aws.String("1"),
			},
			"message": {
				S: aws.String("message"),
			},
			"created_at": {
				N: aws.String(strconv.FormatInt(time.Now().UnixNano(), 10)),
			},
		},
		TableName: aws.String("post"),
	}
	_, err := db.PutItem(params)
	if err != nil {
		return err
	}

	return nil
}
Esempio n. 7
0
func fixtureUser(db *dynamodb.DynamoDB) error {
	params := &dynamodb.PutItemInput{
		Item: map[string]*dynamodb.AttributeValue{
			"id": {
				S: aws.String("uid123"),
			},
			"oauthid": {
				S: aws.String("google:1234"),
			},
			"email": {
				S: aws.String("*****@*****.**"),
			},
			"username": {
				S: aws.String("username"),
			},
			"created_at": {
				N: aws.String("123456789"),
			},
			"lastlogin": {
				N: aws.String("123456789"),
			},
		},
		TableName: aws.String("user"),
	}
	_, err := db.PutItem(params)

	if err != nil {
		return err
	}

	return nil
}
Esempio n. 8
0
func deleteTable(db *dynamodb.DynamoDB, table string) error {
	params := &dynamodb.DeleteTableInput{
		TableName: aws.String(table),
	}
	_, err := db.DeleteTable(params)
	if err != nil {
		return err
	}
	return nil

}
Esempio n. 9
0
func CreateAppTable() {
	var svc *dynamodb.DynamoDB = dbutil.CreateDynamoDBClient()

	params := &dynamodb.CreateTableInput{
		AttributeDefinitions: []*dynamodb.AttributeDefinition{
			{
				AttributeName: aws.String("ClientID"),
				AttributeType: aws.String("S"),
			},
			{
				AttributeName: aws.String("DeveloperEmail"),
				AttributeType: aws.String("S"),
			},
		},
		KeySchema: []*dynamodb.KeySchemaElement{
			{
				AttributeName: aws.String("ClientID"),
				KeyType:       aws.String("HASH"),
			},
		},
		ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
			ReadCapacityUnits:  aws.Int64(1),
			WriteCapacityUnits: aws.Int64(1),
		},
		TableName: aws.String(ApplicationTableName),
		GlobalSecondaryIndexes: []*dynamodb.GlobalSecondaryIndex{
			{ // Required
				IndexName: aws.String("EMail-Index"),
				KeySchema: []*dynamodb.KeySchemaElement{
					{ // Required
						AttributeName: aws.String("DeveloperEmail"),
						KeyType:       aws.String("HASH"),
					},
					// More values...
				},
				Projection: &dynamodb.Projection{
					ProjectionType: aws.String("ALL"),
				},
				ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
					ReadCapacityUnits:  aws.Int64(1),
					WriteCapacityUnits: aws.Int64(1),
				},
			},
		},
	}

	resp, err := svc.CreateTable(params)
	if err != nil {
		log.Fatal(err)
	}

	log.Info(resp)
}
Esempio n. 10
0
func putResource(svc *dynamodb.DynamoDB, resource Resource, tableName string) error {
	item, err := dynamodbattribute.ConvertToMap(resource)
	if err != nil {
		return err
	}
	_, err = svc.PutItem(&dynamodb.PutItemInput{
		Item:      item,
		TableName: aws.String(tableName),
	})
	return err

}
// prepareDynamoDBTable prepares DynamoDB table and it returns table name.
func prepareDynamoDBTable(dynamodbClient *dynamodb.DynamoDB) string {
	dummyTableName := randSeq(10)

	input := newTestCreateTableInput(dummyTableName)
	dynamodbClient.CreateTable(input)

	dynamodbClient.WaitUntilTableExists(&dynamodb.DescribeTableInput{
		TableName: aws.String(dummyTableName),
	})

	return dummyTableName
}
Esempio n. 12
0
func createUserTable(db *dynamodb.DynamoDB) error {
	params := &dynamodb.CreateTableInput{
		TableName: aws.String("user"),
		KeySchema: []*dynamodb.KeySchemaElement{
			{
				AttributeName: aws.String("id"),
				KeyType:       aws.String("HASH"),
			},
		},
		AttributeDefinitions: []*dynamodb.AttributeDefinition{
			{
				AttributeName: aws.String("id"),
				AttributeType: aws.String("S"),
			},
			{
				AttributeName: aws.String("oauthid"),
				AttributeType: aws.String("S"),
			},
		},
		ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
			ReadCapacityUnits:  aws.Int64(1),
			WriteCapacityUnits: aws.Int64(1),
		},
		GlobalSecondaryIndexes: []*dynamodb.GlobalSecondaryIndex{
			{ // Required
				IndexName: aws.String("AuthIDIndex"),
				KeySchema: []*dynamodb.KeySchemaElement{
					{ // Required
						AttributeName: aws.String("oauthid"),
						KeyType:       aws.String("HASH"),
					},
				},
				Projection: &dynamodb.Projection{
					ProjectionType: aws.String("INCLUDE"),
					NonKeyAttributes: []*string{
						aws.String("id"),
					},
				},
				ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
					ReadCapacityUnits:  aws.Int64(1),
					WriteCapacityUnits: aws.Int64(1),
				},
			},
		},
	}
	_, err := db.CreateTable(params)
	if err != nil {
		return err
	}
	return nil
}
Esempio n. 13
0
func createTable(db *dynamodb.DynamoDB, createParams *dynamodb.CreateTableInput) error {
	_, err := db.CreateTable(createParams)
	if err != nil {
		return err
	}

	describeParams := &dynamodb.DescribeTableInput{
		TableName: aws.String(*createParams.TableName),
	}
	if err := db.WaitUntilTableExists(describeParams); err != nil {
		return err
	}

	return nil
}
Esempio n. 14
0
// CheckRows returns a Row accessor for every row in a table. The order of rows
// is unspecified. If an error occurs, the rows will be nil. The returned
// ValueDefiner can be used to define access to custom types across all rows.
func CheckRows(db *dynamodb.DynamoDB, tableName string) ([]Row, ValueDefiner) {
	resp, err := db.Scan(&dynamodb.ScanInput{
		TableName: aws.String(tableName),
	})
	if err != nil {
		return nil, nil
	}
	vd := newValueDefiner()
	rows := make([]Row, len(resp.Items))
	for i, item := range resp.Items {
		reader := valueReader{item, vd}
		rows[i] = Row{reader}
	}
	return rows, vd
}
Esempio n. 15
0
func mockCRCResponse(svc *dynamodb.DynamoDB, status int, body, crc string) (req *aws.Request) {
	header := http.Header{}
	header.Set("x-amz-crc32", crc)

	req, _ = svc.ListTablesRequest(nil)
	req.Handlers.Send.PushBack(func(*aws.Request) {
		req.HTTPResponse = &http.Response{
			StatusCode: status,
			Body:       ioutil.NopCloser(bytes.NewReader([]byte(body))),
			Header:     header,
		}
	})
	req.Send()
	return
}
func getRecord(svc *dynamodb.DynamoDB, tableName string, key string) {
	params := &dynamodb.GetItemInput{
		Key: map[string]*dynamodb.AttributeValue{
			"key": {
				S: aws.String(key),
			},
		},
		TableName: aws.String(tableName),
	}
	_, err := svc.GetItem(params)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
}
func UploadForumData(svc *dynamodb.DynamoDB) (*dynamodb.BatchWriteItemOutput, error) {
	params := &dynamodb.BatchWriteItemInput{
		RequestItems: map[string][]*dynamodb.WriteRequest{
			"Forum": {
				&dynamodb.WriteRequest{
					PutRequest: &dynamodb.PutRequest{
						Item: map[string]*dynamodb.AttributeValue{
							"Name": {
								S: aws.String("Amazon DynamoDB"),
							},
							"Category": {
								S: aws.String("Amazon Web Services"),
							},
							"Threads": {
								N: aws.String("0"),
							},
							"Messages": {
								N: aws.String("0"),
							},
							"Views": {
								N: aws.String("1"),
							},
						},
					},
				},
				&dynamodb.WriteRequest{
					PutRequest: &dynamodb.PutRequest{
						Item: map[string]*dynamodb.AttributeValue{
							"Name": {
								S: aws.String("Amazon S3"),
							},
							"Category": {
								S: aws.String("Amazon Web Services"),
							},
							"Threads": {
								N: aws.String("0"),
							},
						},
					},
				},
			},
		},
	}

	return svc.BatchWriteItem(params)
}
Esempio n. 18
0
func deleteTable(db *dynamodb.DynamoDB, tableName string) error {
	params := &dynamodb.DeleteTableInput{
		TableName: aws.String(tableName),
	}
	_, err := db.DeleteTable(params)
	if err != nil {
		return err
	}

	describeParams := &dynamodb.DescribeTableInput{
		TableName: aws.String(tableName),
	}
	if err := db.WaitUntilTableNotExists(describeParams); err != nil {
		return err
	}

	return nil
}
Esempio n. 19
0
func handleRemove(subject string) {
	fmt.Println("Remove admin", subject)

	var client *dynamodb.DynamoDB = dbutil.CreateDynamoDBClient()
	params := &dynamodb.DeleteItemInput{
		TableName: aws.String(ddl.AdminTableName),
		Key: map[string]*dynamodb.AttributeValue{
			"AdminID": {S: aws.String(subject)},
		},
	}

	_, err := client.DeleteItem(params)

	if err != nil {
		fmt.Println(err.Error())
		return
	}
}
Esempio n. 20
0
func handleList() {
	var client *dynamodb.DynamoDB = dbutil.CreateDynamoDBClient()
	params := &dynamodb.ScanInput{
		TableName: aws.String(ddl.AdminTableName),
		AttributesToGet: []*string{
			aws.String("AdminID"),
		},
	}

	resp, err := client.Scan(params)

	if err != nil {
		fmt.Println(err.Error())
		return
	}

	fmt.Println(resp)
}
Esempio n. 21
0
func teardownTables(t *testing.T, db *dynamodb.DynamoDB) {

	resp, err := db.ListTables(&dynamodb.ListTablesInput{})
	if err != nil {
		t.Fatalf("teardown tables list: %s", err.Error())
	}
	for _, table := range resp.TableNames {
		_, err = db.DeleteTable(&dynamodb.DeleteTableInput{
			TableName: table,
		})
		if err != nil {
			t.Logf("teardown table %s: %s", *table, err.Error())
		}
	}
	if err != nil {
		t.FailNow()
	}
}
Esempio n. 22
0
func handleAdd(subject string) {
	fmt.Println("Add admin", subject)

	var client *dynamodb.DynamoDB = dbutil.CreateDynamoDBClient()
	params := &dynamodb.PutItemInput{
		TableName: aws.String(ddl.AdminTableName),
		Item: map[string]*dynamodb.AttributeValue{
			"AdminID": {S: aws.String(subject)},
		},
		Expected: map[string]*dynamodb.ExpectedAttributeValue{
			"AdminID": {Exists: aws.Bool(false)},
		},
	}
	_, err := client.PutItem(params)

	if err != nil {
		fmt.Println(err.Error())
		return
	}
}
Esempio n. 23
0
// make sure session table exists
func tryCreateTable(db *dynamodb.DynamoDB, sessionTableName string, readCapacityUnits, writeCapacityUnits int64) error {

	describeTableInput := &dynamodb.DescribeTableInput{TableName: aws.String(sessionTableName)}
	if _, err := db.DescribeTable(describeTableInput); err != nil {
		awserr := err.(awserr.Error)
		if awserr.Code() == "ResourceNotFoundException" {
			// table does not exist - create now

			params := &dynamodb.CreateTableInput{
				TableName: aws.String(sessionTableName),
				AttributeDefinitions: []*dynamodb.AttributeDefinition{ // Required
					{ // Required
						AttributeName: aws.String("id"),
						AttributeType: aws.String("S"), // Required
					},
				},
				KeySchema: []*dynamodb.KeySchemaElement{ // Required
					{ // Required
						AttributeName: aws.String("id"),
						KeyType:       aws.String("HASH"), // Required
					},
				},
				ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
					ReadCapacityUnits:  aws.Int64(readCapacityUnits),
					WriteCapacityUnits: aws.Int64(writeCapacityUnits),
				},
			}

			if _, err := db.CreateTable(params); err != nil {
				return err
			}

		} else {
			return err
		}
	}

	return nil

}
Esempio n. 24
0
func DeleteTable(tableName string) {

	localAddr := os.Getenv("LOCAL_DYNAMO_ADDR")
	if localAddr == "" {
		log.Info("DeleteAppTable will only attempt to delete a local dynamodb table... returning.")
		return
	}

	var svc *dynamodb.DynamoDB = dbutil.CreateDynamoDBClient()

	params := &dynamodb.DeleteTableInput{
		TableName: aws.String(tableName),
	}
	resp, err := svc.DeleteTable(params)

	if err != nil {
		log.Info(err.Error())
		return
	}

	log.Printf("Table %s deleted:\n%s\n", tableName, resp.String())
}
func CreateProductTable(svc *dynamodb.DynamoDB) (*dynamodb.CreateTableOutput, error) {
	params := &dynamodb.CreateTableInput{
		TableName: aws.String("ProductCatalog"),
		AttributeDefinitions: []*dynamodb.AttributeDefinition{
			{
				AttributeName: aws.String("Id"),
				AttributeType: aws.String("N"),
			},
		},
		KeySchema: []*dynamodb.KeySchemaElement{
			{
				AttributeName: aws.String("Id"),
				KeyType:       aws.String("HASH"),
			},
		},
		ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
			ReadCapacityUnits:  aws.Int64(10),
			WriteCapacityUnits: aws.Int64(5),
		},
	}

	return svc.CreateTable(params)
}
Esempio n. 26
0
// ensureTableExists creates a DynamoDB table with a given
// DynamoDB client. If the table already exists, it is not
// being reconfigured.
func ensureTableExists(client *dynamodb.DynamoDB, table string, readCapacity, writeCapacity int) error {
	_, err := client.DescribeTable(&dynamodb.DescribeTableInput{
		TableName: aws.String(table),
	})
	if awserr, ok := err.(awserr.Error); ok {
		if awserr.Code() == "ResourceNotFoundException" {
			_, err = client.CreateTable(&dynamodb.CreateTableInput{
				TableName: aws.String(table),
				ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
					ReadCapacityUnits:  aws.Int64(int64(readCapacity)),
					WriteCapacityUnits: aws.Int64(int64(writeCapacity)),
				},
				KeySchema: []*dynamodb.KeySchemaElement{{
					AttributeName: aws.String("Path"),
					KeyType:       aws.String("HASH"),
				}, {
					AttributeName: aws.String("Key"),
					KeyType:       aws.String("RANGE"),
				}},
				AttributeDefinitions: []*dynamodb.AttributeDefinition{{
					AttributeName: aws.String("Path"),
					AttributeType: aws.String("S"),
				}, {
					AttributeName: aws.String("Key"),
					AttributeType: aws.String("S"),
				}},
			})
			if err != nil {
				return err
			}

			err = client.WaitUntilTableExists(&dynamodb.DescribeTableInput{
				TableName: aws.String(table),
			})
			if err != nil {
				return err
			}
		}
	}
	if err != nil {
		return err
	}
	return nil
}
func UploadReplyData(svc *dynamodb.DynamoDB) (*dynamodb.BatchWriteItemOutput, error) {
	oneDayAgo := time.Now().AddDate(0, 0, -1).String()
	sevenDaysAgo := time.Now().AddDate(0, 0, -7).String()
	fourteenDaysAgo := time.Now().AddDate(0, 0, -14).String()
	twentyOneDaysAgo := time.Now().AddDate(0, 0, -21).String()

	params := &dynamodb.BatchWriteItemInput{
		RequestItems: map[string][]*dynamodb.WriteRequest{
			"Reply": {
				&dynamodb.WriteRequest{
					PutRequest: &dynamodb.PutRequest{
						Item: map[string]*dynamodb.AttributeValue{
							"Id": {
								S: aws.String("Amazon DynamoDB#DynamoDB Thread 1"),
							},
							"ReplyDateTime": {
								S: aws.String(fourteenDaysAgo),
							},
							"Message": {
								S: aws.String("DynamoDB Thread 1 Reply 2 text"),
							},
							"PostedBy": {
								S: aws.String("User B"),
							},
						},
					},
				},
				&dynamodb.WriteRequest{
					PutRequest: &dynamodb.PutRequest{
						Item: map[string]*dynamodb.AttributeValue{
							"Id": {
								S: aws.String("Amazon DynamoDB#DynamoDB Thread 2"),
							},
							"ReplyDateTime": {
								S: aws.String(twentyOneDaysAgo),
							},
							"Message": {
								S: aws.String("DynamoDB Thread 2 Reply 3 text"),
							},
							"PostedBy": {
								S: aws.String("User B"),
							},
						},
					},
				},
				&dynamodb.WriteRequest{
					PutRequest: &dynamodb.PutRequest{
						Item: map[string]*dynamodb.AttributeValue{
							"Id": {
								S: aws.String("Amazon DynamoDB#DynamoDB Thread 2"),
							},
							"ReplyDateTime": {
								S: aws.String(sevenDaysAgo),
							},
							"Message": {
								S: aws.String("DynamoDB Thread 2 Reply 2 text"),
							},
							"PostedBy": {
								S: aws.String("User A"),
							},
						},
					},
				},
				&dynamodb.WriteRequest{
					PutRequest: &dynamodb.PutRequest{
						Item: map[string]*dynamodb.AttributeValue{
							"Id": {
								S: aws.String("Amazon DynamoDB#DynamoDB Thread 2"),
							},
							"ReplyDateTime": {
								S: aws.String(oneDayAgo),
							},
							"Message": {
								S: aws.String("DynamoDB Thread 2 Reply 1 text"),
							},
							"PostedBy": {
								S: aws.String("User A"),
							},
						},
					},
				},
			},
		},
	}

	return svc.BatchWriteItem(params)
}
Esempio n. 28
0
func retrieveItem(db *dynamodb.DynamoDB) error {
	var _, err = db.GetItem(getItemInput())
	return err
}
Esempio n. 29
0
func deleteAccountsTable(db *dynamodb.DynamoDB) error {
	var input = new(dynamodb.DeleteTableInput)
	input.TableName = aws.String("Accounts")
	var _, err = db.DeleteTable(input)
	return err
}
Esempio n. 30
0
func deleteItem(db *dynamodb.DynamoDB) error {
	var _, err = db.DeleteItem(deleteItemInput())
	return err
}