protected void AssertContainerIsLeased(string containerName, string leaseId)
        {
            var client    = StorageAccount.CreateCloudBlobClient();
            var container = client.GetContainerReference(containerName);

            if (!container.Exists())
            {
                Assert.Fail(String.Format("AssertContainerIsLeased: The container '{0}' does not exist", containerName));
            }

            try
            {
                container.RenewLease(new AccessCondition()
                {
                    LeaseId = leaseId
                });
            }
            catch (Exception exc)
            {
                Assert.Fail(String.Format("AssertContainerIsLeased: The container '{0}' gave an {1} exception when renewing with the specified lease id: {2}", containerName, exc.GetType().Name, exc.Message));
            }
        }
        private async Task SetupAsync(StorageAccount account, object contents)
        {
            CloudQueueMessage message;

            if (contents is string str)
            {
                message = new CloudQueueMessage(str);
            }
            else if (contents is byte[] bytearray)
            {
                message = CloudQueueMessage.CreateCloudQueueMessageFromByteArray(bytearray);
            }
            else
            {
                throw new InvalidOperationException("bad test");
            }

            var queue = await CreateQueue(account, QueueName);

            // message.InsertionTime is provided by FakeStorageAccount when the message is inserted.
            await queue.AddMessageAsync(message);
        }
Beispiel #3
0
        public void Bind(StorageAccount storageAccount, TargetTreeView targetTreeView)
        {
            try
            {
                this.IsBinding          = true;
                _TargetTreeView         = targetTreeView;
                _StorageAccount         = storageAccount;
                txtTargetName.MaxLength = StorageAccount.MaximumTargetNameLength(targetTreeView.TargetSettings);

                if (_StorageAccount.SourceAccount == null)
                {
                    lblAccountType.Text   = String.Empty;
                    lblSourceASMName.Text = String.Empty;
                }
                else
                {
                    lblAccountType.Text   = _StorageAccount.SourceAccount.AccountType;
                    lblSourceASMName.Text = _StorageAccount.SourceAccount.Name;
                }

                if (storageAccount.TargetName != null)
                {
                    if (storageAccount.TargetName.Length > txtTargetName.MaxLength)
                    {
                        txtTargetName.Text = storageAccount.TargetName.Substring(0, txtTargetName.MaxLength);
                    }
                    else
                    {
                        txtTargetName.Text = storageAccount.TargetName;
                    }
                }

                cmbAccountType.SelectedIndex = cmbAccountType.FindString(storageAccount.StorageAccountType.ToString());
            }
            finally
            {
                this.IsBinding = false;
            }
        }
Beispiel #4
0
        /// <summary>
        /// Make sure we set the correct storage account endpoint.
        /// We can get the value from multiple places, we only take the one with higher precedence. And the precedence is:
        /// 1. Directly specified from command line parameter
        /// 2. The one get from StorageContext parameter
        /// 3. The one get from the storage account we list
        /// 4. The one get from current Azure Environment
        /// </summary>
        /// <param name="storageAccount">The storage account to help get the endpoint.</param>
        private void InitializeStorageAccountEndpoint(StorageAccount storageAccount)
        {
            if (string.IsNullOrEmpty(this.StorageAccountEndpoint))
            {
                var context = this.StorageContext;
                if (context == null)
                {
                    Uri blobEndpoint = null, queueEndpoint = null, tableEndpoint = null, fileEndpoint = null;
                    if (storageAccount != null)
                    {
                        // Create storage context from storage account
                        var endpoints = storageAccount.PrimaryEndpoints;
                        blobEndpoint  = endpoints.Blob;
                        queueEndpoint = endpoints.Queue;
                        tableEndpoint = endpoints.Table;
                        fileEndpoint  = endpoints.File;
                    }
                    else if (this.DefaultContext != null && this.DefaultContext.Environment != null)
                    {
                        // Create storage context from default azure environment. Default to use https
                        blobEndpoint  = DefaultContext.Environment.GetStorageBlobEndpoint(this.StorageAccountName);
                        queueEndpoint = DefaultContext.Environment.GetStorageQueueEndpoint(this.StorageAccountName);
                        tableEndpoint = DefaultContext.Environment.GetStorageTableEndpoint(this.StorageAccountName);
                        fileEndpoint  = DefaultContext.Environment.GetStorageFileEndpoint(this.StorageAccountName);
                    }
                    else
                    {
                        // Can't automatically get the endpoint to create storage context
                        throw new ArgumentNullException(Properties.Resources.DiagnosticsExtensionStorageAccountEndpointNotDefined);
                    }
                    var credentials         = new StorageCredentials(this.StorageAccountName, this.StorageAccountKey);
                    var cloudStorageAccount = new CloudStorageAccount(credentials, blobEndpoint, queueEndpoint, tableEndpoint, fileEndpoint);
                    context = new AzureStorageContext(cloudStorageAccount);
                }

                var scheme = context.BlobEndPoint.StartsWith("https://", StringComparison.OrdinalIgnoreCase) ? "https://" : "http://";
                this.StorageAccountEndpoint = scheme + context.EndPointSuffix;
            }
        }
        public void TestCreateGetDeleteSqlVirtualMachineGroup()
        {
            using (SqlVirtualMachineTestContext context = new SqlVirtualMachineTestContext(this))
            {
                // Create SQL VM group
                ISqlVirtualMachineGroupsOperations sqlOperations = context.getSqlClient().SqlVirtualMachineGroups;
                StorageAccount         storageAccount            = VirtualMachineTestBase.CreateStorageAccount(context);
                SqlVirtualMachineGroup group = SqlVirtualMachineTestBase.CreateSqlVirtualMachineGroup(context, storageAccount, "test-group");

                // Recover
                SqlVirtualMachineGroup group2 = sqlOperations.Get(context.resourceGroup.Name, group.Name);
                SqlVirtualMachineTestBase.ValidateSqlVirtualMachineGroups(group, group2);

                // Delete
                sqlOperations.Delete(context.resourceGroup.Name, group.Name);

                // Recover
                IPage <SqlVirtualMachineGroup>       list = sqlOperations.ListByResourceGroup(context.resourceGroup.Name);
                IEnumerator <SqlVirtualMachineGroup> iter = list.GetEnumerator();
                Assert.False(iter.MoveNext());
            }
        }
Beispiel #6
0
        /// <summary>
        /// Initialize the storage account key if it's not specified.
        /// It can be defined in multiple places, we only take the one with higher precedence. And the precedence is:
        /// 1. The one we try to resolve within current subscription
        /// 2. The one defined in PrivateConfig in the configuration file
        /// </summary>
        public static string InitializeStorageAccountKey(IStorageManagementClient storageClient, string storageAccountName = null, string configurationPath = null)
        {
            string         storageAccountKey = null;
            StorageAccount storageAccount    = null;

            if (TryGetStorageAccount(storageClient, storageAccountName, out storageAccount))
            {
                // Help user retrieve the storage account key
                var keys = storageClient.StorageAccounts.GetKeys(storageAccount.Name);
                if (keys != null)
                {
                    storageAccountKey = !string.IsNullOrEmpty(keys.PrimaryKey) ? keys.PrimaryKey : keys.SecondaryKey;
                }
            }
            else
            {
                // Use the one defined in PrivateConfig
                storageAccountKey = GetStorageAccountInfoFromPrivateConfig(configurationPath, PrivConfKeyAttr);
            }

            return(storageAccountKey);
        }
        public void TestVMScaleSetOperations_Redeploy()
        {
            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                string originalTestLocation = Environment.GetEnvironmentVariable("AZURE_VM_TEST_LOCATION");

                string rgName             = TestUtilities.GenerateName(TestPrefix) + 1;
                string vmssName           = TestUtilities.GenerateName("vmss");
                string storageAccountName = TestUtilities.GenerateName(TestPrefix);
                VirtualMachineScaleSet inputVMScaleSet;

                bool passed = false;

                try
                {
                    Environment.SetEnvironmentVariable("AZURE_VM_TEST_LOCATION", "EastUS2");
                    EnsureClientsInitialized(context);

                    ImageReference imageRef             = GetPlatformVMImage(useWindowsImage: true);
                    StorageAccount storageAccountOutput = CreateStorageAccount(rgName, storageAccountName);

                    VirtualMachineScaleSet vmScaleSet = CreateVMScaleSet_NoAsyncTracking(rgName, vmssName,
                                                                                         storageAccountOutput, imageRef, out inputVMScaleSet, createWithManagedDisks: true);

                    m_CrpClient.VirtualMachineScaleSets.Redeploy(rgName, vmScaleSet.Name);

                    passed = true;
                }
                finally
                {
                    Environment.SetEnvironmentVariable("AZURE_VM_TEST_LOCATION", originalTestLocation);
                    // Cleanup the created resources. But don't wait since it takes too long, and it's not the purpose
                    // of the test to cover deletion. CSM does persistent retrying over all RG resources.
                    m_ResourcesClient.ResourceGroups.DeleteIfExists(rgName);
                }

                Assert.True(passed);
            }
        }
Beispiel #8
0
        public TResult RunTrigger <TResult>(StorageAccount account, Type programType,
                                            Action <TaskCompletionSource <TResult> > setTaskSource, IJobActivator activator,
                                            IEnumerable <string> ignoreFailureFunctions)
        {
            // Arrange
            TaskCompletionSource <TResult> taskSource = new TaskCompletionSource <TResult>();
            var serviceProvider = CreateConfigurationForManualCompletion <TResult>(account, programType,
                                                                                   activator, taskSource, ignoreFailureFunctions);
            Task <TResult> task = taskSource.Task;

            setTaskSource.Invoke(taskSource);

            try
            {
                // Act & Assert
                return(RunTrigger <TResult>(serviceProvider, task));
            }
            finally
            {
                setTaskSource.Invoke(null);
            }
        }
Beispiel #9
0
        public async Task CreateUpdateGetEncryptionScope()
        {
            //create resource group and storage account
            string accountName = await CreateValidAccountNameAsync(namePrefix);

            _resourceGroup = await CreateResourceGroupAsync();

            StorageAccountContainer        storageAccountContainer = _resourceGroup.GetStorageAccounts();
            StorageAccountCreateParameters parameters         = GetDefaultStorageAccountParameters(kind: Kind.StorageV2);
            StorageAccount           account                  = (await storageAccountContainer.CreateOrUpdateAsync(accountName, parameters)).Value;
            EncryptionScopeContainer encryptionScopeContainer = account.GetEncryptionScopes();

            //create encryption scope
            EncryptionScopeData parameter = new EncryptionScopeData()
            {
                Source = EncryptionScopeSource.MicrosoftStorage,
                State  = EncryptionScopeState.Enabled,
                RequireInfrastructureEncryption = false
            };
            EncryptionScope encryptionScope = (await encryptionScopeContainer.CreateOrUpdateAsync("scope", parameter)).Value;

            Assert.AreEqual("scope", encryptionScope.Id.Name);
            Assert.AreEqual(EncryptionScopeState.Enabled, encryptionScope.Data.State);
            Assert.AreEqual(EncryptionScopeSource.MicrosoftStorage, encryptionScope.Data.Source);

            //patch encryption scope
            encryptionScope.Data.State = EncryptionScopeState.Disabled;
            encryptionScope            = await encryptionScope.PatchAsync(encryptionScope.Data);

            Assert.AreEqual(encryptionScope.Data.State, EncryptionScopeState.Disabled);

            //get all encryption scopes
            List <EncryptionScope> encryptionScopes = await encryptionScopeContainer.GetAllAsync().ToEnumerableAsync();

            encryptionScope = encryptionScopes.First();
            Assert.AreEqual("scope", encryptionScope.Id.Name);
            Assert.AreEqual(EncryptionScopeState.Disabled, encryptionScope.Data.State);
            Assert.AreEqual(EncryptionScopeSource.MicrosoftStorage, encryptionScope.Data.Source);
        }
Beispiel #10
0
        public async Task ListStorageAccountSASWithDefaultProperties()
        {
            //create storage account
            string accountName = await CreateValidAccountNameAsync(namePrefix);

            _resourceGroup = await CreateResourceGroupAsync();

            StorageAccountContainer        storageAccountContainer = _resourceGroup.GetStorageAccounts();
            StorageAccountCreateParameters parameters = GetDefaultStorageAccountParameters();
            StorageAccount account = (await storageAccountContainer.CreateOrUpdateAsync(accountName, parameters)).Value;

            // Test for default values of sas credentials.
            AccountSasParameters accountSasParameters = new AccountSasParameters(services: "b", resourceTypes: "sco", permissions: "rl", sharedAccessExpiryTime: Recording.UtcNow.AddHours(1));
            Response <ListAccountSasResponse> result  = await account.GetAccountSASAsync(accountSasParameters);

            AccountSasParameters resultCredentials = ParseAccountSASToken(result.Value.AccountSasToken);

            Assert.AreEqual(accountSasParameters.Services, resultCredentials.Services);
            Assert.AreEqual(accountSasParameters.ResourceTypes, resultCredentials.ResourceTypes);
            Assert.AreEqual(accountSasParameters.Permissions, resultCredentials.Permissions);
            Assert.NotNull(accountSasParameters.SharedAccessExpiryTime);
        }
Beispiel #11
0
        private void TestVMWithOSProfile(
            string rgName,
            bool useWindowsProfile,
            Action <VirtualMachine> vmCustomizer = null,
            Action <VirtualMachine> vmValidator  = null)
        {
            string storageAccountName = ComputeManagementTestUtilities.GenerateName(TestPrefix);
            string asName             = ComputeManagementTestUtilities.GenerateName("as");

            ImageReference imageRef = GetPlatformVMImage(useWindowsProfile);

            VirtualMachine inputVM;

            try
            {
                StorageAccount storageAccountOutput = CreateStorageAccount(rgName, storageAccountName);

                VirtualMachine vm = CreateVM(rgName, asName, storageAccountOutput, imageRef, out inputVM, vmCustomizer);

                var getVMWithInstanceViewResponse = m_CrpClient.VirtualMachines.Get(rgName, inputVM.Name, InstanceViewTypes.InstanceView);
                ValidateVMInstanceView(inputVM, getVMWithInstanceViewResponse);

                var lroResponse = m_CrpClient.VirtualMachines.CreateOrUpdate(rgName, vm.Name, vm);
                Assert.True(lroResponse.ProvisioningState == "Succeeded");
                if (vmValidator != null)
                {
                    vmValidator(vm);
                }

                m_CrpClient.VirtualMachines.BeginDelete(rgName, vm.Name);
            }
            finally
            {
                if (m_ResourcesClient != null)
                {
                    m_ResourcesClient.ResourceGroups.Delete(rgName);
                }
            }
        }
        public override void Initialize(MockContext context)
        {
            base.Initialize(context);

            MockContext = context;

            ResourceManagementClient rmClient = context.GetServiceClient <ResourceManagementClient>();

            // Register subscription for storage and networking
            Provider provider = rmClient.Providers.Register("Microsoft.Storage");

            Assert.NotNull(provider);

            provider = rmClient.Providers.Register("Microsoft.Network");
            Assert.NotNull(provider);

            StorageAccountName = SearchTestUtilities.GenerateStorageAccountName();

            StorageManagementClient client = MockContext.GetServiceClient <StorageManagementClient>();

            StorageAccount account = client.StorageAccounts.Create(
                ResourceGroupName,
                StorageAccountName,
                new StorageAccountCreateParameters(
                    new Sku("Standard_LRS"),
                    kind: "StorageV2",
                    location: "eastus2euap"));

            Assert.NotNull(account);
            StorageAccountId = account.Id;

            SearchManagementClient searchMgmt = context.GetServiceClient <SearchManagementClient>();

            ServiceName = SearchTestUtilities.GenerateServiceName();
            SearchService service = DefineServiceWithSku(Management.Search.Models.SkuName.Basic);

            searchMgmt.Services.CreateOrUpdate(ResourceGroupName, ServiceName, service);
        }
Beispiel #13
0
        private void TestVMScaleSetEvictionPolicyInternal(
            string rgName,
            StorageAccount storageAccount,
            ImageReference imageRef,
            string evictionPolicy = null)
        {
            VirtualMachineScaleSet inputVMScaleSet;

            var vmssName = TestUtilities.GenerateName("vmss");

            var getResponse = CreateVMScaleSet_NoAsyncTracking(
                rgName,
                vmssName,
                storageAccount,
                imageRef,
                out inputVMScaleSet,
                null,
                (vmScaleSet) =>
            {
                vmScaleSet.Overprovision = true;
                vmScaleSet.VirtualMachineProfile.Priority = VirtualMachinePriorityTypes.Low;
                if (evictionPolicy != null)
                {
                    vmScaleSet.VirtualMachineProfile.EvictionPolicy = evictionPolicy;
                }
                vmScaleSet.Sku.Name     = VirtualMachineSizeTypes.StandardA1;
                vmScaleSet.Sku.Tier     = "Standard";
                vmScaleSet.Sku.Capacity = 2;
            });

            ValidateVMScaleSet(inputVMScaleSet, getResponse);

            evictionPolicy = evictionPolicy ?? VirtualMachineEvictionPolicyTypes.Deallocate;

            Assert.Equal(getResponse.VirtualMachineProfile.EvictionPolicy.ToString(), evictionPolicy);

            m_CrpClient.VirtualMachineScaleSets.Delete(rgName, vmssName);
        }
Beispiel #14
0
        public static void Call(StorageAccount account, Type programType, MethodInfo method,
                                IDictionary <string, object> arguments, params Type[] cloudBlobStreamBinderTypes)
        {
            // Arrange
            TaskCompletionSource <object> backgroundTaskSource = new TaskCompletionSource <object>();
            IHost host = CreateConfigurationForManualCompletion <object>(account,
                                                                         programType, backgroundTaskSource, cloudBlobStreamBinderTypes: cloudBlobStreamBinderTypes);
            Task backgroundTask = backgroundTaskSource.Task;

            using (host)
            {
                var  jobHost = host.GetJobHost();
                Task task    = jobHost.CallAsync(method, arguments);

                // Act
                bool completed = Task.WhenAny(task, backgroundTask).WaitUntilCompleted(3 * 1000);

                // Assert
                Assert.True(completed);

                // Give a nicer test failure message for faulted tasks.
                if (backgroundTask.Status == TaskStatus.Faulted)
                {
                    backgroundTask.GetAwaiter().GetResult();
                }

                // The background task should not complete.
                Assert.Equal(TaskStatus.WaitingForActivation, backgroundTask.Status);

                // Give a nicer test failure message for faulted tasks.
                if (task.Status == TaskStatus.Faulted)
                {
                    task.GetAwaiter().GetResult();
                }

                Assert.Equal(TaskStatus.RanToCompletion, task.Status);
            }
        }
Beispiel #15
0
        public void BlobOrContainerWithReadPermission(StorageObjectType objectType, BlobType blobType)
        {
            blobUtil.SetupTestContainerAndBlob(SpecialCharsPrefix, blobType);

            try
            {
                CommandAgent.SetContextWithSASToken(StorageAccount.Credentials.AccountName, blobUtil, objectType, StorageEndpoint, string.Empty, "r");

                // Get blob with the generated SAS token
                Test.Assert(CommandAgent.GetAzureStorageBlob(blobUtil.Blob.Name, blobUtil.ContainerName),
                            string.Format("Get existing blob {0} in container {1} should succeed", blobUtil.Blob.Name, blobUtil.ContainerName));

                // Download blob with the generated SAS token
                string downloadFilePath = Path.Combine(downloadDirPath, blobUtil.Blob.Name);
                Test.Assert(CommandAgent.GetAzureStorageBlobContent(blobUtil.Blob.Name, downloadFilePath, blobUtil.ContainerName),
                            string.Format("Download blob {0} in container {1} to File {2} should succeed", blobUtil.Blob.Name, blobUtil.ContainerName, downloadFilePath));

                // Copy blob with the generated SAS token(as source)
                string copiedName  = Utility.GenNameString("copied");
                object destContext = null;
                if (lang == Language.PowerShell)
                {
                    destContext = PowerShellAgent.GetStorageContext(StorageAccount.ToString(true));
                }
                else
                {
                    destContext = StorageAccount;
                }

                Test.Assert(CommandAgent.StartAzureStorageBlobCopy(blobUtil.ContainerName, blobUtil.Blob.Name, blobUtil.ContainerName, copiedName, destContext: destContext),
                            string.Format("Copy blob {0} in container {1} to blob {2} in container {3} should succeed",
                                          blobUtil.Blob.Name, blobUtil.ContainerName, blobUtil.ContainerName, copiedName));
            }
            finally
            {
                blobUtil.CleanupTestContainerAndBlob();
            }
        }
    public async void QueueStorageTest()
    {
        ClearOutput();
        WriteLine("-- Testing Queue Storage --");

        WriteLine("0. Creating queue client");
        // Create a queue client for interacting with the queue service
        CloudQueueClient queueClient = StorageAccount.CreateCloudQueueClient();

        WriteLine("1. Create a queue for the demo");
        CloudQueue queue = queueClient.GetQueueReference("samplequeue");

        try
        {
            await queue.CreateIfNotExistsAsync();
        }
        catch (StorageException)
        {
            WriteLine("If you are running with the default configuration please make sure you have started the storage emulator. Press the Windows key and type Azure Storage to select and run it from the list of applications - then restart the sample.");
            throw;
        }

        // Demonstrate basic queue functionality
        await BasicQueueOperationsAsync(queue);

        // Demonstrate how to update an enqueued message
        await UpdateEnqueuedMessageAsync(queue);

        // Demonstrate advanced functionality such as processing of batches of messages
        await ProcessBatchOfMessagesAsync(queue);

        // When you delete a queue it could take several seconds before you can recreate a queue with the same
        // name - hence to enable you to run the demo in quick succession the queue is not deleted. If you want
        // to delete the queue uncomment the line of code below.
        await DeleteQueueAsync(queue);

        WriteLine("-- Test Complete --");
    }
        public static async Task Run(
            [TimerTrigger("0 */10 * * * *")] TimerInfo timer,
            ILogger log,
            CancellationToken cancellationToken)
        {
            log.LogInformation($"C# CleanupOldImages timer trigger function executed at: {DateTime.Now}");

            var account = StorageAccount.NewFromConnectionString(Environment.GetEnvironmentVariable("AzureWebJobsStorage"));
            var client  = account.CreateCloudBlobClient();

            var expirationTime = TimeZoneInfo.ConvertTime(DateTimeOffset.Now, localTimeZone).Subtract(RetentionPeriod);

            log.LogInformation($"Looking for pictures taken before {expirationTime}");

            var picsContainer = client.GetContainerReference("pics");
            int cleanedUp     = 0;
            BlobContinuationToken continuationToken = null;
            BlobResultSegment     result;

            do
            {
                result = await picsContainer.ListBlobsSegmentedAsync(null, true, BlobListingDetails.Metadata, 10, continuationToken, null, null, cancellationToken);

                var expiredItems = result.Results.Cast <CloudBlockBlob>().Where(i => ParseName(i.Name) <= expirationTime);
                var cleanupTasks = expiredItems.Select(async item =>
                {
                    log.LogInformation("Found expired picture: {0}", item.Name);
                    await item.DeleteAsync(DeleteSnapshotsOption.IncludeSnapshots, null, null, null, cancellationToken);
                    Interlocked.Increment(ref cleanedUp);
                });

                await Task.WhenAll(cleanupTasks);

                continuationToken = result.ContinuationToken;
            } while (continuationToken != null);

            log.LogInformation("{0} pictures cleaned up", cleanedUp);
        }
Beispiel #18
0
        private static async Task <byte[]> GetFile(string fileName)
        {
            fileName = fileName.Replace("/", "\\");
            var topLevelDirectory = Path.Combine(Path.GetTempPath(), "shiurim");
            var cachedFilePath    = Path.Combine(topLevelDirectory, fileName);

            var fileDirectory = Path.GetDirectoryName(cachedFilePath);

            if (!Directory.Exists(fileDirectory))
            {
                Directory.CreateDirectory(fileDirectory);
            }

            foreach (var filePath in Directory.GetFiles(topLevelDirectory, "*", SearchOption.AllDirectories).Where(filePath => filePath != fileName))
            {
                // only keep files cached for 7 days
                if (File.GetLastWriteTime(filePath) < DateTime.Now.AddDays(-7))
                {
                    File.Delete(filePath);
                }
            }

            if (!File.Exists(cachedFilePath))
            {
                await StorageAccount.NewFromConnectionString(Environment.GetEnvironmentVariable("AzureWebJobsStorage", EnvironmentVariableTarget.Process))
                .CreateCloudBlobClient()
                .GetContainerReference(StaticData.ShiurimContainerName)
                .GetBlockBlobReference(fileName)
                .DownloadToFileAsync(cachedFilePath, FileMode.Create)
                .ConfigureAwait(false);
            }

            using var fileStream = File.OpenRead(cachedFilePath);
            byte[] fileBytes = new byte[fileStream.Length];
            fileStream.Read(fileBytes, 0, fileBytes.Length);

            return(fileBytes);
        }
Beispiel #19
0
        public StorageFileStreamResult DownloadFile(StorageAccount storageAccount, string url)
        {
            if (storageAccount == null)
            {
                throw new ArgumentNullException("storageAccount");
            }
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }

            IStoragePlugin plugin = GetStoragePlugin(storageAccount.StoragePluginId);

            if (plugin == null)
            {
                throw new StoragePluginNotFoundException(storageAccount.StoragePluginId);
            }

            var streamResult = plugin.GetFileStream(url, storageAccount.Id);

            streamResult.FileStream = _streamFactory.MakeDownloadStream(streamResult.FileStream);
            return(streamResult);
        }
        public static async Task <StorageAccount> Load(this StorageAccount storageAccount, CsmWrapper <CsmStorageAccount> csmStorageAccount = null)
        {
            Validate.ValidateCsmStorageAccount(storageAccount);
            if (!storageAccount.IsFunctionsStorageAccount)
            {
                return(storageAccount);
            }

            if (csmStorageAccount?.properties?.provisioningState != "Succeeded")
            {
                csmStorageAccount = await WaitUntilReady(storageAccount);
            }

            var csmStorageResponse = await csmClient.HttpInvoke(HttpMethod.Post, ArmUriTemplates.StorageListKeys.Bind(storageAccount));

            await csmStorageResponse.EnsureSuccessStatusCodeWithFullError();

            var keys = await csmStorageResponse.Content.ReadAsAsync <Dictionary <string, string> >();

            storageAccount.StorageAccountKey = keys.Select(s => s.Value).FirstOrDefault();

            return(storageAccount);
        }
        private async static Task Clean(StorageAccount account)
        {
            CloudBlobClient blobClient = account.CreateCloudBlobClient();

            foreach (var testContainer in (await blobClient.ListContainersSegmentedAsync(TestArtifactPrefix, null)).Results)
            {
                await testContainer.DeleteAsync();
            }

            CloudTableClient tableClient = account.CreateCloudTableClient();

            foreach (var table in await tableClient.ListTablesSegmentedAsync(TestArtifactPrefix, null))
            {
                await table.DeleteAsync();
            }

            CloudQueueClient queueClient = account.CreateCloudQueueClient();

            foreach (var queue in (await queueClient.ListQueuesSegmentedAsync(TestArtifactPrefix, null)).Results)
            {
                await queue.DeleteAsync();
            }
        }
Beispiel #22
0
        // Call the method, and expect a failure. Return the exception.
        internal static async Task <Exception> CallFailureAsync(StorageAccount account, Type programType, MethodInfo methodInfo, object arguments)
        {
            var host = new HostBuilder()
                       .ConfigureDefaultTestHost(b =>
            {
                b.AddAzureStorage()
                .UseStorage(account);
            }, programType)
                       .Build();

            var jobHost = host.GetJobHost();

            try
            {
                await jobHost.CallAsync(methodInfo, arguments);
            }
            catch (Exception e)
            {
                return(e);
            }
            Assert.True(false, "Expected trigger to fail"); // throws
            return(null);
        }
Beispiel #23
0
        /// <summary>
        /// Negative Functional Cases : for New-AzureStorageContainer
        /// 1. Create a blob container that already exists (Negative 3)
        /// </summary>
        internal void CreateExistingContainer(Agent agent)
        {
            string CONTAINER_NAME = Utility.GenNameString("existing");

            // create container if not exists
            CloudBlobContainer container = StorageAccount.CreateCloudBlobClient().GetContainerReference(CONTAINER_NAME);

            container.CreateIfNotExists();

            try
            {
                //--------------New operation--------------
                Test.Assert(!agent.NewAzureStorageContainer(CONTAINER_NAME), Utility.GenComparisonData("NewAzureStorageContainer", false));
                // Verification for returned values
                Test.Assert(agent.Output.Count == 0, "Only 0 row returned : {0}", agent.Output.Count);
                agent.ValidateErrorMessage(MethodBase.GetCurrentMethod().Name, CONTAINER_NAME);
            }
            finally
            {
                // Recover the environment
                container.DeleteIfExists();
            }
        }
Beispiel #24
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StorageAccountFixture"/> class.
        /// </summary>
        /// <param name="fixture">HpcCacheTestFixture.</param>
        public StorageAccountFixture(HpcCacheTestFixture fixture)
        {
            this.fixture = fixture;
            using (this.Context = new HpcCacheTestContext(this.GetType().Name))
            {
                this.Context       = new HpcCacheTestContext(this.GetType().Name);
                this.StorageTarget = this.AddClfsStorageTarget(this.Context);
                Match clfsTargetMatch    = ClfsTargetRegex.Match(this.StorageTarget.Clfs.Target);
                var   storageAccountName = clfsTargetMatch.Groups["StorageAccountName"].Value;
                StorageManagementClient storageManagementClient = this.Context.GetClient <StorageManagementClient>();
                StorageAccountsHelper   storageAccountsHelper   = new StorageAccountsHelper(storageManagementClient, this.fixture.ResourceGroup);
                StorageAccount          existingStorageAccount  = storageAccountsHelper.GetStorageAccount(this.fixture.ResourceGroup.Name, storageAccountName);
                if (!this.storageAccountsCache.TryGetValue(existingStorageAccount.Name, out StorageAccount _))
                {
                    this.storageAccountsCache.Add(existingStorageAccount.Name, existingStorageAccount);
                }

                for (int i = 0; i < 10; i++)
                {
                    this.AddBlobContainer(this.Context, this.fixture.ResourceGroup, existingStorageAccount, suffix: i.ToString());
                }
            }
        }
        private PSDataBoxEdgeStorageAccount CreateResource()
        {
            var storageAccountCredential = this.DataBoxEdgeManagementClient.StorageAccountCredentials.Get(
                this.DeviceName,
                this.StorageAccountCredentialName,
                this.ResourceGroupName);

            var edgeStorageAccount = new StorageAccount(
                name: Name,
                dataPolicy: "Cloud",
                storageAccountStatus: "OK",
                description: "",
                storageAccountCredentialId: storageAccountCredential.Id);

            edgeStorageAccount = this.DataBoxEdgeManagementClient.StorageAccounts.CreateOrUpdate(
                DeviceName,
                Name,
                edgeStorageAccount,
                this.ResourceGroupName
                );
            return(new PSDataBoxEdgeStorageAccount(edgeStorageAccount
                                                   ));
        }
Beispiel #26
0
        public void CreateExistingTable()
        {
            string TABLE_NAME = Utility.GenNameString("existing");

            // create table if not exists
            CloudTable table = StorageAccount.CreateCloudTableClient().GetTableReference(TABLE_NAME);

            table.CreateIfNotExists();

            try
            {
                //--------------New operation--------------
                Test.Assert(!CommandAgent.NewAzureStorageTable(TABLE_NAME), Utility.GenComparisonData("NewAzureStorageTable", false));
                // Verification for returned values
                Test.Assert(CommandAgent.Output.Count == 0, "Only 0 row returned : {0}", CommandAgent.Output.Count);
                CommandAgent.ValidateErrorMessage(MethodBase.GetCurrentMethod().Name, TABLE_NAME);
            }
            finally
            {
                // Recover the environment
                table.DeleteIfExists();
            }
        }
Beispiel #27
0
        private static async Task <V2Data> GetData()
        {
            var    cachedFilePath = Path.Combine(Path.GetTempPath(), "data.json");
            string serializedData;

            if (File.Exists(cachedFilePath))
            {
                serializedData = File.ReadAllText(cachedFilePath);
            }
            else
            {
                serializedData = await StorageAccount.NewFromConnectionString(Environment.GetEnvironmentVariable("AzureWebJobsStorage", EnvironmentVariableTarget.Process))
                                 .CreateCloudBlobClient()
                                 .GetContainerReference(StaticData.ShiurimContainerName)
                                 .GetBlockBlobReference("data.json")
                                 .DownloadTextAsync()
                                 .ConfigureAwait(false);

                File.WriteAllText(cachedFilePath, serializedData);
            }

            return(JsonSerializer.Deserialize <V2Data>(serializedData));
        }
Beispiel #28
0
        /// <summary>
        /// Asychronously deploys a storage container analogous to the given model
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public override async Task DeployAsync(StorageTable model)
        {
            var tableName = ExtractTableName(model);

            Trace.TraceInformation("Creating table '{0}' if not exists", tableName);


            // Setup connection
            StorageAccount = await StorageAccounts.ReadAsync(model.StorageAccountId);

            if (StorageAccount == null)
            {
                Trace.TraceError(String.Format("Unable to find storage account {0}", model.StorageAccountId));
                return;
            }

            CloudTableFactory.StorageConnectionString = StorageAccount.ToConnectionString();

            //Create or return table
            var table = await CloudTableFactory.GetTableAsync(tableName);

            Trace.TraceInformation("Created table '{0}'", model.Name);
        }
Beispiel #29
0
        private async Task <StorageAccountInformation> CreateStorageAccountAsync(SqlManagementTestContext context, ResourceGroup resourceGroup)
        {
            string accountName                     = SqlManagementTestUtilities.GenerateName(prefix: StorageNamePrefix);
            StorageManagementClient client         = context.GetClient <StorageManagementClient>();
            StorageAccount          storageAccount = await client.StorageAccounts.CreateAsync(
                resourceGroup.Name,
                accountName : accountName,
                parameters : new StorageAccountCreateParameters(
                    new Microsoft.Azure.Management.Storage.Models.Sku(SkuName.StandardLRS, SkuTier.Standard),
                    Kind.BlobStorage,
                    resourceGroup.Location,
                    accessTier: AccessTier.Cool));

            StorageAccountListKeysResult keys =
                client.StorageAccounts.ListKeys(resourceGroup.Name, storageAccount.Name);

            return(new StorageAccountInformation
            {
                Name = accountName,
                Endpoint = storageAccount.PrimaryEndpoints.Blob,
                PrimaryKey = keys.Keys.First().Value
            });
        }
Beispiel #30
0
        public async Task AddRemoveTag()
        {
            //create storage account
            string accountName = await CreateValidAccountNameAsync(namePrefix);

            _resourceGroup = await CreateResourceGroupAsync();

            StorageAccountContainer        storageAccountContainer = _resourceGroup.GetStorageAccounts();
            StorageAccountCreateParameters parameters = GetDefaultStorageAccountParameters();
            StorageAccount account = (await storageAccountContainer.CreateOrUpdateAsync(accountName, parameters)).Value;

            //add tag to this storage account
            account = await account.AddTagAsync("key", "value");

            //verify the tag is added successfully
            Assert.AreEqual(account.Data.Tags.Count, 1);

            //remove tag
            account = await account.RemoveTagAsync("key");

            //verify the tag is removed successfully
            Assert.AreEqual(account.Data.Tags.Count, 0);
        }
Beispiel #31
0
        static void Main(string[] args)
        {
            try
            {
                // Variables for the cloud storage objects.
                var storageAccount = new StorageAccount();

               // Use the emulatedstorage account.
                var cloudStorageAccount = new CloudStorageAccount(new StorageCredentialsAccountAndKey(storageAccount.Account, storageAccount.Key), storageAccount.Https);

                // If you want to use Windows Azure cloud storage account, use the following
                // code (after uncommenting) instead of the code above.
                // cloudStorageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=http;AccountName=your_storage_account_name;AccountKey=your_storage_account_key");

                // Create the blob client, which provides
                // authenticated access to the Blob service.
                CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();

                // Get the container reference.
                Console.Write("input your container:");
                storageAccount.Container = Console.ReadLine();
                CloudBlobContainer blobContainer = blobClient.GetContainerReference(storageAccount.Container);
                // Create the container if it does not exist.
                blobContainer.CreateIfNotExist();

                // Set permissions on the container.
                var containerPermissions = new BlobContainerPermissions
                    {
                        PublicAccess = BlobContainerPublicAccessType.Blob
                    };
                // This sample sets the container to have public blobs. Your application
                // needs may be different. See the documentation for BlobContainerPermissions
                // for more information about blob container permissions.
                blobContainer.SetPermissions(containerPermissions);

                var directory = GetDirectory();
                var fileInfos = GetAllFiles(directory);
                foreach (var @fileInfo in fileInfos)
                {
                    CloudBlob blob = blobContainer.GetBlobReference(@fileInfo.FullName.Replace(directory,""));

                // Upload a file from the local system to the blob.

                    Console.WriteLine("Starting file upload");
                    blob.UploadFile(@fileInfo.FullName);  // File from emulated storage.
                    Console.WriteLine("File upload complete to blob: " + blob.Uri);
                }
            }
            catch (StorageClientException e)
            {
                Console.WriteLine("Storage client error encountered: " + e.Message);

                // Exit the application with exit code 1.
                System.Environment.Exit(1);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error encountered: " + e.Message);

                // Exit the application with exit code 1.
                System.Environment.Exit(1);
            }
            finally
            {
                // Exit the application.
                System.Environment.Exit(0);
            }
        }