Ejemplo n.º 1
0
        internal List <InternetGateway> getInternetGW(string GWId)
        {
            DescribeInternetGatewaysRequest req = new DescribeInternetGatewaysRequest();

            req.Filters.Add(new Filter()
            {
                Name   = "internet-gateway-id",
                Values = new List <string>()
                {
                    GWId
                }
            }); return(_service.DescribeInternetGateways(req).InternetGateways);
        }
Ejemplo n.º 2
0
        public bool load_internet_gateway_id()
        {
            write_log(region + " のインターネットゲートウェイを確認しています。");
            try
            {
                var client    = get_client();
                var query_req = new DescribeInternetGatewaysRequest();
                query_req.Filters.Add(new Filter()
                {
                    Name = "tag-value", Values = new List <string>()
                    {
                        Helper.build_name(setting_, "igw")
                    }
                });
                var query_res = client.DescribeInternetGateways(query_req);
                if (query_res.InternetGateways.Count != 0)
                {
                    foreach (var row in query_res.InternetGateways[0].Attachments)
                    {
                        if (row.VpcId.Equals(vpc_id))
                        {
                            internet_gateway_id = query_res.InternetGateways[0].InternetGatewayId;
                            write_log(vpc_id + " のインターネットゲートウェイは " + internet_gateway_id + " です");
                            return(true);
                        }
                    }
                }

                write_log(region + " にインターネットゲートウェイを作成しています。");
                var create_req = new CreateInternetGatewayRequest();
                var create_res = client.CreateInternetGateway(create_req);

                set_name_tag(client, create_res.InternetGateway.InternetGatewayId, Helper.build_name(setting_, "igw"));
                internet_gateway_id = create_res.InternetGateway.InternetGatewayId;
                write_log("インターネットゲートウェイ " + internet_gateway_id + " を作成しました。");


                write_log("VPC " + vpc_id + " に " + internet_gateway_id + " を関連付けます。");
                var attach_req = new AttachInternetGatewayRequest();
                attach_req.InternetGatewayId = internet_gateway_id;
                attach_req.VpcId             = vpc_id;
                client.AttachInternetGateway(attach_req);
            }
            catch (Exception ex)
            {
                write_log("ERROR: " + ex.ToString());
                return(false);
            }
            return(true);
        }
Ejemplo n.º 3
0
        public override void Invoke(AWSCredentials creds, RegionEndpoint region, int maxItems)
        {
            AmazonEC2Config config = new AmazonEC2Config();

            config.RegionEndpoint = region;
            ConfigureClient(config);
            AmazonEC2Client client = new AmazonEC2Client(creds, config);

            DescribeInternetGatewaysResponse resp = new DescribeInternetGatewaysResponse();
            DescribeInternetGatewaysRequest  req  = new DescribeInternetGatewaysRequest
            {
            };

            resp = client.DescribeInternetGateways(req);
            CheckError(resp.HttpStatusCode, "200");

            foreach (var obj in resp.InternetGateways)
            {
                AddObject(obj);
            }
        }
Ejemplo n.º 4
0
        public async Task detachAndDeleteInternetGatewaysAsync(string vpcid)
        {
            var request = new DescribeInternetGatewaysRequest();
            var filter  = new Filter {
                Name = "attachment.vpc-id"
            };

            filter.Values.Add(vpcid);
            request.Filters.Add(filter);

            var response = await _ec2Client.DescribeInternetGatewaysAsync(request);

            foreach (var gateway in response.InternetGateways)
            {
                await _ec2Client.DetachInternetGatewayAsync(new DetachInternetGatewayRequest {
                    InternetGatewayId = gateway.InternetGatewayId, VpcId = vpcid
                });

                await _ec2Client.DeleteInternetGatewayAsync(new DeleteInternetGatewayRequest {
                    InternetGatewayId = gateway.InternetGatewayId
                });
            }
        }
Ejemplo n.º 5
0
        public bool delete_vpc_all(string region_txt)
        {
            try
            {
                var client = get_client();
                write_log(region + " の VPC を確認しています。");
                var query_req = new DescribeVpcsRequest();
                query_req.Filters.Add(new Filter()
                {
                    Name = "tag-value", Values = new List <string>()
                    {
                        Helper.build_name(setting_, "vpc")
                    }
                });
                var query_res = client.DescribeVpcs(query_req);
                if (query_res.Vpcs.Count == 0)
                {
                    write_log(region + " の VPC が存在しませんでした。");
                    return(true);
                }
                vpc_id = query_res.Vpcs[0].VpcId;

                var query_instance_req = new DescribeInstancesRequest();
                var query_instance_res = client.DescribeInstances(query_instance_req);

                bool flg = true;
                foreach (var row in query_instance_res.Reservations)
                {
                    foreach (var row2 in row.Instances)
                    {
                        if (row2.State != null && row2.State.Code != 48 &&
                            row2.VpcId != null && row2.VpcId.Equals(vpc_id))
                        {
                            flg = false;
                            break;
                        }
                    }
                    if (flg == false)
                    {
                        break;
                    }
                }
                if (flg == false)
                {
                    write_log(region + " にはインスタンスが存在する為、VPC を削除できません。");
                    return(false);
                }

                write_log(region + " のセキュリティグループを確認しています。");

                var query_sg_req = new DescribeSecurityGroupsRequest();
                query_sg_req.Filters.Add(new Filter()
                {
                    Name = "tag-value", Values = new List <string>()
                    {
                        Helper.build_name(setting_, "sg")
                    }
                });
                var query_sg_res = client.DescribeSecurityGroups(query_sg_req);
                foreach (var row in query_sg_res.SecurityGroups)
                {
                    write_log("セキュリティグループ " + row.GroupId + " を削除しています。");
                    var delete_sg_req = new DeleteSecurityGroupRequest();
                    delete_sg_req.GroupId = row.GroupId;
                    client.DeleteSecurityGroup(delete_sg_req);
                }

                using (FileStream fs = new FileStream(region_txt, FileMode.Open, FileAccess.Read))
                {
                    IList <string> list = new List <string>(subnet_ids.Keys);
                    foreach (string tag in list)
                    {
                        if (Helper.check_az(fs, region, tag, 0) == false)
                        {
                            continue;
                        }
                        write_log(region + tag + " のサブネットを確認しています。");
                        var query_sub_req = new DescribeSubnetsRequest();
                        query_sub_req.Filters.Add(new Filter()
                        {
                            Name = "tag-value", Values = new List <string>()
                            {
                                Helper.build_name(setting_, "subnet_" + tag)
                            }
                        });
                        var query_sub_res = client.DescribeSubnets(query_sub_req);
                        foreach (var row in query_sub_res.Subnets)
                        {
                            write_log("サブネット " + row.SubnetId + " を削除しています。");
                            var delete_sub_req = new DeleteSubnetRequest();
                            delete_sub_req.SubnetId = row.SubnetId;
                            client.DeleteSubnet(delete_sub_req);
                        }
                    }
                }

                write_log(region + " のインターネットゲートウェイを確認しています。");

                var query_igw_req = new DescribeInternetGatewaysRequest();
                query_igw_req.Filters.Add(new Filter()
                {
                    Name = "tag-value", Values = new List <string>()
                    {
                        Helper.build_name(setting_, "igw")
                    }
                });
                var query_igw_res = client.DescribeInternetGateways(query_igw_req);
                foreach (var row in query_igw_res.InternetGateways)
                {
                    var detach_igw_req = new DetachInternetGatewayRequest();
                    detach_igw_req.InternetGatewayId = row.InternetGatewayId;
                    detach_igw_req.VpcId             = vpc_id;
                    client.DetachInternetGateway(detach_igw_req);

                    var delete_igw_req = new DeleteInternetGatewayRequest();
                    delete_igw_req.InternetGatewayId = row.InternetGatewayId;
                    client.DeleteInternetGateway(delete_igw_req);
                }

                write_log("VPC " + vpc_id + " を削除しています。");

                var delete_vpc_req = new DeleteVpcRequest();
                delete_vpc_req.VpcId = vpc_id;
                client.DeleteVpc(delete_vpc_req);
            }
            catch (Exception ex)
            {
                write_log("ERROR: " + ex.ToString());
                return(false);
            }
            return(true);
        }