/// <summary>
        /// Creates a new instance of the IdentityServicePocoClient class.
        /// </summary>
        /// <param name="credential">The credential to be used when interacting with OpenStack.</param>
        /// <param name="cancellationToken">The cancellation token to be used when interacting with OpenStack.</param>
        /// <param name="serviceLocator">A service locator to be used to locate/inject dependent services.</param>
        public IdentityServicePocoClient(IOpenStackCredential credential, string serviceName, CancellationToken cancellationToken, IServiceLocator serviceLocator)
        {
            credential.AssertIsNotNull("credential");
            cancellationToken.AssertIsNotNull("cancellationToken");
            serviceLocator.AssertIsNotNull("serviceLocator", "Cannot create an identity service poco client with a null service locator.");
            serviceName.AssertIsNotNullOrEmpty("serviceName", "Cannot create an identity service poco client with a null or empty service name.");

            this.credential        = credential;
            this.cancellationToken = cancellationToken;
            this.ServiceLocator    = serviceLocator;
            this.ServiceName       = serviceName;
        }
Exemple #2
0
        /// <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.");
        }
Exemple #3
0
        /// <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));
        }