DescribeSecurityGroups() private méthode

private DescribeSecurityGroups ( ) : DescribeSecurityGroupsResponse
Résultat DescribeSecurityGroupsResponse
Exemple #1
0
        private static void ManageSecurityGroups()
        {
            IAmazonEC2 ec2        = new Amazon.EC2.AmazonEC2Client();
            var        sgResponse = ec2.DescribeSecurityGroups();

            string        ipRange = "22.22.22.22/0";
            List <string> ranges  = new List <string>()
            {
                ipRange
            };

            var ipPermission = new IpPermission();

            ipPermission.IpProtocol = "tcp";
            ipPermission.FromPort   = 3333;
            ipPermission.ToPort     = 3333;
            ipPermission.IpRanges   = ranges;

            var ingressRequest = new AuthorizeSecurityGroupIngressRequest();

            ingressRequest.IpPermissions.Add(ipPermission);
            var revokeRequest = new RevokeSecurityGroupIngressRequest();

            revokeRequest.IpPermissions.Add(ipPermission);
            foreach (var sg in sgResponse.SecurityGroups)
            {
                try
                {
                    if (new Random().Next(2) == 1)
                    {
                        ingressRequest.GroupId = sg.GroupId;
                        var ingressResponse = ec2.AuthorizeSecurityGroupIngress(ingressRequest);
                    }
                    else
                    {
                        revokeRequest.GroupId = sg.GroupId;
                        ec2.RevokeSecurityGroupIngress(revokeRequest);
                    }
                    //Console.WriteLine("New RDP rule for: " + ipRange);
                }
                catch (AmazonEC2Exception ex)
                {
                    // Check the ErrorCode to see if the rule already exists.
                    if ("InvalidPermission.Duplicate" == ex.ErrorCode)
                    {
                        //Console.WriteLine("An RDP rule for: {0} already exists.", ipRange);
                    }
                    else
                    {
                        // The exception was thrown for another reason, so re-throw the exception.
                        //throw;
                    }
                }
            }
        }
        /// <summary>
        /// Load security groups to view model with AWS data based on region selected and EC2 classic/vpc
        /// </summary>
        private void LoadSecurityGroups(AmazonEC2Client ec2Client)
        {
            try
            {
                DescribeSecurityGroupsRequest sgreq = new DescribeSecurityGroupsRequest();
                DescribeSecurityGroupsResponse sgresp = ec2Client.DescribeSecurityGroups(sgreq);
                Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                {
                    vm.SecurityGroups.Clear();
                }));

                foreach (SecurityGroup sg in sgresp.DescribeSecurityGroupsResult.SecurityGroup)
                {
                    if (vm.IsVpc)
                    {
                        if (sg.VpcId != null && vm.SelectedVpc != null)
                        {
                            if (sg.VpcId == vm.SelectedVpc.VPC.VpcId)
                            {
                                Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                                {
                                    vm.SecurityGroups.Add(new Models.ConsoleSG() { SecurityGroup = sg, DisplayName = string.Concat(sg.GroupName, " ( VPC: ", sg.VpcId, " )") });
                                }));
                            }
                        }
                    }
                    else
                    {
                        if (!(sg.VpcId != null && sg.VpcId != string.Empty && !vm.IsVpc))
                        {
                            //vm.SecurityGroups.Add(new Models.LcSecurityGroup() { SecurityGroup = sg, DisplayName = sg.GroupName });
                            Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                            {
                                vm.SecurityGroups.Add(new Models.ConsoleSG() { SecurityGroup = sg, DisplayName = sg.GroupName });
                            }));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.LogEntry(ex.Message);
                LogManager.LogEntry(ex.StackTrace);
                throw new DataLoadingException("Error occurred loading security groups for region and environment type");
            }
        }