Ejemplo n.º 1
0
        /// <summary>	Add host name to the known endpoints. </summary>
        /// <remarks>	Anthony, 4/24/2015. </remarks>
        /// <param name="vendor">	The Vendor. </param>
        /// <param name="region">	The Region. </param>
        /// <param name="apiUrl">	The API URL. </param>
        private void AddHostName(KnownApiVendor vendor, KnownApiRegion region, string apiUrl)
        {
            string key = string.Concat(vendor.ToString(), '-', region.ToString());

            KnownApiHostNames.Add(key, apiUrl);
            KnownVendorEndPointPairs.Add(new KeyValuePair <KnownApiVendor, KnownApiRegion>(vendor, region));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The get ftp host.
        /// </summary>
        /// <param name="vendor">
        /// The vendor.
        /// </param>
        /// <param name="region">
        /// The region.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        /// <exception cref="ComputeApiException">
        /// </exception>
        public string GetFtpHost(KnownApiVendor vendor, KnownApiRegion region)
        {
            string key = string.Concat(vendor.ToString(), '-', region.ToString());

            if (!KnownApiHostNames.ContainsKey(key))
            {
                throw new ApiHostNotFoundException(vendor, region);
            }
            return(KnownFtpHostNames[key]);
        }
		/// <summary>
		/// Initialises a new instance of the <see cref="ComputeApiClient"/> class. 
		/// Creates a new CaaS API client using a known vendor and region.
		/// </summary>
		/// <param name="vendor">
		/// The vendor
		/// </param>
		/// <param name="region">
		/// The region
		/// </param>
		public ComputeApiClient(KnownApiVendor vendor, KnownApiRegion region)
		{
			Uri baseUri = KnownApiUri.Instance.GetBaseUri(vendor, region);
			_ftpHost = KnownApiUri.Instance.GetFtpHost(vendor, region);
			
			if (!baseUri.IsAbsoluteUri)
				throw new ArgumentException("Base URI supplied is not an absolute URI", "vendor");

			WebApi = new WebApi(baseUri);
		}
Ejemplo n.º 4
0
        /// <summary>
        /// The get Monitoring url.
        /// </summary>
        /// <param name="vendor">
        /// The vendor.
        /// </param>
        /// <param name="region">
        /// The region.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        /// <exception cref="ComputeApiException">
        /// </exception>
        public Uri GetMonitoringUrl(KnownApiVendor vendor, KnownApiRegion region)
        {
            const string urltemplate = "https://{0}/";
            string       key         = region.ToString();

            if (!KnownMonitoringHostNames.ContainsKey(key))
            {
                throw new ApiHostNotFoundException(vendor, region);
            }
            string apiurl = string.Format(urltemplate, KnownMonitoringHostNames[key]);

            return(new Uri(apiurl));
        }
 /// <summary>	Add host name to the known endpoints. </summary>
 /// <remarks>	Anthony, 4/24/2015. </remarks>
 /// <param name="vendor">	The Vendor. </param>
 /// <param name="region">	The Region. </param>
 /// <param name="apiUrl">	The API URL. </param>
 private void AddHostName(KnownApiVendor vendor, KnownApiRegion region, string apiUrl)
 {
     string key = string.Concat(vendor.ToString(), '-', region.ToString());
     KnownApiHostNames.Add(key, apiUrl);
     KnownVendorEndPointPairs.Add(new KeyValuePair<KnownApiVendor, KnownApiRegion>(vendor, region));
 }
		/// <summary>	The add ftp host name. </summary>
		/// <remarks>	Anthony, 4/24/2015. </remarks>
		/// <param name="vendor">	The vendor. </param>
		/// <param name="region">	The region. </param>
		/// <param name="apiUrl">	The API url. </param>
		private void AddFtpHostName(KnownApiVendor vendor, KnownApiRegion region, string apiUrl)
		{
			string key = string.Concat(vendor.ToString(), '-', region.ToString());
			KnownFtpHostNames.Add(key, apiUrl);
		}
		/// <summary>
		/// The get ftp host.
		/// </summary>
		/// <param name="vendor">
		/// The vendor.
		/// </param>
		/// <param name="region">
		/// The region.
		/// </param>
		/// <returns>
		/// The <see cref="string"/>.
		/// </returns>
		/// <exception cref="ComputeApiException">
		/// </exception>
	    public string GetFtpHost(KnownApiVendor vendor, KnownApiRegion region)
	    {
			string key = string.Concat(vendor.ToString(), '-', region.ToString());
			if (!KnownApiHostNames.ContainsKey(key))
				throw new ApiHostNotFoundException(vendor, region);
			return KnownFtpHostNames[key];
	    }
 /// <summary>	List of Known Regions that are valid for the particular Vendor. </summary>
 /// <remarks>	Anthony, 4/24/2015. </remarks>
 /// <param name="vendor">	The Vendor. </param>
 /// <returns>	The list of known regions. </returns>
 public IEnumerable<KnownApiRegion> GetKnownRegionList(KnownApiVendor vendor)
 {
     return KnownVendorEndPointPairs.Where(pair => pair.Key == vendor).Select(pair => pair.Value);
 } 
 /// <summary>
 /// Initialises a new instance of the <see cref="ApiHostNotFoundException"/> class.
 /// </summary>
 /// <param name="vendor">
 /// The vendor.
 /// </param>
 /// <param name="region">
 /// The region.
 /// </param>
 public ApiHostNotFoundException(KnownApiVendor vendor, KnownApiRegion region)
     : base(ComputeApiError.BadRequest, message)
 {
     Vendor = vendor;
     Region = region;
 }
        /// <summary>
        /// Return an known CaaS URI based on vendor and region
        /// </summary>
		/// <param name="vendor">
		/// The vendor
		/// </param>
		/// <param name="region">
		/// The region
		/// </param>
		/// <returns>
		/// The <see cref="Uri"/>.
		/// </returns>
        public Uri GetBaseUri(KnownApiVendor vendor, KnownApiRegion region)
        {
            const string urltemplate = "https://{0}/";
            string key = string.Concat(vendor.ToString(), '-', region.ToString());
	        if (!KnownApiHostNames.ContainsKey(key))
		        throw new ApiHostNotFoundException(vendor, region);


            string apiurl = string.Format(urltemplate, KnownApiHostNames[key]);
            return new Uri(apiurl);
        }
		/// <summary>
		/// The get ftp host.
		/// </summary>
		/// <param name="vendor">
		/// The vendor.
		/// </param>
		/// <param name="region">
		/// The region.
		/// </param>
		/// <returns>
		/// The <see cref="string"/>.
		/// </returns>
		/// <exception cref="ComputeApiException">
		/// </exception>
	    public string GetFtpHost(KnownApiVendor vendor, KnownApiRegion region)
	    {
			string key = string.Concat(vendor.ToString(), '-', region.ToString());
			if (!KnownApiHostNames.ContainsKey(key))
				throw new ComputeApiException(
					ComputeApiError.BadRequest, 
					"Known Cloud API hostname not found with this vendor and region combination.", 
					string.Empty, 
					null, 
					string.Empty);
			return KnownFtpHostNames[key];
	    }
		/// <summary>
		/// Initialises a new instance of the <see cref="ComputeApiClient"/> class. 
		/// Creates a new CaaS API client using a known vendor and region.
		/// </summary>
		/// <param name="vendor">
		/// The vendor
		/// </param>
		/// <param name="region">
		/// The region
		/// </param>
		/// <param name="credentials">
		/// The credentials.
		/// </param>
		/// <param name="organizationId">
		/// The organization Id.
		/// </param>
		/// <returns>
		/// The <see cref="ComputeApiClient"/>.
		/// </returns>
		public static ComputeApiClient GetComputeApiClient(KnownApiVendor vendor, KnownApiRegion region, ICredentials credentials, Guid organizationId = default(Guid))
		{
			Uri baseUri = KnownApiUri.Instance.GetBaseUri(vendor, region);
			return GetComputeApiClient(baseUri, credentials, organizationId);
		}
		/// <summary>
		/// Initialises a new instance of the <see cref="ApiHostNotFoundException"/> class.
		/// </summary>
		/// <param name="vendor">
		/// The vendor.
		/// </param>
		/// <param name="region">
		/// The region.
		/// </param>
		public ApiHostNotFoundException(KnownApiVendor vendor, KnownApiRegion region)
			: base(ComputeApiError.BadRequest, message)
		{
			Vendor = vendor;
			Region = region;
		}
        /// <summary>
        /// Return an known CaaS URI based on vendor and region
        /// </summary>
		/// <param name="vendor">
		/// The vendor
		/// </param>
		/// <param name="region">
		/// The region
		/// </param>
		/// <returns>
		/// The <see cref="Uri"/>.
		/// </returns>
        public Uri GetBaseUri(KnownApiVendor vendor, KnownApiRegion region)
        {
            const string urltemplate = "https://{0}/";
            string key = string.Concat(vendor.ToString(), '-', region.ToString());
			if (!KnownApiHostNames.ContainsKey(key))
				throw new ComputeApiException (
					ComputeApiError.BadRequest, 
					"Known Cloud API hostname not found with this vendor and region combination.", 
					string.Empty, 
					null, 
					string.Empty);


            string apiurl = string.Format(urltemplate, KnownApiHostNames[key]);
            return new Uri(apiurl);
        }
		public async Task<IEnumerable<Geo>> DiscoverHomeMultiGeo(KnownApiVendor vendor, ICredentials credential)
		{
			return await GetListOfMultiGeographyRegionsFromHomeRegion(vendor, credential);
		}			
		/// <summary>
		/// The discover home multi geo.
		/// </summary>
		/// <param name="vendor">
		/// The vendor.
		/// </param>
		/// <param name="credential">
		/// The credential.
		/// </param>
		/// <returns>
		/// The <see cref="Task"/>.
		/// </returns>
		/// <exception cref="Exception">
		/// </exception>
		public static async Task<IEnumerable<Geo>> GetListOfMultiGeographyRegionsFromHomeRegion(
			KnownApiVendor vendor,
			ICredentials credential)
		{
			IEnumerable<KnownApiRegion> regionList = KnownApiUri.Instance.GetKnownRegionList(vendor);

			IDictionary<KnownApiRegion, IComputeApiClient> computeClients = regionList.Select(
				region =>
				new KeyValuePair<KnownApiRegion, IComputeApiClient>(
					region,
					GetComputeApiClient(vendor, region, credential)))
																					.ToDictionary(x => x.Key, x => x.Value);

			if (computeClients.Count == 0)
			{
				throw new Exception("No known end points for this vendor");
			}

			IDictionary<KnownApiRegion, Task<IAccount>> loginTasks = computeClients.Select(
				client => new KeyValuePair<KnownApiRegion, Task<IAccount>>(client.Key, client.Value.Login()))
																					.ToDictionary(x => x.Key, x => x.Value);

			// try login to all known regions simultaneoulsy. Note, not all regions may be enabled for this particular client.
			try
			{
				await Task.WhenAll(loginTasks.Values.ToArray());
			}
			// ReSharper disable once EmptyGeneralCatchClause
			catch (Exception)
			{
				// ignore (there might be region that this user is not enabled)
			}

			var loggedInClients = loginTasks.Where(client => client.Value.Status == TaskStatus.RanToCompletion)
											.Select(login => computeClients[login.Key])
											.ToList();

			if (loggedInClients.Count == 0)
			{
				throw new Exception("Invalid login or user doesn't exists");
			}

			Task<IEnumerable<Geo>>[] multiGeoTasks =
				loggedInClients.Select(client => client.Account.GetListOfMultiGeographyRegions())
								.ToArray();

			// multiGeo only works in the home geo.
			try
			{
				await Task.WhenAll(multiGeoTasks);
			}
			// ReSharper disable once EmptyGeneralCatchClause
			catch (Exception)
			{
				// ignore (only one task will return with valid result)
			}

			IEnumerable<Geo> validMultiGeo =
				multiGeoTasks.Single(task => task.Status == TaskStatus.RanToCompletion && task.Result != null)
							.Result;
			return validMultiGeo;
		}
		/// <summary>
		/// The get ftp host.
		/// </summary>
		/// <param name="vendor">
		/// The vendor.
		/// </param>
		/// <param name="region">
		/// The region.
		/// </param>
		/// <returns>
		/// The <see cref="string"/>.
		/// </returns>
		public static string GetFtpHost(KnownApiVendor vendor, KnownApiRegion region)
		{
			return KnownApiUri.Instance.GetFtpHost(vendor, region);
		}
		/// <summary>
		/// Since MultiGeo call is only valid for the home geo, use this method to discover what is your home geo and the
		///     applicable regions for this user.
		///     This is a multithreaded call that uses the underlying ComputeApiClient.GetListOfMultiGeographyRegions()
		///     to discover the home geo and multi geo for this user to all API endpoints known for vendor.
		///     Note: Most of the user vendor is DimensionData. Use this if you have to guess which vendor the user is under.
		/// </summary>
		/// <param name="vendor">
		/// The vendor of the user
		/// </param>
		/// <param name="credential">
		/// Credential of the user
		/// </param>
		/// <returns>
		/// The <see cref="Task"/>.
		/// </returns>
		public async Task<IEnumerable<Geo>> DiscoverHomeMultiGeo(KnownApiVendor vendor, ICredentials credential)
		{
			IEnumerable<KnownApiRegion> regionList = KnownApiUri.Instance.GetKnownRegionList(vendor);

			ComputeApiClient[] computeClients = regionList.Select(region => new ComputeApiClient(vendor, region)).ToArray();
			if (computeClients.Length == 0)
			{
				throw new Exception("No known end points for this vendor");
			}

			Task<IAccount>[] loginTasks = computeClients.Select(client => client.LoginAsync(credential)).ToArray();

			// try login to all known regions simultaneoulsy. Note, not all regions may be enabled for this particular client.
			try
			{
				await Task.WhenAll(loginTasks);
			}
			catch (Exception aex)
			{
				// ignore (there might be region that this user is not enabled)
			}

			computeClients = computeClients.Where(client => client.WebApi.IsLoggedIn).ToArray();
			if (computeClients.Length == 0)
			{
				throw new Exception("Invalid login or user doesn't exists");
			}

			Task<IEnumerable<Geo>>[] multiGeoTasks =
				computeClients.Select(client => client.GetListOfMultiGeographyRegions()).ToArray();

			// multiGeo only works in the home geo.
			try
			{
				await Task.WhenAll(multiGeoTasks);
			}
			catch (Exception aex)
			{
				// ignore (only one task will return with valid result)
			}

			IEnumerable<Geo> validMultiGeo =
				multiGeoTasks.Single(task => task.Status == TaskStatus.RanToCompletion && task.Result != null).Result;
			return validMultiGeo;
		}
Ejemplo n.º 19
0
        /// <summary>	The add ftp host name. </summary>
        /// <remarks>	Anthony, 4/24/2015. </remarks>
        /// <param name="vendor">	The vendor. </param>
        /// <param name="region">	The region. </param>
        /// <param name="apiUrl">	The API url. </param>
        private void AddFtpHostName(KnownApiVendor vendor, KnownApiRegion region, string apiUrl)
        {
            string key = string.Concat(vendor.ToString(), '-', region.ToString());

            KnownFtpHostNames.Add(key, apiUrl);
        }
Ejemplo n.º 20
0
 /// <summary>	List of Known Regions that are valid for the particular Vendor. </summary>
 /// <remarks>	Anthony, 4/24/2015. </remarks>
 /// <param name="vendor">	The Vendor. </param>
 /// <returns>	The list of known regions. </returns>
 public IEnumerable <KnownApiRegion> GetKnownRegionList(KnownApiVendor vendor)
 {
     return(KnownVendorEndPointPairs.Where(pair => pair.Key == vendor).Select(pair => pair.Value));
 }
		public ComputeApiClient(KnownApiVendor vendor, KnownApiRegion region)
		{
			Uri baseUri = KnownApiUri.Instance.GetBaseUri(vendor, region);
			_httpClientHandler = new HttpClientHandler();
			var httpClient = new HttpClientAdapter(
				new HttpClient(_httpClientHandler, disposeHandler: true)
					{
						BaseAddress = baseUri
				});

			InitializeProperties(httpClient);
		}