Example #1
0
        public async Task <IActionResult> Get(string tableName)
        {
            Console.WriteLine("*** Retrieving table information ***");
            Console.WriteLine($"Searching for table: {tableName}");

            DescribeTableRequest req = new DescribeTableRequest();

            req.TableName = tableName;
            try
            {
                var res = await _dynamoClient.DescribeTableAsync(req);

                var description = res.Table;
                Console.WriteLine($"Name: {description.TableName}");
                Console.WriteLine($"# of items: {description.ItemCount}");
                Console.WriteLine($"Provision Throughput (reads/sec): {description.ProvisionedThroughput.ReadCapacityUnits}");
                Console.WriteLine($"Provision Throughput (writes/sec): {description.ProvisionedThroughput.WriteCapacityUnits}");
                return(Ok(res.Table));
            }
            catch (AmazonDynamoDBException addbe)
            {
                return(AmazonExceptionHandlers.HandleAmazonDynamoDBException(addbe));
            }
            catch (AmazonServiceException ase)
            {
                AmazonExceptionHandlers.HandleAmazonServiceExceptionException(ase);
            }
            catch (AmazonClientException ace)
            {
                AmazonExceptionHandlers.HandleAmazonClientExceptionException(ace);
            }
            return(StatusCode(500));
        }
Example #2
0
 private async Task EnsureTable()
 {
     try
     {
         var poll = await _client.DescribeTableAsync(_tableName);
     }
     catch (ResourceNotFoundException)
     {
         await CreateTable();
     }
 }
Example #3
0
 private async Task EnsureTable(string tableName, Func <Task> createTask)
 {
     try
     {
         await _client.DescribeTableAsync(tableName);
     }
     catch (ResourceNotFoundException)
     {
         _logger.LogWarning($"Provisioning DynamoDb table - {tableName}");
         await createTask();
     }
 }
 public async Task Provision()
 {
     try
     {
         var poll = await _client.DescribeTableAsync(_tableName);
     }
     catch (ResourceNotFoundException)
     {
         _logger.LogInformation($"Creating lock table {_tableName}");
         await CreateTable();
         await SetPurgeTTL();
     }
 }
        public async Task Healthy_Dynamo_Connection_Returns_Healthy()
        {
            _awsDynamo.DescribeTableAsync(Arg.Any <string>()).Returns(new DescribeTableResponse
            {
                Table = new TableDescription
                {
                    TableStatus = TableStatus.ACTIVE
                }
            });

            var check  = new DynamoDbHealthCheck(_pocoDynamo, new NullLoggerFactory());
            var health = await check.CheckHealthAsync(new HealthCheckContext());

            Assert.Equal(HealthStatus.Healthy, health.Status);
        }
Example #6
0
        public static async void GetTablesDetails()
        {
            List <string> tables = await GetTablesList();

            using (IAmazonDynamoDB client = GetDynamoDbClient())
            {
                foreach (string table in tables)
                {
                    DescribeTableRequest  describeTableRequest  = new DescribeTableRequest(table);
                    DescribeTableResponse describeTableResponse = await client.DescribeTableAsync(describeTableRequest);

                    TableDescription tableDescription = describeTableResponse.Table;
                    Debug.WriteLine(string.Format("Printing information about table {0}:", tableDescription.TableName));
                    Debug.WriteLine(string.Format("Created at: {0}", tableDescription.CreationDateTime));
                    List <KeySchemaElement> keySchemaElements = tableDescription.KeySchema;
                    foreach (KeySchemaElement schema in keySchemaElements)
                    {
                        Debug.WriteLine(string.Format("Key name: {0}, key type: {1}", schema.AttributeName, schema.KeyType));
                    }
                    Debug.WriteLine(string.Format("Item count: {0}", tableDescription.ItemCount));
                    ProvisionedThroughputDescription throughput = tableDescription.ProvisionedThroughput;
                    Debug.WriteLine(string.Format("Read capacity: {0}", throughput.ReadCapacityUnits));
                    Debug.WriteLine(string.Format("Write capacity: {0}", throughput.WriteCapacityUnits));
                    List <AttributeDefinition> tableAttributes = tableDescription.AttributeDefinitions;
                    foreach (AttributeDefinition attDefinition in tableAttributes)
                    {
                        Debug.WriteLine(string.Format("Table attribute name: {0}", attDefinition.AttributeName));
                        Debug.WriteLine(string.Format("Table attribute type: {0}", attDefinition.AttributeType));
                    }
                    Debug.WriteLine(string.Format("Table size: {0}b", tableDescription.TableSizeBytes));
                    Debug.WriteLine(string.Format("Table status: {0}", tableDescription.TableStatus));
                    Debug.WriteLine("====================================================");
                }
            }
        }
        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);
        }
Example #8
0
        public async Task <TableDescription> GetResourceAsync(string name)
        {
            await CheckTableNamesLoaded();

            if (!_tableNames.Contains(name))
            {
                return(null);
            }

            if (_cachedTableDescriptions.ContainsKey(name))
            {
                return(_cachedTableDescriptions[name]);
            }

            DescribeTableResponse tableResponse;

            try
            {
                tableResponse = await _dynamoDb.DescribeTableAsync(name);
            }
            catch (ResourceNotFoundException)
            {
                return(null);
            }

            _cachedTableDescriptions.Add(tableResponse.Table.TableName, tableResponse.Table);

            return(tableResponse.Table);
        }
Example #9
0
        public void WaitUntilTableReady()
        {
            string status = null;

            do
            {
                Thread.Sleep(5000);
                try
                {
                    var res = _dynamoDbClient.DescribeTableAsync(new DescribeTableRequest
                    {
                        TableName = "Requirements"
                    });
                    status = res.Result.Table.TableStatus;
                }
                catch (ResourceNotFoundException ex)
                {
                    Console.WriteLine(ex);
                    throw;
                }
            }while (status != "ACTIVE");
            {
                Console.WriteLine("Table created successfully");
            }
        }
        private void TryAgainToCreateTable(string tableName)
        {
            string status = string.Empty;

            do
            {
                Thread.Sleep(5000);

                try
                {
                    var res = _amazonDynamoDBClient.DescribeTableAsync(new DescribeTableRequest()
                    {
                        TableName = tableName
                    });

                    status = res.Result.Table.TableStatus;
                }
                catch (Exception e)
                {
                    throw;
                }
            }while (status != "ACTIVE");
            {
                Console.WriteLine("Table Created Successfully");
            }
        }
Example #11
0
    private async Task WaitUntilTableReadyAsync(string tableName)
    {
        string status = null;

        // Let us wait until table is created. Call DescribeTable.
        do
        {
            System.Threading.Thread.Sleep(5000);     // Wait 5 seconds.
            try
            {
                var res = await _oDynamoDBClient.DescribeTableAsync(new DescribeTableRequest
                {
                    TableName = tableName
                });

                Console.WriteLine("Table name: {0}, status: {1}",
                                  res.Table.TableName,
                                  res.Table.TableStatus);
                status = res.Table.TableStatus;
            }
            catch (ResourceNotFoundException)
            {
                // DescribeTable is eventually consistent. So you might
                // get resource not found. So we handle the potential exception.
            }
        } while (status != "ACTIVE");
    }
Example #12
0
    private async Task WaitUntilTableReadyAsync(params string[] tableNames)
    {
        bool completed = false;

        List <Task <DescribeTableResponse> > describeTasks = null;

        IEnumerable <DescribeTableRequest> describeTableRequests = tableNames.Select(t => new DescribeTableRequest {
            TableName = t
        });

        // Let us wait until table is created. Call DescribeTable.
        do
        {
            describeTasks = new List <Task <DescribeTableResponse> >();

            System.Threading.Thread.Sleep(5000);     // Wait 5 seconds.
            try
            {
                foreach (var req in describeTableRequests)
                {
                    describeTasks.Add(_oDynamoDBClient.DescribeTableAsync(req));
                }

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

                completed = describeTasks.All(t => t.Result.Table.TableStatus == "ACTIVE");
            }
            catch (ResourceNotFoundException)
            {
                // DescribeTable is eventually consistent. So you might
                // get resource not found. So we handle the potential exception.
            }
        } while (!completed);
    }
Example #13
0
        private void waitUntileTableRead(string table)
        {
            string status = null;

            do
            {
                Thread.Sleep(5000);
                try
                {
                    var res = _client.DescribeTableAsync(new DescribeTableRequest
                    {
                        TableName = "testTemp"
                    });

                    status = res.Result.Table.TableStatus;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            } while (status != "ACTIVE");
            {
                Console.WriteLine("Success");
            }
        }
        public async Task <bool> CheckHealthAsync()
        {
            //using AmazonDynamoDBClient client = new();
            DescribeTableResponse tableData = await _amazonDynamoDBClient.DescribeTableAsync("Adverts");

            return(string.Compare(tableData.Table.TableStatus, "active", true) == 0);
        }
Example #15
0
        private static async Task DeleteTable(IAmazonDynamoDB client, CancellationToken token)
        {
            DeleteTableRequest request = new DeleteTableRequest(MyTableName);

            await client.DeleteTableAsync(request, token);

            DescribeTableResponse response;

            string expectedExceptionMessage =
                $"Requested resource not found: Table: {MyTableName} not found";

            try
            {
                do
                {
                    Thread.Sleep(2000);
                    Console.WriteLine($"Deleting table {MyTableName}...");

                    response = await client.DescribeTableAsync(MyTableName, token);
                } while (response.Table.TableStatus == TableStatus.DELETING);
            }
            catch (ResourceNotFoundException e) when(CheckDeleteTableException(e, expectedExceptionMessage))
            {
                Console.WriteLine($"\nTable {MyTableName} was successfully deleted.");
            }

            Console.WriteLine("\nPress any key to continue...\n");
            Console.ReadKey();
        }
        private bool WaitUntilTableIsReady(IAmazonDynamoDB dynamoDBClient)
        {
            string status = null;

            do
            {
                try
                {
                    var res = dynamoDBClient.DescribeTableAsync(
                        new DescribeTableRequest
                    {
                        TableName = _tableName
                    });
                    status = res.Result.Table.TableStatus;
                }
                catch (Exception ex)
                {
                    throw;
                }
            } while (status != "ACTIVE");
            {
                Console.WriteLine("Table created!");
                return(true);
            }
        }
Example #17
0
        /*--------------------------------------------------------------------------
         *                      checkingTableExistence_async
         *--------------------------------------------------------------------------*/
        public async Task <bool> checkingTableExistence_async(string tblNm)
        {
            DescribeTableResponse descResponse;

            ListTablesResponse tblResponse = await client.ListTablesAsync();

            if (tblResponse.TableNames.Contains(tblNm))
            {
                Console.WriteLine("     A table named {0} already exists in DynamoDB!", tblNm);

                // If the table exists, get its description

                try
                {
                    descResponse = await client.DescribeTableAsync(tblNm);
                }
                catch (Exception ex)
                {
                    throw new Exception($"     However, its description is not available ({ex.Message})");
                }

                Console.WriteLine("     Status of the table: '{0}'.", descResponse.Table.TableStatus);
                return(true);
            }
            return(false);
        }
Example #18
0
        public void Scan()
        {
            var table = client.DescribeTableAsync(new DescribeTableRequest(tableName)).GetAwaiter().GetResult();

            WriteMetrics($"There are {table.Table.ItemCount} entities in the db");

            var request = new ScanRequest
            {
                TableName = tableName,
                ReturnConsumedCapacity = "TOTAL",
                Limit = 10000
            };
            var start = Stopwatch.GetTimestamp();

            var response = client.ScanAsync(request).GetAwaiter().GetResult();

            var time = (Stopwatch.GetTimestamp() - start) * 1000 / Stopwatch.Frequency;

            WriteMetrics($"Scanning took {response.ConsumedCapacity.CapacityUnits} units, {time} ms, and returned {response.Count} elements.");

            request = new ScanRequest
            {
                TableName = tableName,
                ReturnConsumedCapacity = "TOTAL",
                Limit             = 100,
                ExclusiveStartKey = response.LastEvaluatedKey
            };
            start = Stopwatch.GetTimestamp();

            response = client.ScanAsync(request).GetAwaiter().GetResult();

            time = (Stopwatch.GetTimestamp() - start) * 1000 / Stopwatch.Frequency;
            WriteMetrics($"Scanning took {response.ConsumedCapacity.CapacityUnits} units, {time} ms, and returned {response.Count} elements.");
        }
Example #19
0
        public async Task <AwsResource <TableDescription> > GetResourceAsync(string name)
        {
            if (_tableNames == null)
            {
                await GetResourceNamesAsync();
            }

            if (!_tableNames.Contains(name))
            {
                return(null);
            }

            if (_cachedTableDescriptions.ContainsKey(name))
            {
                return(_cachedTableDescriptions[name]);
            }

            var tableResponse = await _dynamoDb.DescribeTableAsync(name);

            var dataItem = new AwsResource <TableDescription>(tableResponse.Table.TableName, tableResponse.Table);

            _cachedTableDescriptions.Add(tableResponse.Table.TableName, dataItem);

            return(dataItem);
        }
Example #20
0
        public static async Task <DescribeTableResponse> WaitTillTableDeleted(IAmazonDynamoDB client, string tableName,
                                                                              DeleteTableResponse response)
        {
            DescribeTableResponse resp = new DescribeTableResponse();
            var tableDescription       = response.TableDescription;

            string status = tableDescription.TableStatus;

            Int32 sleepDuration = 1000; // One second

            while ((status == "DELETING") && (sleepDuration < 10000))
            {
                System.Threading.Thread.Sleep(sleepDuration);

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

                status = resp.Table.TableStatus;

                sleepDuration *= 2;
            }

            return(resp);
        }
Example #21
0
        public void isTableReady(string tablename)
        {
            string status = null;

            do
            {
                Thread.Sleep(5000);
                try
                {
                    var result = _dynamoDbClient.DescribeTableAsync(new DescribeTableRequest
                    {
                        TableName = tablename
                    });
                    status = result.Result.Table.TableStatus;
                }
                catch (Amazon.Runtime.Internal.HttpErrorResponseException)
                {
                }
                catch (ResourceNotFoundException)
                {
                }
            }while (status != "ACTIVE");
            {
                Console.WriteLine("Table Created");
            }
        }
Example #22
0
        /// <summary>
        /// Waits for successful creation of a DynamoDB table.
        /// </summary>
        /// <param name="client">An initialized DynamoDB client object.</param>
        /// <param name="tableName">The name of the newly created DynamoDB table.</param>
        /// <param name="response">A DescribeTableResponse object that can
        /// confirm successful creation of the object.</param>
        /// <returns>A DescribeTableResponse object containing information about
        /// the newly created table.</returns>
        public static async Task <DescribeTableResponse> WaitTillTableCreated(
            IAmazonDynamoDB client,
            string tableName,
            CreateTableResponse response)
        {
            DescribeTableResponse resp = new DescribeTableResponse();

            var tableDescription = response.TableDescription;

            string status = tableDescription.TableStatus;

            int sleepDuration = 1000; // One second

            // Don't wait more than 10 seconds.
            while ((status != "ACTIVE") && (sleepDuration < 10000))
            {
                System.Threading.Thread.Sleep(sleepDuration);

                resp = await client.DescribeTableAsync(new DescribeTableRequest
                {
                    TableName = tableName,
                });

                status = resp.Table.TableStatus;

                sleepDuration *= 2;
            }

            return(resp);
        }
        private void WaitUntilTableReady(string tableName)
        {
            string status = null;

            // Let us wait until table is created. Call DescribeTable.
            do
            {
                System.Threading.Thread.Sleep(5000); // Wait 5 seconds.
                try
                {
                    var res = _amazonDynamoDB.DescribeTableAsync(new DescribeTableRequest
                    {
                        TableName = tableName
                    }).Result;

                    _logger.LogInformation("Table name: {TableName}, status: {TableStatus}",
                                           res.Table.TableName,
                                           res.Table.TableStatus);
                    status = res.Table.TableStatus;
                }
                catch (ResourceNotFoundException)
                {
                    // DescribeTable is eventually consistent. So you might
                    // get resource not found. So we handle the potential exception.
                }
            } while (status != "ACTIVE");
        }
Example #24
0
        private async Task <bool> TableIsReady()
        {
            try
            {
                var res = await _dynamoDb.DescribeTableAsync(new DescribeTableRequest
                {
                    TableName = _config.TableName
                });

                return(res.Table.TableStatus == "ACTIVE");
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Table {_config.TableName} isn't ready'");
                return(false);
            }
        }
Example #25
0
        private async Task <bool> TableIsReady()
        {
            try
            {
                var res = await _dynamoDb.DescribeTableAsync(new DescribeTableRequest
                {
                    TableName = _config.TableName
                });

                return(res.Table.TableStatus == "ACTIVE");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(false);
            }
        }
Example #26
0
        public async Task <bool> CheckHealthAsync()
        {
            Console.WriteLine("Health checking...");
            //using var client = new AmazonDynamoDBClient();
            var tableData = await _client.DescribeTableAsync("Adverts");

            return(string.Compare(tableData.Table.TableStatus, "active", true) == 0);
        }
Example #27
0
        public async Task <bool> CheckHealthAsync()
        {
            using (var context = new DynamoDBContext(_client))
            {
                var tableData = await _client.DescribeTableAsync("Adverts");

                return(String.Compare(tableData.Table.TableStatus, "Active", true) == 0);
            }
        }
        private async Task EnsureInitializedImplAsync(IAmazonDynamoDB client, string rolesTableName)
        {
            var defaultProvisionThroughput = new ProvisionedThroughput
            {
                ReadCapacityUnits  = 5,
                WriteCapacityUnits = 5
            };
            var globalSecondaryIndexes = new List <GlobalSecondaryIndex>
            {
                new GlobalSecondaryIndex
                {
                    IndexName = "NormalizedName-DeletedOn-index",
                    KeySchema = new List <KeySchemaElement>
                    {
                        new KeySchemaElement("NormalizedName", KeyType.HASH),
                        new KeySchemaElement("DeletedOn", KeyType.RANGE)
                    },
                    ProvisionedThroughput = defaultProvisionThroughput,
                    Projection            = new Projection
                    {
                        ProjectionType = ProjectionType.ALL
                    }
                }
            };

            var tableNames = await client.ListAllTablesAsync();

            if (!tableNames.Contains(rolesTableName))
            {
                await CreateTableAsync(client, rolesTableName, defaultProvisionThroughput, globalSecondaryIndexes);

                return;
            }

            var response = await client.DescribeTableAsync(new DescribeTableRequest { TableName = rolesTableName });

            var table = response.Table;

            var indexesToAdd =
                globalSecondaryIndexes.Where(
                    g => !table.GlobalSecondaryIndexes.Exists(gd => gd.IndexName.Equals(g.IndexName)));
            var indexUpdates = indexesToAdd.Select(index => new GlobalSecondaryIndexUpdate
            {
                Create = new CreateGlobalSecondaryIndexAction
                {
                    IndexName             = index.IndexName,
                    KeySchema             = index.KeySchema,
                    ProvisionedThroughput = index.ProvisionedThroughput,
                    Projection            = index.Projection
                }
            }).ToList();

            if (indexUpdates.Count > 0)
            {
                await UpdateTableAsync(client, rolesTableName, indexUpdates);
            }
        }
Example #29
0
        private static async Task <string> GetTableStatus(string tableName)
        {
            var response = await DynamoDBClient.DescribeTableAsync(new DescribeTableRequest
            {
                TableName = tableName
            });

            return(response.Table.TableStatus);
        }
        public async Task <bool> CheckHealthAsync()
        {
            using (var context = new DynamoDBContext(_amazonDynamoDB))
            {
                var tableData = await _amazonDynamoDB.DescribeTableAsync("Advert");

                return(tableData.Table.TableStatus == TableStatus.ACTIVE);
            }
        }
Example #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;
        }