public static string GetTagValue(List <Amazon.EC2.Model.Tag> Tags, string tagname) { Amazon.EC2.Model.Tag t = Tags.Find(item => item.Key == tagname); if (t != null) { return(t.Value); } else { return(""); } }
public void createModel(Package pkg, Vpc vpc, bool enableDnsSupport, bool enableDnsHostnames) { this.log.Debug("{"); this.log.Debug(" VpcId = " + vpc.VpcId + ","); this.log.Debug(" CidrBlock = " + vpc.CidrBlock + ","); this.log.Debug(" EnableDnsSupport = " + enableDnsSupport + ","); this.log.Debug(" EnableDnsHostnames = " + enableDnsHostnames + ","); this.log.Debug(" InstanceTenancy = " + vpc.InstanceTenancy + ","); this.log.Debug("}"); // Get the model identifier string vpcId = vpc.VpcId.ToString(); // Create the element Element element = pkg.Elements.AddNew(vpcId, "SerAwsMdl::MDL_EC2_Vpc"); element.Update(); // Language - Default to C# element.Gentype = "C#"; // Stereotype Tagged Values TaggedValue vpcIdTv = this.GetTaggedValue(element, "VpcId"); vpcIdTv.Value = vpc.VpcId; vpcIdTv.Update(); TaggedValue cidrBlockTv = this.GetTaggedValue(element, "CidrBlock"); cidrBlockTv.Value = vpc.CidrBlock; cidrBlockTv.Update(); TaggedValue enableDnsSupportTv = this.GetTaggedValue(element, "EnableDnsSupport"); enableDnsSupportTv.Value = enableDnsSupport.ToString(); enableDnsSupportTv.Update(); TaggedValue enableDnsHostnamesTv = this.GetTaggedValue(element, "EnableDnsHostnames"); enableDnsHostnamesTv.Value = enableDnsSupport.ToString(); enableDnsHostnamesTv.Update(); TaggedValue instanceTenancyTv = this.GetTaggedValue(element, "InstanceTenancy"); instanceTenancyTv.Value = vpc.InstanceTenancy.ToString(); instanceTenancyTv.Update(); // AWS Tags List <Amazon.EC2.Model.Tag> tags = vpc.Tags; string name = null; for (var jdx = 0; jdx < tags.Count; jdx++) { // Set a corresponding tagged value Amazon.EC2.Model.Tag tag = tags[jdx]; this.log.Debug("Key: " + tag.Key + ", Value: " + tag.Value); var tv = element.TaggedValues.AddNew(tag.Key, tag.Value); tv.Update(); // Keep track of the name, as this is a special case if (tag.Key == "Name") { name = tag.Value; } } // Set the name of the element to match "Name" tag if (name != null) { element.Name = name; element.Update(); } // Keep it in the cache this.AwsModelCache.Add(vpcId, element); }
//############################################################################################ public string lunchInstance(IAmazonEC2 ec2, string ami, string subnetid, string Key, string insttype, List <string> secgroupid, bool publicip, string vpc, string NewSG, string privateIP, string Nametag, int rootDiskSize) //create new instance { RunInstancesResponse launchResponse; string subnetID = subnetid; // "subnet-2e107b76"; string amiID = ami; // "ami-c51e3eb6"; string keyPairName = Key; // "sirin-staging"; string itype = insttype; // "t2.small"; // List<string> groups = new List<string>() { "sg-9f90e2f9" }; List <string> groups = secgroupid; // new List<string>() { secgroupid }; if (NewSG.Length > 0) // create and add a new security group { try { //SG.GroupName = "AccessGP"; SecurityGroup SG = new SecurityGroup(); SG = CreateSecurityGroup(ec2, NewSG, vpc); groups.Add(SG.GroupId); } catch (Exception ex) { return(ex.Message + "\n" + ex.StackTrace); } } try { var eni = new InstanceNetworkInterfaceSpecification() { DeviceIndex = 0, SubnetId = subnetID, Groups = groups, AssociatePublicIpAddress = publicip }; if (privateIP.Length > 0) { eni.PrivateIpAddress = privateIP; // eni.PrivateIpAddress = "10.1.200.200"; } List <InstanceNetworkInterfaceSpecification> enis = new List <InstanceNetworkInterfaceSpecification>() { eni }; var launchRequest = new RunInstancesRequest() { ImageId = amiID, InstanceType = itype, MinCount = 1, MaxCount = 1, KeyName = keyPairName, NetworkInterfaces = enis // TagSpecifications = "protectorx" }; if (rootDiskSize != 0) { BlockDeviceMapping mapping = new BlockDeviceMapping { DeviceName = "/dev/sda1", Ebs = new EbsBlockDevice { VolumeType = VolumeType.Gp2, VolumeSize = rootDiskSize } }; List <BlockDeviceMapping> mappinglist; mappinglist = new List <BlockDeviceMapping>(); mappinglist.Add(mapping); launchRequest.BlockDeviceMappings = mappinglist; } // ------------------------- add tag ---------------------------------------------------- List <Amazon.EC2.Model.Tag> TagList; TagSpecification Tagspec; Amazon.EC2.Model.Tag EC2tag; //Amazon.EC2.Model.resorc EC2tag = new Amazon.EC2.Model.Tag(); EC2tag.Key = "Name"; EC2tag.Value = Nametag; //EC2tag.GetType TagList = new List <Amazon.EC2.Model.Tag>(); TagList.Add(EC2tag); // EC2tag(); Tagspec = new Amazon.EC2.Model.TagSpecification(); Tagspec.ResourceType = ResourceType.Instance; //TagSpecification(){ResourceType.Instance}; //Tagspec.Tags.OfType<Instance>; Tagspec.Tags = TagList; launchRequest.TagSpecifications.Add(Tagspec); //ec2.RunInstances(launchRequest); launchResponse = ec2.RunInstances(launchRequest); return("Done"); } catch (Exception ex) { return(ex.Message + "\n" + ex.StackTrace); } }