Exemplo n.º 1
0
        private void CreateUserTable()
        {
            Console.WriteLine("Creating Table");
            tableName = "User";

            var request = new CreateTableRequest
            {
                AttributeDefinitions = new List <AttributeDefinition>
                {
                    new AttributeDefinition
                    {
                        AttributeName = "UserName",
                        AttributeType = "S"
                    }
                },
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement
                    {
                        AttributeName = "UserName",
                        KeyType       = "HASH" // Partition Key
                    }
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 5,
                    WriteCapacityUnits = 5
                },
                TableName = tableName
            };

            var response = _dynamoDbClient.CreateTableAsync(request);

            WaitUntilTableReady(tableName);
        }
Exemplo n.º 2
0
        public async Task Initialize(bool createTables = false, ProvisionedThroughput provisionedThroughput = null, StreamSpecification streamSpecification = null)
        {
            if (createTables)
            {
                Table table = _dynamoDBContext.GetTargetTable <TReadModel>();

                var createTableRequest = new CreateTableRequest()
                {
                    ProvisionedThroughput = provisionedThroughput,
                    TableName             = table.TableName,
                    LocalSecondaryIndexes = table.LocalSecondaryIndexes
                                            .Select(kv => new LocalSecondaryIndex()
                    {
                        KeySchema  = kv.Value.KeySchema,
                        IndexName  = kv.Value.IndexName,
                        Projection = kv.Value.Projection
                    }).ToList(),
                    GlobalSecondaryIndexes = table.GlobalSecondaryIndexes.Select(kv => new GlobalSecondaryIndex()
                    {
                        KeySchema             = kv.Value.KeySchema,
                        Projection            = kv.Value.Projection,
                        IndexName             = kv.Value.IndexName,
                        ProvisionedThroughput = new ProvisionedThroughput(kv.Value.ProvisionedThroughput.ReadCapacityUnits, kv.Value.ProvisionedThroughput.WriteCapacityUnits)
                    }).ToList(),
                    AttributeDefinitions = table.Attributes,
                    StreamSpecification  = streamSpecification,
                    KeySchema            = table.Keys.Select(kv => new KeySchemaElement(kv.Key, kv.Value.IsHash ? KeyType.HASH : KeyType.RANGE)).ToList()
                };

                await _dynamoDBClient.CreateTableAsync(createTableRequest).ConfigureAwait(false);
            }
        }
Exemplo n.º 3
0
        public async Task InitializeGameTable()
        {
            var tables = (await _client.ListTablesAsync()).TableNames;

            if (!tables.Contains("Games"))
            {
                var request = new CreateTableRequest
                {
                    TableName            = "Games",
                    AttributeDefinitions = new List <AttributeDefinition>
                    {
                        new AttributeDefinition
                        {
                            AttributeName = "GameId",
                            AttributeType = "S"
                        }
                    },
                    KeySchema = new List <KeySchemaElement>
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "GameId",
                            KeyType       = "HASH"
                        }
                    },
                    ProvisionedThroughput = new ProvisionedThroughput
                    {
                        ReadCapacityUnits  = 5,
                        WriteCapacityUnits = 5
                    }
                };

                await _client.CreateTableAsync(request);
            }
        }
Exemplo n.º 4
0
        private async Task CreateTableAsync()
        {
            try
            {
                await _client.CreateTableAsync(new CreateTableRequest
                {
                    TableName            = _settings.TableName,
                    AttributeDefinitions = new List <AttributeDefinition>
                    {
                        new AttributeDefinition
                        {
                            AttributeType = ScalarAttributeType.S,
                            AttributeName = ColumnNames.ResourceId
                        }
                    },
                    KeySchema = new List <KeySchemaElement>
                    {
                        new KeySchemaElement
                        {
                            KeyType       = KeyType.HASH,
                            AttributeName = ColumnNames.ResourceId
                        }
                    },
                    BillingMode = BillingMode.PAY_PER_REQUEST
                });

                // need to wait a bit since the table has just been created
                await Task.Delay(TimeSpan.FromSeconds(10));
            }
            catch (ResourceInUseException)
            {
                // ignore, already exists
            }
        }
Exemplo n.º 5
0
 public async Task StartAsync(CancellationToken cancellationToken)
 {
     await Policy.Handle <Exception>(exception =>
     {
         _logger.LogError(exception, "Failed to create Dynamo table");
         return(true);
     }).WaitAndRetryForeverAsync(i => TimeSpan.FromSeconds(5)).ExecuteAsync(async() => {
         _logger.LogInformation("Creating Dynamo table");
         return(await _dynamo.CreateTableAsync("Payments",
                                               new List <KeySchemaElement> {
             new KeySchemaElement {
                 AttributeName = "PaymentId", KeyType = KeyType.HASH
             }
         },
                                               new List <AttributeDefinition> {
             new AttributeDefinition {
                 AttributeName = "PaymentId", AttributeType = ScalarAttributeType.S
             }
         },
                                               new ProvisionedThroughput {
             ReadCapacityUnits = 5, WriteCapacityUnits = 5
         },
                                               cancellationToken));
     });
 }
Exemplo n.º 6
0
        //Create Table if it does not exists
        private async Task CreateTable(IAmazonDynamoDB amazonDynamoDBclient, string tableName)
        {
            //Write Log to Cloud Watch using LambdaLogger.Log Method
            LambdaLogger.Log(string.Format("Creating {0} Table", tableName));

            var tableCollection = await amazonDynamoDBclient.ListTablesAsync();

            if (!tableCollection.TableNames.Contains(tableName))
            {
                await amazonDynamoDBclient.CreateTableAsync(new CreateTableRequest
                {
                    TableName = tableName,
                    KeySchema = new List <KeySchemaElement> {
                        { new KeySchemaElement {
                              AttributeName = "Username", KeyType = KeyType.HASH
                          } }
                    },
                    AttributeDefinitions = new List <AttributeDefinition> {
                        new AttributeDefinition {
                            AttributeName = "Username", AttributeType = "S"
                        }
                    },
                    ProvisionedThroughput = new ProvisionedThroughput
                    {
                        ReadCapacityUnits  = 5,
                        WriteCapacityUnits = 5
                    },
                });
            }
        }
Exemplo n.º 7
0
        private async Task <bool> SetUpTable()
        {
            var createRequest = new CreateTableRequest
            {
                TableName            = Constants.UserTableName,
                AttributeDefinitions = new List <AttributeDefinition>
                {
                    new AttributeDefinition
                    {
                        AttributeName = "id",
                        AttributeType = "N"
                    }
                },
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement
                    {
                        AttributeName = "id",
                        KeyType       = "HASH" //Partition key
                    }
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 3,
                    WriteCapacityUnits = 3
                }
            };
            await _amazonDynamoDB.CreateTableAsync(createRequest);

            return(true);
        }
Exemplo n.º 8
0
        public async Task CreateDynamoTable(string tableName)
        {
            //Might want to do a check to wait for the table to be created before passing back a response

            var request = new CreateTableRequest
            {
                TableName            = tableName,
                AttributeDefinitions = new List <AttributeDefinition>()
                {
                    new AttributeDefinition
                    {
                        AttributeName = "Id",
                        AttributeType = "N"
                    }
                },
                KeySchema = new List <KeySchemaElement>()
                {
                    new KeySchemaElement
                    {
                        AttributeName = "Id",
                        KeyType       = "HASH"
                    }
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 1,
                    WriteCapacityUnits = 1
                }
            };

            await _dynamoDbClient.CreateTableAsync(request);
        }
Exemplo n.º 9
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;

        var requests = ReturnCreateTableRequests(types, tablePrefix, tableNames);

        List <Task <CreateTableResponse> > createTableTasks = new List <Task <CreateTableResponse> >();

        foreach (var reqCreateTable in requests)
        {
            createTableTasks.Add(_oDynamoDBClient.CreateTableAsync(reqCreateTable));
        }

        await Task.WhenAll(createTableTasks.ToArray());

        var createdTableNames = createTableTasks.Select(t => t.Result.TableDescription.TableName);

        // tables aren't created instantly
        await WaitUntilTableReadyAsync(createdTableNames.ToArray());

        foreach (var kv in createdTableNames)
        {
            oSb.AppendLine($"{kv}");
        }

        return(oSb);
    }
Exemplo n.º 10
0
        private void CreateTempTable()
        {
            Console.WriteLine("Creating table");

            var request = new CreateTableRequest
            {
                AttributeDefinitions = new List <AttributeDefinition>
                {
                    new AttributeDefinition
                    {
                        AttributeName = "Id",
                        AttributeType = "N"
                    }
                },
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement
                    {
                        AttributeName = "Id",
                        KeyType       = "HASH"
                    }
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 5,
                    WriteCapacityUnits = 5
                },
                TableName = "testTemp"
            };

            var response = _client.CreateTableAsync(request);

            waitUntileTableRead("testTemp");
        }
        public DynamoDbGlobalTableTest(DynamoDBContainerFixture dynamoDbContainerFixture, ConfigFixture configFixture)
        {
            hostName           = dynamoDbContainerFixture.HostName;
            this.configFixture = configFixture;

            // Use AWS SDK to create client and initialize table
            AmazonDynamoDBConfig amazonDynamoDbConfig = new AmazonDynamoDBConfig
            {
                ServiceURL           = dynamoDbContainerFixture.ServiceUrl,
                AuthenticationRegion = "us-west-2",
            };

            tempDynamoDbClient = new AmazonDynamoDBClient(amazonDynamoDbConfig);
            CreateTableRequest request = new CreateTableRequest
            {
                TableName            = DefaultTableName,
                AttributeDefinitions = new List <AttributeDefinition>
                {
                    new AttributeDefinition(PartitionKey, ScalarAttributeType.S),
                    new AttributeDefinition(SortKey, ScalarAttributeType.N),
                },
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement(PartitionKey, KeyType.HASH),
                    new KeySchemaElement(SortKey, KeyType.RANGE),
                },
                ProvisionedThroughput = new ProvisionedThroughput(1L, 1L),
            };

            tempDynamoDbClient.CreateTableAsync(request).Wait();
        }
        private async Task CreateDeliveryTable()
        {
            _logger.LogInformation($"Creating new DynamoDb table {Constants.DynamoDb.DeliveryTableName}");

            var request = new CreateTableRequest
            {
                AttributeDefinitions = new List <AttributeDefinition>
                {
                    new AttributeDefinition
                    {
                        AttributeName = nameof(DeliveryRecord.DeliveryId),
                        AttributeType = "S"
                    }
                },
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement
                    {
                        AttributeName = Constants.DynamoDb.DeliveryTablePartitionKey,
                        KeyType       = "HASH"
                    }
                },
                ProvisionedThroughput = new ProvisionedThroughput()
                {
                    ReadCapacityUnits  = 10,
                    WriteCapacityUnits = 10
                },
                TableName = Constants.DynamoDb.DeliveryTableName
            };

            await _dynamoDbClient.CreateTableAsync(request);
        }
Exemplo n.º 13
0
        public async Task CreateDynamoDbTable(string tableName)
        {
            var request = new CreateTableRequest
            {
                TableName            = tableName,
                AttributeDefinitions = new List <AttributeDefinition>()
                {
                    new AttributeDefinition
                    {
                        AttributeName = "Id",
                        AttributeType = "N"
                    }
                },
                KeySchema = new List <KeySchemaElement>()
                {
                    new KeySchemaElement
                    {
                        AttributeName = "Id",
                        KeyType       = "HASH"
                    }
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 1,
                    WriteCapacityUnits = 1
                }
            };

            await _dynamoDbClient.CreateTableAsync(request);
        }
        private Amazon.DynamoDBv2.Model.CreateTableResponse CallAWSServiceOperation(IAmazonDynamoDB client, Amazon.DynamoDBv2.Model.CreateTableRequest request)
        {
            Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon DynamoDB", "CreateTable");

            try
            {
#if DESKTOP
                return(client.CreateTable(request));
#elif CORECLR
                return(client.CreateTableAsync(request).GetAwaiter().GetResult());
#else
#error "Unknown build edition"
#endif
            }
            catch (AmazonServiceException exc)
            {
                var webException = exc.InnerException as System.Net.WebException;
                if (webException != null)
                {
                    throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException);
                }

                throw;
            }
        }
Exemplo n.º 15
0
        public Task CreateTableAsync(
            string tableName,
            IReadOnlyList <KeySchemaElement> keySchema,
            IReadOnlyList <AttributeDefinition> attributes,
            ProvisionedThroughput provisionedThroughput)
        {
            if (string.IsNullOrWhiteSpace(tableName))
            {
                throw new ArgumentException("Table name is required.", nameof(tableName));
            }

            return(createTableAsync());

            async Task createTableAsync()
            {
                if (!await IsExistingTableAsync(tableName))
                {
                    var request = new CreateTableRequest
                    {
                        TableName             = tableName,
                        AttributeDefinitions  = attributes.ToList(),
                        KeySchema             = keySchema.ToList(),
                        ProvisionedThroughput = provisionedThroughput
                    };

                    await _dynamoDb.CreateTableAsync(request);
                }
            }
        }
Exemplo n.º 16
0
        private async Task CreateTable()
        {
            var createTableRequest = new CreateTableRequest
            {
                TableName            = ShortLink.TableName,
                AttributeDefinitions = new List <AttributeDefinition>
                {
                    new AttributeDefinition
                    {
                        AttributeName = nameof(ShortLink.Key),
                        AttributeType = "S"
                    }
                },
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement
                    {
                        AttributeName = nameof(ShortLink.Key),
                        KeyType       = "HASH"
                    }
                },
                ProvisionedThroughput = new ProvisionedThroughput(5, 5)
            };

            await _dynamoDbClient.CreateTableAsync(createTableRequest);
        }
Exemplo n.º 17
0
 private async Task <CreateTableResponse> CreateTableIfNotExists(List <string> existingTables, string tableName)
 {
     if (!existingTables.Contains(tableName))
     {
         var createRequest = new CreateTableRequest
         {
             TableName            = tableName,
             AttributeDefinitions = new List <AttributeDefinition>
             {
                 new AttributeDefinition
                 {
                     AttributeName = "Id",
                     AttributeType = "S"
                 }
             },
             KeySchema = new List <KeySchemaElement>
             {
                 new KeySchemaElement
                 {
                     AttributeName = "Id",
                     KeyType       = "HASH" //Partition key
                 }
             },
             ProvisionedThroughput = new ProvisionedThroughput
             {
                 ReadCapacityUnits  = 1,
                 WriteCapacityUnits = 1
             },
         };
         return(await amazonDynamoDb.CreateTableAsync(createRequest));
     }
     return(null);
 }
Exemplo n.º 18
0
        public static async Task <DescribeTableResponse> CreateTableForum(IAmazonDynamoDB client)
        {
            string tableName = "Forum";

            var response = await client.CreateTableAsync(new CreateTableRequest
            {
                TableName            = tableName,
                AttributeDefinitions = new List <AttributeDefinition>()
                {
                    new AttributeDefinition
                    {
                        AttributeName = "Name",
                        AttributeType = "S"
                    }
                },
                KeySchema = new List <KeySchemaElement>()
                {
                    new KeySchemaElement
                    {
                        AttributeName = "Name", // forum Title
                        KeyType       = "HASH"
                    }
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 10,
                    WriteCapacityUnits = 5
                }
            });

            var result = await WaitTillTableCreated(client, tableName, response);

            return(result);
        }
Exemplo n.º 19
0
        private async Task CreateTable()
        {
            var createRequest = new CreateTableRequest(_tableName, new List <KeySchemaElement>()
            {
                new KeySchemaElement("id", KeyType.HASH)
            })
            {
                AttributeDefinitions = new List <AttributeDefinition>()
                {
                    new AttributeDefinition("id", ScalarAttributeType.S)
                },
                BillingMode = BillingMode.PAY_PER_REQUEST
            };

            var createResponse = await _client.CreateTableAsync(createRequest);

            int  i       = 0;
            bool created = false;

            while ((i < 20) && (!created))
            {
                try
                {
                    await Task.Delay(1000);

                    var poll = await _client.DescribeTableAsync(_tableName);

                    created = (poll.Table.TableStatus == TableStatus.ACTIVE);
                    i++;
                }
                catch (ResourceNotFoundException)
                {
                }
            }
        }
Exemplo n.º 20
0
        public async Task <IActionResult> Create([FromBody] CreateTableRequest request)
        {
            Console.WriteLine("Request Info:");
            Console.WriteLine("\tTableName:");
            Console.WriteLine($"\t{request.TableName}");
            if (request.ProvisionedThroughput != null)
            {
                Console.WriteLine("\tReadCapacityUnits:");
                Console.WriteLine($"\t{request.ProvisionedThroughput.ReadCapacityUnits}");
                Console.WriteLine("\tWriteCapacityUnits:");
                Console.WriteLine($"\t{request.ProvisionedThroughput.WriteCapacityUnits}");
            }
            if (request.KeySchema != null)
            {
                System.Console.WriteLine("\tKeySchemas:");
                foreach (var k in request.KeySchema)
                {
                    Console.WriteLine("\tAttributeName");
                    Console.WriteLine($"\t{k.AttributeName}");
                    Console.WriteLine("\tKeyType");
                    Console.WriteLine($"\t{k.KeyType}");
                }
            }
            try
            {
                var res = await _dynamoClient.CreateTableAsync(request);

                Console.WriteLine("Created table");
                System.Console.WriteLine("\tTableName");
                System.Console.WriteLine($"\t{res.TableDescription.TableName}");
                System.Console.WriteLine("\tTableArn");
                System.Console.WriteLine($"\t{res.TableDescription.TableArn}");
                System.Console.WriteLine("\tTableId");
                System.Console.WriteLine($"\t{res.TableDescription.TableId}");
                System.Console.WriteLine("\tTableStatus");
                System.Console.WriteLine($"\t{res.TableDescription.TableStatus}");
                return(new JsonResult(
                           new
                {
                    message = $"Creating new table: {res.TableDescription.TableName}",
                    TableDescription = res.TableDescription
                })
                {
                    StatusCode = 202
                });
            }
            catch (AmazonDynamoDBException addbe)
            {
                return(AmazonExceptionHandlers.HandleAmazonDynamoDBException(addbe));
            }
            catch (AmazonServiceException ase)
            {
                AmazonExceptionHandlers.HandleAmazonServiceExceptionException(ase);
            }
            catch (AmazonClientException ace)
            {
                AmazonExceptionHandlers.HandleAmazonClientExceptionException(ace);
            }
            return(StatusCode(500));
        }
Exemplo n.º 21
0
        private async Task <string> SetupTable(IAmazonDynamoDB dynamoDBClient)
        {
            string tableName = "aws-sdk-dotnet-truncate-test-" + DateTime.Now.Ticks;

            await dynamoDBClient.CreateTableAsync(
                tableName,
                new List <KeySchemaElement>
            {
                new KeySchemaElement {
                    KeyType = KeyType.HASH, AttributeName = "Id"
                }
            },
                new List <AttributeDefinition>
            {
                new AttributeDefinition {
                    AttributeName = "Id", AttributeType = ScalarAttributeType.S
                }
            },
                new ProvisionedThroughput { ReadCapacityUnits = 10, WriteCapacityUnits = 10 });

            DescribeTableResponse response = null;

            do
            {
                System.Threading.Thread.Sleep(300);
                response = await dynamoDBClient.DescribeTableAsync(tableName);
            } while (response.Table.TableStatus != TableStatus.ACTIVE);

            return(tableName);
        }
        /// <summary>
        /// Creates a table in dynamodb.
        /// </summary>
        /// <param name="tableName">The name of the table to create.</param>
        /// <param name="tableThroughput">Initial provisioned throughput settings for the table.</param>
        /// <param name="indexThroughput">Initial provisioned throughput settings for the indexes.</param>
        public async void CreateTable(
            string tableName,
            ProvisionedThroughput tableThroughput,
            ProvisionedThroughput indexThroughput)
        {
            _logger.LogInformation("Starting CreateTable using table name {TableName}.", tableName);
            var request = new CreateTableRequest
            {
                TableName            = tableName,
                AttributeDefinitions = new List <AttributeDefinition>()
                {
                    new AttributeDefinition
                    {
                        AttributeName = "Id",
                        AttributeType = "S"
                    }
                },
                KeySchema = new List <KeySchemaElement>()
                {
                    new KeySchemaElement
                    {
                        AttributeName = "Id",
                        KeyType       = "HASH"
                    }
                },
                ProvisionedThroughput = tableThroughput
            };

            try
            {
                var response = await _amazonDynamoDB.CreateTableAsync(request);

                var tableDescription = response.TableDescription;
                _logger.LogInformation("{TableStatus}: {TableName} \t ReadsPerSec: {ReadCapacityUnits} \t WritesPerSec: {WriteCapacityUnits}",
                                       tableDescription.TableStatus,
                                       tableDescription.TableName,
                                       tableDescription.ProvisionedThroughput.ReadCapacityUnits,
                                       tableDescription.ProvisionedThroughput.WriteCapacityUnits);

                string status = tableDescription.TableStatus;
                _logger.LogInformation("{TableName} - {Status}", tableName, status);

                if (status != "ACTIVE")
                {
                    //Await for table to be created
                    WaitUntilTableReady(tableName);
                }
            }
            catch (ResourceInUseException rx)
            {
                _logger.LogInformation("The table has already been created. {Message}", rx.Message);
            }
            catch (Exception ex)
            {
                _logger.LogError("There was a fatal error when provisioning dynamodb. {Exception}", ex);
                //throw exception and kill the service.
                throw ex;
            }
        }
        private async Task CreateIfNotExist(CreateTableRequest request, string tableName)
        {
            if (await TableExist(tableName))
            {
                return;
            }

            await _dynamoDb.CreateTableAsync(request);
        }
Exemplo n.º 24
0
        static async Task <CreateTableResponse> MakeTableAsync(IAmazonDynamoDB client, string table)
        {
            var response = await client.CreateTableAsync(new CreateTableRequest
            {
                TableName            = table,
                AttributeDefinitions = new List <AttributeDefinition>
                {
                    new AttributeDefinition
                    {
                        AttributeName = "ID",
                        AttributeType = "S"
                    },
                    new AttributeDefinition
                    {
                        AttributeName = "Area",
                        AttributeType = "S"
                    }
                },
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement
                    {
                        AttributeName = "ID",
                        KeyType       = "HASH"
                    },
                    new KeySchemaElement
                    {
                        AttributeName = "Area",
                        KeyType       = "RANGE"
                    }
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 10,
                    WriteCapacityUnits = 5
                }
            });

            // Wait for table to be created
            bool ready = false;
            int  wait  = 1; // Milliseconds to wait

            while (!ready)
            {
                Thread.Sleep(wait);

                var resp = await client.DescribeTableAsync(new DescribeTableRequest
                {
                    TableName = table
                });

                ready = (resp.Table.TableStatus == TableStatus.ACTIVE);
                wait *= 2;
            }

            return(response);
        }
        public void CreateTable(string tableName, string templatePath, string logicalName)
        {
            var createTableReq = TemplateParser.GetDynamoDbTable(templatePath, logicalName);

            createTableReq.TableName = tableName;
            var tableTask = dynamoDbClient.CreateTableAsync(createTableReq);

            tableTask.Wait();
            WaitUntilTableIsActive(tableName);
        }
Exemplo n.º 26
0
        private static async Task CreateTable(IAmazonDynamoDB client, CancellationToken token)
        {
            CreateTableRequest request = new CreateTableRequest()
            {
                TableName            = MyTableName,
                AttributeDefinitions =
                {
                    new AttributeDefinition
                    {
                        AttributeName = "Id",
                        AttributeType = "N"
                    },
                    new AttributeDefinition
                    {
                        AttributeName = "Movie",
                        AttributeType = "S"
                    }
                },
                KeySchema =
                {
                    new KeySchemaElement
                    {
                        AttributeName = "Id",
                        KeyType       = "HASH" // Partition Key
                    },
                    new KeySchemaElement
                    {
                        AttributeName = "Movie",
                        KeyType       = "Range" // Sort Key
                    }
                },

                BillingMode = BillingMode.PAY_PER_REQUEST
            };

            await client.CreateTableAsync(request, token);

            DescribeTableResponse response;

            do
            {
                Thread.Sleep(1000);
                Console.WriteLine($"Creating table {MyTableName}...");

                response = await client.DescribeTableAsync(MyTableName, token);
            } while (response.Table.TableStatus == TableStatus.CREATING);

            if (response.Table.TableStatus == TableStatus.ACTIVE)
            {
                Console.WriteLine($"\nTable {MyTableName} was successfully created.");
            }

            Console.WriteLine("\nPress any key to continue...\n");
            Console.ReadKey();
        }
        /// <summary>
        /// Helper function to create a testing table
        /// </summary>
        /// <returns></returns>
        private async Task SetupTableAsync()
        {
            var listTablesResponse = await _dynamoDbClient.ListTablesAsync(new ListTablesRequest());

            var existingTestTable =
                listTablesResponse.TableNames.FindAll(s => s.StartsWith(TablePrefix)).FirstOrDefault();

            if (existingTestTable == null)
            {
                _tableName = TablePrefix + DateTime.Now.Ticks;

                CreateTableRequest request = new CreateTableRequest
                {
                    TableName             = _tableName,
                    ProvisionedThroughput = new ProvisionedThroughput
                    {
                        ReadCapacityUnits  = 2,
                        WriteCapacityUnits = 2
                    },
                    KeySchema = new List <KeySchemaElement>
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "IncidentId"
                        }
                    },
                    AttributeDefinitions = new List <AttributeDefinition>
                    {
                        new AttributeDefinition
                        {
                            AttributeName = "IncidentId",
                            AttributeType = ScalarAttributeType.S
                        }
                    }
                };

                await _dynamoDbClient.CreateTableAsync(request);

                var describeRequest = new DescribeTableRequest {
                    TableName = _tableName
                };
                DescribeTableResponse response;

                do
                {
                    Thread.Sleep(1000);
                    response = await _dynamoDbClient.DescribeTableAsync(describeRequest);
                } while (response.Table.TableStatus != TableStatus.ACTIVE);
            }
            else
            {
                Console.WriteLine($"Using existing test table {existingTestTable}");
                _tableName = existingTestTable;
            }
        }
Exemplo n.º 28
0
        public async Task Initialise()
        {
            var request = new ListTablesRequest
            {
                Limit = 10
            };

            var response = await _amazonDynamoDb.ListTablesAsync(request);

            var results = response.TableNames;

            if (!results.Contains(PendingRequestsTableName))
            {
                var createRequest = new CreateTableRequest
                {
                    TableName            = PendingRequestsTableName,
                    AttributeDefinitions = new List <AttributeDefinition>
                    {
                        new AttributeDefinition
                        {
                            AttributeName = "ApplicationId",
                            AttributeType = "S"
                        }
                    },
                    KeySchema = new List <KeySchemaElement>
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "ApplicationId",
                            KeyType       = "HASH" //Partition key
                        }
                    },
                    ProvisionedThroughput = new ProvisionedThroughput
                    {
                        ReadCapacityUnits  = 2,
                        WriteCapacityUnits = 2
                    }
                };

                await _amazonDynamoDb.CreateTableAsync(createRequest);
            }
        }
Exemplo n.º 29
0
        public async Task CreateOwnerTable()
        {
            try
            {
                var tableName = "Owners";

                var request = new CreateTableRequest
                {
                    AttributeDefinitions = new List <AttributeDefinition>
                    {
                        new AttributeDefinition
                        {
                            AttributeName = "OwnerId",
                            AttributeType = "N"
                        }
                    },
                    KeySchema = new List <KeySchemaElement>
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "OwnerId",
                            KeyType       = "HASH" // Partition Key
                        }
                    },
                    ProvisionedThroughput = new ProvisionedThroughput
                    {
                        ReadCapacityUnits  = 5,
                        WriteCapacityUnits = 5
                    },
                    TableName = tableName
                };

                DynamoDbClient.CreateTableAsync(request);

                WaitUntilTableReady(tableName);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Exemplo n.º 30
0
        /// <summary>
        /// Creates the Recipes table, if it does not already exist.
        /// </summary>
        /// <returns>True if there's a good table to use, false otherwise.</returns>
        public static async Task <bool> EnsureTableExists(IAmazonDynamoDB client)
        {
            Console.WriteLine($"Dynamo URL is {client.Config.ServiceURL}");
            Console.WriteLine($"Dynamo RegionName is {client.Config.RegionEndpointServiceName}");
            var request = new ListTablesRequest {
                Limit = 10
            };
            var response = await client.ListTablesAsync(request);

            if (!response.TableNames.Contains("Recipe"))
            {
                var createRequest = new CreateTableRequest
                {
                    TableName            = "Recipe",
                    AttributeDefinitions = new List <AttributeDefinition>
                    {
                        new AttributeDefinition
                        {
                            AttributeName = "UserId",
                            AttributeType = "S"
                        },
                        new AttributeDefinition
                        {
                            AttributeName = "RecipeId",
                            AttributeType = "N"
                        }
                    },
                    KeySchema = new List <KeySchemaElement>
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "UserId",
                            KeyType       = "HASH"
                        },
                        new KeySchemaElement
                        {
                            AttributeName = "RecipeId",
                            KeyType       = "RANGE"
                        }
                    },
                    ProvisionedThroughput = new ProvisionedThroughput
                    {
                        ReadCapacityUnits  = 5,
                        WriteCapacityUnits = 5
                    }
                };

                var created = await client.CreateTableAsync(createRequest);

                return(created.HttpStatusCode == System.Net.HttpStatusCode.OK);
            }

            return(true); // It already existed.
        }
Exemplo n.º 31
0
        private async Task<string> SetupTable(IAmazonDynamoDB dynamoDBClient)
        {
            string tableName = "aws-sdk-dotnet-truncate-test-" + DateTime.Now.Ticks;

            await dynamoDBClient.CreateTableAsync(
                tableName,
                new List<KeySchemaElement>
                {
                    new KeySchemaElement { KeyType = KeyType.HASH, AttributeName = "Id" }
                },
                new List<AttributeDefinition>
                {
                    new AttributeDefinition { AttributeName = "Id", AttributeType = ScalarAttributeType.S }
                },
                new ProvisionedThroughput { ReadCapacityUnits = 10, WriteCapacityUnits = 10 });

            DescribeTableResponse response = null;
            do
            {
                System.Threading.Thread.Sleep(300);
                response = await dynamoDBClient.DescribeTableAsync(tableName);
            } while (response.Table.TableStatus != TableStatus.ACTIVE);

            return tableName;
        }