async Task <BastionHost> GetResourceInternalAsync(string resourceGroupName, string bastionHostName, bool failIfNotFound = true)
        {
            try
            {
                using (var client = new Microsoft.Azure.Management.Network.NetworkManagementClient(_credentials))
                {
                    client.SubscriptionId = _subscriptionId;
                    var bastion = await client.BastionHosts.GetAsync(resourceGroupName, bastionHostName);

                    return(bastion);
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.ToLower().Contains("could not be found") || ex.Message.ToLower().Contains("was not found"))
                {
                    if (failIfNotFound)
                    {
                        throw NotFoundException.CreateForAzureResource(bastionHostName, resourceGroupName, ex);
                    }
                    else
                    {
                        return(null);
                    }
                }

                throw;
            }
        }
Beispiel #2
0
        public async Task RemoveStorageAccountFromVNet(string resourceGroupForStorageAccount, string storageAccountName, string resourceGroupForVnet, string vNetName, CancellationToken cancellation)
        {
            try
            {
                var storageAccount = await GetResourceAsync(resourceGroupForStorageAccount, storageAccountName, cancellationToken : cancellation);

                var network = await _azure.Networks.GetByResourceGroupAsync(resourceGroupForVnet, vNetName, cancellation);

                if (network == null)
                {
                    throw NotFoundException.CreateForAzureResource(vNetName, resourceGroupForVnet);
                }

                var sandboxSubnet = AzureVNetUtil.GetSandboxSubnetOrThrow(network);

                var networkRuleSet = GetNetworkRuleSetForUpdate(storageAccount, true);

                if (GetRuleForSubnet(networkRuleSet, sandboxSubnet.Inner.Id, Microsoft.Azure.Management.Storage.Fluent.Models.Action.Allow, out VirtualNetworkRule existingRule))
                {
                    networkRuleSet = RemoveVNetFromRuleSet(networkRuleSet, sandboxSubnet.Inner.Id);

                    var updateParameters = new StorageAccountUpdateParameters()
                    {
                        NetworkRuleSet = networkRuleSet
                    };

                    await _azure.StorageAccounts.Inner.UpdateAsync(resourceGroupForStorageAccount, storageAccountName, updateParameters, cancellation);
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"Could not add Storage Account {storageAccountName} to VNet {vNetName}", ex);
            }
        }
        public async Task <IStorageAccount> GetResourceAsync(string resourceGroupName, string resourceName, bool failIfNotFound = true, CancellationToken cancellationToken = default)
        {
            var resource = await _azure.StorageAccounts.GetByResourceGroupAsync(resourceGroupName, resourceName, cancellationToken);

            if (resource == null && failIfNotFound)
            {
                throw NotFoundException.CreateForAzureResource(resourceName, resourceGroupName);
            }

            return(resource);
        }
        protected async Task <IVirtualMachine> GetInternalAsync(string resourceGroupName, string resourceName, bool failIfNotFound = true)
        {
            var resource = await _azure.VirtualMachines.GetByResourceGroupAsync(resourceGroupName, resourceName);

            if (resource == null)
            {
                if (failIfNotFound)
                {
                    throw NotFoundException.CreateForAzureResource(resourceName, resourceGroupName);
                }
                else
                {
                    return(null);
                }
            }

            return(resource);
        }
        public async Task <INetworkSecurityGroup> GetResourceInternalAsync(string resourceGroupName, string resourceName, bool failIfNotFound = true)
        {
            var resource = await _azure.NetworkSecurityGroups.GetByResourceGroupAsync(resourceGroupName, resourceName);

            if (resource == null)
            {
                if (failIfNotFound)
                {
                    throw NotFoundException.CreateForAzureResource(resourceName, resourceGroupName);
                }
                else
                {
                    return(null);
                }
            }

            return(resource);
        }