/// <inheritdoc/> public IOpenStackClient CreateClient(ICredential credential, CancellationToken token, string version) { credential.AssertIsNotNull("credential", "Cannot create an OpenStack client with a null credential."); version.AssertIsNotNull("version", "Cannot create an OpenStack client with a null version."); //Ensure that the assembly that contains the credential has a chance to register itself. this.ServiceLocator.EnsureAssemblyRegistration(credential.GetType().GetAssembly()); return(this.GetSupportedClient(this.clients, credential, token, version)); }
/// <inheritdoc/> public IOpenStackClient CreateClient <T>(ICredential credential, CancellationToken token, string version) where T : IOpenStackClient { credential.AssertIsNotNull("credential", "Cannot create an OpenStack client with a null credential."); version.AssertIsNotNull("version", "Cannot create an OpenStack client with a null version."); //Ensure that the assemblies that contain the credential and client type has had a chance to register itself. this.ServiceLocator.EnsureAssemblyRegistration(credential.GetType().GetAssembly()); this.ServiceLocator.EnsureAssemblyRegistration(typeof(T).GetAssembly()); return(this.GetSupportedClient(this.clients.Where(c => c == typeof(T)), credential, token, version)); }
/// <inheritdoc/> public T CreateServiceClient <T>(ICredential credential, string serviceName, CancellationToken cancellationToken) where T : IOpenStackServiceClient { credential.AssertIsNotNull("credential", "Cannot create an OpenStack service with a null credential."); cancellationToken.AssertIsNotNull("cancellationToken", "Cannot create an OpenStack service with a null cancellationToken."); credential.ServiceCatalog.AssertIsNotNull("credential.ServiceCatalog", "Cannot create an OpenStack service with a null service catalog."); //Ensure that the assembly that contains this credential has had a chance to be service located. //This is, at least for now, the entry point for third-parties can extend the API/SDK. this.ServiceLocator.EnsureAssemblyRegistration(credential.GetType().GetAssembly()); this.ServiceLocator.EnsureAssemblyRegistration(typeof(T).GetAssembly()); foreach (var serviceClientDef in this.serviceClientDefinitions.Where(s => typeof(T).IsAssignableFrom(s.Key))) { if (serviceClientDef.Value != null && serviceClientDef.Value.IsSupported(credential, serviceName)) { var client = this.CreateServiceClientInstance(serviceClientDef.Value, credential, serviceName, cancellationToken); return((T)client); } } throw new InvalidOperationException("A client that supports the requested service for the given instance of OpenStack could not be found."); }
/// <summary> /// Creates a new instance of the requested client type /// </summary> /// <param name="clientType">The type of the client to create.</param> /// <param name="credential">A credential that needs to be supported.</param> /// <param name="token">A cancellation token that can be used to cancel operations.</param> /// <returns>An instance of the requested client.</returns> internal IOpenStackClient CreateClientInstance(Type clientType, ICredential credential, CancellationToken token) { clientType.AssertIsNotNull("clientType", "Cannot create an OpenStack client with a null type."); credential.AssertIsNotNull("credential", "Cannot create an OpenStack client with a null credential."); token.AssertIsNotNull("credential", "Cannot create an OpenStack client with a null cancellation token. Use CancellationToken.None."); IOpenStackClient instance; try { instance = Activator.CreateInstance(clientType, credential, token, this.ServiceLocator) as IOpenStackClient; } catch (Exception ex) { throw new InvalidOperationException(string.Format("Could not create a client of type '{0}'. See inner exception for details.", clientType.Name), ex); } if (instance != null) { return(instance); } throw new InvalidOperationException(string.Format("Could not create a client of type '{0}'. The type does not derive from or cast to IOpenStackClient. ", clientType.Name)); }