示例#1
0
        public async Task <IActionResult> Index()
        {
            if (_context.VPCs.ToList().Count == 0)
            {
                return(RedirectToAction("", "Home", ""));
            }
            ChallengeNetworkParentViewModel model = new ChallengeNetworkParentViewModel
            {
                RetrievedSubnets = await _context.Subnets.ToListAsync(),
                RetrievedRoutes  = await _context.Routes.ToListAsync()
            };

            return(View(model));
        }
示例#2
0
        public async Task <IActionResult> Index(string action, string subnetID)
        {
            if (action.Equals("Delete") && !String.IsNullOrEmpty(subnetID))
            {
                Subnet Deletesubnet = await _context.Subnets.FindAsync(Int32.Parse(subnetID));

                if (Deletesubnet == null)
                {
                    TempData["Result"] = "Invaild Subnet!";
                    ChallengeNetworkParentViewModel model = new ChallengeNetworkParentViewModel
                    {
                        RetrievedSubnets = await _context.Subnets.ToListAsync(),
                        RetrievedRoutes  = await _context.Routes.ToListAsync()
                    };
                    return(View(model));
                }
                else if (Deletesubnet.editable == false)
                {
                    TempData["Result"] = "You cannot delete a default subnet!";
                    ChallengeNetworkParentViewModel model = new ChallengeNetworkParentViewModel
                    {
                        RetrievedSubnets = await _context.Subnets.ToListAsync(),
                        RetrievedRoutes  = await _context.Routes.ToListAsync()
                    };
                    return(View(model));
                }
                else
                {
                    DescribeSubnetsResponse response = await EC2Client.DescribeSubnetsAsync(new DescribeSubnetsRequest
                    {
                        Filters = new List <Amazon.EC2.Model.Filter>
                        {
                            new Filter("vpc-id", new List <string>
                            {
                                Deletesubnet.LinkedVPC.AWSVPCReference
                            })
                        }
                    });

                    Boolean flag = false;
                    for (int i = 0; i < response.Subnets.Count; i++)
                    {
                        Amazon.EC2.Model.Subnet subnet = response.Subnets[i];
                        String retrievedID             = subnet.SubnetId;
                        if (Deletesubnet.AWSVPCSubnetReference.Equals(retrievedID))
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag == false)
                    {
                        ViewData["Result"] = "Subnet not found! The subnet may have been modified by another user";
                        ChallengeNetworkParentViewModel model = new ChallengeNetworkParentViewModel
                        {
                            RetrievedSubnets = await _context.Subnets.ToListAsync(),
                            RetrievedRoutes  = await _context.Routes.ToListAsync()
                        };
                        return(View(model));
                    }
                    else
                    {
                        try
                        {
                            List <RouteTable> RTs = await _context.RouteTables.ToListAsync();

                            DeleteSubnetRequest  request     = new DeleteSubnetRequest(Deletesubnet.AWSVPCSubnetReference);
                            DeleteSubnetResponse responseEC2 = await EC2Client.DeleteSubnetAsync(request);

                            if (responseEC2.HttpStatusCode == HttpStatusCode.OK)
                            {
                                _context.Subnets.Remove(Deletesubnet);
                                await _context.SaveChangesAsync();

                                TempData["Result"] = "Successfully Deleted!";
                                ChallengeNetworkParentViewModel model = new ChallengeNetworkParentViewModel
                                {
                                    RetrievedSubnets = await _context.Subnets.ToListAsync(),
                                    RetrievedRoutes  = await _context.Routes.ToListAsync()
                                };
                                return(View(model));
                            }
                            else
                            {
                                TempData["Result"] = "Failed!";
                                ChallengeNetworkParentViewModel model = new ChallengeNetworkParentViewModel
                                {
                                    RetrievedSubnets = await _context.Subnets.ToListAsync(),
                                    RetrievedRoutes  = await _context.Routes.ToListAsync()
                                };
                                return(View(model));
                            }
                        }
                        catch (AmazonEC2Exception e)
                        {
                            TempData["Result"] = e.Message;
                            ChallengeNetworkParentViewModel model = new ChallengeNetworkParentViewModel
                            {
                                RetrievedSubnets = await _context.Subnets.ToListAsync(),
                                RetrievedRoutes  = await _context.Routes.ToListAsync()
                            };
                            return(View(model));
                        }
                    }
                }
            }
            else if (action.Equals("Modify") && !String.IsNullOrEmpty(subnetID))
            {
                var Modifysubnet = await _context.Subnets.FindAsync(Int32.Parse(subnetID));

                if (Modifysubnet.editable == false)
                {
                    ViewData["Result"] = "You cannot modify a default subnet!";
                    ChallengeNetworkParentViewModel model = new ChallengeNetworkParentViewModel
                    {
                        RetrievedSubnets = await _context.Subnets.ToListAsync(),
                        RetrievedRoutes  = await _context.Routes.ToListAsync()
                    };
                    return(View(model));
                }
                return(RedirectToAction("Edit", new { id = subnetID }));
            }
            else
            {
                ChallengeNetworkParentViewModel model = new ChallengeNetworkParentViewModel
                {
                    RetrievedSubnets = await _context.Subnets.ToListAsync(),
                    RetrievedRoutes  = await _context.Routes.ToListAsync()
                };
                return(View(model));
            }
        }