void ProcessKeys(PropertyInfo[] properties, CreateTableRequest createTableRequest)
        {
            foreach (var property in properties)
            {
                KeySchemaElement keySchemaElement = null;

                var hashKeyAttribute = property.GetCustomAttributes <DynamoDBHashKeyAttribute>().FirstOrDefault(x => x.GetType() == typeof(DynamoDBHashKeyAttribute));
                if (hashKeyAttribute != null)
                {
                    keySchemaElement = BuildKey(property, hashKeyAttribute);
                }

                var rangeKeyAttribute = property.GetCustomAttributes <DynamoDBRangeKeyAttribute>().FirstOrDefault(x => x.GetType() == typeof(DynamoDBRangeKeyAttribute));
                if (rangeKeyAttribute != null)
                {
                    if (keySchemaElement != null)
                    {
                        throw new InvalidOperationException("A property can not have both the DynamoDBRangeKeyAttribute and DynamoDBHashKeyAttribute applied");
                    }
                    keySchemaElement = BuildKey(property, rangeKeyAttribute);
                }

                if (keySchemaElement == null)
                {
                    continue;
                }

                AddAttributeIfMissing(createTableRequest, property, keySchemaElement.AttributeName);
                createTableRequest.KeySchema.Add(keySchemaElement);
            }
        }
Beispiel #2
0
        public static void CreateLockTableInDynamoDB(AmazonDynamoDBClient client, string lockTableName,
                                                     ProvisionedThroughput provisionedThroughput, string partitionKeyName)
        {
            KeySchemaElement        partitionKeyElement = new KeySchemaElement(partitionKeyName, KeyType.HASH);
            List <KeySchemaElement> keySchema           = new List <KeySchemaElement> {
                partitionKeyElement
            };

            List <AttributeDefinition> attributeDefinitions =
                new List <AttributeDefinition> {
                new AttributeDefinition(partitionKeyName, ScalarAttributeType.S)
            };

            //if (!string.IsNullOrEmpty(sortKeyName))
            //{
            //    KeySchemaElement sortKeyElement = new KeySchemaElement(sortKeyName, KeyType.RANGE);
            //    keySchema.Add(sortKeyElement);

            //    attributeDefinitions.Add(new AttributeDefinition(sortKeyName, ScalarAttributeType.S));
            //}

            CreateTableRequest createTableRequest =
                new CreateTableRequest(lockTableName, keySchema, attributeDefinitions, provisionedThroughput);

            var createTableResponse = client.CreateTableAsync(createTableRequest).Result;

            if (createTableResponse.HttpStatusCode != HttpStatusCode.OK)
            {
                //todo: fill this out with identifying information for the error
                throw new LockTableCreationFailedException("failed");
            }
        }
Beispiel #3
0
        private static void AddGSIToMap(string indexName, GlobalSecondaryIndexDetails gsiHashKeyResult, Dictionary <string, GlobalSecondaryIndex> gsiMap)
        {
            var gsi = new GlobalSecondaryIndex();

            gsi.IndexName = indexName;
            var gsiHashKey = new KeySchemaElement(gsiHashKeyResult.Prop.Name, KeyType.HASH);

            gsi.KeySchema.Add(gsiHashKey);
            gsiMap.Add(gsi.IndexName, gsi);
        }
Beispiel #4
0
        private static void UpdateGSIMapWithRangeKey(Dictionary <string, GlobalSecondaryIndex> gsiMap, string indexName,
                                                     GlobalSecondaryIndexDetails gsiRangeKeyResult)
        {
            if (!gsiMap.TryGetValue(indexName, out GlobalSecondaryIndex entry))
            {
                throw new InvalidOperationException(
                          $"The global secondary index {gsiRangeKeyResult.Prop.Name} lacks a hash key");
            }

            var gsiRangeKey = new KeySchemaElement(gsiRangeKeyResult.Prop.Name, KeyType.RANGE);

            entry.KeySchema.Add(gsiRangeKey);
        }
        private static List <KeySchemaElement> GetKeySchema(IDictionary <YamlNode, YamlNode> props)
        {
            var result        = new List <KeySchemaElement>();
            var keySchemaNode = (YamlSequenceNode)props["KeySchema"];

            foreach (YamlMappingNode entry in keySchemaNode.Children)
            {
                var attrName         = entry.Children["AttributeName"].ToString();
                var keyType          = entry.Children["KeyType"].ToString();
                var keySchemaElement = new KeySchemaElement(attrName, keyType);
                result.Add(keySchemaElement);
            }
            return(result);
        }
Beispiel #6
0
        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);
            }
        }
Beispiel #7
0
        private void CloneGlobalSecondaryIndexSchema(IEnumerable <GlobalSecondaryIndex> sourceSchema)
        {
            foreach (var sourceIndex in sourceSchema)
            {
                var clonedIndex = new GlobalSecondaryIndex
                {
                    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);
                    }
                }

                clonedIndex.ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = sourceIndex.ProvisionedThroughput.ReadCapacityUnits,
                    WriteCapacityUnits = sourceIndex.ProvisionedThroughput.WriteCapacityUnits
                };

                GlobalSecondaryIndexSchema.Add(clonedIndex);
            }
        }
        /// <summary>
        /// Uses the partitionKey and sortKey to create a unique table identifier
        /// </summary>
        /// <param name="partitionKey"></param>
        /// <param name="sortKey"></param>
        /// <returns>List<KeySchemaElement></returns>
        private List <KeySchemaElement> CreateKeySchemaList(string partitionKey, string sortKey)
        {
            var elements = new List <KeySchemaElement>
            {
                new KeySchemaElement
                {
                    AttributeName = partitionKey,
                    KeyType       = KeyType.HASH
                }
            };

            if (sortKey != null)
            {
                var sortElement = new KeySchemaElement
                {
                    AttributeName = sortKey,
                    KeyType       = KeyType.RANGE //sort key
                };
                elements.Add(sortElement);
            }

            return(elements);
        }
Beispiel #9
0
        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);
            }
        }
Beispiel #10
0
        public IRequest Marshall(CreateTableRequest createTableRequest)
        {
            IRequest request = new DefaultRequest(createTableRequest, "AmazonDynamoDB");
            string   target  = "DynamoDB_20111205.CreateTable";

            request.Headers["X-Amz-Target"] = target;
            request.Headers["Content-Type"] = "application/x-amz-json-1.0";

            using (StringWriter stringWriter = new StringWriter())
            {
                JsonWriter writer = new JsonWriter(stringWriter);
                writer.WriteObjectStart();

                if (createTableRequest != null && createTableRequest.IsSetTableName())
                {
                    writer.WritePropertyName("TableName");
                    writer.Write(createTableRequest.TableName);
                }

                if (createTableRequest != null)
                {
                    KeySchema keySchema = createTableRequest.KeySchema;
                    if (keySchema != null)
                    {
                        writer.WritePropertyName("KeySchema");
                        writer.WriteObjectStart();

                        if (keySchema != null)
                        {
                            KeySchemaElement hashKeyElement = keySchema.HashKeyElement;
                            if (hashKeyElement != null)
                            {
                                writer.WritePropertyName("HashKeyElement");
                                writer.WriteObjectStart();
                                if (hashKeyElement != null && hashKeyElement.IsSetAttributeName())
                                {
                                    writer.WritePropertyName("AttributeName");
                                    writer.Write(hashKeyElement.AttributeName);
                                }
                                if (hashKeyElement != null && hashKeyElement.IsSetAttributeType())
                                {
                                    writer.WritePropertyName("AttributeType");
                                    writer.Write(hashKeyElement.AttributeType);
                                }
                                writer.WriteObjectEnd();
                            }
                        }

                        if (keySchema != null)
                        {
                            KeySchemaElement rangeKeyElement = keySchema.RangeKeyElement;
                            if (rangeKeyElement != null)
                            {
                                writer.WritePropertyName("RangeKeyElement");
                                writer.WriteObjectStart();
                                if (rangeKeyElement != null && rangeKeyElement.IsSetAttributeName())
                                {
                                    writer.WritePropertyName("AttributeName");
                                    writer.Write(rangeKeyElement.AttributeName);
                                }
                                if (rangeKeyElement != null && rangeKeyElement.IsSetAttributeType())
                                {
                                    writer.WritePropertyName("AttributeType");
                                    writer.Write(rangeKeyElement.AttributeType);
                                }
                                writer.WriteObjectEnd();
                            }
                        }
                        writer.WriteObjectEnd();
                    }
                }

                if (createTableRequest != null)
                {
                    ProvisionedThroughput provisionedThroughput = createTableRequest.ProvisionedThroughput;
                    if (provisionedThroughput != null)
                    {
                        writer.WritePropertyName("ProvisionedThroughput");
                        writer.WriteObjectStart();
                        if (provisionedThroughput != null && provisionedThroughput.IsSetReadCapacityUnits())
                        {
                            writer.WritePropertyName("ReadCapacityUnits");
                            writer.Write(provisionedThroughput.ReadCapacityUnits);
                        }
                        if (provisionedThroughput != null && provisionedThroughput.IsSetWriteCapacityUnits())
                        {
                            writer.WritePropertyName("WriteCapacityUnits");
                            writer.Write(provisionedThroughput.WriteCapacityUnits);
                        }
                        writer.WriteObjectEnd();
                    }
                }

                writer.WriteObjectEnd();

                string snippet = stringWriter.ToString();
                request.Content = System.Text.Encoding.UTF8.GetBytes(snippet);
                return(request);
            }
        }
        private void CreateLSITable()
        {
            #region CreateTable LSI Sample

            // Create a client
            AmazonDynamoDBClient client = new AmazonDynamoDBClient();

            // Define table schema:
            //  Table has a hash-key "Author" and a range-key "Title"
            List <KeySchemaElement> schema = new List <KeySchemaElement>
            {
                new KeySchemaElement
                {
                    AttributeName = "Author", KeyType = "HASH"
                },
                new KeySchemaElement
                {
                    AttributeName = "Title", KeyType = "RANGE"
                }
            };

            // Define local secondary indexes:
            //  Table has two indexes, one on "Year" and the other on "Setting"
            List <LocalSecondaryIndex> indexes = new List <LocalSecondaryIndex>
            {
                new LocalSecondaryIndex
                {
                    IndexName = "YearsIndex",
                    KeySchema = new List <KeySchemaElement>
                    {
                        // Hash key must match table hash key
                        new KeySchemaElement {
                            AttributeName = "Author", KeyType = "HASH"
                        },
                        // Secondary index on "Year" attribute
                        new KeySchemaElement {
                            AttributeName = "Year", KeyType = "RANGE"
                        }
                    },
                    // Projection type is set to ALL, all attributes returned for this index
                    Projection = new Projection
                    {
                        ProjectionType = "ALL"
                    }
                },
                new LocalSecondaryIndex
                {
                    IndexName = "SettingsIndex",
                    KeySchema = new List <KeySchemaElement>
                    {
                        // Hash key must match table hash key
                        new KeySchemaElement {
                            AttributeName = "Author", KeyType = "HASH"
                        },
                        // Secondary index on "Setting" attribute
                        new KeySchemaElement {
                            AttributeName = "Setting", KeyType = "RANGE"
                        }
                    },
                    // Projection type is set to INCLUDE, the specified attributes + keys are returned
                    Projection = new Projection
                    {
                        ProjectionType   = "INCLUDE",
                        NonKeyAttributes = new List <string>
                        {
                            "Pages", "Genres"
                        }
                    }
                }
            };

            // Define key attributes:
            //  The key attributes "Author" and "Title" are string types.
            //  The local secondary index attributes are "Year" (numerical) and "Setting" (string).
            List <AttributeDefinition> definitions = new List <AttributeDefinition>
            {
                new AttributeDefinition
                {
                    AttributeName = "Author", AttributeType = "S"
                },
                new AttributeDefinition
                {
                    AttributeName = "Title", AttributeType = "S"
                },
                new AttributeDefinition
                {
                    AttributeName = "Year", AttributeType = "N"
                },
                new AttributeDefinition
                {
                    AttributeName = "Setting", AttributeType = "S"
                }
            };

            // Define table throughput:
            //  Table has capacity of 20 reads and 50 writes
            ProvisionedThroughput throughput = new ProvisionedThroughput
            {
                ReadCapacityUnits  = 20,
                WriteCapacityUnits = 50
            };

            // Configure the CreateTable request
            CreateTableRequest request = new CreateTableRequest
            {
                TableName             = "SampleTable",
                KeySchema             = schema,
                ProvisionedThroughput = throughput,
                AttributeDefinitions  = definitions,
                LocalSecondaryIndexes = indexes
            };

            // View new table properties
            TableDescription tableDescription = client.CreateTable(request).TableDescription;
            Console.WriteLine("Table name: {0}", tableDescription.TableName);
            Console.WriteLine("Creation time: {0}", tableDescription.CreationDateTime);
            Console.WriteLine("Item count: {0}", tableDescription.ItemCount);
            Console.WriteLine("Table size (bytes): {0}", tableDescription.TableSizeBytes);
            Console.WriteLine("Table status: {0}", tableDescription.TableStatus);

            // List table key schema
            List <KeySchemaElement> tableSchema = tableDescription.KeySchema;
            for (int i = 0; i < tableSchema.Count; i++)
            {
                KeySchemaElement element = tableSchema[i];
                Console.WriteLine("Key: Name = {0}, KeyType = {1}",
                                  element.AttributeName, element.KeyType);
            }

            // List attribute definitions
            List <AttributeDefinition> attributeDefinitions = tableDescription.AttributeDefinitions;
            for (int i = 0; i < attributeDefinitions.Count; i++)
            {
                AttributeDefinition definition = attributeDefinitions[i];
                Console.WriteLine("Attribute: Name = {0}, Type = {1}",
                                  definition.AttributeName, definition.AttributeType);
            }

            Console.WriteLine("Throughput: Reads = {0}, Writes = {1}",
                              tableDescription.ProvisionedThroughput.ReadCapacityUnits,
                              tableDescription.ProvisionedThroughput.WriteCapacityUnits);

            #endregion
        }
        public void DataPlaneSamples()
        {
            {
                #region CreateTable Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Define table schema:
                //  Table has a hash-key "Author" and a range-key "Title"
                List <KeySchemaElement> schema = new List <KeySchemaElement>
                {
                    new KeySchemaElement
                    {
                        AttributeName = "Author", KeyType = "HASH"
                    },
                    new KeySchemaElement
                    {
                        AttributeName = "Title", KeyType = "RANGE"
                    }
                };

                // Define key attributes:
                //  The key attributes "Author" and "Title" are string types
                List <AttributeDefinition> definitions = new List <AttributeDefinition>
                {
                    new AttributeDefinition
                    {
                        AttributeName = "Author", AttributeType = "S"
                    },
                    new AttributeDefinition
                    {
                        AttributeName = "Title", AttributeType = "S"
                    }
                };

                // Define table throughput:
                //  Table has capacity of 20 reads and 50 writes
                ProvisionedThroughput throughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 20,
                    WriteCapacityUnits = 50
                };

                // Configure the CreateTable request
                CreateTableRequest request = new CreateTableRequest
                {
                    TableName             = "SampleTable",
                    KeySchema             = schema,
                    ProvisionedThroughput = throughput,
                    AttributeDefinitions  = definitions
                };

                // View new table properties
                TableDescription tableDescription = client.CreateTable(request).TableDescription;
                Console.WriteLine("Table name: {0}", tableDescription.TableName);
                Console.WriteLine("Creation time: {0}", tableDescription.CreationDateTime);
                Console.WriteLine("Item count: {0}", tableDescription.ItemCount);
                Console.WriteLine("Table size (bytes): {0}", tableDescription.TableSizeBytes);
                Console.WriteLine("Table status: {0}", tableDescription.TableStatus);

                // List table key schema
                List <KeySchemaElement> tableSchema = tableDescription.KeySchema;
                for (int i = 0; i < tableSchema.Count; i++)
                {
                    KeySchemaElement element = tableSchema[i];
                    Console.WriteLine("Key: Name = {0}, KeyType = {1}",
                                      element.AttributeName, element.KeyType);
                }

                // List attribute definitions
                List <AttributeDefinition> attributeDefinitions = tableDescription.AttributeDefinitions;
                for (int i = 0; i < attributeDefinitions.Count; i++)
                {
                    AttributeDefinition definition = attributeDefinitions[i];
                    Console.WriteLine("Attribute: Name = {0}, Type = {1}",
                                      definition.AttributeName, definition.AttributeType);
                }

                Console.WriteLine("Throughput: Reads = {0}, Writes = {1}",
                                  tableDescription.ProvisionedThroughput.ReadCapacityUnits,
                                  tableDescription.ProvisionedThroughput.WriteCapacityUnits);

                #endregion
            }

            {
                #region DescribeTable Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Create DescribeTable request
                DescribeTableRequest request = new DescribeTableRequest
                {
                    TableName = "SampleTable"
                };

                // Issue DescribeTable request and retrieve the table description
                TableDescription tableDescription = client.DescribeTable(request).Table;

                // View new table properties
                Console.WriteLine("Table name: {0}", tableDescription.TableName);
                Console.WriteLine("Creation time: {0}", tableDescription.CreationDateTime);
                Console.WriteLine("Item count: {0}", tableDescription.ItemCount);
                Console.WriteLine("Table size (bytes): {0}", tableDescription.TableSizeBytes);
                Console.WriteLine("Table status: {0}", tableDescription.TableStatus);
                // List table key schema
                List <KeySchemaElement> tableSchema = tableDescription.KeySchema;
                for (int i = 0; i < tableSchema.Count; i++)
                {
                    KeySchemaElement element = tableSchema[i];
                    Console.WriteLine("Key: Name = {0}, KeyType = {1}",
                                      element.AttributeName, element.KeyType);
                }

                // List attribute definitions
                List <AttributeDefinition> attributeDefinitions = tableDescription.AttributeDefinitions;
                for (int i = 0; i < attributeDefinitions.Count; i++)
                {
                    AttributeDefinition definition = attributeDefinitions[i];
                    Console.WriteLine("Attribute: Name = {0}, Type = {1}",
                                      definition.AttributeName, definition.AttributeType);
                }
                Console.WriteLine("Throughput: Reads = {0}, Writes = {1}",
                                  tableDescription.ProvisionedThroughput.ReadCapacityUnits,
                                  tableDescription.ProvisionedThroughput.WriteCapacityUnits);

                #endregion
            }

            {
                #region ListTables Paging Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                string startTableName = null;
                do
                {
                    // Configure ListTables request with the marker value
                    ListTablesRequest request = new ListTablesRequest
                    {
                        ExclusiveStartTableName = startTableName,
                    };

                    // Issue call
                    ListTablesResult result = client.ListTables(request);

                    // List retrieved tables
                    List <string> tables = result.TableNames;
                    Console.WriteLine("Retrieved tables: {0}",
                                      string.Join(", ", tables));

                    // Update marker value from the result
                    startTableName = result.LastEvaluatedTableName;
                } while (!string.IsNullOrEmpty(startTableName)); // Test marker value

                #endregion
            }

            {
                #region ListTables NonPaging Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Issue call
                ListTablesResult result = client.ListTables();

                // List retrieved tables
                List <string> tables = result.TableNames;
                Console.WriteLine("Retrieved tables: {0}",
                                  string.Join(", ", tables));

                #endregion
            }

            TableUtils.WaitUntilTableActive("SampleTable", TestClient);

            {
                #region UpdateTable Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Define new table throughput:
                //  Table will now have capacity of 40 reads and 50 writes
                ProvisionedThroughput throughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 40,
                    WriteCapacityUnits = 50
                };

                // Compose the UpdateTable request
                UpdateTableRequest request = new UpdateTableRequest
                {
                    TableName             = "SampleTable",
                    ProvisionedThroughput = throughput
                };

                // View new table properties
                TableDescription tableDescription = client.UpdateTable(request).TableDescription;
                Console.WriteLine("Table name: {0}", tableDescription.TableName);
                Console.WriteLine("Throughput: Reads = {0}, Writes = {1}",
                                  tableDescription.ProvisionedThroughput.ReadCapacityUnits,
                                  tableDescription.ProvisionedThroughput.WriteCapacityUnits);

                #endregion
            }

            TableUtils.WaitUntilTableActive("SampleTable", TestClient);

            {
                #region DeleteTable Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Configure the DeleteTable request
                DeleteTableRequest request = new DeleteTableRequest
                {
                    TableName = "SampleTable"
                };

                // Issue DeleteTable request and retrieve the table description
                TableDescription tableDescription = client.DeleteTable(request).TableDescription;
                Console.WriteLine("Table name: {0}", tableDescription.TableName);
                Console.WriteLine("Table status: {0}", tableDescription.TableStatus);

                #endregion
            }
        }
        public IRequest Marshall(CreateTableRequest createTableRequest)
        {
            IRequest request = new DefaultRequest(createTableRequest, "AmazonDynamoDB");
            string   target  = "DynamoDB_20111205.CreateTable";

            request.Headers["X-Amz-Target"] = target;
            request.Headers["Content-Type"] = "application/x-amz-json-1.0";



            string uriResourcePath = "";

            if (uriResourcePath.Contains("?"))
            {
                string queryString = uriResourcePath.Substring(uriResourcePath.IndexOf("?") + 1);
                uriResourcePath = uriResourcePath.Substring(0, uriResourcePath.IndexOf("?"));

                foreach (string s in queryString.Split('&', ';'))
                {
                    string[] nameValuePair = s.Split('=');
                    if (nameValuePair.Length == 2 && nameValuePair[1].Length > 0)
                    {
                        request.Parameters.Add(nameValuePair[0], nameValuePair[1]);
                    }
                    else
                    {
                        request.Parameters.Add(nameValuePair[0], null);
                    }
                }
            }

            request.ResourcePath = uriResourcePath;


            using (StringWriter stringWriter = new StringWriter())
            {
                JsonWriter writer = new JsonWriter(stringWriter);
                writer.WriteObjectStart();

                if (createTableRequest != null && createTableRequest.IsSetTableName())
                {
                    writer.WritePropertyName("TableName");
                    writer.Write(createTableRequest.TableName);
                }

                if (createTableRequest != null)
                {
                    KeySchema keySchema = createTableRequest.KeySchema;
                    if (keySchema != null)
                    {
                        writer.WritePropertyName("KeySchema");
                        writer.WriteObjectStart();

                        if (keySchema != null)
                        {
                            KeySchemaElement hashKeyElement = keySchema.HashKeyElement;
                            if (hashKeyElement != null)
                            {
                                writer.WritePropertyName("HashKeyElement");
                                writer.WriteObjectStart();
                                if (hashKeyElement != null && hashKeyElement.IsSetAttributeName())
                                {
                                    writer.WritePropertyName("AttributeName");
                                    writer.Write(hashKeyElement.AttributeName);
                                }
                                if (hashKeyElement != null && hashKeyElement.IsSetAttributeType())
                                {
                                    writer.WritePropertyName("AttributeType");
                                    writer.Write(hashKeyElement.AttributeType);
                                }
                                writer.WriteObjectEnd();
                            }
                        }

                        if (keySchema != null)
                        {
                            KeySchemaElement rangeKeyElement = keySchema.RangeKeyElement;
                            if (rangeKeyElement != null)
                            {
                                writer.WritePropertyName("RangeKeyElement");
                                writer.WriteObjectStart();
                                if (rangeKeyElement != null && rangeKeyElement.IsSetAttributeName())
                                {
                                    writer.WritePropertyName("AttributeName");
                                    writer.Write(rangeKeyElement.AttributeName);
                                }
                                if (rangeKeyElement != null && rangeKeyElement.IsSetAttributeType())
                                {
                                    writer.WritePropertyName("AttributeType");
                                    writer.Write(rangeKeyElement.AttributeType);
                                }
                                writer.WriteObjectEnd();
                            }
                        }
                        writer.WriteObjectEnd();
                    }
                }

                if (createTableRequest != null)
                {
                    ProvisionedThroughput provisionedThroughput = createTableRequest.ProvisionedThroughput;
                    if (provisionedThroughput != null)
                    {
                        writer.WritePropertyName("ProvisionedThroughput");
                        writer.WriteObjectStart();
                        if (provisionedThroughput != null && provisionedThroughput.IsSetReadCapacityUnits())
                        {
                            writer.WritePropertyName("ReadCapacityUnits");
                            writer.Write(provisionedThroughput.ReadCapacityUnits);
                        }
                        if (provisionedThroughput != null && provisionedThroughput.IsSetWriteCapacityUnits())
                        {
                            writer.WritePropertyName("WriteCapacityUnits");
                            writer.Write(provisionedThroughput.WriteCapacityUnits);
                        }
                        writer.WriteObjectEnd();
                    }
                }

                writer.WriteObjectEnd();

                string snippet = stringWriter.ToString();
                request.Content = System.Text.Encoding.UTF8.GetBytes(snippet);
            }


            return(request);
        }
Beispiel #14
0
    /// <summary>
    /// Creates tables that don't exist, with LocalIndexes and GlobalIndexes when found
    /// </summary>
    /// <param name="assembly">The assembly that contains DynamoDb entities</param>
    /// <returns></returns>
    public async Task <StringBuilder> CreateTablesAsync(Assembly assembly)
    {
        StringBuilder oSb = new StringBuilder();

        var currentTables = await _oDynamoDBClient.ListTablesAsync();

        var tableNames = currentTables.TableNames;

        var types = GetTypesWithDynamoTableAttribute(assembly);

        var tablePrefix = _dynamoConfig.TablePrefix;

        foreach (var tbl in types)
        {
            var tblName = tablePrefix + tbl.GetCustomAttribute <DynamoDBTableAttribute>().TableName;
            if (!tableNames.Contains(tblName))
            {
                Projection projection = new Projection()
                {
                    ProjectionType = "INCLUDE"
                };
                List <string> nonKeyAttributes = tbl.GetProperties().Where(p => p.GetCustomAttribute <DynamoDBPropertyAttribute>() != null).Select(s => s.Name).ToList();
                projection.NonKeyAttributes = nonKeyAttributes;

                CreateTableRequest reqCreateTable = new CreateTableRequest();
                reqCreateTable.TableName             = tblName;
                reqCreateTable.ProvisionedThroughput = new ProvisionedThroughput();
                reqCreateTable.ProvisionedThroughput.ReadCapacityUnits  = 1;
                reqCreateTable.ProvisionedThroughput.WriteCapacityUnits = 1;

                var hashKeyProperty = tbl.GetProperties().FirstOrDefault(p => p.GetCustomAttribute <DynamoDBHashKeyAttribute>() != null);

                if (hashKeyProperty != null)
                {
                    string hashKeyName = hashKeyProperty.Name;
                    if (hashKeyProperty.GetCustomAttribute <DynamoDBHashKeyAttribute>().AttributeName != null)
                    {
                        hashKeyName = hashKeyProperty.GetCustomAttribute <DynamoDBHashKeyAttribute>().AttributeName;
                    }

                    var hashKeySchema = new KeySchemaElement()
                    {
                        AttributeName = hashKeyName, KeyType = KeyType.HASH
                    };
                    reqCreateTable.KeySchema.Add(hashKeySchema);

                    reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(hashKeyProperty, hashKeyName));

                    // adding range property if exists
                    var rangeyProperty = tbl.GetProperties().Where(p => p.GetCustomAttribute <DynamoDBRangeKeyAttribute>() != null).FirstOrDefault();

                    if (rangeyProperty != null)
                    {
                        string rangeKeyName = rangeyProperty.Name;
                        if (rangeyProperty.GetCustomAttribute <DynamoDBRangeKeyAttribute>().AttributeName != null)
                        {
                            rangeKeyName = rangeyProperty.GetCustomAttribute <DynamoDBRangeKeyAttribute>().AttributeName;
                        }

                        reqCreateTable.KeySchema.Add(new KeySchemaElement()
                        {
                            AttributeName = rangeKeyName, KeyType = KeyType.RANGE
                        });
                        reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(rangeyProperty, rangeKeyName));

                        // adding local secondary indexes
                        var localIndexes = tbl.GetProperties().Where(p => p.GetCustomAttribute <DynamoDBLocalSecondaryIndexRangeKeyAttribute>() != null);
                        foreach (var indexProperty in localIndexes)
                        {
                            // usually only one index is assigned
                            string indexName = indexProperty.GetCustomAttribute <DynamoDBLocalSecondaryIndexRangeKeyAttribute>().IndexNames.First();
                            List <KeySchemaElement> indexKeySchema = new List <KeySchemaElement>();
                            indexKeySchema.Add(hashKeySchema);
                            indexKeySchema.Add(new KeySchemaElement()
                            {
                                AttributeName = indexProperty.Name, KeyType = KeyType.RANGE
                            });

                            reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(indexProperty, indexProperty.Name));

                            LocalSecondaryIndex localSecondaryIndex = new LocalSecondaryIndex()
                            {
                                IndexName  = indexName,
                                KeySchema  = indexKeySchema,
                                Projection = projection
                            };

                            reqCreateTable.LocalSecondaryIndexes.Add(localSecondaryIndex);
                        }
                    }

                    // adding local secondary indexes
                    var globalIndexes = tbl.GetProperties().Where(p => p.GetCustomAttribute <DynamoDBGlobalSecondaryIndexHashKeyAttribute>() != null);
                    if (globalIndexes.Count() > 0)
                    {
                        foreach (var globalIndexProperty in globalIndexes)
                        {
                            var globalKeySchema = new List <KeySchemaElement>();

                            string globalHashKeyName = globalIndexProperty.Name;
                            if (globalIndexProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexHashKeyAttribute>().AttributeName != null)
                            {
                                globalHashKeyName = globalIndexProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexHashKeyAttribute>().AttributeName;
                            }

                            var globalHashKeySchema = new KeySchemaElement()
                            {
                                AttributeName = globalHashKeyName, KeyType = KeyType.HASH
                            };
                            globalKeySchema.Add(globalHashKeySchema);
                            reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(globalIndexProperty, globalHashKeyName));

                            string thisGlobalIndexName = globalIndexProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexHashKeyAttribute>().IndexNames.First();

                            // find the range keys related to this index
                            var globalRangeProperty = tbl.GetProperties().SingleOrDefault(p => p.GetCustomAttribute <DynamoDBGlobalSecondaryIndexRangeKeyAttribute>() != null && p.GetCustomAttribute <DynamoDBGlobalSecondaryIndexRangeKeyAttribute>().IndexNames.Contains(thisGlobalIndexName));
                            if (globalRangeProperty != null)
                            {
                                string globalRangeKeyName = globalRangeProperty.Name;
                                if (globalRangeProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexRangeKeyAttribute>().AttributeName != null)
                                {
                                    globalRangeKeyName = globalRangeProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexRangeKeyAttribute>().AttributeName;
                                }

                                var globalRangeKeySchema = new KeySchemaElement()
                                {
                                    AttributeName = globalRangeKeyName, KeyType = KeyType.RANGE
                                };
                                globalKeySchema.Add(globalRangeKeySchema);
                                reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(globalRangeProperty, globalRangeKeyName));
                            }

                            GlobalSecondaryIndex globalSecondaryIndex = new GlobalSecondaryIndex()
                            {
                                IndexName             = thisGlobalIndexName,
                                KeySchema             = globalKeySchema,
                                Projection            = projection,
                                ProvisionedThroughput = new ProvisionedThroughput(1, 1)
                            };

                            reqCreateTable.GlobalSecondaryIndexes.Add(globalSecondaryIndex);
                        }
                    }

                    var response = await _oDynamoDBClient.CreateTableAsync(reqCreateTable);

                    // tables aren't created instantly
                    await WaitUntilTableReadyAsync(reqCreateTable.TableName);

                    foreach (var kv in response.ResponseMetadata.Metadata)
                    {
                        oSb.AppendLine($"{kv.Key} {kv.Value}");
                    }
                }
                else
                {
                    oSb.AppendLine($"{tblName} table has no hash key property");
                }
            }
        }
        return(oSb);
    }
Beispiel #15
0
        public static async Task <UpdateTableResponse> AddIndexAsync(IAmazonDynamoDB client, string table, string indexname, string partitionkey, string partitionkeytype, string sortkey, string sortkeytype)
        {
            if (null == client)
            {
                throw new ArgumentNullException("client parameter is null");
            }

            if (string.IsNullOrEmpty(table))
            {
                throw new ArgumentNullException("table parameter is null");
            }

            if (string.IsNullOrEmpty(indexname))
            {
                throw new ArgumentNullException("indexname parameter is null");
            }

            if (string.IsNullOrEmpty(partitionkey))
            {
                throw new ArgumentNullException("partitionkey parameter is null");
            }

            if (string.IsNullOrEmpty(sortkey))
            {
                throw new ArgumentNullException("sortkey parameter is null");
            }

            ProvisionedThroughput pt = new ProvisionedThroughput
            {
                ReadCapacityUnits  = 10L,
                WriteCapacityUnits = 5L
            };

            KeySchemaElement kse1 = new KeySchemaElement
            {
                AttributeName = partitionkey,
                KeyType       = "HASH"
            };

            KeySchemaElement kse2 = new KeySchemaElement
            {
                AttributeName = sortkey,
                KeyType       = "RANGE"
            };

            List <KeySchemaElement> kses = new List <KeySchemaElement>
            {
                kse1,
                kse2
            };

            Projection p = new Projection
            {
                ProjectionType = "ALL"
            };

            var newIndex = new CreateGlobalSecondaryIndexAction()
            {
                IndexName             = indexname,
                ProvisionedThroughput = pt,
                KeySchema             = kses,
                Projection            = p
            };

            GlobalSecondaryIndexUpdate update = new GlobalSecondaryIndexUpdate
            {
                Create = newIndex
            };

            List <GlobalSecondaryIndexUpdate> updates = new List <GlobalSecondaryIndexUpdate>
            {
                update
            };

            AttributeDefinition ad1;

            if (partitionkeytype == "string")
            {
                ad1 = new AttributeDefinition
                {
                    AttributeName = partitionkey,
                    AttributeType = "S"
                };
            }
            else
            {
                ad1 = new AttributeDefinition
                {
                    AttributeName = partitionkey,
                    AttributeType = "N"
                };
            }

            AttributeDefinition ad2;

            if (sortkeytype == "string")
            {
                ad2 = new AttributeDefinition
                {
                    AttributeName = sortkey,
                    AttributeType = "S"
                };
            }
            else
            {
                ad2 = new AttributeDefinition
                {
                    AttributeName = sortkey,
                    AttributeType = "N"
                };
            }

            UpdateTableRequest request = new UpdateTableRequest
            {
                TableName            = table,
                AttributeDefinitions =
                {
                    ad1,
                    ad2
                },
                GlobalSecondaryIndexUpdates = updates
            };

            var response = await client.UpdateTableAsync(request);

            return(response);
        }
    private IEnumerable <CreateTableRequest> ReturnCreateTableRequests(IEnumerable <Type> types, string tablePrefix, IEnumerable <string> tableNames)
    {
        List <CreateTableRequest> tRequests = new List <CreateTableRequest>();

        foreach (var tbl in types)
        {
            var tblName = tablePrefix + tbl.GetCustomAttribute <DynamoDBTableAttribute>().TableName;
            if (!tableNames.Contains(tblName))
            {
                Projection projection = new Projection()
                {
                    ProjectionType = "INCLUDE"
                };
                List <string> nonKeyAttributes = tbl.GetProperties().Where(p => p.GetCustomAttribute <DynamoDBPropertyAttribute>() != null).Select(s => s.Name).ToList();
                projection.NonKeyAttributes = nonKeyAttributes;

                CreateTableRequest reqCreateTable = new CreateTableRequest();
                reqCreateTable.TableName             = tblName;
                reqCreateTable.ProvisionedThroughput = new ProvisionedThroughput();
                reqCreateTable.ProvisionedThroughput.ReadCapacityUnits  = 1;
                reqCreateTable.ProvisionedThroughput.WriteCapacityUnits = 1;

                var hashKeyProperty = tbl.GetProperties().FirstOrDefault(p => p.GetCustomAttribute <DynamoDBHashKeyAttribute>() != null);

                if (hashKeyProperty != null)
                {
                    string hashKeyName = hashKeyProperty.Name;
                    if (hashKeyProperty.GetCustomAttribute <DynamoDBHashKeyAttribute>().AttributeName != null)
                    {
                        hashKeyName = hashKeyProperty.GetCustomAttribute <DynamoDBHashKeyAttribute>().AttributeName;
                    }

                    var hashKeySchema = new KeySchemaElement()
                    {
                        AttributeName = hashKeyName, KeyType = KeyType.HASH
                    };
                    reqCreateTable.KeySchema.Add(hashKeySchema);

                    reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(hashKeyProperty, hashKeyName));

                    // adding range property if exists
                    var rangeyProperty = tbl.GetProperties().Where(p => p.GetCustomAttribute <DynamoDBRangeKeyAttribute>() != null).FirstOrDefault();

                    if (rangeyProperty != null)
                    {
                        string rangeKeyName = rangeyProperty.Name;
                        if (rangeyProperty.GetCustomAttribute <DynamoDBRangeKeyAttribute>().AttributeName != null)
                        {
                            rangeKeyName = rangeyProperty.GetCustomAttribute <DynamoDBRangeKeyAttribute>().AttributeName;
                        }

                        reqCreateTable.KeySchema.Add(new KeySchemaElement()
                        {
                            AttributeName = rangeKeyName, KeyType = KeyType.RANGE
                        });
                        reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(rangeyProperty, rangeKeyName));

                        // adding local secondary indexes
                        var localIndexes = tbl.GetProperties().Where(p => p.GetCustomAttribute <DynamoDBLocalSecondaryIndexRangeKeyAttribute>() != null);
                        foreach (var indexProperty in localIndexes)
                        {
                            // usually only one index is assigned
                            string indexName = indexProperty.GetCustomAttribute <DynamoDBLocalSecondaryIndexRangeKeyAttribute>().IndexNames.First();
                            List <KeySchemaElement> indexKeySchema = new List <KeySchemaElement>();
                            indexKeySchema.Add(hashKeySchema);
                            indexKeySchema.Add(new KeySchemaElement()
                            {
                                AttributeName = indexProperty.Name, KeyType = KeyType.RANGE
                            });

                            reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(indexProperty, indexProperty.Name));

                            LocalSecondaryIndex localSecondaryIndex = new LocalSecondaryIndex()
                            {
                                IndexName  = indexName,
                                KeySchema  = indexKeySchema,
                                Projection = projection
                            };

                            reqCreateTable.LocalSecondaryIndexes.Add(localSecondaryIndex);
                        }
                    }

                    // adding global secondary indexes
                    var globalIndexes = tbl.GetProperties().Where(p => p.GetCustomAttribute <DynamoDBGlobalSecondaryIndexHashKeyAttribute>() != null);
                    if (globalIndexes.Count() > 0)
                    {
                        foreach (var globalIndexProperty in globalIndexes)
                        {
                            var globalKeySchema = new List <KeySchemaElement>();

                            string globalHashKeyName = globalIndexProperty.Name;
                            if (globalIndexProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexHashKeyAttribute>().AttributeName != null)
                            {
                                globalHashKeyName = globalIndexProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexHashKeyAttribute>().AttributeName;
                            }

                            var globalHashKeySchema = new KeySchemaElement()
                            {
                                AttributeName = globalHashKeyName, KeyType = KeyType.HASH
                            };
                            globalKeySchema.Add(globalHashKeySchema);
                            reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(globalIndexProperty, globalHashKeyName));

                            string thisGlobalIndexName = globalIndexProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexHashKeyAttribute>().IndexNames.First();

                            // find the range keys related to this index
                            var globalRangeProperty = tbl.GetProperties().SingleOrDefault(p => p.GetCustomAttribute <DynamoDBGlobalSecondaryIndexRangeKeyAttribute>() != null && p.GetCustomAttribute <DynamoDBGlobalSecondaryIndexRangeKeyAttribute>().IndexNames.Contains(thisGlobalIndexName));
                            if (globalRangeProperty != null)
                            {
                                string globalRangeKeyName = globalRangeProperty.Name;
                                if (globalRangeProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexRangeKeyAttribute>().AttributeName != null)
                                {
                                    globalRangeKeyName = globalRangeProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexRangeKeyAttribute>().AttributeName;
                                }

                                var globalRangeKeySchema = new KeySchemaElement()
                                {
                                    AttributeName = globalRangeKeyName, KeyType = KeyType.RANGE
                                };
                                globalKeySchema.Add(globalRangeKeySchema);
                                reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(globalRangeProperty, globalRangeKeyName));
                            }

                            GlobalSecondaryIndex globalSecondaryIndex = new GlobalSecondaryIndex()
                            {
                                IndexName             = thisGlobalIndexName,
                                KeySchema             = globalKeySchema,
                                Projection            = projection,
                                ProvisionedThroughput = new ProvisionedThroughput(1, 1)
                            };

                            reqCreateTable.GlobalSecondaryIndexes.Add(globalSecondaryIndex);
                        }
                    }

                    tRequests.Add(reqCreateTable);
                }
            }
        }

        return(tRequests);
    }