Exemple #1
0
        public static SmsProvider[] QueryProvider(string server, string userName, string password)
        {
            SmsProvider[] providers;

            var binding =
                new BasicHttpsBinding {
                AllowCookies = true
            };

            var vimEndpoint =
                new EndpointAddress(string.Format("https://{0}/sdk/vimService", server));

            var smsEndpoint =
                new EndpointAddress(string.Format("https://{0}/sms/sdk", server));

            ServicePointManager.ServerCertificateValidationCallback += (sender, x509Certificate, chain, errors) => true;

            using (var client = new VimPortTypeClient(binding, vimEndpoint))
            {
                client.Open();

                var vimProxy = new VimProxy(client);

                string sessionId;

                var content =
                    vimProxy.RetrieveServiceContent();

                var credential =
                    new NetworkCredential {
                    UserName = userName, Password = password
                };

                vimProxy.Login(content.sessionManager, credential, null, out sessionId);

                using (var smsClient = new SmsPortTypeClient(binding, smsEndpoint))
                {
                    smsClient.Open();

                    var smsProxy = new SmsProxy(smsClient, sessionId);

                    var storageManager =
                        smsProxy.QueryStorageManager();

                    var providerRefs =
                        smsProxy.QueryProvider(storageManager);

                    providers =
                        providerRefs.Select(smsProxy.QueryProviderInfo).Select(GetProvider).ToArray();

                    smsClient.Close();
                }

                vimProxy.Logout(content.sessionManager);

                client.Close();
            }

            return(providers);
        }
Exemple #2
0
        public static SmsProvider RegisterProvider(
            ProviderElement providerConfiguration,
            X509Certificate2 vasaCertificate,
            String vCenter,
            String vCenterUserName,
            String vCenterPassword)
        {
            SmsProvider provider;

            var smsData = new VasaProviderSpec
            {
                name        = providerConfiguration.Name,
                url         = String.Format("{0}", providerConfiguration.VasaVersionXmlUri),
                username    = providerConfiguration.Credentials.Login,
                password    = providerConfiguration.Credentials.Password,
                certificate = Convert.ToBase64String(vasaCertificate.RawData)
            };

            var binding = new BasicHttpsBinding {
                AllowCookies = true
            };
            var vimEndpoint = new EndpointAddress(string.Format("https://{0}/sdk/vimService", vCenter));
            var smsEndpoint = new EndpointAddress(string.Format("https://{0}/sms/sdk", vCenter));

            ServicePointManager.ServerCertificateValidationCallback += (sender, x509Certificate, chain, errors) => true;

            using (var client = new VimPortTypeClient(binding, vimEndpoint))
            {
                client.Open();

                var vimProxy = new VimProxy(client);

                string sessionId;

                var content = vimProxy.RetrieveServiceContent();

                var vCenterCredential = new NetworkCredential
                {
                    UserName = vCenterUserName, Password = vCenterPassword
                };
                vimProxy.Login(content.sessionManager, vCenterCredential, null, out sessionId);

                using (var smsClient = new SmsPortTypeClient(binding, smsEndpoint))
                {
                    smsClient.Open();

                    var smsProxy       = new SmsProxy(smsClient, sessionId);
                    var storageManager = smsProxy.QueryStorageManager();
                    var taskRef        = smsProxy.RegisterProvider(storageManager, smsData);

                    var timeout = TimeSpan.FromMinutes(10);
                    if (!smsProxy.WaitSmsTask(taskRef, timeout))
                    {
                        try
                        {
                            var vimTaskRef = new DataCore.Vim.ManagedObjectReference {
                                type = taskRef.type, Value = taskRef.Value
                            };
                            client.CancelTask(vimTaskRef);
                        }
                        catch (Exception ex)
                        {
                            _traceSource.TraceEvent(TraceEventType.Warning, 0,
                                                    Resources.ErrorCancelingVasaProviderRegistration,
                                                    taskRef.Value, ex.ToString());
                        }

                        var msg = String.Format(
                            Resources.VasaProviderRegistrationTimeoutError,
                            taskRef.Value, timeout);

                        _traceSource.TraceEvent(TraceEventType.Error, 0, msg);
                        throw new TimeoutException(msg);
                    }

                    var task = smsProxy.QuerySmsTaskInfo(taskRef);

                    if (task.state.Equals("error", StringComparison.OrdinalIgnoreCase))
                    {
                        var error = task.error;

                        var msg = String.Format(
                            Resources.ErrorOnVasaProviderRegistration,
                            error.fault.GetType().Name, error.localizedMessage);

                        _traceSource.TraceEvent(TraceEventType.Error, 0, msg);
                        throw new Exception(msg);
                    }

                    var result =
                        smsProxy.QuerySmsTaskResult(taskRef);

                    var providerInfo =
                        smsProxy.QueryProviderInfo((Sms.ManagedObjectReference)result);

                    provider = GetProvider(providerInfo);

                    smsClient.Close();
                }
                vimProxy.Logout(content.sessionManager);
                client.Close();
            }

            return(provider);
        }