Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
dtylman committed Dec 20, 2018
1 parent 13baaad commit d2c225c
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 77 deletions.
File renamed without changes.
3 changes: 1 addition & 2 deletions APIs/AWSAPI.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using Amazon;
using Amazon.Runtime;

Expand Down
2 changes: 1 addition & 1 deletion APIs/AWSLambdaAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Amazon.Lambda;
using Amazon.Lambda.Model;
using Amazon.Runtime;
using heaven.APIs;

namespace heaven.APIs
{
Expand Down Expand Up @@ -55,6 +54,7 @@ public override void Read(AWSCredentials credentials, RegionEndpoint region)
}
}
while (!string.IsNullOrEmpty(resp.NextMarker));

}

}
Expand Down
3 changes: 1 addition & 2 deletions AWSObject.cs → APIs/AWSObject.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using Amazon;

namespace heaven
namespace heaven.APIs
{
[Serializable]
public class AWSObject
Expand Down
49 changes: 21 additions & 28 deletions ResourceLister.cs → APIs/AWSResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@
using System.IO;
using Amazon;
using Amazon.Runtime;
using heaven.APIs;
using Newtonsoft.Json;

namespace heaven
namespace heaven.APIs
{
internal class ResourceLoader
internal class AWSResources
{
private readonly List<AWSAPI> apis = new List<AWSAPI>();
private string accessKey;
private string secretKey;
private int maxItems = 100;
private readonly List<AWSObject> objects = new List<AWSObject>();

public string AccessKey { get => accessKey; set => accessKey = value; }

private int maxItems = 100;

public string SecretKey { get => secretKey; set => secretKey = value; }
public int MaxItems { get => maxItems; set => maxItems = value; }

public IEnumerable<AWSObject> Objects
Expand All @@ -31,20 +25,29 @@ public IEnumerable<AWSObject> Objects
}
}

public ResourceLoader()
public AWSResources()
{
this.apis.Add(new AWSLambdaAPI(this.objects, this.MaxItems));
this.apis.Add(new AWSS3API(this.objects, this.maxItems));

LoadFromFile();
}


internal void Load(BackgroundWorker worker, DoWorkEventArgs e)
/// <summary>
/// Connects to AWS and reads all resources
/// </summary>
/// <param name="creds">Creds.</param>
/// <param name="worker">Worker.</param>
/// <param name="e">E.</param>
public void Scan(AWSCredentials creds, BackgroundWorker worker, DoWorkEventArgs e)
{
if (creds == null)
{
throw new ApplicationException("No Credentials are provided");
}
this.objects.Clear();
try
{
AWSCredentials creds = GetCredentials();
List<RegionEndpoint> regions = new List<RegionEndpoint>(RegionEndpoint.EnumerableAllRegions);
int totalItems = this.apis.Count * regions.Count;
int currentItem = 0;
Expand All @@ -53,15 +56,17 @@ internal void Load(BackgroundWorker worker, DoWorkEventArgs e)
foreach (RegionEndpoint region in regions)
{
int progress = (currentItem * 100) / totalItems;
worker.ReportProgress(progress, string.Format("Fetching '{0}' from {1}", api.Name, region));
worker.ReportProgress(progress, string.Format("Fetching '{0}' from {1}...", api.Name, region));
try
{
if (worker.CancellationPending)
{
e.Cancel = true;
return;
}
int before = this.objects.Count;
api.Read(creds, region);
worker.ReportProgress(progress, string.Format("{0} items read.\n", objects.Count - before));
}
catch (Exception ex)
{
Expand All @@ -78,11 +83,10 @@ internal void Load(BackgroundWorker worker, DoWorkEventArgs e)
finally
{
SaveToFile();
worker.ReportProgress(100, "Done");
worker.ReportProgress(100, "Done\n");
}
}


private void SaveToFile()
{
StreamWriter sw = new StreamWriter("objects.json", false);
Expand Down Expand Up @@ -137,16 +141,5 @@ private void LoadFromFile()
Console.WriteLine(ex);
}
}

private AWSCredentials GetCredentials()
{
if (!string.IsNullOrEmpty(this.AccessKey))
{
return new BasicAWSCredentials(this.AccessKey, this.SecretKey);
}
return FallbackCredentialsFactory.GetCredentials();
}


}
}
}
1 change: 0 additions & 1 deletion FormCredentials.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 50 additions & 4 deletions FormCredentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Amazon.Runtime;
using Amazon.Runtime.CredentialManagement;

namespace heaven
{
Expand All @@ -15,22 +17,66 @@ public partial class FormCredentials : Form
public FormCredentials()
{
InitializeComponent();
this.txtProfile.Text = SharedCredentialsFile.DefaultProfileName;
}

private void buttonOK_Click(object sender, EventArgs e)
/// <summary>
/// Gets the credentials from the GUI
/// </summary>
/// <value>The credentials.</value>
public AWSCredentials Credentials
{
get
{
if (!string.IsNullOrEmpty(this.AccessKey) && (!string.IsNullOrEmpty(this.SecretKey)))
{
return new BasicAWSCredentials(this.AccessKey, this.SecretKey);
}
else if (!string.IsNullOrEmpty(this.ProfileName))
{
SharedCredentialsFile credentialsFile = new SharedCredentialsFile();
if (!credentialsFile.TryGetProfile(ProfileName, out CredentialProfile credentialProfile))
{
throw new ApplicationException(string.Format("Profile '{0}' does not exists", ProfileName));
}
if (!AWSCredentialsFactory.TryGetAWSCredentials(credentialProfile, credentialsFile, out AWSCredentials credentials))
{
throw new ApplicationException(string.Format("Failed to get credentials for profile '{0}'", ProfileName));
}
return credentials;
}
else
{
return FallbackCredentialsFactory.GetCredentials();
}
}
}

public string AccessKey { get{
return this.txtAccessKey.Text;
} }

public string SecretKey { get {
return this.txtSecretKey.Text;
}}

public string ProfileName
{
get
{
return this.txtProfile.Text;
}
}

private void txtAccessKey_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtAccessKey.Text))
{
this.txtProfile.Enabled = true;
this.txtProfile.Text = SharedCredentialsFile.DefaultProfileName;
} else
{
this.txtProfile.Enabled = false;
this.txtProfile.Text = "";
}
}
}
}
}
1 change: 0 additions & 1 deletion FormMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 31 additions & 32 deletions FormMain.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
using Amazon.CloudFormation;
using Amazon.CloudFormation.Model;
using Amazon.Runtime;
using Amazon.Runtime.CredentialManagement;
using System;
using System.Collections.Generic;
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Amazon.Runtime;
using heaven.APIs;

namespace heaven
{
public partial class FormMain : Form
{
private ResourceLoader resourceLoader = new ResourceLoader();
private readonly AWSResources awsResources = new AWSResources();
private AWSCredentials creds;

public FormMain()
{
InitializeComponent();
InitializeBackgroundWorker();
PopulateListView();

}

private void PopulateListView()
{
this.listViewObjects.Columns.Clear();
this.listViewObjects.Items.Clear();
foreach (AWSObject obj in this.resourceLoader.Objects)
foreach (AWSObject obj in this.awsResources.Objects)
{
ListViewItem item = this.listViewObjects.Items.Add(obj.Type);
item.SubItems.Add(obj.Region);
Expand All @@ -51,16 +44,26 @@ private void PopulateListView()
}
}

private AWSCredentials GetCredentials()
{
if (this.creds == null)
{
FormCredentials formCredentials = new FormCredentials();
DialogResult dr = formCredentials.ShowDialog(this);
if (dr == DialogResult.OK)
{
this.creds = formCredentials.Credentials;
}

}
return this.creds;
}

private void InitializeBackgroundWorker()
{
backgroundWorker.DoWork +=
new DoWorkEventHandler(BackgroundWorker_DoWork);
backgroundWorker.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(
BackgroundWorker_RunWorkerCompleted);
backgroundWorker.ProgressChanged +=
new ProgressChangedEventHandler(
BackgroundWorker_ProgressChanged);
backgroundWorker.DoWork += new DoWorkEventHandler(BackgroundWorker_DoWork);
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorker_RunWorkerCompleted);
backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged);
}

private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
Expand All @@ -85,7 +88,7 @@ private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerComplet
}
else if (e.Cancelled)
{
Print("Canceled");
Print("Canceled\n");
this.progressBar.Value = 0;
}
else
Expand All @@ -100,20 +103,20 @@ private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerComplet

private void Print(Exception e)
{
Print("Error: " + e.Message);
Print("Error: " + e.Message + "\n");
}

private void Print(object message)
{
this.statusLabel.Text = message.ToString();
this.textLog.AppendText(message.ToString() + "\n");
this.textLog.AppendText(message.ToString());
}


private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
this.resourceLoader.Load(worker, e);
this.awsResources.Scan(this.creds, worker, e);
}

private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
Expand All @@ -122,12 +125,13 @@ private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
}

private void ToolStripButtonClose_Click(object sender, EventArgs e)
{
{
Close();
}

private void ToolStripButtonLoad_Click(object sender, EventArgs e)
{
this.creds = GetCredentials();
statusLabel.Text = String.Empty;
this.buttonLoad.Enabled = false;
this.buttonStop.Enabled = true;
Expand All @@ -140,10 +144,5 @@ private void ToolStripButtonStop_Click(object sender, EventArgs e)
buttonStop.Enabled = false;
}

private void FormMain_Load(object sender, EventArgs e)
{
FormCredentials formCredentials= new FormCredentials();
formCredentials.ShowDialog(this);
}
}
}
Loading

0 comments on commit d2c225c

Please sign in to comment.