Пример #1
0
        private async Task <CloudTable> CreateTableIfNotExists()
        {
            _cloudStorageAccount = CloudStorageAccount.Parse(_connstionString);
            var        cloudTableClient = _cloudStorageAccount.CreateCloudTableClient();
            CloudTable table            = cloudTableClient.GetTableReference(_tableName);

            try
            {
                await table.CreateAsync();
            }
            catch (StorageException storageException)
            {
                //We do not fire exception if table exists - there is no need in such actions
                if (storageException.RequestInformation.HttpStatusCode != (int)HttpStatusCode.Conflict &&
                    storageException.RequestInformation.ExtendedErrorInformation.ErrorCode != TableErrorCodeStrings.TableAlreadyExists)
                {
                    await _log?.WriteFatalErrorAsync("Table storage: " + _tableName, "CreateTable error", "", storageException);

                    throw;
                }
            }
            catch (Exception exception)
            {
                await _log?.WriteFatalErrorAsync("Table storage: " + _tableName, "CreateTable error", "unknown case", exception);

                throw;
            }

            return(table);
        }
        public void TableGetPermissionsRequestOptionsOperationContextCancellationTokenTask()
        {
            CloudTableClient    tableClient       = GenerateCloudTableClient();
            string              tableName         = GenerateRandomTableName();
            CloudTable          table             = tableClient.GetTableReference(tableName);
            TableRequestOptions requestOptions    = new TableRequestOptions();
            OperationContext    operationContext  = new OperationContext();
            CancellationToken   cancellationToken = CancellationToken.None;

            try
            {
                table.CreateAsync().Wait();
                var entity = new BaseEntity("PK", "RK");
                entity.Populate();
                table.ExecuteAsync(TableOperation.Insert(entity)).Wait();

                TablePermissions expected = new TablePermissions();
                TablePermissions actual   = table.GetPermissionsAsync(requestOptions, operationContext, cancellationToken).Result;
                AssertPermissionsEqual(expected, actual);
            }
            finally
            {
                table.DeleteIfExistsAsync().Wait();
            }
        }
        public void TableSetPermissionsPermissionsTask()
        {
            CloudTableClient tableClient = GenerateCloudTableClient();
            string           tableName   = GenerateRandomTableName();
            CloudTable       table       = tableClient.GetTableReference(tableName);
            TablePermissions permissions = new TablePermissions();

            permissions.SharedAccessPolicies.Add(Guid.NewGuid().ToString(), new SharedAccessTablePolicy()
            {
                Permissions            = SharedAccessTablePermissions.Query,
                SharedAccessStartTime  = DateTimeOffset.Now.Add(new TimeSpan(-1, 0, 0)),
                SharedAccessExpiryTime = DateTimeOffset.Now.Add(new TimeSpan(1, 0, 0))
            });

            try
            {
                table.CreateAsync().Wait();

                var entity = new BaseEntity("PK", "RK");
                entity.Populate();

                table.ExecuteAsync(TableOperation.Insert(entity)).Wait();

                table.SetPermissionsAsync(permissions);
            }
            finally
            {
                table.DeleteIfExistsAsync().Wait();
            }
        }
        internal async Task <ResultObject> HandleAsync(CreateEventCommand command)
        {
            if (!await _cloudTable.ExistsAsync())
            {
                await _cloudTable.CreateAsync();
            }

            // Validation logic goes here...

            var model     = CreateEventModel(command);
            var operation = TableOperation.Insert(model);

            try
            {
                var tableResult = await _cloudTable.ExecuteAsync(operation);

                if (tableResult.HttpStatusCode == 204 && tableResult.Result is EventModel eventModel)
                {
                    return new ResultObject {
                               Success = true, Result = eventModel.RowKey, Message = string.Empty
                    }
                }
                ;
                else
                {
                    return new ResultObject {
                               Success = false, Result = null, Message = "Unable to create entity"
                    }
                };
            }
            catch (StorageException)
            {
                throw;
            }
        }
        public void TableSetPermissionsPermissionsRequestOptionsOperationContextCancellationTokenTask()
        {
            CloudTableClient    tableClient       = GenerateCloudTableClient();
            string              tableName         = GenerateRandomTableName();
            CloudTable          table             = tableClient.GetTableReference(tableName);
            TablePermissions    permissions       = new TablePermissions();
            TableRequestOptions requestOptions    = new TableRequestOptions();
            OperationContext    operationContext  = new OperationContext();
            CancellationToken   cancellationToken = CancellationToken.None;

            permissions.SharedAccessPolicies.Add(Guid.NewGuid().ToString(), new SharedAccessTablePolicy()
            {
                Permissions            = SharedAccessTablePermissions.Query,
                SharedAccessStartTime  = DateTimeOffset.Now.Add(new TimeSpan(-1, 0, 0)),
                SharedAccessExpiryTime = DateTimeOffset.Now.Add(new TimeSpan(1, 0, 0))
            });

            try
            {
                table.CreateAsync().Wait();
                table.ExecuteAsync(TableOperation.Insert(new BaseEntity("PK", "RK"))).Wait();

                table.SetPermissionsAsync(permissions, requestOptions, operationContext, cancellationToken);
            }
            finally
            {
                table.DeleteIfExistsAsync().Wait();
            }
        }
Пример #6
0
        public static async Task <CloudTable> CreateTableAsync(string tableName)
        {
            var connectionString = "DefaultEndpointsProtocol=https;AccountName=Your storage account name;AccountKey=Your account key;EndpointSuffix=core.windows.net";
            // Retrieve storage account information from connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

            // Create a table client for interacting with the table service
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient(new TableClientConfiguration());

            Console.WriteLine("Create a Table for the demo\n");

            // Create a table client for interacting with the table service
            CloudTable table = tableClient.GetTableReference(tableName);

            if (await table.ExistsAsync())
            {
                Console.WriteLine("Table {0} already exists \n", tableName);
            }
            else
            {
                await table.CreateAsync();

                Console.WriteLine("Table {0} created \n", tableName);
            }

            Console.WriteLine();
            return(table);
        }
        private Task UpdateTableAsync(PartitionContext context, string messagingtext)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Properties.Settings.Default.StorageConnectionString);
            CloudTableClient    tableClient    = storageAccount.CreateCloudTableClient();

            CloudTable heartbeatTable = tableClient.GetTableReference("heartbeat");

            if (heartbeatTable == null)
            {
                heartbeatTable.CreateAsync().Wait();
            }

            MemoryStream memStream = new MemoryStream(Encoding.UTF8.GetBytes(messagingtext));
            StreamReader reader    = new StreamReader(memStream);
            string       line      = null;

            while ((line = reader.ReadLine()) != null)
            {
                var json = JsonConvert.DeserializeObject(line);

                JObject jParse           = JObject.Parse(Convert.ToString(json));
                string  DeviceID         = (string)jParse["device_id"];
                string  lastdatarecieved = (string)jParse["lastdatarecieved"];

                HeartbeatEntity heartbeatObj    = Task.Factory.StartNew(() => JsonConvert.DeserializeObject <HeartbeatEntity>(line)).Result;
                TableOperation  insertorreplace = TableOperation.InsertOrReplace(heartbeatObj);
                Task.Factory.StartNew(() => heartbeatTable.ExecuteAsync(insertorreplace));

                Console.WriteLine(DeviceID + ", " + lastdatarecieved + ", " + context.Lease.PartitionId);
            }

            return(Task.FromResult(true));
        }
        public static async Task <CloudTable> CreateTableAsync(string tableName)
        {
            StorageCredentials  creds   = new StorageCredentials(accountName, accountKey);
            CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);
            CloudTableClient    client  = account.CreateCloudTableClient();
            CloudTable          table   = client.GetTableReference(tableName);
            await table.CreateAsync();

            return(table);
        }
Пример #9
0
        private static async Task <CloudTable> GetTable()
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage"));
            CloudTableClient    client         = storageAccount.CreateCloudTableClient();
            CloudTable          table          = client.GetTableReference("MockApi");

            if (!await table.ExistsAsync())
            {
                await table.CreateAsync();
            }
            return(table);
        }
        public async Task TestInitialize()
        {
            CloudTable table = CloudStorageAccount
                               .DevelopmentStorageAccount
                               .CreateCloudTableClient()
                               .GetTableReference("DetectorTestingEventStore");

            await table.DeleteIfExistsAsync();

            await table.CreateAsync();

            Table = table;
        }
        public async Task CloudTableExtensionsTest()
        {
            // Arrange
            var permissions = new TablePermissions();

            permissions.SharedAccessPolicies.Add("default", new SharedAccessTablePolicy
            {
                Permissions            = SharedAccessTablePermissions.Query,
                SharedAccessExpiryTime = DateTimeOffset.UtcNow,
                SharedAccessStartTime  = DateTimeOffset.UtcNow
            });

            // Act

            // Create table
            await _cloudTable.CreateAsync();

            // Create table
            bool createIfNotExistsResult = await _cloudTable.CreateIfNotExistsAsync();

            // Check whether table exists
            bool existsResult = await _cloudTable.ExistsAsync();

            // Set & get permissions
            await _cloudTable.SetPermissionsAsync(permissions);

            TablePermissions permissionsResult = await _cloudTable.GetPermissionsAsync();

            // Delete table

            await _cloudTable.DeleteAsync();

            bool deleteIfExistsResult = await _cloudTable.DeleteIfExistsAsync();

            // Assert
            Assert.False(createIfNotExistsResult);
            Assert.True(existsResult);
            Assert.NotNull(permissionsResult);
            Assert.NotNull(permissionsResult.SharedAccessPolicies);
            Assert.Equal(permissionsResult.SharedAccessPolicies.Count, permissions.SharedAccessPolicies.Count);

            foreach (var policy in permissionsResult.SharedAccessPolicies)
            {
                Assert.Contains(policy.Key, permissions.SharedAccessPolicies.Keys);
                SharedAccessTablePolicy value = permissions.SharedAccessPolicies[policy.Key];
                Assert.Equal(policy.Value.Permissions, value.Permissions);
            }

            Assert.False(deleteIfExistsResult);
        }
Пример #12
0
        public Task CreateTableAsync(Type entityType, bool ignoreErrorIfExists = true)
        {
            // Retrieve a reference to the table.
            CloudTable table = GetTableReference(GetTableName(entityType));

            if (ignoreErrorIfExists)
            {
                // Create the table if it doesn't exist.
                return(table.CreateIfNotExistsAsync());
            }
            else
            {
                // Create table and throw error
                return(table.CreateAsync());
            }
        }
Пример #13
0
        private async Task <CloudTable> GetTable()
        {
            if (!_table.Exists())
            {
                try
                {
                    await _table.CreateAsync();
                }
                catch (Exception exception)
                {
                    Trace.TraceError(exception.ToString());
                }
            }

            return(_table);
        }
Пример #14
0
        private async Task CreateAzureTableAsync(CloudTable table)
        {
            try
            {
                await table.CreateAsync();
            }
            catch (StorageException ex) when(ex.RequestInformation.HttpStatusCode == 409)
            {
                await Task.Delay(20000);

                await CreateAzureTableAsync(table);
            }
            catch (Exception)
            {
                throw;
            }
        }
Пример #15
0
        public async Task FlushAllPendingEvents_sends_all_pending_events()
        {
            // Arrange
            await s_eventTable.DeleteIfExistsAsync();

            await s_eventTable.CreateAsync();

            var messageBus = new MessageLogger();
            var sut        = new AzureEventPublisher(s_eventTable, s_serializer, messageBus);

            var expected = new List <Envelope <IDomainEvent> >();
            var fixture  = new Fixture();
            var userIds  = fixture.CreateMany <Guid>().ToList();

            foreach (Guid userId in userIds)
            {
                var user = new FakeUser(userId, fixture.Create <string>());
                user.ChangeUsername(fixture.Create <string>());

                string operationId   = fixture.Create <string>();
                var    correlationId = Guid.NewGuid();
                string contributor   = fixture.Create <string>();

                var envelopes = new List <Envelope <IDomainEvent> >(
                    from message in user.FlushPendingEvents()
                    let messageId = Guid.NewGuid()
                                    select new Envelope <IDomainEvent>(messageId, message, operationId, correlationId, contributor));

                expected.AddRange(envelopes);

                var batch = new TableBatchOperation();
                foreach (Envelope <IDomainEvent> envelope in envelopes)
                {
                    batch.Insert(PendingEvent.Create(typeof(FakeUser), envelope, s_serializer));
                }

                await s_eventTable.ExecuteBatchAsync(batch);
            }

            // Act
            await sut.FlushAllPendingEvents(CancellationToken.None);

            // Assert
            messageBus.Log.ShouldAllBeEquivalentTo(expected);
        }
        public static async Task ClassInitialize(TestContext context)
        {
            try
            {
                CloudTableClient tableClient = CloudStorageAccount.DevelopmentStorageAccount.CreateCloudTableClient();
                s_eventTable = tableClient.GetTableReference("AzureEventStoreTestEventStore");
                await s_eventTable.DeleteIfExistsAsync(
                    new TableRequestOptions { RetryPolicy = new NoRetry() },
                    operationContext : default);

                await s_eventTable.CreateAsync();
            }
            catch (StorageException exception)
            {
                context.WriteLine($"{exception}");
                Assert.Inconclusive("Could not connect to Azure Storage Emulator. See the output for details. Refer to the following URL for more information: http://go.microsoft.com/fwlink/?LinkId=392237");
            }
        }
Пример #17
0
        private static async Task CheckAndInitializeTable <T>(CloudTableClient tableClient, string funcAppDirectory, string seedFilename, ILogger log) where T : EntityBase, new()
        {
            var        tableName = typeof(T).Name.ToLower();
            CloudTable table     = tableClient.GetTableReference(tableName);

            var exists = await table.ExistsAsync();

            if (exists)
            {
                return;
            }

            await table.CreateAsync();

            var rowNumber      = 0;
            var batchOperation = new TableBatchOperation();

            var seedPath    = Path.Combine(funcAppDirectory, Constants.Data.Directory, seedFilename);
            var seedJson    = File.ReadAllText(seedPath);
            var seedEntries = JsonConvert.DeserializeObject <ICollection <T> >(seedJson);

            foreach (var entry in seedEntries)
            {
                batchOperation.Add(TableOperation.InsertOrReplace(entry));

                if (batchOperation.Count > 99)
                {
                    table.ExecuteBatch(batchOperation);
                    batchOperation.Clear();
                }

                rowNumber++;
            }

            if (batchOperation.Count > 0)
            {
                table.ExecuteBatch(batchOperation);
                batchOperation.Clear();
            }

            log.LogInformation($"{seedEntries.Count} {tableName}s initially seeded");
        }
        public void TableGetPermissionsTask()
        {
            CloudTableClient tableClient = GenerateCloudTableClient();
            string           tableName   = GenerateRandomTableName();
            CloudTable       table       = tableClient.GetTableReference(tableName);

            try
            {
                table.CreateAsync().Wait();
                table.ExecuteAsync(TableOperation.Insert(new BaseEntity("PK", "RK"))).Wait();

                TablePermissions expected = new TablePermissions();
                TablePermissions actual   = table.GetPermissionsAsync().Result;
                AssertPermissionsEqual(expected, actual);
            }
            finally
            {
                table.DeleteIfExistsAsync().Wait();
            }
        }
        async Task ExecuteCreateTableAsync()
        {
            if (IsBusy)
            {
                return;
            }

            if (!ReadyToSave)
            {
                Logger.Report(new Exception("Create table called when ReadyToSave was false"), "Method", "ExecuteCreateTableAsync");
                return;
            }
            if (tableName.Length < 3 || tableName.Length > 63 ||
                !Regex.IsMatch(tableName, @"^[A-Za-z][A-Za-z0-9]*$"))
            {
                MessagingService.Current.SendMessage <MessagingServiceAlert>(MessageKeys.Message, new MessagingServiceAlert
                {
                    Title   = "Table name is invalid",
                    Message = "Table names must be between 3 and 63 chars, only contain letters and numbers, and start with a letter.",
                    Cancel  = "OK"
                });
                return;
            }
            IProgressDialog savingDialog = UserDialogs.Loading("Saving Table");

            savingDialog.Show();
            try
            {
                IsBusy = true;
                string connectionString = Constants.StorageConnectionString;
                connectionString = connectionString.Replace("<ACCOUNTNAME>", SelectedStorageAccount.Name);
                connectionString = connectionString.Replace("<ACCOUNTKEY>", SelectedStorageAccount.PrimaryKey);
                CloudStorageAccount sa = CloudStorageAccount.Parse(connectionString);
                var        tableClient = sa.CreateCloudTableClient();
                CloudTable table       = tableClient.GetTableReference(tableName);
                if (table == null)
                {
                    Console.WriteLine("Table is null");
                }
                if (await table.ExistsAsync())
                {
                    MessagingService.Current.SendMessage <MessagingServiceAlert>(MessageKeys.Message, new MessagingServiceAlert
                    {
                        Title   = "Table Exists",
                        Message = "A table with the name \"" + TableName + "\" already exists in this storage account.",
                        Cancel  = "OK"
                    });
                    return;
                }
                else
                {
                    await table.CreateAsync();

                    var realm = App.GetRealm();
                    var storageAccountName = selectedStorageAccount.Name;
                    realm.Write(() =>
                    {
                        realm.Add(new RealmCloudTable(table.Name,
                                                      selectedStorageAccount.Name,
                                                      table.Uri.ToString()));
                    });
                    if (tablesVM != null)
                    {
                        tablesVM.AddTable(new ASECloudTable(table, selectedStorageAccount.Name));
                        App.Logger.Track(AppEvent.CreatedTable.ToString());
                    }
                    //This is here and in finally so we'll dismiss this before popping the page so the
                    //Loader doesn't stay longer than the popup
                    if (savingDialog != null)
                    {
                        if (savingDialog.IsShowing)
                        {
                            savingDialog.Hide();
                        }
                        savingDialog.Dispose();
                    }
                    await PopupNavigation.PopAsync();
                }
            }
            catch (Exception ex)
            {
                Logger.Report(ex, "Method", "ExecuteCreateTableAsync");
                MessagingService.Current.SendMessage(MessageKeys.Error, ex);
            }
            finally
            {
                IsBusy = false;
                if (savingDialog != null)
                {
                    if (savingDialog.IsShowing)
                    {
                        savingDialog.Hide();
                    }
                    savingDialog.Dispose();
                }
            }
            return;
        }
Пример #20
0
        private async Task prepPointHistory(long pointid)
        {
            CloudTable cloudTable           = null;
            Dictionary <int, DateTime?> res = new Dictionary <int, DateTime?>();

            var StartHistory = _startHistoryDataDateUtc; //.ToString("yyyyMM");
            var Now          = DateTime.UtcNow;          //.ToString("yyyyMM");

            long sampCount      = 0;
            long sampCountTimer = DateTime.UtcNow.Ticks;
            int  expCount       = 3;

            string presentTableName = "";

            while (((Now.Ticks - StartHistory.Ticks) / TimeSpan.TicksPerSecond) > 0)
            {
                // Check that table exists for month
                var tableName = "P" + support.padZeroOnItem(6, pointid) + StartHistory.ToString("yyyyMM");
                if (presentTableName != tableName)
                {
                    Exception tableExp = null;
                    while (expCount > 0)
                    {
                        try
                        {
                            // Check if table exists
                            cloudTable = _cloudTableClient.GetTableReference(tableName);
                            if (!cloudTable.Exists())
                            {
                                cloudTable.CreateAsync().Wait();
                            }
                            tableExp = null;
                            break;
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine((DateTime.Now.ToString("HH:mm:ss.fff") + ": Table Error " + ex.Message));
                            await Task.Delay(1000 *(3 - expCount));

                            tableExp = ex;
                            expCount--;
                        }
                    }

                    if (tableExp != null)
                    {
                        throw tableExp;
                    }

                    presentTableName = tableName;
                }

                // Generate new partion, thats 1 hour of 60 rows
                var tuple = generatePartionDataSetForPoint(pointid, StartHistory);
                StartHistory = tuple.Item2;
                var rows = tuple.Item1;

                expCount = 3;
                Exception insertExp = null;
                while (expCount > 0)
                {
                    try
                    {
                        // Insert into Table source with batch
                        var t = InsertBatchEntiy(cloudTable, rows);
                        t.Wait();
                        var insertRes = t.Result;
                        insertExp = null;
                        Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + ": Inserted  Blockstart:" + rows[0].RowKey + " " + rows.Count + " rows");
                        sampCount += (rows.Count * (60 * 1000 / _scanRateMs));
                        break;
                    }
                    catch (Exception ex)
                    {
                        insertExp = ex;
                        expCount--;
                        Console.WriteLine((DateTime.Now.ToString("HH:mm:ss.fff") + ": Error " + ex.Message));
                        await Task.Delay(2000);
                    }
                }
                if (insertExp != null)
                {
                    // Failed insert
                    Console.WriteLine((DateTime.Now.ToString("HH:mm:ss.fff") + ": FAILED insert " + insertExp.Message));
                }
            }

            Console.WriteLine("Point " + pointid + " insert " + sampCount + " samps in " + ((DateTime.UtcNow.Ticks - sampCountTimer) / TimeSpan.TicksPerSecond));
        }
Пример #21
0
 public static Task CreateAsync(this CloudTable tbl, CancellationToken token)
 {
     return(tbl.CreateAsync(null, null, token));
 }
Пример #22
0
 public void Create()
 {
     table.CreateAsync().Wait();
 }
Пример #23
0
 public Task CreateAsync(CancellationToken cancellationToken)
 {
     return(_table.CreateAsync(cancellationToken));
 }
Пример #24
0
 public Task CreateAsync(TableRequestOptions requestOptions = null, OperationContext operationContext = null)
 {
     return(cloudTableImpl.CreateAsync(requestOptions, operationContext));
 }