/// <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); } }
private void CloneLocalSecondaryIndexSchema(IEnumerable <LocalSecondaryIndex> sourceSchema) { foreach (var sourceIndex in sourceSchema) { var clonedIndex = new LocalSecondaryIndex { IndexName = new string(sourceIndex.IndexName.ToCharArray()), Projection = null, KeySchema = new List <KeySchemaElement>() }; foreach (var sourceKey in sourceIndex.KeySchema) { var clonedKey = new KeySchemaElement { AttributeName = new string(sourceKey.AttributeName.ToCharArray()), KeyType = new DynamoDBv2.KeyType(sourceKey.KeyType) }; clonedIndex.KeySchema.Add(clonedKey); } if (sourceIndex.Projection != null) { clonedIndex.Projection = new Projection { ProjectionType = new DynamoDBv2.ProjectionType(sourceIndex.Projection.ProjectionType), NonKeyAttributes = new List <string>() }; foreach (var nonKeyAttr in sourceIndex.Projection.NonKeyAttributes) { clonedIndex.Projection.NonKeyAttributes.Add(nonKeyAttr); } } LocalSecondaryIndexSchema.Add(clonedIndex); } }