internal void AddAttribute(string attributeName, string attributeType) { if (string.IsNullOrEmpty(attributeType)) { throw new ArgumentException(string.Format("Attribute type must be specified", "AttributeType")); } var existingAttr = AttributeSchema.FirstOrDefault(attr => attr.AttributeName.Equals(attributeName, StringComparison.OrdinalIgnoreCase)); if (existingAttr != null) { if (!attributeType.Equals(existingAttr.AttributeType, StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException(string.Format("An attribute with name {0} had been defined previously but conflicting type ({1} vs {2}) has already been defined.", attributeName, existingAttr.AttributeType, attributeType), "AttributeName"); } return; } AttributeSchema.Add(new AttributeDefinition { AttributeName = attributeName, AttributeType = attributeType.ToUpper() }); }
/// <summary> /// Adds a new local secondary index or updates an index if it has been defined already /// </summary> /// <param name="indexName"></param> /// <param name="rangeKeyName"></param> /// <param name="rangeKeyDataType"></param> /// <param name="projectionType"></param> /// <param name="nonKeyAttributes"></param> internal void SetLocalSecondaryIndex(string indexName, string rangeKeyName, string rangeKeyDataType, string projectionType = null, string[] nonKeyAttributes = null) { // to add additional range keys, the user invokes this cmdlet multiple times in the // pipeline bool creatingNewIndex = false; var lsi = LocalSecondaryIndexSchema.FirstOrDefault(l => l.IndexName.Equals(indexName, StringComparison.OrdinalIgnoreCase)); if (lsi == null) { creatingNewIndex = true; lsi = new LocalSecondaryIndex { IndexName = indexName, KeySchema = new List <KeySchemaElement>() }; } if (AttributeSchema.FirstOrDefault(a => a.AttributeName.Equals(rangeKeyName, StringComparison.Ordinal)) == null) { // could validate that data type matches here AttributeSchema.Add(new AttributeDefinition { AttributeName = rangeKeyName, AttributeType = rangeKeyDataType }); } lsi.KeySchema.Add(new KeySchemaElement { AttributeName = rangeKeyName, KeyType = Amazon.DynamoDBv2.KeyType.RANGE }); if (!string.IsNullOrEmpty(projectionType)) { lsi.Projection = new Projection(); if (_projectionTypes.Contains(projectionType, StringComparer.OrdinalIgnoreCase)) { lsi.Projection.ProjectionType = projectionType.ToUpper(); if (nonKeyAttributes != null && nonKeyAttributes.Length > 0) { lsi.Projection.NonKeyAttributes.AddRange(nonKeyAttributes); } } else { throw new ArgumentException(string.Format("Invalid ProjectionType '{0}'; allowed values: {1}.", projectionType, string.Join(", ", _projectionTypes)), "ProjectionType"); } } if (creatingNewIndex) { LocalSecondaryIndexSchema.Add(lsi); } }
internal void AddKey(string keyName, string keyType, string dataType) { var keyElement = KeySchema.FirstOrDefault(key => key.AttributeName.Equals(keyName, StringComparison.OrdinalIgnoreCase)); if (keyElement != null) { throw new ArgumentException(string.Format("A key with name '{0}' has already been defined in the schema.", keyName), "KeyName"); } // if the key doesn't already exist as an attribute, add it var attribute = AttributeSchema.FirstOrDefault(a => a.AttributeName.Equals(keyName, StringComparison.OrdinalIgnoreCase)); if (attribute == null) { if (string.IsNullOrEmpty(dataType)) { throw new ArgumentException("An attribute for the key was not found in the supplied schema. The data type is needed before it can be added automatically.", "DataType"); } AttributeSchema.Add(new AttributeDefinition { AttributeName = keyName, AttributeType = dataType }); } keyElement = new KeySchemaElement { AttributeName = keyName, KeyType = keyType }; // allow for user possibly defining keys in any order; DDB requires the primary hash key to be first // and there may be multiple hash keys allowed in future. if (!HasDefinedKeys || keyType.Equals(Amazon.DynamoDBv2.KeyType.RANGE, StringComparison.OrdinalIgnoreCase)) { KeySchema.Add(keyElement); } else if (KeySchema[0].KeyType.Equals(Amazon.DynamoDBv2.KeyType.HASH)) { KeySchema.Add(keyElement); } else { KeySchema.Insert(0, keyElement); } }
/// <summary> /// Adds a new global secondary index or updates an index if it has been defined already. /// </summary> /// <param name="indexName"></param> /// <param name="hashKeyName"></param> /// <param name="hashKeyDataType"></param> /// <param name="rangeKeyName"></param> /// <param name="rangeKeyDataType"></param> /// <param name="readCapacityUnits"></param> /// <param name="writeCapacityUnits"></param> /// <param name="projectionType"></param> /// <param name="nonKeyAttributes"></param> internal void SetGlobalSecondaryIndex(string indexName, string hashKeyName, string hashKeyDataType, string rangeKeyName, string rangeKeyDataType, Int64 readCapacityUnits, Int64 writeCapacityUnits, string projectionType = null, string[] nonKeyAttributes = null) { // to add additional range keys, the user invokes this cmdlet multiple times in the // pipeline bool creatingNewIndex = false; var gsi = GlobalSecondaryIndexSchema.FirstOrDefault(g => g.IndexName.Equals(indexName, StringComparison.OrdinalIgnoreCase)); if (gsi == null) { creatingNewIndex = true; gsi = new GlobalSecondaryIndex { IndexName = indexName, KeySchema = new List <KeySchemaElement>() }; } // for a GSI, an additional hash key can be defined that does not have to match that used by the table if (!string.IsNullOrEmpty(hashKeyName)) { if (AttributeSchema.FirstOrDefault(a => a.AttributeName.Equals(hashKeyName, StringComparison.Ordinal)) == null) { // could validate that data type matches here AttributeSchema.Add(new AttributeDefinition { AttributeName = hashKeyName, AttributeType = hashKeyDataType }); } gsi.KeySchema.Add(new KeySchemaElement { AttributeName = hashKeyName, KeyType = Amazon.DynamoDBv2.KeyType.HASH }); } // for global indexes, range key is optional (assuming a hash key has been specified); for local indexes the range key is // mandatory if (!string.IsNullOrEmpty(rangeKeyName)) { if (AttributeSchema.FirstOrDefault(a => a.AttributeName.Equals(rangeKeyName, StringComparison.Ordinal)) == null) { // could validate that data type matches here AttributeSchema.Add(new AttributeDefinition { AttributeName = rangeKeyName, AttributeType = rangeKeyDataType }); } gsi.KeySchema.Add(new KeySchemaElement { AttributeName = rangeKeyName, KeyType = Amazon.DynamoDBv2.KeyType.RANGE }); } gsi.ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = readCapacityUnits, WriteCapacityUnits = writeCapacityUnits }; if (!string.IsNullOrEmpty(projectionType)) { gsi.Projection = new Projection(); if (_projectionTypes.Contains(projectionType, StringComparer.OrdinalIgnoreCase)) { gsi.Projection.ProjectionType = projectionType.ToUpper(); if (nonKeyAttributes != null && nonKeyAttributes.Length > 0) { gsi.Projection.NonKeyAttributes.AddRange(nonKeyAttributes); } } else { throw new ArgumentException(string.Format("Invalid ProjectionType '{0}'; allowed values: {1}.", projectionType, string.Join(", ", _projectionTypes)), "ProjectionType"); } } if (creatingNewIndex) { GlobalSecondaryIndexSchema.Add(gsi); } }