Describes a reservation.
public ZAwsEc2(ZAwsEc2Controller controller, Amazon.EC2.Model.Reservation res) : base(controller) { StatisticsValid = false; LatestBootConsoleTimestamp = ""; ConsoleOutput = ""; Update(res); /* * Trace.WriteLine("Now will see to configure new applications."); * ConfigureAppsWhenBootingComplete = myController.HandleNewEc2Instance(this) ? 3 : 0; * Trace.WriteLine("ConfigureAppsWhenBootingComplete = " + ConfigureAppsWhenBootingComplete.ToString()); * */ }
private static void PopulateReservationSecurityGroupNames(Reservation reservation) { if (reservation != null) { var groupNames = new List<string>(); foreach (GroupIdentifier group in reservation.Groups) { groupNames.Add(group.GroupName); } reservation.GroupNames = groupNames; } }
/// <summary> /// Sets the Reservation property /// </summary> /// <param name="reservation">Reservation property</param> /// <returns>this instance</returns> public RunInstancesResult WithReservation(Reservation reservation) { this.reservationField = reservation; return this; }
/// <summary> /// Gets the data for EC2 Instances in a given Profile and Region. /// </summary> /// <param name="aprofile"></param> /// <param name="Region2Scan"></param> /// <returns></returns> public DataTable GetEC2Instances(string aprofile, string Region2Scan) { DataTable ToReturn = AWSTables.GetComponentTable("EC2"); RegionEndpoint Endpoint2scan = RegionEndpoint.USEast1; Amazon.Runtime.AWSCredentials credential; credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); //Convert the Region2Scan to an AWS Endpoint. foreach (var aregion in RegionEndpoint.EnumerableAllRegions) { if (aregion.DisplayName.Equals(Region2Scan)) { Endpoint2scan = aregion; continue; } } var ec2 = new Amazon.EC2.AmazonEC2Client(credential, Endpoint2scan); string accountid = GetAccountID(aprofile); var request = new DescribeInstanceStatusRequest(); request.IncludeAllInstances = true; DescribeInstanceStatusResponse instatresponse = new DescribeInstanceStatusResponse(); var indatarequest = new DescribeInstancesRequest(); try { instatresponse = ec2.DescribeInstanceStatus(request); } catch (Exception ex) { string test = "";//Quepaso? } //Get a list of the InstanceIDs. foreach (var instat in instatresponse.InstanceStatuses) { indatarequest.InstanceIds.Add(instat.InstanceId); indatarequest.InstanceIds.Sort(); } DescribeInstancesResponse DescResult = ec2.DescribeInstances(); int count = instatresponse.InstanceStatuses.Count(); //Build data dictionary of instances Dictionary<String, Instance> Bunchadata = new Dictionary<string, Instance>(); foreach (var urtburgle in DescResult.Reservations) { foreach (var instancedata in urtburgle.Instances) { try { Bunchadata.Add(instancedata.InstanceId, instancedata); } catch (Exception ex) { var ff ="";//a duplicate?? }; } } //Go through list of instances... foreach (var instat in instatresponse.InstanceStatuses) { string instanceid = instat.InstanceId; Instance thisinstance = new Instance(); try { thisinstance = Bunchadata[instanceid]; } catch(Exception ex) { continue; } DataRow thisinstancedatarow = ToReturn.NewRow(); //Collect the datases string instancename = ""; var status = instat.Status.Status; string AZ = instat.AvailabilityZone; var istate = instat.InstanceState.Name; string profile = aprofile; string myregion = Region2Scan; int eventnumber = instat.Events.Count(); List<string> eventlist = new List<string>(); var reservations = DescResult.Reservations; var myinstance = new Reservation(); var atreq = new DescribeInstanceAttributeRequest(); atreq.InstanceId = instanceid; atreq.Attribute = "disableApiTermination"; var atresp = ec2.DescribeInstanceAttribute(atreq).InstanceAttribute; string TerminationProtection = atresp.DisableApiTermination.ToString(); List<String> innies = new List<String>(); foreach (Reservation arez in DescResult.Reservations) { var checky = arez.Instances[0].InstanceId; innies.Add(checky); if (arez.Instances[0].InstanceId.Equals(instanceid)) { myinstance = arez; } } innies.Sort(); List<string> tags = new List<string>(); var loadtags = thisinstance.Tags.AsEnumerable(); foreach (var atag in loadtags) { tags.Add(atag.Key + ": " + atag.Value); if (atag.Key.Equals("Name")) instancename = atag.Value; } Dictionary<string, string> taglist = new Dictionary<string, string>(); foreach (var rekey in loadtags) { taglist.Add(rekey.Key, rekey.Value); } if (eventnumber > 0) { foreach (var anevent in instat.Events) { eventlist.Add(anevent.Description); } } String platform = ""; try { platform = thisinstance.Platform.Value; } catch { platform = "Linux"; } if (String.IsNullOrEmpty(platform)) platform = "Linux"; String Priv_IP = ""; try { Priv_IP = thisinstance.PrivateIpAddress; } catch { } if (String.IsNullOrEmpty(Priv_IP)) { Priv_IP = "?"; } String disinstance = thisinstance.InstanceId; String publicIP = ""; try { publicIP = thisinstance.PublicIpAddress; } catch { } if (String.IsNullOrEmpty(publicIP)) publicIP = ""; String publicDNS = ""; try { publicDNS = thisinstance.PublicDnsName; } catch { } if (String.IsNullOrEmpty(publicDNS)) publicDNS = ""; string myvpcid = ""; try { myvpcid = thisinstance.VpcId; } catch { } if (String.IsNullOrEmpty(myvpcid)) myvpcid = ""; string mysubnetid = ""; try { mysubnetid = thisinstance.SubnetId; } catch { } if (String.IsNullOrEmpty(mysubnetid)) mysubnetid = ""; //Virtualization type (HVM, Paravirtual) string ivirtType = ""; try { ivirtType = thisinstance.VirtualizationType; } catch { } if (String.IsNullOrEmpty(ivirtType)) ivirtType = "?"; // InstanceType (m3/Large etc) String instancetype = ""; try { instancetype = thisinstance.InstanceType.Value; } catch { } if (String.IsNullOrEmpty(instancetype)) instancetype = "?"; //Test section to try to pull out AMI data string AMI = ""; string AMIName = ""; string AMIDesc = ""; string AMILocation = ""; string AMIState = ""; try { AMI = thisinstance.ImageId; } catch { } if (string.IsNullOrEmpty(AMI)) AMI = ""; else { DescribeImagesRequest DIR = new DescribeImagesRequest(); DIR.ImageIds.Add(AMI); var imresp = ec2.DescribeImages(DIR); var idata = imresp.Images; try { if (idata.Count > 0) { AMIDesc = idata[0].Description; AMIName = idata[0].Name; AMILocation = idata[0].ImageLocation; AMIState = idata[0].State; } else { AMIDesc = "na"; AMIName = "na"; AMILocation = "na"; AMIState = "na"; } if (String.IsNullOrEmpty(AMIDesc)) { AMIDesc = ""; } if (String.IsNullOrEmpty(AMIName)) { AMIName = ""; } } catch(Exception ex) { string whyforerror = ex.Message; } } // var SGs = thisinstance.SecurityGroups; List<string> SGids = new List<string>(); List<String> SGNames = new List<string>(); foreach (var wabbit in SGs) { SGids.Add(wabbit.GroupId); SGNames.Add(wabbit.GroupName); } //Add to table if (SGids.Count < 1) SGids.Add("NullOrEmpty"); if (SGNames.Count < 1) SGNames.Add(""); if (String.IsNullOrEmpty(SGids[0])) SGids[0] = "NullOrEmpty"; if (String.IsNullOrEmpty(SGNames[0])) SGNames[0] = ""; if (String.IsNullOrEmpty(instancename)) instancename = ""; //EC2DetailsTable.Rows.Add(accountid, profile, myregion, instancename, instanceid, AMI, AMIDesc, AZ, platform, status, eventnumber, eventlist, tags, Priv_IP, publicIP, publicDNS, istate, ivirtType, instancetype, sglist); //Is list for Profile and Region, so can key off of InstanceID. In theory InstanceID is unique //Build our dictionary of values and keys for this instance This is dependent on the table created by GetEC2DetailsTable() Dictionary<string, string> datafields = new Dictionary<string, string>(); thisinstancedatarow["AccountID"] = accountid; thisinstancedatarow["Profile"] = profile; thisinstancedatarow["Region"] = myregion; thisinstancedatarow["InstanceName"] = instancename; thisinstancedatarow["InstanceID"] = instanceid; thisinstancedatarow["TerminationProtection"] = TerminationProtection; thisinstancedatarow["AMI"] = AMI; thisinstancedatarow["AMIState"] = AMIState; thisinstancedatarow["AMILocation"] = AMILocation; thisinstancedatarow["AMIDescription"] = AMIDesc; thisinstancedatarow["AvailabilityZone"] = AZ; thisinstancedatarow["Status"] = status; thisinstancedatarow["Events"] = eventnumber.ToString(); thisinstancedatarow["EventList"] = List2String(eventlist); thisinstancedatarow["Tags"] = List2String(tags); thisinstancedatarow["PrivateIP"] = Priv_IP; thisinstancedatarow["PublicIP"] = publicIP; thisinstancedatarow["PublicDNS"] = publicDNS; thisinstancedatarow["PublicDNS"] = publicDNS; thisinstancedatarow["VPC"] = myvpcid; thisinstancedatarow["SubnetID"] = mysubnetid; thisinstancedatarow["InstanceState"] = istate.Value; thisinstancedatarow["VirtualizationType"] = ivirtType; thisinstancedatarow["InstanceType"] = instancetype; thisinstancedatarow["SecurityGroups"] = List2String(SGids); thisinstancedatarow["SGNames"] = List2String(SGNames); //Add this instance to the data returned. ToReturn.Rows.Add(thisinstancedatarow); }//End for of instances return ToReturn; }//EndGetEC2
private void SetUpResponse() { var response = new DescribeInstancesResponse(); var reservationOne = new Reservation { Instances = new List<Instance> { GenerateInstance("one", InstanceStateName.Running), GenerateInstance("two", InstanceStateName.Stopped), GenerateInstance("three", InstanceStateName.Terminated) } }; var reservationTwo = new Reservation { Instances = new List<Instance> { GenerateInstance("four", InstanceStateName.Terminated), GenerateInstance("five", InstanceStateName.Terminated), GenerateInstance("six", InstanceStateName.Running), GenerateInstance("seven", InstanceStateName.Stopping) } }; response.Reservations = new List<Reservation> {reservationOne, reservationTwo}; Ec2ClientMock.Setup(x => x.DescribeInstances()).Returns(response); }