// mappa risultati della API private static AWSInstance SetViewData(InstanceState instanceStatus) { AWSInstance ret = new AWSInstance(); ret.StatusCode = instanceStatus.Code; switch (ret.StatusCode) { case 16: ret.DivClass = "alert alert-success"; ret.ButtonState = "disabled"; break; case 0: ret.DivClass = "alert alert-warning"; ret.ButtonState = "disabled"; break; case 80: ret.DivClass = "alert alert-danger"; ret.ButtonState = "enabled"; break; default: ret.DivClass = "alert alert-danger"; ret.ButtonState = "disabled"; break; } ret.StatusDescription = instanceStatus.Name; return(ret); }
// chiama API e valorizza stato dell'istanza protected void SetPageData() { // Crea i parametri di lancio var myRegion = RegionEndpoint.GetBySystemName(ConfigurationManager.AppSettings["AWS_REGION"]); // non funziona in appsettings.json... var ec2Client = new AmazonEC2Client(new BasicAWSCredentials(ConfigurationManager.AppSettings["AWS_ACCESSKEY"], ConfigurationManager.AppSettings["AWS_SECRETKEY"]), myRegion); var rq2 = new DescribeInstancesRequest { InstanceIds = { ConfigurationManager.AppSettings["AWS_INSTANCEID"] } }; // Recupera lo stato dell'instanza DescribeInstancesResponse r2 = ec2Client.DescribeInstances(rq2); // Status //0 : pending //16 : running //32 : shutting - down //48 : terminated //64 : stopping //80 : stopped AWSInstance ModelData = SetViewData(r2.Reservations[0].Instances[0].State); lbStato.Text = ModelData.StatusDescription; switch (ModelData.StatusCode) { case 16: // running lbStato.ForeColor = System.Drawing.ColorTranslator.FromHtml("#008000"); // green InsertButton.Visible = false; break; case 80: // stopperd lbStato.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF0000"); // red InsertButton.Visible = true; break; default: lbStato.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FFA500"); // orange InsertButton.Visible = false; break; } // se mancano le autorizzazioni spegne comunque il tasto if (!Auth.ReturnPermission("STARTAWS", "START")) { InsertButton.Visible = false; } }
private static string GetServerIconAndTooltip(AWSInstance ec2Instance) { var result = string.Empty; try { foreach (var securityGroupID in ec2Instance.SecurityGroupIDs) { var securityGroup = GetSecurityGroupFromID(securityGroupID); result = string.Format(Constants.IconDivLine, ec2Instance.Icon) + Environment.NewLine; foreach (var rule in securityGroup.IpPermissions) { if (rule.IpRanges.Count > 0) { foreach (var ipRange in rule.IpRanges) { result += GetRangeText(rule, ipRange); } } else { foreach (var secGroup in rule.UserIdGroupPairs) { result += GetRuleText(rule, secGroup.GroupId); } } } } result += Constants.IconDivLineEnd + Environment.NewLine; return(result); } catch (Exception ex) { Logger.Log(LogLevel.Error, ex, "Exception in GetServerIconAndTooltip()"); } return(string.Empty); }
private static string GetServerDetails(AWSInstance ec2Instance) { try { var details = "Desc: <b>No description found</b>" + Constants.LineBreak + Environment.NewLine; var owner = "Owner: <b>No owner found</b>" + Constants.LineBreak + Environment.NewLine; if (!ec2Instance.IsRDS) { if (!string.IsNullOrEmpty(ec2Instance.Description)) { details = "Desc: <b>" + ec2Instance.Description + "</b>" + Constants.LineBreak + Environment.NewLine; } if (!string.IsNullOrEmpty(ec2Instance.Owner)) { owner = "Owner: " + ec2Instance.Owner + Constants.LineBreak + Environment.NewLine; } } else { details = "Desc: <b>RDS Instance</b>" + Constants.LineBreak + Environment.NewLine; if (ec2Instance.PubliclyAccessible) { owner = "Public: <b>Yes</b>" + Constants.LineBreak + Environment.NewLine; } else { owner = "Public: <b>No</b>" + Constants.LineBreak + Environment.NewLine; } if (ec2Instance.Encrypted) { owner += "Encrypted: <b>Yes</b>" + Constants.LineBreak + Environment.NewLine; } else { owner += "Encrypted: <b>No</b>" + Constants.LineBreak + Environment.NewLine; } } details += owner + "Subnet(s): " + Constants.LineBreak + Environment.NewLine; foreach (var subnetID in ec2Instance.SubnetIDs) { details += GetSubnetNameFromID(subnetID) + "  (" + subnetID + ")" + Constants.LineBreak + Environment.NewLine; } details += "Security Group(s): " + Constants.LineBreak + Environment.NewLine; foreach (var securityGroupID in ec2Instance.SecurityGroupIDs) { details += GetSecurityGroupFromID(securityGroupID).GroupName + Constants.LineBreak + Environment.NewLine; } if (ec2Instance.PrivateIPs.Count > 0) { details += "Private IPs:" + Constants.LineBreak + Environment.NewLine; details = ec2Instance.PrivateIPs.Aggregate(details, (current, privateIP) => current + (privateIP + Constants.LineBreak + Environment.NewLine)); } if (ec2Instance.PublicIPs.Count > 0) { details += "Public IPs:" + Constants.LineBreak + Environment.NewLine; details = ec2Instance.PublicIPs.Aggregate(details, (current, publicIP) => current + (publicIP + Constants.LineBreak + Environment.NewLine)); } if (ec2Instance.Volumes.Count > 0) { details += "Volumes:" + Constants.LineBreak + Environment.NewLine; foreach (var volume in ec2Instance.Volumes) { var device = volume.Attachments[0].Device; if (volume.Encrypted) { details += device + " (" + volume.Size + "GB encrypted)" + Constants.LineBreak + Environment.NewLine; } else { details += device + " (" + volume.Size + "GB)" + Constants.LineBreak + Environment.NewLine; } } } details = string.Format(Constants.DetailsDivLine, details); details += Constants.DetailsDivEndLine; return details; } catch (Exception ex) { Logger.Log(LogLevel.Error, ex, "Exception in GetServerDetails()"); } return string.Empty; }
private static string GetServerIconAndTooltip(AWSInstance ec2Instance) { var result = string.Empty; try { foreach (var securityGroupID in ec2Instance.SecurityGroupIDs) { var securityGroup = GetSecurityGroupFromID(securityGroupID); result = string.Format(Constants.IconDivLine, ec2Instance.Icon) + Environment.NewLine; foreach (var rule in securityGroup.IpPermissions) { if (rule.IpRanges.Count > 0) { foreach (var ipRange in rule.IpRanges) { result += GetRangeText(rule, ipRange); } } else { foreach (var secGroup in rule.UserIdGroupPairs) { result += GetRuleText(rule, secGroup.GroupId); } } } } result += Constants.IconDivLineEnd + Environment.NewLine; return result; } catch (Exception ex) { Logger.Log(LogLevel.Error, ex, "Exception in GetServerIconAndTooltip()"); } return string.Empty; }
private static string GetServerDetails(AWSInstance ec2Instance) { try { var details = "Desc: <b>No description found</b>" + Constants.LineBreak + Environment.NewLine; var owner = "Owner: <b>No owner found</b>" + Constants.LineBreak + Environment.NewLine; if (!ec2Instance.IsRDS) { if (!string.IsNullOrEmpty(ec2Instance.Description)) { details = "Desc: <b>" + ec2Instance.Description + "</b>" + Constants.LineBreak + Environment.NewLine; } if (!string.IsNullOrEmpty(ec2Instance.Owner)) { owner = "Owner: " + ec2Instance.Owner + Constants.LineBreak + Environment.NewLine; } } else { details = "Desc: <b>RDS Instance</b>" + Constants.LineBreak + Environment.NewLine; if (ec2Instance.PubliclyAccessible) { owner = "Public: <b>Yes</b>" + Constants.LineBreak + Environment.NewLine; } else { owner = "Public: <b>No</b>" + Constants.LineBreak + Environment.NewLine; } if (ec2Instance.Encrypted) { owner += "Encrypted: <b>Yes</b>" + Constants.LineBreak + Environment.NewLine; } else { owner += "Encrypted: <b>No</b>" + Constants.LineBreak + Environment.NewLine; } } details += owner + "Subnet(s): " + Constants.LineBreak + Environment.NewLine; foreach (var subnetID in ec2Instance.SubnetIDs) { details += GetSubnetNameFromID(subnetID) + "  (" + subnetID + ")" + Constants.LineBreak + Environment.NewLine; } details += "Security Group(s): " + Constants.LineBreak + Environment.NewLine; foreach (var securityGroupID in ec2Instance.SecurityGroupIDs) { details += GetSecurityGroupFromID(securityGroupID).GroupName + Constants.LineBreak + Environment.NewLine; } if (ec2Instance.PrivateIPs.Count > 0) { details += "Private IPs:" + Constants.LineBreak + Environment.NewLine; details = ec2Instance.PrivateIPs.Aggregate(details, (current, privateIP) => current + (privateIP + Constants.LineBreak + Environment.NewLine)); } if (ec2Instance.PublicIPs.Count > 0) { details += "Public IPs:" + Constants.LineBreak + Environment.NewLine; details = ec2Instance.PublicIPs.Aggregate(details, (current, publicIP) => current + (publicIP + Constants.LineBreak + Environment.NewLine)); } if (ec2Instance.Volumes.Count > 0) { details += "Volumes:" + Constants.LineBreak + Environment.NewLine; foreach (var volume in ec2Instance.Volumes) { var device = volume.Attachments[0].Device; if (volume.Encrypted) { details += device + " (" + volume.Size + "GB encrypted)" + Constants.LineBreak + Environment.NewLine; } else { details += device + " (" + volume.Size + "GB)" + Constants.LineBreak + Environment.NewLine; } } } details = string.Format(Constants.DetailsDivLine, details); details += Constants.DetailsDivEndLine; return(details); } catch (Exception ex) { Logger.Log(LogLevel.Error, ex, "Exception in GetServerDetails()"); } return(string.Empty); }