Example #1
0
        /// <summary>
        ///     The begin processing.
        /// </summary>
        protected override void BeginProcessing()
        {
            base.BeginProcessing();
            _logger = new HttpTraceLogger(this);

            // If CaaS connection is NOT set via parameter, get it from the PS session
            if (Connection == null)
            {
                Connection = SessionState.GetDefaultComputeServiceConnection();
                if (Connection == null)
                {
                    ThrowTerminatingError(
                        new ErrorRecord(
                            new AuthenticationException(
                                "Cannot find a valid connection. Use New-CaasConnection to create or Set-CaasActiveConnection to set a valid connection"),
                            "-1",
                            ErrorCategory.AuthenticationError,
                            this));
                }
            }
            Connection.OnRequestStart += _logger.LogRequestHandler;
        }
Example #2
0
        /// <summary>
        ///     Try to login into the account using the credentials.
        ///     If succeed, it will return the account details.
        /// </summary>
        /// <returns>
        ///     The CaaS connection
        /// </returns>
        private async Task <ComputeServiceConnection> LoginTask()
        {
            string            ftpHost   = string.Empty;
            IComputeApiClient apiClient = null;
            var messageHandler          = GetMessageHandler(ApiCredentials.GetNetworkCredential());

            _logger = new HttpTraceLogger(this);
            messageHandler.LogEventHandler += _logger.LogRequestHandler;

            if (ParameterSetName == "KnownApiUri")
            {
                var baseUri = KnownApiUri.Instance.GetBaseUri(Vendor, Region);
                apiClient = GetComputeApiClient(baseUri, messageHandler);
                ftpHost   = ComputeApiClient.GetFtpHost(Vendor, Region);
            }

            if (ParameterSetName == "ApiDomainName")
            {
                Uri baseUri;
                // Support ApiDomainName containing https://
                if (Uri.TryCreate(ApiDomainName, UriKind.Absolute, out baseUri))
                {
                    ftpHost = baseUri.Host;
                }
                else
                {
                    // Support ApiDomainName as in just the domainName
                    baseUri = new Uri(string.Format("https://{0}/", ApiDomainName));
                    ftpHost = ApiDomainName;
                }

                // Handle explicit FTP host name
                if (!string.IsNullOrWhiteSpace(FtpDomainName))
                {
                    ftpHost = FtpDomainName;
                    Uri ftpUri;
                    if (Uri.TryCreate(FtpDomainName, UriKind.Absolute, out ftpUri))
                    {
                        ftpHost = ftpUri.Host;
                    }
                }

                apiClient = GetComputeApiClient(baseUri, messageHandler);
            }
            if (ParameterSetName == "HttpClient")
            {
                apiClient = new ComputeApiClient(new HttpClientAdapter(HttpClient));
            }

            var newCloudComputeConnection = new ComputeServiceConnection(apiClient, messageHandler);

            WriteDebug("Trying to login into the CaaS");
            newCloudComputeConnection.User = await apiClient.LoginAsync();

            // await newCloudComputeConnection.ApiClient.LoginAsync();

            if (!string.IsNullOrWhiteSpace(ftpHost))
            {
                // Right now we dont need to do a connect, as ftp is used in only a few commands
                newCloudComputeConnection.FtpClient = new FtpClient
                {
                    Host                     = ftpHost,
                    EncryptionMode           = FtpEncryptionMode.Explicit,
                    DataConnectionEncryption = true,
                    Credentials              = ApiCredentials.GetNetworkCredential()
                                               .GetCredential(new Uri(string.Format("ftp://{0}", ftpHost)), "Basic")
                };
            }
            messageHandler.LogEventHandler -= _logger.LogRequestHandler;
            return(newCloudComputeConnection);
        }