public SharedBlobListener(IStorageAccount storageAccount,
     IBackgroundExceptionDispatcher backgroundExceptionDispatcher)
 {
     _strategy = CreateStrategy(storageAccount);
     // Start the first iteration immediately.
     _timer = new TaskSeriesTimer(_strategy, backgroundExceptionDispatcher, initialWait: Task.Delay(0));
 }
 public FakeQueueProcessorFactory(IStorageAccountProvider accountProvider)
 {
     CancellationToken token = new CancellationToken();
     Task<IStorageAccount> task = accountProvider.GetStorageAccountAsync(token);
     task.Wait();
     _storageAccount = task.Result;
 }
예제 #3
0
 public static bool IsDevelopmentStorageAccount(IStorageAccount account)
 {
     // see the section "Addressing local storage resources" in http://msdn.microsoft.com/en-us/library/windowsazure/hh403989.aspx 
     return String.Equals(
         account.BlobEndpoint.PathAndQuery.TrimStart('/'),
         account.Credentials.AccountName,
         StringComparison.OrdinalIgnoreCase);
 }
예제 #4
0
        public static string GetAccountName(IStorageAccount account)
        {
            if (account == null)
            {
                return null;
            }

            return GetAccountName(account.Credentials);
        }
 private static string GetSharedHostName(IEnumerable<MethodInfo> indexedMethods, IStorageAccount storageAccount)
 {
     // Determine the host name from the method list
     MethodInfo firstMethod = indexedMethods.FirstOrDefault();
     Assembly hostAssembly = firstMethod != null ? firstMethod.DeclaringType.Assembly : null;
     string hostName = hostAssembly != null ? hostAssembly.FullName : "Unknown";
     string sharedHostName = storageAccount.Credentials.AccountName + "/" + hostName;
     return sharedHostName;
 }
 public FakeQueueProcessor(QueueProcessorFactoryContext context, IStorageAccount storageAccount) :
     base(context)
 {
     // map the queue names from the context to fake queues
     IStorageQueueClient client = storageAccount.CreateQueueClient();
     _queue = client.GetQueueReference(context.Queue.Name);
     if(context.PoisonQueue != null)
     {
         _poisonQueue = client.GetQueueReference(context.PoisonQueue.Name);
     }
 }
        public async Task ValidateCredentialsAsync(IStorageAccount account, CancellationToken cancellationToken)
        {
            if (account == null)
            {
                throw new ArgumentNullException("account");
            }

            StorageCredentials credentials = account.Credentials;

            // Avoid double-validating the same account and credentials.
            if (_validatedCredentials.Contains(credentials))
            {
                return;
            }

            await ValidateCredentialsAsyncCore(account, cancellationToken);
            _validatedCredentials.Add(credentials);
        }
        public CapacityBasedAccountSelectionStrategyListEntry(IStorageAccount storageAccount, long availableCapacity)
        {
            if (storageAccount == null)
            {
                throw new ArgumentNullException("storageAccount");
            }

            if (String.IsNullOrWhiteSpace(storageAccount.Name))
            {
                throw new ArgumentNullException("storageAccount", "AccountName cannot be null or empty");
            }

            if (availableCapacity < 0)
            {
                throw new ArgumentOutOfRangeException("availableCapacity", "Available capacity must be greater than 0.");
            }

            StorageAccount = storageAccount;
            AvailableCapacity = availableCapacity;
        }
예제 #9
0
        public void Table_IfBoundToICollectorITableEntity_AddInsertsEntity()
        {
            // Arrange
            const string expectedValue = "abc";
            IStorageAccount account = CreateFakeStorageAccount();
            IStorageQueue triggerQueue = CreateQueue(account, TriggerQueueName);
            triggerQueue.AddMessage(triggerQueue.CreateMessage(expectedValue));

            // Act
            RunTrigger(account, typeof(BindToICollectorITableEntityProgram));

            // Assert
            IStorageTableClient client = account.CreateTableClient();
            IStorageTable table = client.GetTableReference(TableName);
            Assert.True(table.Exists());
            DynamicTableEntity entity = table.Retrieve<DynamicTableEntity>(PartitionKey, RowKey);
            Assert.NotNull(entity);
            Assert.NotNull(entity.Properties);
            Assert.True(entity.Properties.ContainsKey(PropertyName));
            EntityProperty property = entity.Properties[PropertyName];
            Assert.NotNull(property);
            Assert.Equal(EdmType.String, property.PropertyType);
            Assert.Equal(expectedValue, property.StringValue);
        }
예제 #10
0
        public void Table_IfBoundToIQueryableDynamicTableEntityAndExists_Binds()
        {
            // Arrange
            Guid expectedValue = Guid.NewGuid();
            IStorageAccount account = CreateFakeStorageAccount();
            IStorageQueue triggerQueue = CreateQueue(account, TriggerQueueName);
            triggerQueue.AddMessage(triggerQueue.CreateMessage("ignore"));
            IStorageTableClient client = account.CreateTableClient();
            IStorageTable table = client.GetTableReference(TableName);
            table.CreateIfNotExists();
            Dictionary<string, EntityProperty> properties = new Dictionary<string, EntityProperty>
            {
                { PropertyName, new EntityProperty(expectedValue) }
            };
            table.Insert(new DynamicTableEntity(PartitionKey, RowKey, etag: null, properties: properties));

            // Act
            IQueryable<DynamicTableEntity> result = RunTrigger<IQueryable<DynamicTableEntity>>(account,
                typeof(BindToIQueryableDynamicTableEntityProgram),
                (s) => BindToIQueryableDynamicTableEntityProgram.TaskSource = s);

            // Assert
            Assert.NotNull(result);
            DynamicTableEntity[] entities = result.ToArray();
            Assert.Equal(1, entities.Length);
            DynamicTableEntity entity = entities[0];
            Assert.NotNull(entity);
            Assert.Equal(PartitionKey, entity.PartitionKey);
            Assert.Equal(RowKey, entity.RowKey);
            Assert.NotNull(entity.Properties);
            Assert.True(entity.Properties.ContainsKey(PropertyName));
            EntityProperty property = entity.Properties[PropertyName];
            Assert.NotNull(property);
            Assert.Equal(EdmType.Guid, property.PropertyType);
            Assert.Equal(expectedValue, property.GuidValue);
        }
예제 #11
0
        private static async Task ValidateCredentialsAsyncCore(IStorageAccount account,
                                                               CancellationToken cancellationToken)
        {
            // Verify the credentials are correct.
            // Have to actually ping a storage operation.
            IStorageBlobClient client = account.CreateBlobClient();

            try
            {
                // This can hang for a long time if the account name is wrong.
                // If will fail fast if the password is incorrect.
                await client.GetServicePropertiesAsync(cancellationToken);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch
            {
                string message = String.Format(CultureInfo.CurrentCulture,
                                               "The account credentials for '{0}' are incorrect.", account.Credentials.AccountName);
                throw new InvalidOperationException(message);
            }
        }
예제 #12
0
        public void Queue_IfBoundToICollectorCloudQueueMessage_AddEnqueuesMessage()
        {
            // Arrange
            const string        expectedContent = "message";
            IStorageAccount     account         = CreateFakeStorageAccount();
            IStorageQueueClient client          = account.CreateQueueClient();
            IStorageQueue       triggerQueue    = CreateQueue(client, TriggerQueueName);

            triggerQueue.AddMessage(triggerQueue.CreateMessage(expectedContent));

            // Act
            RunTrigger <object>(account, typeof(BindToICollectorCloudQueueMessageProgram),
                                (s) => BindToICollectorCloudQueueMessageProgram.TaskSource = s);

            // Assert
            IStorageQueue queue = client.GetQueueReference(QueueName);
            IEnumerable <IStorageQueueMessage> messages = queue.GetMessages(messageCount: 10);

            Assert.NotNull(messages);
            Assert.Equal(1, messages.Count());
            IStorageQueueMessage message = messages.Single();

            Assert.Same(expectedContent, message.AsString);
        }
예제 #13
0
        private static JobHostConfiguration CreateConfiguration <TResult>(IStorageAccount storageAccount, Type programType,
                                                                          IExtensionTypeLocator extensionTypeLocator, IJobActivator activator,
                                                                          TaskCompletionSource <TResult> taskSource, IFunctionInstanceLogger functionInstanceLogger, IExtensionRegistry extensions = null)
        {
            IStorageAccountProvider storageAccountProvider = new FakeStorageAccountProvider
            {
                StorageAccount = storageAccount
            };
            IHostIdProvider          hostIdProvider   = new FakeHostIdProvider();
            IWebJobsExceptionHandler exceptionHandler = new TaskBackgroundExceptionHandler <TResult>(taskSource);

            return(TestHelpers.NewConfig(
                       programType,
                       new FakeQueueConfiguration(storageAccountProvider),
                       storageAccountProvider,
                       extensionTypeLocator,
                       activator,
                       extensions,
                       exceptionHandler,
                       new NullFunctionInstanceLoggerProvider(functionInstanceLogger),
                       new NullHostInstanceLoggerProvider(),
                       new NullFunctionOutputLoggerProvider()
                       ));
        }
        public void CloudQueueAddMessage_AddsMessage()
        {
            // Arrange
            CloudStorageAccount sdkAccount = CreateSdkAccount();
            string queueName = GetQueueName("add-message");

            CloudQueue sdkQueue = CreateSdkQueue(sdkAccount, queueName);

            sdkQueue.CreateIfNotExists();

            try
            {
                string expectedContent = "hello";

                IStorageAccount     product = CreateProductUnderTest(sdkAccount);
                IStorageQueueClient client  = product.CreateQueueClient();
                Assert.NotNull(client); // Guard
                IStorageQueue queue = client.GetQueueReference(queueName);
                Assert.NotNull(queue);  // Guard

                IStorageQueueMessage message = queue.CreateMessage(expectedContent);
                Assert.NotNull(message); // Guard

                // Act
                queue.AddMessageAsync(message, CancellationToken.None).GetAwaiter().GetResult();

                // Assert
                CloudQueueMessage sdkMessage = sdkQueue.GetMessage();
                Assert.NotNull(sdkMessage);
                Assert.Equal(expectedContent, sdkMessage.AsString);
            }
            finally
            {
                sdkQueue.Delete();
            }
        }
        private static async Task ValidateCredentialsAsyncCore(IStorageAccount account,
            CancellationToken cancellationToken)
        {
            // Verify the credentials are correct.
            // Have to actually ping a storage operation.
            IStorageBlobClient client = account.CreateBlobClient();

            try
            {
                // This can hang for a long time if the account name is wrong. 
                // If will fail fast if the password is incorrect.
                await client.GetServicePropertiesAsync(cancellationToken);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch
            {
                string message = String.Format(CultureInfo.CurrentCulture,
                    "The account credentials for '{0}' are incorrect.", account.Credentials.AccountName);
                throw new InvalidOperationException(message);
            }
        }
예제 #16
0
        // Assert the given table has the given entity with PropertyName=ExpectedValue
        void AssertStringProperty(
            IStorageAccount account,
            string propertyName,
            string expectedValue,
            string tableName    = TableName,
            string partitionKey = PartitionKey,
            string rowKey       = RowKey)
        {
            // Assert
            IStorageTableClient client = account.CreateTableClient();
            IStorageTable       table  = client.GetTableReference(tableName);

            Assert.True(table.Exists());
            DynamicTableEntity entity = table.Retrieve <DynamicTableEntity>(partitionKey, rowKey);

            Assert.NotNull(entity);
            Assert.NotNull(entity.Properties);
            Assert.True(entity.Properties.ContainsKey(propertyName));
            EntityProperty property = entity.Properties[propertyName];

            Assert.NotNull(property);
            Assert.Equal(EdmType.String, property.PropertyType);
            Assert.Equal(expectedValue, property.StringValue);
        }
        protected override async Task <IReadOnlyList <StorageAccountKey> > RotateKeyringValue(IStorageAccount resource, string keyType)
        {
            var result = await resource.RegenerateKeyAsync(keyType);

            var expectedNewKey = result.FirstOrDefault(k => k.KeyName == keyType);
            var end            = DateTime.Now.AddMinutes(2);

            while (DateTime.Now < end)
            {
                var keyring = await RetrieveCurrentKeyring(resource, keyType);

                var key = keyring.FirstOrDefault(k => k.KeyName == keyType);
                if (key == null)
                {
                    continue;
                }
                if (key.Value == expectedNewKey.Value)
                {
                    return(result);
                }
            }
            throw new Exception("Storage key was reported as rotated, but didn't resync within 2 minutes!");
        }
 private static IStorageCredentialsValidator CreateValidator(IStorageAccount account)
 {
     Mock<IStorageCredentialsValidator> mock = new Mock<IStorageCredentialsValidator>(MockBehavior.Strict);
     mock.Setup(v => v.ValidateCredentialsAsync(account, It.IsAny<CancellationToken>()))
         .Returns(Task.FromResult(0));
     return mock.Object;
 }
 public FunctionAppImpl WithExistingStorageAccount(IStorageAccount storageAccount)
 {
     this.storageAccountToSet = storageAccount;
     return(this);
 }
예제 #20
0
        private static IStorageAccountParser CreateParser(IServiceProvider services, string connectionStringName, string connectionString, IStorageAccount parsedAccount)
        {
            Mock <IStorageAccountParser> mock = new Mock <IStorageAccountParser>(MockBehavior.Strict);

            mock.Setup(p => p.ParseAccount(connectionString, connectionStringName, services)).Returns(parsedAccount);
            return(mock.Object);
        }
 public GetAllBlobContainersQueryHandler(IStorageAccount storageAccount)
 {
     _storageAccount  = storageAccount;
     _cloudBlobClient = _storageAccount.GetCloudBlobClient();
 }
예제 #22
0
 private void JustDecompileGenerated_set_Account(IStorageAccount value)
 {
     this.JustDecompileGenerated_Account_k__BackingField = value;
 }
예제 #23
0
 public StorageAccount(IStorageAccount source, TargetSettings targetSettings, ILogProvider logProvider) : base(ArmConst.MicrosoftStorage, ArmConst.StorageAccounts, logProvider)
 {
     _Source = source;
     this.SetTargetName(source.Name, targetSettings);
     this.StorageAccountType = MigrationTarget.StorageAccount.GetStorageAccountType(source.AccountType);
 }
예제 #24
0
        public void CanCRUDLocks()
        {
            using (var context = FluentMockContext.Start(GetType().FullName))
            {
                IAuthorizationManager authorizationManager = TestHelper.CreateAuthorizationManager();
                IComputeManager       computeManager       = TestHelper.CreateComputeManager();
                IResourceManager      managerResources     = computeManager.ResourceManager;
                INetworkManager       networkManager       = TestHelper.CreateNetworkManager();
                IStorageManager       storageManager       = TestHelper.CreateStorageManager();

                // Prepare a VM
                string password    = SdkContext.RandomResourceName("P@s", 14);
                string rgName      = SdkContext.RandomResourceName("rg", 15);
                string vmName      = SdkContext.RandomResourceName("vm", 15);
                string storageName = SdkContext.RandomResourceName("st", 15);
                string diskName    = SdkContext.RandomResourceName("dsk", 15);
                string netName     = SdkContext.RandomResourceName("net", 15);
                Region region      = Region.USEast;

                IResourceGroup  resourceGroup = null;
                IManagementLock lockGroup     = null,
                                lockVM        = null,
                                lockStorage   = null,
                                lockDiskRO    = null,
                                lockDiskDel   = null,
                                lockSubnet    = null;

                try
                {
                    resourceGroup = managerResources.ResourceGroups.Define(rgName)
                                    .WithRegion(region)
                                    .Create();
                    Assert.NotNull(resourceGroup);

                    ICreatable <INetwork> netDefinition = networkManager.Networks.Define(netName)
                                                          .WithRegion(region)
                                                          .WithExistingResourceGroup(resourceGroup)
                                                          .WithAddressSpace("10.0.0.0/28");

                    // Define a VM for testing VM locks
                    ICreatable <IVirtualMachine> vmDefinition = computeManager.VirtualMachines.Define(vmName)
                                                                .WithRegion(region)
                                                                .WithExistingResourceGroup(resourceGroup)
                                                                .WithNewPrimaryNetwork(netDefinition)
                                                                .WithPrimaryPrivateIPAddressDynamic()
                                                                .WithoutPrimaryPublicIPAddress()
                                                                .WithPopularWindowsImage(KnownWindowsVirtualMachineImage.WindowsServer2012R2Datacenter)
                                                                .WithAdminUsername("tester")
                                                                .WithAdminPassword(password)
                                                                .WithSize(VirtualMachineSizeTypes.BasicA1);

                    // Define a managed disk for testing locks on that
                    ICreatable <IDisk> diskDefinition = computeManager.Disks.Define(diskName)
                                                        .WithRegion(region)
                                                        .WithExistingResourceGroup(resourceGroup)
                                                        .WithData()
                                                        .WithSizeInGB(100);

                    // Define a storage account for testing locks on that
                    ICreatable <IStorageAccount> storageDefinition = storageManager.StorageAccounts.Define(storageName)
                                                                     .WithRegion(region)
                                                                     .WithExistingResourceGroup(resourceGroup);

                    // Create resources in parallel to save time and money
                    Extensions.Synchronize(() => Task.WhenAll(
                                               storageDefinition.CreateAsync(),
                                               vmDefinition.CreateAsync(),
                                               diskDefinition.CreateAsync()));

                    IVirtualMachine vm      = (IVirtualMachine)vmDefinition;
                    IStorageAccount storage = (IStorageAccount)storageDefinition;
                    IDisk           disk    = (IDisk)diskDefinition;
                    INetwork        network = vm.GetPrimaryNetworkInterface().PrimaryIPConfiguration.GetNetwork();
                    ISubnet         subnet  = network.Subnets.Values.FirstOrDefault();

                    // Lock subnet
                    ICreatable <IManagementLock> lockSubnetDef = authorizationManager.ManagementLocks.Define("subnetLock")
                                                                 .WithLockedResource(subnet.Inner.Id)
                                                                 .WithLevel(LockLevel.ReadOnly);

                    // Lock VM
                    ICreatable <IManagementLock> lockVMDef = authorizationManager.ManagementLocks.Define("vmlock")
                                                             .WithLockedResource(vm)
                                                             .WithLevel(LockLevel.ReadOnly)
                                                             .WithNotes("vm readonly lock");

                    // Lock resource group
                    ICreatable <IManagementLock> lockGroupDef = authorizationManager.ManagementLocks.Define("rglock")
                                                                .WithLockedResource(resourceGroup.Id)
                                                                .WithLevel(LockLevel.CanNotDelete);

                    // Lock storage
                    ICreatable <IManagementLock> lockStorageDef = authorizationManager.ManagementLocks.Define("stLock")
                                                                  .WithLockedResource(storage)
                                                                  .WithLevel(LockLevel.CanNotDelete);

                    // Create locks in parallel
                    ICreatedResources <IManagementLock> created = authorizationManager.ManagementLocks.Create(lockVMDef, lockGroupDef, lockStorageDef, lockSubnetDef);

                    lockVM      = created.FirstOrDefault(o => o.Key.Equals(lockVMDef.Key));
                    lockStorage = created.FirstOrDefault(o => o.Key.Equals(lockStorageDef.Key));
                    lockGroup   = created.FirstOrDefault(o => o.Key.Equals(lockGroupDef.Key));
                    lockSubnet  = created.FirstOrDefault(o => o.Key.Equals(lockSubnetDef.Key));

                    // Lock disk synchronously
                    lockDiskRO = authorizationManager.ManagementLocks.Define("diskLockRO")
                                 .WithLockedResource(disk)
                                 .WithLevel(LockLevel.ReadOnly)
                                 .Create();

                    lockDiskDel = authorizationManager.ManagementLocks.Define("diskLockDel")
                                  .WithLockedResource(disk)
                                  .WithLevel(LockLevel.CanNotDelete)
                                  .Create();

                    // Verify VM lock
                    Assert.Equal(2, authorizationManager.ManagementLocks.ListForResource(vm.Id).Count());

                    Assert.NotNull(lockVM);
                    lockVM = authorizationManager.ManagementLocks.GetById(lockVM.Id);
                    Assert.NotNull(lockVM);
                    Assert.Equal(LockLevel.ReadOnly, lockVM.Level);
                    Assert.Equal(vm.Id, lockVM.LockedResourceId, true);

                    // Verify resource group lock
                    Assert.NotNull(lockGroup);
                    lockGroup = authorizationManager.ManagementLocks.GetByResourceGroup(resourceGroup.Name, "rglock");
                    Assert.NotNull(lockGroup);
                    Assert.Equal(LockLevel.CanNotDelete, lockGroup.Level);
                    Assert.Equal(resourceGroup.Id, lockGroup.LockedResourceId, true);

                    // Verify storage account lock
                    Assert.Equal(2, authorizationManager.ManagementLocks.ListForResource(storage.Id).Count());

                    Assert.NotNull(lockStorage);
                    lockStorage = authorizationManager.ManagementLocks.GetById(lockStorage.Id);
                    Assert.NotNull(lockStorage);
                    Assert.Equal(LockLevel.CanNotDelete, lockStorage.Level);
                    Assert.Equal(storage.Id, lockStorage.LockedResourceId, true);

                    // Verify disk lock
                    Assert.Equal(3, authorizationManager.ManagementLocks.ListForResource(disk.Id).Count());

                    Assert.NotNull(lockDiskRO);
                    lockDiskRO = authorizationManager.ManagementLocks.GetById(lockDiskRO.Id);
                    Assert.NotNull(lockDiskRO);
                    Assert.Equal(LockLevel.ReadOnly, lockDiskRO.Level);
                    Assert.Equal(disk.Id, lockDiskRO.LockedResourceId, true);

                    Assert.NotNull(lockDiskDel);
                    lockDiskDel = authorizationManager.ManagementLocks.GetById(lockDiskDel.Id);
                    Assert.NotNull(lockDiskDel);
                    Assert.Equal(LockLevel.CanNotDelete, lockDiskDel.Level);
                    Assert.Equal(disk.Id, lockDiskDel.LockedResourceId, true);

                    // Verify subnet lock
                    Assert.Equal(2, authorizationManager.ManagementLocks.ListForResource(network.Id).Count());

                    lockSubnet = authorizationManager.ManagementLocks.GetById(lockSubnet.Id);
                    Assert.NotNull(lockSubnet);
                    Assert.Equal(LockLevel.ReadOnly, lockSubnet.Level);
                    Assert.Equal(subnet.Inner.Id, lockSubnet.LockedResourceId, true);

                    // Verify lock collection
                    var locksSubscription = authorizationManager.ManagementLocks.List();
                    var locksGroup        = authorizationManager.ManagementLocks.ListByResourceGroup(vm.ResourceGroupName);
                    Assert.NotNull(locksSubscription);
                    Assert.NotNull(locksGroup);

                    int locksAllCount = locksSubscription.Count();
                    Assert.True(6 <= locksAllCount);

                    int locksGroupCount = locksGroup.Count();
                    Assert.Equal(6, locksGroup.Count());
                }
                finally
                {
                    try
                    {
                        if (resourceGroup != null)
                        {
                            if (lockGroup != null)
                            {
                                authorizationManager.ManagementLocks.DeleteById(lockGroup.Id);
                            }
                            if (lockVM != null)
                            {
                                authorizationManager.ManagementLocks.DeleteById(lockVM.Id);
                            }
                            if (lockDiskRO != null)
                            {
                                authorizationManager.ManagementLocks.DeleteById(lockDiskRO.Id);
                            }
                            if (lockDiskDel != null)
                            {
                                authorizationManager.ManagementLocks.DeleteById(lockDiskDel.Id);
                            }
                            if (lockStorage != null)
                            {
                                authorizationManager.ManagementLocks.DeleteById(lockStorage.Id);
                            }
                            if (lockSubnet != null)
                            {
                                authorizationManager.ManagementLocks.DeleteById(lockSubnet.Id);
                            }

                            managerResources.ResourceGroups.BeginDeleteByName(rgName);
                        }
                    }
                    catch { }
                }
            }
        }
예제 #25
0
        public BlobListenerFactory(IHostIdProvider hostIdProvider,
            IQueueConfiguration queueConfiguration,
            IBackgroundExceptionDispatcher backgroundExceptionDispatcher,
            IContextSetter<IBlobWrittenWatcher> blobWrittenWatcherSetter,
            IContextSetter<IMessageEnqueuedWatcher> messageEnqueuedWatcherSetter,
            ISharedContextProvider sharedContextProvider,
            TraceWriter trace,
            string functionId,
            IStorageAccount hostAccount,
            IStorageAccount dataAccount,
            IStorageBlobContainer container,
            IBlobPathSource input,
            ITriggeredFunctionExecutor executor,
            SingletonManager singletonManager)
        {
            if (hostIdProvider == null)
            {
                throw new ArgumentNullException("hostIdProvider");
            }

            if (queueConfiguration == null)
            {
                throw new ArgumentNullException("queueConfiguration");
            }

            if (backgroundExceptionDispatcher == null)
            {
                throw new ArgumentNullException("backgroundExceptionDispatcher");
            }

            if (blobWrittenWatcherSetter == null)
            {
                throw new ArgumentNullException("blobWrittenWatcherSetter");
            }

            if (messageEnqueuedWatcherSetter == null)
            {
                throw new ArgumentNullException("messageEnqueuedWatcherSetter");
            }

            if (sharedContextProvider == null)
            {
                throw new ArgumentNullException("sharedContextProvider");
            }

            if (trace == null)
            {
                throw new ArgumentNullException("trace");
            }

            if (hostAccount == null)
            {
                throw new ArgumentNullException("hostAccount");
            }

            if (dataAccount == null)
            {
                throw new ArgumentNullException("dataAccount");
            }

            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            if (executor == null)
            {
                throw new ArgumentNullException("executor");
            }

            if (singletonManager == null)
            {
                throw new ArgumentNullException("singletonManager");
            }

            _hostIdProvider = hostIdProvider;
            _queueConfiguration = queueConfiguration;
            _backgroundExceptionDispatcher = backgroundExceptionDispatcher;
            _blobWrittenWatcherSetter = blobWrittenWatcherSetter;
            _messageEnqueuedWatcherSetter = messageEnqueuedWatcherSetter;
            _sharedContextProvider = sharedContextProvider;
            _trace = trace;
            _functionId = functionId;
            _hostAccount = hostAccount;
            _dataAccount = dataAccount;
            _container = container;
            _input = input;
            _executor = executor;
            _singletonManager = singletonManager;
        }
        public BlobTriggerBinding(ParameterInfo parameter,
                                  IArgumentBinding <IStorageBlob> argumentBinding,
                                  IStorageAccount hostAccount,
                                  IStorageAccount dataAccount,
                                  IBlobPathSource path,
                                  IHostIdProvider hostIdProvider,
                                  IQueueConfiguration queueConfiguration,
                                  IBackgroundExceptionDispatcher backgroundExceptionDispatcher,
                                  IContextSetter <IBlobWrittenWatcher> blobWrittenWatcherSetter,
                                  IContextSetter <IMessageEnqueuedWatcher> messageEnqueuedWatcherSetter,
                                  ISharedContextProvider sharedContextProvider,
                                  SingletonManager singletonManager,
                                  TraceWriter trace)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            if (argumentBinding == null)
            {
                throw new ArgumentNullException("argumentBinding");
            }

            if (hostAccount == null)
            {
                throw new ArgumentNullException("hostAccount");
            }

            if (dataAccount == null)
            {
                throw new ArgumentNullException("dataAccount");
            }

            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (hostIdProvider == null)
            {
                throw new ArgumentNullException("hostIdProvider");
            }

            if (queueConfiguration == null)
            {
                throw new ArgumentNullException("queueConfiguration");
            }

            if (backgroundExceptionDispatcher == null)
            {
                throw new ArgumentNullException("backgroundExceptionDispatcher");
            }

            if (blobWrittenWatcherSetter == null)
            {
                throw new ArgumentNullException("blobWrittenWatcherSetter");
            }

            if (messageEnqueuedWatcherSetter == null)
            {
                throw new ArgumentNullException("messageEnqueuedWatcherSetter");
            }

            if (sharedContextProvider == null)
            {
                throw new ArgumentNullException("sharedContextProvider");
            }

            if (singletonManager == null)
            {
                throw new ArgumentNullException("singletonManager");
            }

            if (trace == null)
            {
                throw new ArgumentNullException("trace");
            }

            _parameter       = parameter;
            _argumentBinding = argumentBinding;
            _hostAccount     = hostAccount;
            _dataAccount     = dataAccount;
            StorageClientFactoryContext context = new StorageClientFactoryContext
            {
                Parameter = parameter
            };

            _blobClient                    = dataAccount.CreateBlobClient(context);
            _accountName                   = BlobClient.GetAccountName(_blobClient);
            _path                          = path;
            _hostIdProvider                = hostIdProvider;
            _queueConfiguration            = queueConfiguration;
            _backgroundExceptionDispatcher = backgroundExceptionDispatcher;
            _blobWrittenWatcherSetter      = blobWrittenWatcherSetter;
            _messageEnqueuedWatcherSetter  = messageEnqueuedWatcherSetter;
            _sharedContextProvider         = sharedContextProvider;
            _singletonManager              = singletonManager;
            _trace                         = trace;
            _converter                     = CreateConverter(_blobClient);
            _bindingDataContract           = CreateBindingDataContract(path);
        }
        public BlobTriggerBinding(ParameterInfo parameter,
            IArgumentBinding<IStorageBlob> argumentBinding,
            IStorageAccount hostAccount,
            IStorageAccount dataAccount,
            IBlobPathSource path,
            IHostIdProvider hostIdProvider,
            IQueueConfiguration queueConfiguration,
            IBackgroundExceptionDispatcher backgroundExceptionDispatcher,
            IContextSetter<IBlobWrittenWatcher> blobWrittenWatcherSetter,
            IContextSetter<IMessageEnqueuedWatcher> messageEnqueuedWatcherSetter,
            ISharedContextProvider sharedContextProvider,
            TraceWriter trace)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            if (argumentBinding == null)
            {
                throw new ArgumentNullException("argumentBinding");
            }

            if (hostAccount == null)
            {
                throw new ArgumentNullException("hostAccount");
            }

            if (dataAccount == null)
            {
                throw new ArgumentNullException("dataAccount");
            }

            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (hostIdProvider == null)
            {
                throw new ArgumentNullException("hostIdProvider");
            }

            if (queueConfiguration == null)
            {
                throw new ArgumentNullException("queueConfiguration");
            }

            if (backgroundExceptionDispatcher == null)
            {
                throw new ArgumentNullException("backgroundExceptionDispatcher");
            }

            if (blobWrittenWatcherSetter == null)
            {
                throw new ArgumentNullException("blobWrittenWatcherSetter");
            }

            if (messageEnqueuedWatcherSetter == null)
            {
                throw new ArgumentNullException("messageEnqueuedWatcherSetter");
            }

            if (sharedContextProvider == null)
            {
                throw new ArgumentNullException("sharedContextProvider");
            }

            if (trace == null)
            {
                throw new ArgumentNullException("trace");
            }

            _parameter = parameter;
            _argumentBinding = argumentBinding;
            _hostAccount = hostAccount;
            _dataAccount = dataAccount;
            StorageClientFactoryContext context = new StorageClientFactoryContext
            {
                Parameter = parameter
            };
            _blobClient = dataAccount.CreateBlobClient(context);
            _accountName = BlobClient.GetAccountName(_blobClient);
            _path = path;
            _hostIdProvider = hostIdProvider;
            _queueConfiguration = queueConfiguration;
            _backgroundExceptionDispatcher = backgroundExceptionDispatcher;
            _blobWrittenWatcherSetter = blobWrittenWatcherSetter;
            _messageEnqueuedWatcherSetter = messageEnqueuedWatcherSetter;
            _sharedContextProvider = sharedContextProvider;
            _trace = trace;
            _converter = CreateConverter(_blobClient);
            _bindingDataContract = CreateBindingDataContract(path);
        }
        public BlobListenerFactory(IHostIdProvider hostIdProvider,
            IQueueConfiguration queueConfiguration,
            IBackgroundExceptionDispatcher backgroundExceptionDispatcher,
            IContextSetter<IBlobWrittenWatcher> blobWrittenWatcherSetter,
            IContextSetter<IMessageEnqueuedWatcher> messageEnqueuedWatcherSetter,
            ISharedContextProvider sharedContextProvider,
            TextWriter log,
            string functionId,
            IStorageAccount account,
            IStorageBlobContainer container,
            IBlobPathSource input,
            ITriggeredFunctionExecutor<IStorageBlob> executor)
        {
            if (hostIdProvider == null)
            {
                throw new ArgumentNullException("hostIdProvider");
            }

            if (queueConfiguration == null)
            {
                throw new ArgumentNullException("queueConfiguration");
            }

            if (backgroundExceptionDispatcher == null)
            {
                throw new ArgumentNullException("backgroundExceptionDispatcher");
            }

            if (blobWrittenWatcherSetter == null)
            {
                throw new ArgumentNullException("blobWrittenWatcherSetter");
            }

            if (messageEnqueuedWatcherSetter == null)
            {
                throw new ArgumentNullException("messageEnqueuedWatcherSetter");
            }

            if (sharedContextProvider == null)
            {
                throw new ArgumentNullException("sharedContextProvider");
            }

            if (log == null)
            {
                throw new ArgumentNullException("log");
            }

            if (account == null)
            {
                throw new ArgumentNullException("account");
            }

            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            if (executor == null)
            {
                throw new ArgumentNullException("executor");
            }

            _hostIdProvider = hostIdProvider;
            _queueConfiguration = queueConfiguration;
            _backgroundExceptionDispatcher = backgroundExceptionDispatcher;
            _blobWrittenWatcherSetter = blobWrittenWatcherSetter;
            _messageEnqueuedWatcherSetter = messageEnqueuedWatcherSetter;
            _sharedContextProvider = sharedContextProvider;
            _log = log;
            _functionId = functionId;
            _account = account;
            _container = container;
            _input = input;
            _executor = executor;
        }
예제 #29
0
        public BlobTriggerBinding(string parameterName,
            IArgumentBinding<IStorageBlob> argumentBinding,
            IStorageAccount account,
            IBlobPathSource path,
            IHostIdProvider hostIdProvider,
            IQueueConfiguration queueConfiguration,
            IBackgroundExceptionDispatcher backgroundExceptionDispatcher,
            IContextSetter<IBlobWrittenWatcher> blobWrittenWatcherSetter,
            IContextSetter<IMessageEnqueuedWatcher> messageEnqueuedWatcherSetter,
            ISharedContextProvider sharedContextProvider,
            TextWriter log)
        {
            if (argumentBinding == null)
            {
                throw new ArgumentNullException("argumentBinding");
            }

            if (account == null)
            {
                throw new ArgumentNullException("account");
            }

            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (hostIdProvider == null)
            {
                throw new ArgumentNullException("hostIdProvider");
            }

            if (queueConfiguration == null)
            {
                throw new ArgumentNullException("queueConfiguration");
            }

            if (backgroundExceptionDispatcher == null)
            {
                throw new ArgumentNullException("backgroundExceptionDispatcher");
            }

            if (blobWrittenWatcherSetter == null)
            {
                throw new ArgumentNullException("blobWrittenWatcherSetter");
            }

            if (messageEnqueuedWatcherSetter == null)
            {
                throw new ArgumentNullException("messageEnqueuedWatcherSetter");
            }

            if (sharedContextProvider == null)
            {
                throw new ArgumentNullException("sharedContextProvider");
            }

            if (log == null)
            {
                throw new ArgumentNullException("log");
            }

            _parameterName = parameterName;
            _argumentBinding = argumentBinding;
            _account = account;
            _client = account.CreateBlobClient();
            _accountName = BlobClient.GetAccountName(_client);
            _path = path;
            _hostIdProvider = hostIdProvider;
            _queueConfiguration = queueConfiguration;
            _backgroundExceptionDispatcher = backgroundExceptionDispatcher;
            _blobWrittenWatcherSetter = blobWrittenWatcherSetter;
            _messageEnqueuedWatcherSetter = messageEnqueuedWatcherSetter;
            _sharedContextProvider = sharedContextProvider;
            _log = log;
            _converter = CreateConverter(_client);
            _bindingDataContract = CreateBindingDataContract(path);
        }
 ///GENMHASH:FE78AAEC8A3F18AD3D30F2F674958269:FEAE7C5EDC8B0DDC022045F56D258F16
 public EventHubImpl WithExistingStorageAccountForCapturedData(IStorageAccount storageAccount, string containerName)
 {
     this.captureSettings.WithExistingStorageAccountForCapturedData(storageAccount, containerName);
     return(this);
 }
 private static IBlobListenerStrategy CreateStrategy(IStorageAccount account)
 {
     if (!StorageClient.IsDevelopmentStorageAccount(account))
     {
         return new PollLogsStrategy();
     }
     else
     {
         return new ScanContainersStrategy();
     }
 }
        /**
         * Azure App Service basic sample for managing web apps.
         *  - Create a storage account and upload a couple blobs
         *  - Create a web app that contains the connection string to the storage account
         *  - Deploy a Tomcat application that reads from the storage account
         *  - Clean up
         */
        public static void RunSample(IAzure azure)
        {
            string app1Name      = SdkContext.RandomResourceName("webapp1-", 20);
            string app1Url       = app1Name + SUFFIX;
            string storageName   = SdkContext.RandomResourceName("jsdkstore", 20);
            string containerName = SdkContext.RandomResourceName("jcontainer", 20);
            string rgName        = SdkContext.RandomResourceName("rg1NEMV_", 24);

            try {
                //============================================================
                // Create a storage account for the web app to use

                Utilities.Log("Creating storage account " + storageName + "...");

                IStorageAccount storageAccount = azure.StorageAccounts.Define(storageName)
                                                 .WithRegion(Region.USWest)
                                                 .WithNewResourceGroup(rgName)
                                                 .Create();

                string accountKey = storageAccount.GetKeys()[0].Value;

                string connectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",
                                                        storageAccount.Name, accountKey);

                Utilities.Log("Created storage account " + storageAccount.Name);

                //============================================================
                // Upload a few files to the storage account blobs

                Utilities.Log("Uploading 2 blobs to container " + containerName + "...");

                Utilities.UploadFilesToContainer(
                    connectionString,
                    containerName,
                    new[]
                {
                    Path.Combine(Utilities.ProjectPath, "Asset", "helloworld.war"),
                    Path.Combine(Utilities.ProjectPath, "Asset", "install_apache.Sh")
                });

                Utilities.Log("Uploaded 2 blobs to container " + containerName);

                //============================================================
                // Create a web app with a new app service plan

                Utilities.Log("Creating web app " + app1Name + "...");

                IWebApp app1 = azure.WebApps.Define(app1Name)
                               .WithRegion(Region.USWest)
                               .WithExistingResourceGroup(rgName)
                               .WithNewLinuxPlan(PricingTier.StandardS1)
                               .WithPublicDockerHubImage("tomcat:8-jre8")
                               .WithStartUpCommand("/bin/bash -c \"sed -ie 's/appBase=\\\"webapps\\\"/appBase=\\\"\\\\/home\\\\/site\\\\/wwwroot\\\\/webapps\\\"/g' conf/server.xml && catalina.sh run\"")
                               .WithConnectionString("storage.connectionString", connectionString, ConnectionStringType.Custom)
                               .WithAppSetting("storage.containerName", containerName)
                               .WithAppSetting("PORT", "8080")
                               .Create();

                Utilities.Log("Created web app " + app1.Name);
                Utilities.Print(app1);

                //============================================================
                // Deploy a web app that connects to the storage account
                // Source code: https://github.com/jianghaolu/azure-samples-blob-explorer

                Utilities.Log("Deploying azure-samples-blob-traverser.war to " + app1Name + " through FTP...");

                Utilities.UploadFileToWebApp(
                    app1.GetPublishingProfile(),
                    Path.Combine(Utilities.ProjectPath, "Asset", "azure-samples-blob-traverser.war"));

                Utilities.Log("Deployment azure-samples-blob-traverser.war to web app " + app1.Name + " completed");
                Utilities.Print(app1);

                // warm up
                Utilities.Log("Warming up " + app1Url + "/azure-samples-blob-traverser...");
                Utilities.CheckAddress("http://" + app1Url + "/azure-samples-blob-traverser");
                SdkContext.DelayProvider.Delay(5000);
                Utilities.Log("CURLing " + app1Url + "/azure-samples-blob-traverser...");
                Utilities.Log(Utilities.CheckAddress("http://" + app1Url + "/azure-samples-blob-traverser"));
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    azure.ResourceGroups.BeginDeleteByName(rgName);
                    Utilities.Log("Deleted Resource Group: " + rgName);
                }
                catch (NullReferenceException)
                {
                    Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
                }
                catch (Exception g)
                {
                    Utilities.Log(g);
                }
            }
        }
예제 #33
0
 public StorageAccount(IStorageAccount source, TargetSettings targetSettings)
 {
     _Source = source;
     this.SetTargetName(source.Name, targetSettings);
     this.StorageAccountType = MigrationTarget.StorageAccount.GetStorageAccountType(source.AccountType);
 }
예제 #34
0
 public override AccountIdentifier CreateAccountIdentifier(IStorageAccount account)
 {
     return(new TableSignedAccessAccountIdentifier(account, this.TableName, this.StartingPartitionKey, this.StartingRowKey, this.EndingPartitionKey, this.EndingRowKey, base.KeyUsedForSigning.Permissions));
 }
예제 #35
0
 public AccountSasAccessIdentifier(IStorageAccount account, SecretKeyPermissions keyPermission) : base(account, keyPermission)
 {
     this.SignedAccessPermission = SASPermission.None;
     this.SignedProtocol         = SasProtocol.All;
 }
        public static async Task <JobHostContext> CreateAndLogHostStartedAsync(
            JobHost host,
            IStorageAccountProvider storageAccountProvider,
            IQueueConfiguration queueConfiguration,
            ITypeLocator typeLocator,
            IJobActivator activator,
            INameResolver nameResolver,
            IConsoleProvider consoleProvider,
            JobHostConfiguration config,
            CancellationToken shutdownToken,
            CancellationToken cancellationToken,
            IHostIdProvider hostIdProvider                                 = null,
            FunctionExecutor functionExecutor                              = null,
            IFunctionIndexProvider functionIndexProvider                   = null,
            IBindingProvider bindingProvider                               = null,
            IHostInstanceLoggerProvider hostInstanceLogerProvider          = null,
            IFunctionInstanceLoggerProvider functionInstanceLoggerProvider = null,
            IFunctionOutputLoggerProvider functionOutputLoggerProvider     = null,
            IBackgroundExceptionDispatcher backgroundExceptionDispatcher   = null,
            SingletonManager singletonManager                              = null)
        {
            if (hostIdProvider == null)
            {
                hostIdProvider = new DynamicHostIdProvider(storageAccountProvider, () => functionIndexProvider);
            }

            IExtensionTypeLocator extensionTypeLocator = new ExtensionTypeLocator(typeLocator);

            if (backgroundExceptionDispatcher == null)
            {
                backgroundExceptionDispatcher = BackgroundExceptionDispatcher.Instance;
            }
            ContextAccessor <IMessageEnqueuedWatcher> messageEnqueuedWatcherAccessor = new ContextAccessor <IMessageEnqueuedWatcher>();
            ContextAccessor <IBlobWrittenWatcher>     blobWrittenWatcherAccessor     = new ContextAccessor <IBlobWrittenWatcher>();
            ISharedContextProvider sharedContextProvider = new SharedContextProvider();

            // Create a wrapper TraceWriter that delegates to both the user
            // TraceWriter specified on Config (if present), as well as to Console
            TraceWriter trace = new ConsoleTraceWriter(config.Tracing, consoleProvider.Out);

            // Register system services with the service container
            config.AddService <INameResolver>(nameResolver);

            ExtensionConfigContext context = new ExtensionConfigContext
            {
                Config = config,
                Trace  = trace,
                Host   = host
            };

            InvokeExtensionConfigProviders(context);

            IExtensionRegistry      extensions             = config.GetExtensions();
            ITriggerBindingProvider triggerBindingProvider = DefaultTriggerBindingProvider.Create(nameResolver,
                                                                                                  storageAccountProvider, extensionTypeLocator, hostIdProvider, queueConfiguration, backgroundExceptionDispatcher,
                                                                                                  messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, sharedContextProvider, extensions, trace);

            if (bindingProvider == null)
            {
                bindingProvider = DefaultBindingProvider.Create(nameResolver, storageAccountProvider, extensionTypeLocator, messageEnqueuedWatcherAccessor, blobWrittenWatcherAccessor, extensions);
            }

            DefaultLoggerProvider loggerProvider = new DefaultLoggerProvider(storageAccountProvider, trace);

            if (singletonManager == null)
            {
                singletonManager = new SingletonManager(storageAccountProvider, backgroundExceptionDispatcher, config.Singleton, trace, config.NameResolver);
            }

            using (CancellationTokenSource combinedCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, shutdownToken))
            {
                CancellationToken combinedCancellationToken = combinedCancellationSource.Token;

                await WriteSiteExtensionManifestAsync(combinedCancellationToken);

                IStorageAccount dashboardAccount = await storageAccountProvider.GetDashboardAccountAsync(combinedCancellationToken);

                IHostInstanceLogger hostInstanceLogger = null;
                if (hostInstanceLogerProvider != null)
                {
                    hostInstanceLogger = await hostInstanceLogerProvider.GetAsync(combinedCancellationToken);
                }
                else
                {
                    hostInstanceLogger = await((IHostInstanceLoggerProvider)loggerProvider).GetAsync(combinedCancellationToken);
                }

                IFunctionInstanceLogger functionInstanceLogger = null;
                if (functionInstanceLoggerProvider != null)
                {
                    functionInstanceLogger = await functionInstanceLoggerProvider.GetAsync(combinedCancellationToken);
                }
                else
                {
                    functionInstanceLogger = (IFunctionInstanceLogger)(await((IFunctionInstanceLoggerProvider)loggerProvider).GetAsync(combinedCancellationToken));
                }

                IFunctionOutputLogger functionOutputLogger = null;
                if (functionOutputLoggerProvider != null)
                {
                    functionOutputLogger = await functionOutputLoggerProvider.GetAsync(combinedCancellationToken);
                }
                else
                {
                    functionOutputLogger = (IFunctionOutputLogger)(await((IFunctionOutputLoggerProvider)loggerProvider).GetAsync(combinedCancellationToken));
                }

                if (functionExecutor == null)
                {
                    functionExecutor = new FunctionExecutor(functionInstanceLogger, functionOutputLogger, backgroundExceptionDispatcher, trace, config.FunctionTimeout);
                }

                if (functionIndexProvider == null)
                {
                    functionIndexProvider = new FunctionIndexProvider(typeLocator, triggerBindingProvider, bindingProvider, activator, functionExecutor, extensions, singletonManager);
                }

                IFunctionIndex functions = await functionIndexProvider.GetAsync(combinedCancellationToken);

                IListenerFactory functionsListenerFactory = new HostListenerFactory(functions.ReadAll(), singletonManager, activator, nameResolver, trace);

                IFunctionExecutor hostCallExecutor;
                IListener         listener;
                HostOutputMessage hostOutputMessage;

                if (dashboardAccount != null)
                {
                    string hostId = await hostIdProvider.GetHostIdAsync(cancellationToken);

                    string sharedQueueName = HostQueueNames.GetHostQueueName(hostId);
                    IStorageQueueClient dashboardQueueClient       = dashboardAccount.CreateQueueClient();
                    IStorageQueue       sharedQueue                = dashboardQueueClient.GetQueueReference(sharedQueueName);
                    IListenerFactory    sharedQueueListenerFactory = new HostMessageListenerFactory(sharedQueue,
                                                                                                    queueConfiguration, backgroundExceptionDispatcher, trace, functions,
                                                                                                    functionInstanceLogger, functionExecutor);

                    Guid             hostInstanceId               = Guid.NewGuid();
                    string           instanceQueueName            = HostQueueNames.GetHostQueueName(hostInstanceId.ToString("N"));
                    IStorageQueue    instanceQueue                = dashboardQueueClient.GetQueueReference(instanceQueueName);
                    IListenerFactory instanceQueueListenerFactory = new HostMessageListenerFactory(instanceQueue,
                                                                                                   queueConfiguration, backgroundExceptionDispatcher, trace, functions,
                                                                                                   functionInstanceLogger, functionExecutor);

                    HeartbeatDescriptor heartbeatDescriptor = new HeartbeatDescriptor
                    {
                        SharedContainerName = HostContainerNames.Hosts,
                        SharedDirectoryName = HostDirectoryNames.Heartbeats + "/" + hostId,
                        InstanceBlobName    = hostInstanceId.ToString("N"),
                        ExpirationInSeconds = (int)HeartbeatIntervals.ExpirationInterval.TotalSeconds
                    };

                    IStorageBlockBlob blob = dashboardAccount.CreateBlobClient()
                                             .GetContainerReference(heartbeatDescriptor.SharedContainerName)
                                             .GetBlockBlobReference(heartbeatDescriptor.SharedDirectoryName + "/" + heartbeatDescriptor.InstanceBlobName);
                    IRecurrentCommand heartbeatCommand = new UpdateHostHeartbeatCommand(new HeartbeatCommand(blob));

                    IEnumerable <MethodInfo> indexedMethods = functions.ReadAllMethods();
                    Assembly hostAssembly = GetHostAssembly(indexedMethods);
                    string   displayName  = hostAssembly != null?hostAssembly.GetName().Name : "Unknown";

                    hostOutputMessage = new DataOnlyHostOutputMessage
                    {
                        HostInstanceId      = hostInstanceId,
                        HostDisplayName     = displayName,
                        SharedQueueName     = sharedQueueName,
                        InstanceQueueName   = instanceQueueName,
                        Heartbeat           = heartbeatDescriptor,
                        WebJobRunIdentifier = WebJobRunIdentifier.Current
                    };

                    hostCallExecutor = CreateHostCallExecutor(instanceQueueListenerFactory, heartbeatCommand,
                                                              backgroundExceptionDispatcher, shutdownToken, functionExecutor);
                    IListenerFactory hostListenerFactory = new CompositeListenerFactory(functionsListenerFactory,
                                                                                        sharedQueueListenerFactory, instanceQueueListenerFactory);
                    listener = CreateHostListener(hostListenerFactory, heartbeatCommand, backgroundExceptionDispatcher, shutdownToken);

                    // Publish this to Azure logging account so that a web dashboard can see it.
                    await LogHostStartedAsync(functions, hostOutputMessage, hostInstanceLogger, combinedCancellationToken);
                }
                else
                {
                    hostCallExecutor = new ShutdownFunctionExecutor(shutdownToken, functionExecutor);

                    IListener factoryListener  = new ListenerFactoryListener(functionsListenerFactory);
                    IListener shutdownListener = new ShutdownListener(shutdownToken, factoryListener);
                    listener = shutdownListener;

                    hostOutputMessage = new DataOnlyHostOutputMessage();
                }

                functionExecutor.HostOutputMessage = hostOutputMessage;

                IEnumerable <FunctionDescriptor> descriptors = functions.ReadAllDescriptors();
                int descriptorsCount = descriptors.Count();

                if (descriptorsCount == 0)
                {
                    trace.Warning(string.Format("No job functions found. Try making your job classes and methods public. {0}",
                                                Constants.ExtensionInitializationMessage), TraceSource.Indexing);
                }
                else
                {
                    StringBuilder functionsTrace = new StringBuilder();
                    functionsTrace.AppendLine("Found the following functions:");

                    foreach (FunctionDescriptor descriptor in descriptors)
                    {
                        functionsTrace.AppendLine(descriptor.FullName);
                    }

                    trace.Info(functionsTrace.ToString(), TraceSource.Indexing);
                }

                return(new JobHostContext(functions, hostCallExecutor, listener, trace));
            }
        }
예제 #37
0
        public static void RunSample(IAzure azure)
        {
            string rgName             = SdkContext.RandomResourceName("rgSTMS", 20);
            string networkName        = SdkContext.RandomResourceName("nw", 20);
            string subnetName         = "subnetA";
            string storageAccountName = SdkContext.RandomResourceName("sa", 15);
            string publicIpName       = SdkContext.RandomResourceName("pip", 20);
            string vmName             = SdkContext.RandomResourceName("vm", 10);

            try
            {
                // ============================================================
                // Create a virtual network and a subnet with storage service subnet access enabled

                Utilities.Log("Creating a Virtual network and subnet with storage service subnet access enabled:");

                INetwork network = azure.Networks.Define(networkName)
                                   .WithRegion(Region.USEast)
                                   .WithNewResourceGroup(rgName)
                                   .WithAddressSpace("10.0.0.0/28")
                                   .DefineSubnet(subnetName)
                                   .WithAddressPrefix("10.0.0.8/29")
                                   .WithAccessFromService(ServiceEndpointType.MicrosoftStorage)
                                   .Attach()
                                   .Create();

                Utilities.Log("Created a Virtual network with subnet:");
                Utilities.PrintVirtualNetwork(network);

                // ============================================================
                // Create a storage account with access to it allowed only from a specific subnet

                var subnetId = $"{network.Id}/subnets/{subnetName}";

                Utilities.Log($"Creating a storage account with access allowed only from the subnet{subnetId}");

                IStorageAccount storageAccount = azure.StorageAccounts.Define(storageAccountName)
                                                 .WithRegion(Region.USEast)
                                                 .WithExistingResourceGroup(rgName)
                                                 .WithAccessFromSelectedNetworks()
                                                 .WithAccessFromNetworkSubnet(subnetId)
                                                 .Create();

                Utilities.Log("Created storage account:");
                Utilities.PrintStorageAccount(storageAccount);


                // ============================================================
                // Create a public IP address

                Utilities.Log("Creating a Public IP address");

                IPublicIPAddress publicIPAddress = azure.PublicIPAddresses
                                                   .Define(publicIpName)
                                                   .WithRegion(Region.USEast)
                                                   .WithExistingResourceGroup(rgName)
                                                   .WithLeafDomainLabel(publicIpName)
                                                   .Create();

                Utilities.Log("Created Public IP address:");
                Utilities.PrintIPAddress(publicIPAddress);

                // ============================================================
                // Create a virtual machine and associate the public IP address

                Utilities.Log("Creating a VM with the Public IP address");

                IVirtualMachine linuxVM = azure.VirtualMachines
                                          .Define(vmName)
                                          .WithRegion(Region.USEast)
                                          .WithExistingResourceGroup(rgName)
                                          .WithNewPrimaryNetwork("10.1.0.0/28")
                                          .WithPrimaryPrivateIPAddressDynamic()
                                          .WithExistingPrimaryPublicIPAddress(publicIPAddress)
                                          .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
                                          .WithRootUsername(Utilities.CreateUsername())
                                          .WithRootPassword(Utilities.CreatePassword())
                                          .WithSize(VirtualMachineSizeTypes.Parse("Standard_D2a_v4"))
                                          .Create();

                Utilities.Log($"Created the VM: {linuxVM.Id}");
                Utilities.PrintVirtualMachine(linuxVM);

                publicIPAddress.Refresh();  // Refresh public IP resource to populate the assigned IPv4 address

                // ============================================================
                // Update the storage account so that it can also be accessed from the PublicIP address

                Utilities.Log($"Updating storage account with access also allowed from publicIP{publicIPAddress.IPAddress}");

                storageAccount.Update()
                .WithAccessFromIpAddress(publicIPAddress.IPAddress)
                .Apply();

                Utilities.Log("Updated storage account:");
                Utilities.PrintStorageAccount(storageAccount);

                // ============================================================
                //  Update the storage account to restrict incoming traffic to HTTPS

                Utilities.Log("Restricting access to storage account only via HTTPS");

                storageAccount.Update()
                .WithOnlyHttpsTraffic()
                .Apply();

                Utilities.Log("Updated the storage account:");
                Utilities.PrintStorageAccount(storageAccount);
            }
            finally
            {
                if (azure.ResourceGroups.GetByName(rgName) != null)
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    azure.ResourceGroups.DeleteByName(rgName);
                    Utilities.Log("Deleted Resource Group: " + rgName);
                }
                else
                {
                    Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
                }
            }
        }
예제 #38
0
        public static void AddServices(this JobHostConfiguration config, params object[] services)
        {
            // Set extensionRegistry first since other services may depend on it.
            foreach (var obj in services)
            {
                IExtensionRegistry extensionRegistry = obj as IExtensionRegistry;
                if (extensionRegistry != null)
                {
                    config.AddService <IExtensionRegistry>(extensionRegistry);
                    break;
                }
            }

            IExtensionRegistry extensions = config.GetService <IExtensionRegistry>();

            var types = new Type[] {
                typeof(IAsyncCollector <FunctionInstanceLogEntry>),
                typeof(IHostInstanceLoggerProvider),
                typeof(IFunctionInstanceLoggerProvider),
                typeof(IFunctionOutputLoggerProvider),
                typeof(IConsoleProvider),
                typeof(ITypeLocator),
                typeof(IWebJobsExceptionHandler),
                typeof(INameResolver),
                typeof(IJobActivator),
                typeof(IExtensionTypeLocator),
                typeof(SingletonManager),
                typeof(IHostIdProvider),
                typeof(IQueueConfiguration),
                typeof(IExtensionRegistry),
                typeof(IDistributedLockManager),
                typeof(ILoggerFactory),
                typeof(IFunctionIndexProvider) // set to unit test indexing.
            };

            foreach (var obj in services)
            {
                if (obj == null ||
                    obj is ILoggerProvider)
                {
                    continue;
                }

                IStorageAccountProvider storageAccountProvider = obj as IStorageAccountProvider;
                IStorageAccount         account = obj as IStorageAccount;
                if (account != null)
                {
                    storageAccountProvider = new FakeStorageAccountProvider
                    {
                        StorageAccount = account
                    };
                }
                if (storageAccountProvider != null)
                {
                    config.AddService <IStorageAccountProvider>(storageAccountProvider);
                    continue;
                }

                // A new extension
                IExtensionConfigProvider extension = obj as IExtensionConfigProvider;
                if (extension != null)
                {
                    extensions.RegisterExtension <IExtensionConfigProvider>(extension);
                    continue;
                }

                // A function filter
                if (obj is IFunctionInvocationFilter)
                {
                    extensions.RegisterExtension <IFunctionInvocationFilter>((IFunctionInvocationFilter)obj);
                    continue;
                }

                if (obj is IFunctionExceptionFilter)
                {
                    extensions.RegisterExtension <IFunctionExceptionFilter>((IFunctionExceptionFilter)obj);
                    continue;
                }

                // basic pattern.
                bool ok = false;
                foreach (var type in types)
                {
                    if (type.IsAssignableFrom(obj.GetType()))
                    {
                        config.AddService(type, obj);
                        ok = true;
                        break;
                    }
                }
                if (ok)
                {
                    continue;
                }

                throw new InvalidOperationException("Test bug: Unrecognized type: " + obj.GetType().FullName);
            }
        }
예제 #39
0
 /// <summary>Initializes a new instance of the <see cref="HeartbeatCommand"/> class.</summary>
 /// <param name="account">The storage account in which to write the heartbeat.</param>
 /// <param name="containerName">The name of the container in which to write the heartbeat.</param>
 /// <param name="blobName">The name of the heartbeat blob (including the directory name, if any).</param>
 public HeartbeatCommand(IStorageAccount account, string containerName, string blobName)
     : this(account.CreateBlobClient().GetContainerReference(containerName).GetBlockBlobReference(blobName))
 {
 }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation($"{DateAndTime()} | C# HTTP trigger function processed a request.");

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            ProvisioningModel provisioningModel = JsonConvert.DeserializeObject <ProvisioningModel>(requestBody);


            if (string.IsNullOrEmpty(provisioningModel.ClientId) ||
                string.IsNullOrEmpty(provisioningModel.ClientSecret) ||
                string.IsNullOrEmpty(provisioningModel.TenantId) ||
                string.IsNullOrEmpty(provisioningModel.SubscriptionId) ||
                string.IsNullOrEmpty(provisioningModel.ClustrerName) ||
                string.IsNullOrEmpty(provisioningModel.ResourceGroupName) ||
                string.IsNullOrEmpty(provisioningModel.MainVhdURL) ||
                string.IsNullOrEmpty(provisioningModel.SmtpServer) ||
                string.IsNullOrEmpty(provisioningModel.SmtpPort.ToString()) ||
                string.IsNullOrEmpty(provisioningModel.SmtpEmail) ||
                string.IsNullOrEmpty(provisioningModel.SmtpPassword))
            {
                log.LogInformation($"{DateAndTime()} | Error |  Missing parameter | \n{requestBody}");
                return(new BadRequestObjectResult(false));
            }
            else
            {
                bool isSingleInstance;

                switch (provisioningModel.InstanceCount)
                {
                case "1": { isSingleInstance = true; break; }

                case "3": {
                    isSingleInstance = false;
                    if (
                        string.IsNullOrEmpty(provisioningModel.MysqlVhdURL) ||
                        string.IsNullOrEmpty(provisioningModel.MongoVhdURL))
                    {
                        log.LogInformation($"{DateAndTime()} | Error | Missing parameter for 3 instance (MysqlVhdURL/MongoVhdURL) | \n{requestBody}");
                        return(new BadRequestObjectResult(false));
                    }
                    break;
                }

                default:
                {
                    log.LogInformation($"{DateAndTime()} | Error | Please set valid instance count (1 or 3) | \n{requestBody}");
                    return(new BadRequestObjectResult(false));
                }
                }

                SmtpClient smtpClient = new SmtpClient()
                {
                    Host                  = provisioningModel.SmtpServer,
                    Port                  = provisioningModel.SmtpPort,
                    EnableSsl             = true,
                    UseDefaultCredentials = false,
                    Credentials           = new NetworkCredential(provisioningModel.SmtpEmail, provisioningModel.SmtpPassword)
                };

                MailMessage mailMessage = new MailMessage();

                mailMessage.From = new MailAddress(provisioningModel.SmtpEmail);
                mailMessage.To.Add(new MailAddress(provisioningModel.SmtpEmail));
                mailMessage.Subject = "Branch Academy Installation";

                try
                {
                    string resourceGroupName = provisioningModel.ResourceGroupName;
                    string clusterName       = provisioningModel.ClustrerName;
                    string MainVhdURL        = provisioningModel.MainVhdURL;
                    string MysqlVhdURL       = provisioningModel.MysqlVhdURL;
                    string MongoVhdURL       = provisioningModel.MongoVhdURL;
                    string subnet            = "default";
                    string username          = provisioningModel.Username;
                    string password          = provisioningModel.Password;

                    string contactPerson = provisioningModel.SmtpEmail;

                    log.LogInformation("deploying Main instance");
                    Utils.Email(smtpClient, "Main Instance Deployed Successfully", log, mailMessage);

                    ServicePrincipalLoginInformation principalLogIn = new ServicePrincipalLoginInformation();
                    principalLogIn.ClientId     = provisioningModel.ClientId;
                    principalLogIn.ClientSecret = provisioningModel.ClientSecret;

                    AzureEnvironment environment = AzureEnvironment.AzureGlobalCloud;
                    AzureCredentials credentials = new AzureCredentials(principalLogIn, provisioningModel.TenantId, environment);

                    IAzure _azureProd = Azure.Configure()
                                        .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                                        .Authenticate(credentials)
                                        .WithSubscription(provisioningModel.SubscriptionId);


                    IResourceGroup resourceGroup = _azureProd.ResourceGroups.GetByName(resourceGroupName);
                    Region         region        = resourceGroup.Region;

                    #region comment

                    #region Create Virtual Network
                    INetwork virtualNetwork = _azureProd.Networks.Define($"{clusterName}-vnet")
                                              .WithRegion(region)
                                              .WithExistingResourceGroup(resourceGroupName)
                                              .WithAddressSpace("10.0.0.0/16")
                                              .DefineSubnet(subnet)
                                              .WithAddressPrefix("10.0.0.0/24")
                                              .Attach()
                                              .WithTag("_contact_person", contactPerson)
                                              .Create();

                    #endregion

                    log.LogInformation($"{DateAndTime()} | Created | VNET");

                    #region Create VM IP
                    IPublicIPAddress publicIpAddress = _azureProd.PublicIPAddresses.Define($"{clusterName}-vm-ip")
                                                       .WithRegion(region)
                                                       .WithExistingResourceGroup(resourceGroupName)
                                                       .WithDynamicIP()
                                                       .WithLeafDomainLabel(clusterName)
                                                       .WithTag("_contact_person", contactPerson)
                                                       .Create();
                    #endregion

                    log.LogInformation($"{DateAndTime()} | Created | VM IP Address");

                    #region NSG
                    INetworkSecurityGroup networkSecurityGroup = _azureProd.NetworkSecurityGroups.Define($"{clusterName}-nsg")
                                                                 .WithRegion(region)
                                                                 .WithExistingResourceGroup(resourceGroupName)
                                                                 .DefineRule("ALLOW-SSH")
                                                                 .AllowInbound()
                                                                 .FromAnyAddress()
                                                                 .FromAnyPort()
                                                                 .ToAnyAddress()
                                                                 .ToPort(22)
                                                                 .WithProtocol(SecurityRuleProtocol.Tcp)
                                                                 .WithPriority(100)
                                                                 .WithDescription("Allow SSH")
                                                                 .Attach()
                                                                 .DefineRule("LMS")
                                                                 .AllowInbound()
                                                                 .FromAnyAddress()
                                                                 .FromAnyPort()
                                                                 .ToAnyAddress()
                                                                 .ToPort(80)
                                                                 .WithProtocol(SecurityRuleProtocol.Tcp)
                                                                 .WithPriority(101)
                                                                 .WithDescription("LMS")
                                                                 .Attach()
                                                                 .DefineRule("CMS")
                                                                 .AllowInbound()
                                                                 .FromAnyAddress()
                                                                 .FromAnyPort()
                                                                 .ToAnyAddress()
                                                                 .ToPort(18010)
                                                                 .WithProtocol(SecurityRuleProtocol.Tcp)
                                                                 .WithPriority(102)
                                                                 .WithDescription("CMS")
                                                                 .Attach()
                                                                 .DefineRule("CMSSSLPort")
                                                                 .AllowInbound()
                                                                 .FromAnyAddress()
                                                                 .FromAnyPort()
                                                                 .ToAnyAddress()
                                                                 .ToPort(48010)
                                                                 .WithProtocol(SecurityRuleProtocol.Tcp)
                                                                 .WithPriority(112)
                                                                 .WithDescription("CMSSSLPort")
                                                                 .Attach()
                                                                 .DefineRule("LMSSSLPort")
                                                                 .AllowInbound()
                                                                 .FromAnyAddress()
                                                                 .FromAnyPort()
                                                                 .ToAnyAddress()
                                                                 .ToPort(443)
                                                                 .WithProtocol(SecurityRuleProtocol.Tcp)
                                                                 .WithPriority(122)
                                                                 .WithDescription("LMSSSLPort")
                                                                 .Attach()
                                                                 .DefineRule("Certs")
                                                                 .AllowInbound()
                                                                 .FromAnyAddress()
                                                                 .FromAnyPort()
                                                                 .ToAnyAddress()
                                                                 .ToPort(18090)
                                                                 .WithProtocol(SecurityRuleProtocol.Tcp)
                                                                 .WithPriority(132)
                                                                 .WithDescription("Certs")
                                                                 .Attach()
                                                                 .DefineRule("Discovery")
                                                                 .AllowInbound()
                                                                 .FromAnyAddress()
                                                                 .FromAnyPort()
                                                                 .ToAnyAddress()
                                                                 .ToPort(18381)
                                                                 .WithProtocol(SecurityRuleProtocol.Tcp)
                                                                 .WithPriority(142)
                                                                 .WithDescription("Discovery")
                                                                 .Attach()
                                                                 .DefineRule("Ecommerce")
                                                                 .AllowInbound()
                                                                 .FromAnyAddress()
                                                                 .FromAnyPort()
                                                                 .ToAnyAddress()
                                                                 .ToPort(18130)
                                                                 .WithProtocol(SecurityRuleProtocol.Tcp)
                                                                 .WithPriority(152)
                                                                 .WithDescription("Ecommerce")
                                                                 .Attach()
                                                                 .DefineRule("edx-release")
                                                                 .AllowInbound()
                                                                 .FromAnyAddress()
                                                                 .FromAnyPort()
                                                                 .ToAnyAddress()
                                                                 .ToPort(8099)
                                                                 .WithProtocol(SecurityRuleProtocol.Tcp)
                                                                 .WithPriority(162)
                                                                 .WithDescription("edx-release")
                                                                 .Attach()
                                                                 .DefineRule("Forum")
                                                                 .AllowInbound()
                                                                 .FromAnyAddress()
                                                                 .FromAnyPort()
                                                                 .ToAnyAddress()
                                                                 .ToPort(18080)
                                                                 .WithProtocol(SecurityRuleProtocol.Tcp)
                                                                 .WithPriority(172)
                                                                 .WithDescription("Forum")
                                                                 .Attach()
                                                                 .WithTag("_contact_person", contactPerson)
                                                                 .DefineRule("Xqueue")
                                                                 .AllowInbound()
                                                                 .FromAnyAddress()
                                                                 .FromAnyPort()
                                                                 .ToAnyAddress()
                                                                 .ToPort(18040)
                                                                 .WithProtocol(SecurityRuleProtocol.Tcp)
                                                                 .WithPriority(182)
                                                                 .WithDescription("Xqueue")
                                                                 .Attach()
                                                                 .WithTag("_contact_person", contactPerson)
                                                                 .Create();
                    #endregion

                    log.LogInformation($"{DateAndTime()} | Created | Network Security Group");

                    #region nic
                    INetworkInterface networkInterface = _azureProd.NetworkInterfaces.Define($"{clusterName}-nic")
                                                         .WithRegion(region)
                                                         .WithExistingResourceGroup(resourceGroupName)
                                                         .WithExistingPrimaryNetwork(virtualNetwork)
                                                         .WithSubnet(subnet)
                                                         .WithPrimaryPrivateIPAddressDynamic()
                                                         .WithExistingPrimaryPublicIPAddress(publicIpAddress)
                                                         .WithExistingNetworkSecurityGroup(networkSecurityGroup)
                                                         .WithTag("_contact_person", contactPerson)
                                                         .Create();
                    #endregion

                    log.LogInformation($"{DateAndTime()} | Created | Network Interface");

                    IStorageAccount storageAccount = _azureProd.StorageAccounts.GetByResourceGroup(resourceGroupName, $"{clusterName}vhdsa");

                    #region vm
                    IVirtualMachine createVm = _azureProd.VirtualMachines.Define($"{clusterName}-jb")
                                               .WithRegion(region)
                                               .WithExistingResourceGroup(resourceGroupName)
                                               .WithExistingPrimaryNetworkInterface(networkInterface)
                                               .WithStoredLinuxImage(MainVhdURL)
                                               .WithRootUsername(username)
                                               .WithRootPassword(password)
                                               .WithComputerName(username)
                                               .WithBootDiagnostics(storageAccount)
                                               .WithSize(VirtualMachineSizeTypes.StandardD2sV3)
                                               .WithTag("_contact_person", contactPerson)
                                               .Create();
                    #endregion

                    log.LogInformation($"{DateAndTime()} | Created | Main Virtual Machine");

                    #region LMS IP
                    IPublicIPAddress publicIPAddressLMS = _azureProd.PublicIPAddresses.Define($"{clusterName}-lms-ip")
                                                          .WithRegion(region)
                                                          .WithExistingResourceGroup(resourceGroupName)
                                                          .WithDynamicIP()
                                                          .WithLeafDomainLabel($"{clusterName}-lms-ip")
                                                          .WithTag("_contact_person", contactPerson)
                                                          .Create();
                    #endregion

                    log.LogInformation($"{DateAndTime()} | Created | LMS Public IP Address");

                    #region CMS IP
                    IPublicIPAddress publicIPAddressCMS = _azureProd.PublicIPAddresses.Define($"{clusterName}-cms-ip")
                                                          .WithRegion(region)
                                                          .WithExistingResourceGroup(resourceGroupName)
                                                          .WithDynamicIP()
                                                          .WithLeafDomainLabel($"{clusterName}-cms-ip")
                                                          .WithTag("_contact_person", contactPerson)
                                                          .Create();
                    #endregion

                    log.LogInformation($"{DateAndTime()} | Created | CMS Public IP Address");

                    #region LoadBalancer
                    ILoadBalancer loadBalancer = _azureProd.LoadBalancers.Define($"{clusterName}-lb")
                                                 .WithRegion(region)
                                                 .WithExistingResourceGroup(resourceGroupName)

                                                 .DefineLoadBalancingRule("LBRuleCMS")
                                                 .WithProtocol(TransportProtocol.Tcp)
                                                 .FromFrontend("CMS")
                                                 .FromFrontendPort(80)
                                                 .ToBackend($"{clusterName}-bepool")
                                                 .ToBackendPort(18010)
                                                 .WithProbe("tcpProbeCMS")
                                                 .WithFloatingIPDisabled()
                                                 .Attach()

                                                 .DefineLoadBalancingRule("LBRuleCMS_SSL")
                                                 .WithProtocol(TransportProtocol.Tcp)
                                                 .FromFrontend("CMS")
                                                 .FromFrontendPort(443)
                                                 .ToBackend($"{clusterName}-bepool")
                                                 .ToBackendPort(48010)
                                                 .WithProbe("tcpProbeCMSSSL")
                                                 .WithFloatingIPDisabled()
                                                 .Attach()

                                                 .DefineLoadBalancingRule("LBRuleLMS")
                                                 .WithProtocol(TransportProtocol.Tcp)
                                                 .FromFrontend("LMS")
                                                 .FromFrontendPort(80)
                                                 .ToBackend($"{clusterName}-bepool")
                                                 .ToBackendPort(80)
                                                 .WithProbe("tcpProbeLMS")
                                                 .WithFloatingIPDisabled()
                                                 .Attach()

                                                 .DefineLoadBalancingRule("LBRuleLMS_SSL")
                                                 .WithProtocol(TransportProtocol.Tcp)
                                                 .FromFrontend("LMS")
                                                 .FromFrontendPort(443)
                                                 .ToBackend($"{clusterName}-bepool")
                                                 .ToBackendPort(443)
                                                 .WithProbe("tcpProbeLMSSSL")
                                                 .WithFloatingIPDisabled()
                                                 .Attach()

                                                 .DefineBackend($"{clusterName}-bepool")
                                                 .WithExistingVirtualMachines(createVm)
                                                 .Attach()

                                                 .DefinePublicFrontend("LMS")
                                                 .WithExistingPublicIPAddress(publicIPAddressLMS)
                                                 .Attach()

                                                 .DefinePublicFrontend("CMS")
                                                 .WithExistingPublicIPAddress(publicIPAddressCMS)
                                                 .Attach()

                                                 .DefineHttpProbe("tcpProbeCMS")
                                                 .WithRequestPath("/heartbeat")
                                                 .WithPort(18010)
                                                 .WithIntervalInSeconds(5)
                                                 .WithNumberOfProbes(6)
                                                 .Attach()

                                                 .DefineTcpProbe("tcpProbeCMSSSL")
                                                 .WithPort(48010)
                                                 .WithIntervalInSeconds(5)
                                                 .WithNumberOfProbes(6)
                                                 .Attach()

                                                 .DefineHttpProbe("tcpProbeLMS")
                                                 .WithRequestPath("/heartbeat")
                                                 .WithPort(80)
                                                 .WithIntervalInSeconds(5)
                                                 .WithNumberOfProbes(6)
                                                 .Attach()

                                                 .DefineTcpProbe("tcpProbeLMSSSL")
                                                 .WithPort(443)
                                                 .WithIntervalInSeconds(5)
                                                 .WithNumberOfProbes(6)
                                                 .Attach()
                                                 .WithTag("_contact_person", contactPerson)
                                                 .Create();
                    #endregion

                    log.LogInformation($"{DateAndTime()} | Created | Load Balancer");

                    #region tm
                    IWithEndpoint tmDefinitionLMS = _azureProd.TrafficManagerProfiles
                                                    .Define($"{clusterName}-lms-tm")
                                                    .WithExistingResourceGroup(resourceGroupName)
                                                    .WithLeafDomainLabel($"{clusterName}-lms-tm")
                                                    .WithPriorityBasedRouting();
                    ICreatable <ITrafficManagerProfile> tmCreatableLMS = null;

                    tmCreatableLMS = tmDefinitionLMS
                                     .DefineExternalTargetEndpoint($"{clusterName}-lms-tm")
                                     .ToFqdn(publicIPAddressLMS.Fqdn)
                                     .FromRegion(region)
                                     .WithRoutingPriority(1)
                                     .Attach()
                                     .WithTag("_contact_person", contactPerson);

                    ITrafficManagerProfile trafficManagerProfileLMS = tmCreatableLMS.Create();

                    log.LogInformation($"{DateAndTime()} | Created | LMS Traffic Manager");

                    IWithEndpoint tmDefinitionCMS = _azureProd.TrafficManagerProfiles
                                                    .Define($"{clusterName}-cms-tm")
                                                    .WithExistingResourceGroup(resourceGroupName)
                                                    .WithLeafDomainLabel($"{clusterName}-cms-tm")
                                                    .WithPriorityBasedRouting();
                    ICreatable <ITrafficManagerProfile> tmCreatableCMS = null;

                    tmCreatableCMS = tmDefinitionCMS
                                     .DefineExternalTargetEndpoint($"{clusterName}-cms-tm")
                                     .ToFqdn(publicIPAddressCMS.Fqdn)
                                     .FromRegion(region)
                                     .WithRoutingPriority(1)
                                     .Attach()
                                     .WithTag("_contact_person", contactPerson);

                    ITrafficManagerProfile trafficManagerProfileCMS = tmCreatableCMS.Create();

                    log.LogInformation($"{DateAndTime()} | Created | CMS Traffic Manager");

                    #endregion

                    #endregion

                    if (!isSingleInstance)
                    {
                        #region mysql
                        INetworkSecurityGroup networkSecurityGroupmysql = _azureProd.NetworkSecurityGroups.Define($"{clusterName}-mysql-nsg")
                                                                          .WithRegion(region)
                                                                          .WithExistingResourceGroup(resourceGroup)
                                                                          .DefineRule("ALLOW-SSH")
                                                                          .AllowInbound()
                                                                          .FromAnyAddress()
                                                                          .FromAnyPort()
                                                                          .ToAnyAddress()
                                                                          .ToPort(22)
                                                                          .WithProtocol(SecurityRuleProtocol.Tcp)
                                                                          .WithPriority(100)
                                                                          .WithDescription("Allow SSH")
                                                                          .Attach()
                                                                          .DefineRule("mysql")
                                                                          .AllowInbound()
                                                                          .FromAnyAddress()
                                                                          .FromAnyPort()
                                                                          .ToAnyAddress()
                                                                          .ToPort(3306)
                                                                          .WithProtocol(SecurityRuleProtocol.Tcp)
                                                                          .WithPriority(101)
                                                                          .WithDescription("mysql")
                                                                          .Attach()
                                                                          .WithTag("_contact_person", contactPerson)
                                                                          .Create();

                        log.LogInformation($"{DateAndTime()} | Created | MySQL Network Security Group");

                        INetworkInterface networkInterfacemysql = _azureProd.NetworkInterfaces.Define($"{clusterName}-mysql-nic")
                                                                  .WithRegion(region)
                                                                  .WithExistingResourceGroup(resourceGroup)
                                                                  .WithExistingPrimaryNetwork(virtualNetwork)
                                                                  .WithSubnet(subnet)
                                                                  .WithPrimaryPrivateIPAddressDynamic()
                                                                  .WithExistingNetworkSecurityGroup(networkSecurityGroupmysql)
                                                                  .WithTag("_contact_person", contactPerson)
                                                                  .Create();

                        log.LogInformation($"{DateAndTime()} | Created | MySQL Network Interface");

                        IVirtualMachine createVmmysql = _azureProd.VirtualMachines.Define($"{clusterName}-mysql")
                                                        .WithRegion(region)
                                                        .WithExistingResourceGroup(resourceGroup)
                                                        .WithExistingPrimaryNetworkInterface(networkInterfacemysql)
                                                        .WithStoredLinuxImage(MysqlVhdURL)
                                                        .WithRootUsername(username)
                                                        .WithRootPassword(password)
                                                        .WithComputerName("mysql")
                                                        .WithBootDiagnostics(storageAccount)
                                                        .WithSize(VirtualMachineSizeTypes.StandardD2V2)
                                                        .WithTag("_contact_person", contactPerson)
                                                        .Create();

                        log.LogInformation($"{DateAndTime()} | Created | MySQL Virtual Machine");

                        #endregion

                        #region mongodb
                        INetworkSecurityGroup networkSecurityGroupmongo = _azureProd.NetworkSecurityGroups.Define($"{clusterName}-mongo-nsg")
                                                                          .WithRegion(region)
                                                                          .WithExistingResourceGroup(resourceGroup)
                                                                          .DefineRule("ALLOW-SSH")
                                                                          .AllowInbound()
                                                                          .FromAnyAddress()
                                                                          .FromAnyPort()
                                                                          .ToAnyAddress()
                                                                          .ToPort(22)
                                                                          .WithProtocol(SecurityRuleProtocol.Tcp)
                                                                          .WithPriority(100)
                                                                          .WithDescription("Allow SSH")
                                                                          .Attach()
                                                                          .DefineRule("mongodb")
                                                                          .AllowInbound()
                                                                          .FromAnyAddress()
                                                                          .FromAnyPort()
                                                                          .ToAnyAddress()
                                                                          .ToPort(27017)
                                                                          .WithProtocol(SecurityRuleProtocol.Tcp)
                                                                          .WithPriority(101)
                                                                          .WithDescription("mongodb")
                                                                          .Attach()
                                                                          .WithTag("_contact_person", contactPerson)
                                                                          .Create();

                        log.LogInformation($"{DateAndTime()} | Created | MongoDB Network Security Group");

                        INetworkInterface networkInterfacemongo = _azureProd.NetworkInterfaces.Define($"{clusterName}-mongo-nic")
                                                                  .WithRegion(region)
                                                                  .WithExistingResourceGroup(resourceGroup)
                                                                  .WithExistingPrimaryNetwork(virtualNetwork)
                                                                  .WithSubnet(subnet)
                                                                  .WithPrimaryPrivateIPAddressDynamic()
                                                                  .WithExistingNetworkSecurityGroup(networkSecurityGroupmongo)
                                                                  .WithTag("_contact_person", contactPerson)
                                                                  .Create();

                        log.LogInformation($"{DateAndTime()} | Created | MongoDB Network Interface");

                        IVirtualMachine createVmmongo = _azureProd.VirtualMachines.Define($"{clusterName}-mongo")
                                                        .WithRegion(region)
                                                        .WithExistingResourceGroup(resourceGroup)
                                                        .WithExistingPrimaryNetworkInterface(networkInterfacemongo)
                                                        .WithStoredLinuxImage(MongoVhdURL)
                                                        .WithRootUsername(username)
                                                        .WithRootPassword(password)
                                                        .WithComputerName("mongo")
                                                        .WithBootDiagnostics(storageAccount)
                                                        .WithSize(VirtualMachineSizeTypes.StandardD2V2)
                                                        .WithTag("_contact_person", contactPerson)
                                                        .Create();

                        log.LogInformation($"{DateAndTime()} | Created | MongoDB Virtual Machine");

                        #endregion

                        log.LogInformation("deploying 3 instance");

                        Utils.Email(smtpClient, "MySQL Instance Deployed Successfully", log, mailMessage);
                    }


                    string cmsUrl = trafficManagerProfileCMS.DnsLabel;
                    string lmsUrl = trafficManagerProfileLMS.DnsLabel;

                    Utils.Email(smtpClient, "Your Learning Platform is Ready to use." +
                                "<br/>"
                                + $"<a href=\"{lmsUrl}\">LMS</a>" +
                                "<br/>" +
                                $"<a href=\"{cmsUrl}\">CMS</a>"
                                , log, mailMessage);
                    log.LogInformation($"Done");
                }
                catch (Exception e)
                {
                    log.LogInformation($"{DateAndTime()} | Error | {e.Message}");

                    return(new BadRequestObjectResult(false));
                }

                log.LogInformation($"{DateAndTime()} | Done");
                return(new OkObjectResult(true));
            }
        }
 protected override Task <IReadOnlyList <StorageAccountKey> > RetrieveCurrentKeyring(IStorageAccount resource, string keyType) => resource.GetKeysAsync();
예제 #42
0
        public void CanSetMSIOnNewVMWithMultipleRoleAssignments()
        {
            using (var context = FluentMockContext.Start(GetType().FullName))
            {
                var    groupName          = TestUtilities.GenerateName("rgmsi");
                var    storageAccountName = TestUtilities.GenerateName("ja");
                var    region             = Region.USSouthCentral;
                var    vmName             = "javavm";
                IAzure azure = null;
                try
                {
                    azure = TestHelper.CreateRollupClient();


                    IStorageAccount storageAccount = azure.StorageAccounts
                                                     .Define(storageAccountName)
                                                     .WithRegion(region)
                                                     .WithNewResourceGroup(groupName)
                                                     .Create();

                    var resourceGroup = azure.ResourceGroups.GetByName(storageAccount.ResourceGroupName);

                    IVirtualMachine virtualMachine = azure.VirtualMachines
                                                     .Define(vmName)
                                                     .WithRegion(region)
                                                     .WithExistingResourceGroup(groupName)
                                                     .WithNewPrimaryNetwork("10.0.0.0/28")
                                                     .WithPrimaryPrivateIPAddressDynamic()
                                                     .WithoutPrimaryPublicIPAddress()
                                                     .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
                                                     .WithRootUsername("Foo12")
                                                     .WithRootPassword("abc!@#F0orL")
                                                     .WithSize(VirtualMachineSizeTypes.StandardDS2V2)
                                                     .WithOSDiskCaching(CachingTypes.ReadWrite)
                                                     .WithSystemAssignedManagedServiceIdentity()
                                                     .WithSystemAssignedIdentityBasedAccessTo(resourceGroup.Id, BuiltInRole.Contributor)
                                                     .WithSystemAssignedIdentityBasedAccessTo(storageAccount.Id, BuiltInRole.Contributor)
                                                     .Create();

                    var authenticatedClient = TestHelper.CreateAuthenticatedClient();
                    // Validate service created service principal
                    //
                    IServicePrincipal servicePrincipal = authenticatedClient
                                                         .ServicePrincipals
                                                         .GetById(virtualMachine.SystemAssignedManagedServiceIdentityPrincipalId);

                    Assert.NotNull(servicePrincipal);
                    Assert.NotNull(servicePrincipal.Inner);

                    // Ensure the MSI extension is set
                    //
                    var  extensions     = virtualMachine.ListExtensions();
                    bool extensionFound = false;
                    foreach (var extension in extensions.Values)
                    {
                        if (extension.PublisherName.Equals("Microsoft.ManagedIdentity", StringComparison.OrdinalIgnoreCase) &&
                            extension.TypeName.Equals("ManagedIdentityExtensionForLinux", StringComparison.OrdinalIgnoreCase))
                        {
                            extensionFound = true;
                            break;
                        }
                    }
                    Assert.True(extensionFound);


                    // Ensure role assigned for resource group
                    //

                    var rgRoleAssignments = authenticatedClient.RoleAssignments.ListByScope(resourceGroup.Id);
                    Assert.NotNull(rgRoleAssignments);
                    bool found = false;
                    foreach (var roleAssignment in rgRoleAssignments)
                    {
                        if (roleAssignment.PrincipalId != null && roleAssignment.PrincipalId.Equals(virtualMachine.SystemAssignedManagedServiceIdentityPrincipalId, StringComparison.OrdinalIgnoreCase))
                        {
                            found = true;
                            break;
                        }
                    }
                    Assert.True(found, "Resource group should have a role assignment with virtual machine MSI principal");

                    // Ensure role assigned for storage account
                    //
                    var stgRoleAssignments = authenticatedClient.RoleAssignments.ListByScope(storageAccount.Id);
                    Assert.NotNull(stgRoleAssignments);
                    found = false;
                    foreach (var roleAssignment in stgRoleAssignments)
                    {
                        if (roleAssignment.PrincipalId != null && roleAssignment.PrincipalId.Equals(virtualMachine.SystemAssignedManagedServiceIdentityPrincipalId, StringComparison.OrdinalIgnoreCase))
                        {
                            found = true;
                            break;
                        }
                    }
                    Assert.True(found, "Storage account should have a role assignment with virtual machine MSI principal");
                }
                finally
                {
                    try
                    {
                        if (azure != null)
                        {
                            azure.ResourceGroups.BeginDeleteByName(groupName);
                        }
                    }
                    catch { }
                }
            }
        }
 private static IStorageAccountParser CreateParser(IServiceProvider services, string connectionStringName, string connectionString, IStorageAccount parsedAccount)
 {
     Mock<IStorageAccountParser> mock = new Mock<IStorageAccountParser>(MockBehavior.Strict);
     mock.Setup(p => p.ParseAccount(connectionString, connectionStringName, services)).Returns(parsedAccount);
     return mock.Object;
 }
 private static TResult RunTrigger <TResult>(IStorageAccount account, Type programType,
                                             Action <TaskCompletionSource <TResult> > setTaskSource, IJobActivator activator)
 {
     return(FunctionalTest.RunTrigger <TResult>(account, programType, activator, setTaskSource));
 }
예제 #45
0
 /// <summary>
 /// Specifies the storage account.
 /// </summary>
 /// <param name="account">The storage account.</param>
 /// <returns>The next stage of the definition.</returns>
 Microsoft.Azure.Management.Compute.Fluent.Disk.Definition.IWithCreateAndSize IWithStorageAccount.WithStorageAccount(IStorageAccount account)
 {
     return(this.WithStorageAccount(account));
 }
예제 #46
0
        public ResourceInfo Create(string storageName, string region, string resourceGroup, IDictionary <string, string> tags = null)
        {
            ResourceInfo resourceInfo = null;

            try
            {
                Region regionType = Region.Create(region);
                if (regionType == null)
                {
                    regionType = Region.USCentral;
                }

                CheckNameAvailabilityResult result = null;
                Task.Run(async() =>
                {
                    result = await azure.StorageAccounts.CheckNameAvailabilityAsync(storageName);
                }).Wait();

                if (result != null && result.IsAvailalbe != null)
                {
                    bool available = (bool)result.IsAvailalbe;
                    if (!available)
                    {
                        return(resourceInfo);
                    }
                }

                Task.Run(async() =>
                {
                    IStorageAccount storageAcct = null;
                    if (tags != null)
                    {
                        storageAcct = await azure.StorageAccounts
                                      .Define(storageName)
                                      .WithRegion(region)
                                      .WithExistingResourceGroup(resourceGroup)
                                      .WithBlobStorageAccountKind()
                                      .WithTags(tags)
                                      .CreateAsync();
                    }
                    else
                    {
                        storageAcct = await azure.StorageAccounts
                                      .Define(storageName)
                                      .WithRegion(region)
                                      .WithExistingResourceGroup(resourceGroup)
                                      .WithBlobStorageAccountKind()
                                      .CreateAsync();
                    }

                    resourceInfo = GetInfo(storageAcct);
                }).Wait();
            }
            catch (Exception ex)
            {
                if (feedback != null)
                {
                    feedback.OnTaskException(ex);
                }

                return(resourceInfo);
            }

            return(resourceInfo);
        }
 /// <summary>Initializes a new instance of the <see cref="HeartbeatCommand"/> class.</summary>
 /// <param name="account">The storage account in which to write the heartbeat.</param>
 /// <param name="containerName">The name of the container in which to write the heartbeat.</param>
 /// <param name="blobName">The name of the heartbeat blob (including the directory name, if any).</param>
 public HeartbeatCommand(IStorageAccount account, string containerName, string blobName)
     : this(account.CreateBlobClient().GetContainerReference(containerName).GetBlockBlobReference(blobName))
 {
 }