コード例 #1
0
ファイル: Program.cs プロジェクト: yanborowski/Industrial-IoT
        /// <summary>
        /// Create and delete resource grouop
        /// </summary>
        /// <param name="context"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        private static async Task TestVmCreateDeleteCreate(IComponentContext context,
                                                           CliOptions options)
        {
            var manager = context.Resolve <IResourceGroupFactory>();
            var name    = options.GetValueOrDefault("-n", "--name", StringEx.CreateUnique(9, "test"));

            Console.WriteLine("Creating resource group....");
            using (var resourceGroup = await manager.CreateAsync(true)) {
                Console.WriteLine("Resource group created.");
                var vms = context.Resolve <IVirtualMachineFactory>();
                Console.WriteLine("Creating virtual machine...");
                var vm = await vms.CreateAsync(resourceGroup, name);

                Console.WriteLine("Virtual machine created.");

                Console.WriteLine("Deleting virtual machine...");
                await vm.DeleteAsync();

                Console.WriteLine("Virtual machine deleted.");

                Console.WriteLine("Recreating virtual machine...");
                vm = await vms.CreateAsync(resourceGroup, name);

                Console.WriteLine("Virtual machine created.");
            }
            Console.WriteLine("Resource group deleted.");
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: yanborowski/Industrial-IoT
        /// <summary>
        /// Create and delete resource grouop
        /// </summary>
        /// <param name="context"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        private static async Task TestIoTHubCreateDeleteCreate(IComponentContext context,
                                                               CliOptions options)
        {
            var manager = context.Resolve <IResourceGroupFactory>();
            var name    = options.GetValueOrDefault("-n", "--name", StringEx.CreateUnique(9, "test"));

            Console.WriteLine("Creating resource group....");
            using (var resourceGroup = await manager.CreateAsync(true)) {
                Console.WriteLine("Resource group created.");

                var hubs = context.Resolve <IIoTHubFactory>();
                Console.WriteLine("Creating iothub...");
                var hub = await hubs.CreateAsync(resourceGroup, name);

                Console.WriteLine("iothub created.");

                Console.WriteLine("Deleting iothub...");
                await hub.DeleteAsync();

                Console.WriteLine("iothub deleted.");

                Console.WriteLine("Recreating iothub...");
                hub = await hubs.CreateAsync(resourceGroup, name);

                Console.WriteLine("iothub created.");
            }
            Console.WriteLine("Resource group deleted.");
        }
コード例 #3
0
        /// <summary>
        /// Select new name and avoid collision in the resource
        /// group.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <param name="resourceGroup"></param>
        /// <param name="prefix"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static async Task <string> SelectResourceNameAsync <T>(
            this ISupportsGettingByResourceGroup <T> collection,
            string resourceGroup, string prefix, string name = null) where T : class
        {
            if (string.IsNullOrEmpty(prefix))
            {
                throw new ArgumentNullException(nameof(prefix));
            }
            // Check name - null means we need to create one
            if (string.IsNullOrEmpty(name))
            {
                while (true)
                {
                    name = StringEx.CreateUnique(10, prefix);
                    var exists = await collection.ContainsAsync(resourceGroup, name);

                    if (!exists)
                    {
                        break;
                    }
                }
            }
            else
            {
                var exists = await collection.ContainsAsync(resourceGroup, name);

                if (exists)
                {
                    throw new ArgumentException(
                              $"A {prefix} resource exists with name {name}", nameof(name));
                }
            }
            return(name);
        }
コード例 #4
0
        /// <inheritdoc/>
        public async Task <IResourceGroupResource> CreateAsync(string resourceGroup,
                                                               bool deleteOnDispose, ISubscriptionInfo subscription)
        {
            subscription = CompositeInfo.Create(subscription, _subscription);

            // Create resource group
            var client = await CreateClientAsync(subscription);

            if (string.IsNullOrEmpty(resourceGroup))
            {
                // Create group name
                while (true)
                {
                    resourceGroup = StringEx.CreateUnique(8, "rg");
                    var exists = await client.ResourceGroups.ContainAsync(
                        resourceGroup);

                    if (!exists)
                    {
                        break;
                    }
                }
            }
            else
            {
                var exists = await client.ResourceGroups.ContainAsync(
                    resourceGroup);

                if (exists)
                {
                    throw new ExternalDependencyException("resource group already exists");
                }
            }

            var region = await subscription.GetRegionAsync();

            _logger.Information("Creating simulation group {resourceGroup} in {region}...",
                                resourceGroup, region);
            var rg = await client.ResourceGroups.Define(resourceGroup)
                     .WithRegion(region)
                     .CreateAsync();

            _logger.Information("Created resource group {resourceGroup}.", rg.Name);
            return(new ResourceGroupResource(this, rg, deleteOnDispose, subscription, _logger));
        }
コード例 #5
0
        /// <inheritdoc/>
        public async Task <IIoTHubResource> CreateAsync(
            IResourceGroupResource resourceGroup, string hubName)
        {
            if (resourceGroup == null)
            {
                throw new ArgumentNullException(nameof(resourceGroup));
            }

            var client = await CreateIoTHubClientAsync(resourceGroup);

            // Check quota
            var quota = await client.ResourceProviderCommon
                        .GetSubscriptionQuotaAsync();

            var limit = quota.Value
                        .FirstOrDefault(x => x.Name.Value.Equals("paidIotHubCount"));

            if (limit?.Limit == limit?.CurrentValue)
            {
                throw new ExternalDependencyException(
                          $"Subscription limit reached at {limit?.Limit ?? -1}");
            }

            // Check name - null means we need to create one
            if (string.IsNullOrEmpty(hubName))
            {
                while (true)
                {
                    hubName = StringEx.CreateUnique(10, "iothub");
                    var result = client.IotHubResource.CheckNameAvailability(
                        new OperationInputs {
                        Name = hubName
                    });
                    if (result.NameAvailable ?? false)
                    {
                        break;
                    }
                }
            }
            else
            {
                var result = client.IotHubResource.CheckNameAvailability(
                    new OperationInputs {
                    Name = hubName
                });
                if (!(result.NameAvailable ?? false))
                {
                    throw new ArgumentException("hub exists with this name",
                                                nameof(hubName));
                }
            }

            // Create hub
            var hub = await client.IotHubResource.CreateOrUpdateAsync(
                resourceGroup.Name, hubName, new IotHubDescription {
                Location = await resourceGroup.Subscription.GetRegionAsync(),
                Sku      = new IotHubSkuInfo {
                    Name     = IotHubSku.S1,
                    Capacity = 1
                },
                Properties = new IotHubProperties {
                    AuthorizationPolicies =
                        new List <SharedAccessSignatureAuthorizationRule> {
                        new SharedAccessSignatureAuthorizationRule(
                            kIoTHubOwner,
                            AccessRights.RegistryReadRegistryWriteServiceConnectDeviceConnect)
                    }
                }
            });

            _logger.Info($"Created iot hub {hubName} in resource " +
                         $"group {resourceGroup.Name}...");

            var keys = await client.IotHubResource.GetKeysForKeyNameAsync(
                resourceGroup.Name, hubName, kIoTHubOwner);

            return(new IoTHubResource(this, resourceGroup, hubName,
                                      hub.Properties, _logger, keys));
        }
コード例 #6
0
 /// <summary>
 /// Run task that requires a string
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="provider"></param>
 /// <param name="id"></param>
 /// <param name="task"></param>
 /// <returns></returns>
 public static Task <T> RunTaskAsync <T>(this IPersistenceProvider provider,
                                         string id, Func <string, Task <T> > task) =>
 provider.RunTaskAsync(id, task, StringEx.CreateUnique(10, id));