예제 #1
0
        private async void UploadToPuppetForgeAsync(ForgeData forgeData, string tarballName)
        {
            if (null == forgeData)
            {
                throw new ArgumentNullException("forgeData");
            }

            if (string.IsNullOrEmpty(tarballName))
            {
                throw new ArgumentNullException("tarballName");
            }

            var cancelationSource    = new CancellationTokenSource();
            var cancelationToken     = cancelationSource.Token;
            var uploadProgressWindow = new UploadProgressWindow(cancelationSource);

            try
            {
                var username        = forgeData[Conatants.PuppetForgeUserName];
                var forgeModuleName = forgeData[Conatants.PuppetForgeModuleName];
                var modulename      = forgeModuleName.ToLower();
                var moduleversion   = forgeData[Conatants.PuppetForgeModuleVersion];;

                var forgePublishWindow = new ForgePublishWindow
                {
                    tbAccountName   = { Text = username },
                    tbModuleName    = { Text = modulename },
                    tbModuleVersion = { Text = moduleversion }
                };

                if (this.password != null && this.password.Length != 0)
                {
                    forgePublishWindow.pwdAcountPassword.Password = SELF.ConvertToUnsecureString(this.password);
                    forgePublishWindow.btnPublish.IsEnabled       = true;
                }

                var res = forgePublishWindow.ShowDialog();

                if (!res.HasValue || res.Value == false)
                {
                    return;
                }

                this.password = forgePublishWindow.pwdAcountPassword.SecurePassword;

                uploadProgressWindow.Show();

                var accessToken = await System.Threading.Tasks.Task.Run(() =>
                {
                    cancelationToken.ThrowIfCancellationRequested();
                    return(SELF.GetAccessToken(username, this.password));
                }, cancelationToken);

                await System.Threading.Tasks.Task.Run(() =>
                {
                    cancelationToken.ThrowIfCancellationRequested();
                    SELF.UploadTarball(accessToken, username, modulename, tarballName);
                }, cancelationToken);

                uploadProgressWindow.lblStatus.Content = Resources.PuppetModuleUploadedSuccessfully;
                uploadProgressWindow.progressBar.Value = 100;
            }
            catch (OperationCanceledException)
            {
                Debug.WriteLine("Canceled");
                uploadProgressWindow.lblStatus.Content = "Canceled by user.";
                uploadProgressWindow.progressBar.Value = 0;
            }
            catch (WebException ex)
            {
                // This exception will be raised if the server didn't return 200 - OK
                // Retrieve more information about the error

                if (ex.Response != null)
                {
                    using (var err = (HttpWebResponse)ex.Response)
                    {
                        var info = string.Format(Resources.ServerReturnedTemplate,
                                                 err.StatusDescription, err.StatusCode, err.StatusCode);

                        uploadProgressWindow.lblStatus.Content = info;
                    }
                }
                else
                {
                    uploadProgressWindow.lblStatus.Content = ex.Message;
                }

                uploadProgressWindow.progressBar.Value = 0;
            }
            catch (Exception ex)
            {
                uploadProgressWindow.lblStatus.Content = ex.Message;
                uploadProgressWindow.progressBar.Value = 0;
            }

            uploadProgressWindow.progressBar.IsIndeterminate = false;
            uploadProgressWindow.btnCancel.Content           = "Close";
        }
예제 #2
0
        private static string GetAccessToken(string username, SecureString password)
        {
            string result = null;

            // Setup the variables necessary to make the Request
            const string url = "http://puppetforgegate.cloudapp.net:8080/api/Token";

            HttpWebResponse response = null;

            try
            {
                // Create the data to send
                var data = new StringBuilder();
                data.Append("&username="******"&password="******"POST";
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = byteArray.Length;

                // Write data
                Stream postStream = request.GetRequestStream();
                postStream.Write(byteArray, 0, byteArray.Length);
                postStream.Close();

                // Send Request & Get Response

                response = (HttpWebResponse)request.GetResponse();
                var responseStream = response.GetResponseStream();

                if (responseStream == null)
                {
                    return(null);
                }


                using (var reader = new StreamReader(responseStream))
                {
                    // Get the Response Stream
                    string json = reader.ReadLine();

                    // Retrieve and Return the Access Token
                    var ser         = new JavaScriptSerializer();
                    var x           = (Dictionary <string, object>)ser.DeserializeObject(json);
                    var accessToken = x["AccessToken"].ToString();
                    result = accessToken;
                }
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }

            return(result);
        }