Exemple #1
0
func (serv DeviceService) GetDevices(cursor string) DeviceList {

	restError := restutil.GetRestError(serv.ResponseBuilder())
	defer restutil.HandleErrors(restError)

	var startKey map[string]dynamodb.Attribute
	if len(cursor) > 0 {
		startKey = make(map[string]dynamodb.Attribute)
		startKey["alias"] = dynamodb.Attribute{S: cursor}
	}

	scanRequest := dynamodb.ScanRequest{ExclusiveStartKey: startKey, TableName: config.AWSConfigInstance().DynamoTable(), Limit: 1000}

	response, err := dynamodb.ScanForItems(
		scanRequest,
		config.AWSConfigInstance().UserID(),
		config.AWSConfigInstance().UserSecret(),
		config.AWSConfigInstance().Region())
	restutil.CheckError(err, restError, 500)

	cursor = ""
	if len(response.LastEvaluatedKey) != 0 {
		cursor = response.LastEvaluatedKey["alias"].S
	}

	return DeviceList{convertToDevices(response.Items), cursor}
}
Exemple #2
0
func (serv DeviceService) GetDevice(deviceAlias string) Device {
	restError := restutil.GetRestError(serv.ResponseBuilder())
	defer restutil.HandleErrors(restError)

	key := make(map[string]dynamodb.Attribute)
	key["alias"] = dynamodb.Attribute{S: deviceAlias}
	getItemRequest := dynamodb.GetItemRequest{Key: key, TableName: config.AWSConfigInstance().DynamoTable()}

	item, err := dynamodb.GetItem(
		getItemRequest,
		config.AWSConfigInstance().UserID(),
		config.AWSConfigInstance().UserSecret(),
		config.AWSConfigInstance().Region())
	restutil.CheckError(err, restError, 500)

	return Device{item["alias"].S, item["locale"].S, item["arns"].SS, item["tags"].SS}
}
Exemple #3
0
func (serv DeviceService) RegisterDevice(device DeviceRegistration) {

	restError := restutil.GetRestError(serv.ResponseBuilder())
	defer restutil.HandleErrors(restError)

	err := device.ValidateLocale()
	restutil.CheckError(err, restError, 400)

	arn, err := sns.RegisterDevice(
		device.Id,
		formatTags(device.Locale, device.Alias, device.Tags),
		config.AWSConfigInstance().UserID(),
		config.AWSConfigInstance().UserSecret(),
		config.AWSConfigInstance().Region(),
		config.AWSConfigInstance().PlatformApps()[device.PlatformApp].Arn())
	restutil.CheckError(err, restError, 400)

	key := make(map[string]dynamodb.Attribute)
	key["alias"] = dynamodb.Attribute{S: device.Alias}
	attributeUpdates := make(map[string]dynamodb.AttributeUpdate)
	attributeUpdates["arns"] = dynamodb.AttributeUpdate{"ADD", dynamodb.Attribute{SS: []string{arn}}}
	attributeUpdates["locale"] = dynamodb.AttributeUpdate{"PUT", dynamodb.Attribute{S: device.Locale}}
	attributeUpdates["tags"] = dynamodb.AttributeUpdate{"ADD", dynamodb.Attribute{SS: device.Tags}}

	updateItemRequest := dynamodb.UpdateItemRequest{
		Key:              key,
		AttributeUpdates: attributeUpdates,
		TableName:        config.AWSConfigInstance().DynamoTable()}
	err = dynamodb.UpdateItem(
		updateItemRequest,
		config.AWSConfigInstance().UserID(),
		config.AWSConfigInstance().UserSecret(),
		config.AWSConfigInstance().Region())
	restutil.CheckError(err, restError, 500)

	return
}