Ejemplo n.º 1
0
        /// <summary>
        /// Gets the production client.
        /// </summary>
        /// <param name="accountDetails">The account details.</param>
        /// <param name="mediaType">The media format.</param>
        /// <returns>The client.</returns>
        private static IHttpClient GetProductionClient(CaasAccountDetails accountDetails, string mediaType)
        {
            var handler = new HttpClientHandler { Credentials = accountDetails.Credentials };
            var client = new HttpClient(handler);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType));

            return new HttpClientAdapter(client);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets a new client.
        /// </summary>
        /// <param name="accountDetails">The account details.</param>
        /// <param name="mediaType">The media format.</param>
        /// <returns>The client.</returns>
        public static IHttpClient GetClient(CaasAccountDetails accountDetails, string mediaType)
        {
            if (FakeClient != null)
            {
                return FakeClient;
            }

            return GetProductionClient(accountDetails, mediaType);
        }
        /// <summary>
        /// Lists the available public IP addresses.
        /// </summary>
        /// <param name="accountDetails">The account details.</param>
        /// <param name="networkDomainId">The network domain identifier.</param>
        /// <returns></returns>
        private async Task<List<string>> ListAvailablePublicIps(CaasAccountDetails accountDetails, string networkDomainId)
        {
            var result = new List<string>();

            using (var client = HttpClientFactory.GetClient(accountDetails, "application/json"))
            {
                // Get the reserved public IPs.
                var url = string.Format(ListReservedPublicIpv4AddressesUrl, accountDetails.BaseUrl, accountDetails.OrgId, networkDomainId);
                var response = await client.GetAsync(url);
                response.ThrowForHttpFailure();

                var responseBody = await response.Content.ReadAsStringAsync();
                var document = JObject.Parse(responseBody);
                var reservedPublicIps = document["ip"].Value<JArray>()
                    .Cast<JObject>()
                    .Select(e => e["value"].Value<string>())
                    .ToList();

                // Get the public IP blocks.
                url = string.Format(ListPublicIpBlocksUrl, accountDetails.BaseUrl, accountDetails.OrgId, networkDomainId);
                response = await client.GetAsync(url);
                response.ThrowForHttpFailure();

                responseBody = await response.Content.ReadAsStringAsync();
                document = JObject.Parse(responseBody);
                var ipBlocks = document["publicIpBlock"].Value<JArray>()
                    .Cast<JObject>()
                    .Select(e => new { BaseIp = e["baseIp"].Value<string>(), Size = e["size"].Value<int>() })
                    .ToList();

                // Get the available public IPs.
                foreach (var ipBlock in ipBlocks)
                {
                    for (int offset = 0; offset < ipBlock.Size; offset++)
                    {
                        var ipAddress = IncrementIpAddress(ipBlock.BaseIp, offset);
                        if (!reservedPublicIps.Contains(ipAddress))
                        {
                            result.Add(ipAddress);
                        }
                    }
                }
            }

            return result;
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ResourceDeployer"/> class.
 /// </summary>
 /// <param name="runtimeContext">The runtime context.</param>
 /// <param name="resourceId">The resource identifier.</param>
 /// <param name="resourceType">Type of the resource.</param>
 public ResourceDeployer(RuntimeContext runtimeContext, string resourceId, ResourceType resourceType)
 {
     _resourceId = resourceId;
     _resourceType = resourceType;
     _resourceApi = ResourceApis[resourceType];
     _accountDetails = runtimeContext.AccountDetails;
     _logProvider = runtimeContext.LogProvider;
 }
 /// <summary>
 /// Adds a new public IP address block.
 /// </summary>
 /// <param name="accountDetails">The account details.</param>
 /// <param name="networkDomainId">The network domain identifier.</param>
 /// <returns></returns>
 private async Task AddPublicIpBlock(CaasAccountDetails accountDetails, string networkDomainId)
 {
     using (var client = HttpClientFactory.GetClient(accountDetails, "application/json"))
     {
         var url = string.Format(AddPublicIpBlockUrl, accountDetails.BaseUrl, accountDetails.OrgId);
         var request = new JObject(new JProperty("networkDomainId", networkDomainId));
         var response = await client.PostAsync(url, new StringContent(request.ToString(), Encoding.UTF8, "application/json"));
         response.ThrowForHttpFailure();
     }
 }