Example #1
0
        /// <summary>
        /// Get hybrid connection details async.
        /// </summary>
        /// <param name="subscription">Selected subscription.</param>
        /// <param name="relayNamespace">Selected relay namespace.</param>
        /// <param name="hybridConnectionName">Hybrid connection name.</param>
        /// <returns>Hybrid connection details.</returns>
        public async Task <HybridConnectionDetails> GetHybridConnectionAsync(
            SubscriptionInner subscription,
            RelayNamespaceInner relayNamespace,
            string hybridConnectionName)
        {
            string accessToken = this.userAuthenticator.GetSubscriptionSpecificUserToken(subscription);

            TokenCredentials      tokenCredentials      = new TokenCredentials(accessToken);
            RelayManagementClient relayManagementClient = new RelayManagementClient(tokenCredentials)
            {
                SubscriptionId = subscription.SubscriptionId,
            };

            int    startIndex = relayNamespace.Id.IndexOf("resourceGroups", StringComparison.OrdinalIgnoreCase) + 15;
            string rgName     = rgName = relayNamespace.Id.Substring(startIndex, relayNamespace.Id.IndexOf('/', startIndex) - startIndex);

            List <HybridConnectionInner> hybridConnections = new List <HybridConnectionInner>();

            hybridConnections.AddRange(await relayManagementClient.HybridConnections.ListByNamespaceAsync(rgName, relayNamespace.Name).ConfigureAwait(false));

            // Create the hybrid connection if one does not exist.
            if (!hybridConnections.Any(connection => connection.Name.Equals(hybridConnectionName, StringComparison.OrdinalIgnoreCase)))
            {
                await relayManagementClient.HybridConnections.CreateOrUpdateAsync(rgName, relayNamespace.Name, hybridConnectionName, new HybridConnectionInner
                {
                    RequiresClientAuthorization = false,
                }).ConfigureAwait(false);
            }

            return(await AzureRelayResourceManager.GetHybridConnectionDetailsAsync(rgName, hybridConnectionName, relayManagementClient, relayNamespace).ConfigureAwait(false));
        }
Example #2
0
        /// <summary>
        /// Creates a new hybrid connection and returns it's details.
        /// </summary>
        /// <param name="subscription">Subscription to create the relay under.</param>
        /// <param name="relayName">Relay name.</param>
        /// <param name="hybridConnectionName">Hybrid connection name.</param>
        /// <param name="locationName">Location for the new relay.</param>
        /// <returns>Hybrid connection details.</returns>
        public async Task <HybridConnectionDetails> CreateHybridConnectionAsync(
            SubscriptionInner subscription,
            string relayName,
            string hybridConnectionName,
            string locationName)
        {
            string accessToken = this.userAuthenticator.GetSubscriptionSpecificUserToken(subscription);

            TokenCredentials tokenCredentials = new TokenCredentials(accessToken);

            ResourceManagementClient resourceManagementClient = new ResourceManagementClient(tokenCredentials)
            {
                SubscriptionId = subscription.SubscriptionId,
            };

            RelayManagementClient relayManagementClient = new RelayManagementClient(tokenCredentials)
            {
                SubscriptionId = subscription.SubscriptionId,
            };

            ResourceGroupInner  resourceGroup  = null;
            RelayNamespaceInner relayNamespace = null;

            Location location = this.userAuthenticator.GetSubscriptionLocations(subscription).First(region =>
                                                                                                    region.DisplayName.Equals(locationName, StringComparison.OrdinalIgnoreCase) ||
                                                                                                    region.Name.Equals(locationName, StringComparison.OrdinalIgnoreCase));

            try
            {
                resourceGroup = await resourceManagementClient.ResourceGroups.GetAsync("TunnelRelay").ConfigureAwait(false);
            }
            catch (CloudException httpEx)
            {
                if (httpEx.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    resourceGroup = await resourceManagementClient.ResourceGroups.CreateOrUpdateAsync("TunnelRelay", new ResourceGroupInner
                    {
                        Location = location.Name,
                        Name     = "TunnelRelay",
                        Tags     = new Dictionary <string, string>()
                        {
                            { "CreatedBy", "TunnelRelayv2" }
                        },
                    }).ConfigureAwait(false);
                }
            }

            string rgName = resourceGroup.Name;

            relayNamespace = await relayManagementClient.Namespaces.CreateOrUpdateAsync(rgName, relayName, new RelayNamespaceInner
            {
                Location = resourceGroup.Location,
                Sku      = new Microsoft.Azure.Management.Relay.Fluent.Models.Sku(SkuTier.Standard),
                Tags     = new Dictionary <string, string>()
                {
                    { "CreatedBy", "TunnelRelayv2" }
                },
            }).ConfigureAwait(false);

            await relayManagementClient.HybridConnections.CreateOrUpdateAsync(rgName, relayName, hybridConnectionName, new HybridConnectionInner
            {
                RequiresClientAuthorization = false,
            }).ConfigureAwait(false);

            return(await AzureRelayResourceManager.GetHybridConnectionDetailsAsync(rgName, hybridConnectionName, relayManagementClient, relayNamespace).ConfigureAwait(false));
        }