コード例 #1
0
        public override void Invoke(AWSCredentials creds, RegionEndpoint region, int maxItems)
        {
            AmazonAPIGatewayConfig config = new AmazonAPIGatewayConfig();

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

            GetRestApisResponse resp = new GetRestApisResponse();

            do
            {
                GetRestApisRequest req = new GetRestApisRequest
                {
                    Position = resp.Position
                    ,
                    Limit = maxItems
                };

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

                foreach (var obj in resp.Items)
                {
                    AddObject(obj);
                }
            }while (!string.IsNullOrEmpty(resp.Position));
        }
        internal static async Task <GetRestApisResponse> GetRestApisAsync(this AmazonAPIGatewayClient client, string apiName)
        {
            var listApiRequest  = new GetRestApisRequest();
            var listApiResponse = await client.GetRestApisAsync(listApiRequest).ConfigureAwait(false);

            listApiResponse.Items.RemoveAll(a => string.Equals(apiName, a.Name) == false);
            return(listApiResponse);
        }
コード例 #3
0
        public async Task <GetRestApisResponse> GetApiDetails(GetRestApisRequest request)
        {
            using (var amazonAPIGatewayClient = new AmazonAPIGatewayClient(awsCredentials, RegionEndpoint.GetBySystemName(Region)))
            {
                var response = await amazonAPIGatewayClient.GetRestApisAsync(request);

                return(response);
            }
        }
コード例 #4
0
ファイル: APIGateway.cs プロジェクト: jameshulse/aws-sdk-net
        private IEnumerable <RestApi> GetRestApis()
        {
            var request = new GetRestApisRequest {
                Limit = 1
            };

            do
            {
                var response = Client.GetRestApis(request);
                request.Position = response.Position;
                foreach (var r in response.Items)
                {
                    yield return(r);
                }
            } while (!string.IsNullOrEmpty(request.Position));
        }
コード例 #5
0
        public async Task DeleteOrphanLogsAsync(bool dryRun, string awsProfile, string awsRegion)
        {
            Console.WriteLine();

            // initialize AWS profile
            await InitializeAwsProfile(awsProfile, awsRegion : awsRegion);

            var logsClient = new AmazonCloudWatchLogsClient(AWSConfigs.RegionEndpoint);

            // delete orphaned logs
            var totalLogGroups    = 0;
            var activeLogGroups   = 0;
            var orphanedLogGroups = 0;
            var skippedLogGroups  = 0;

            await DeleteOrphanLambdaLogsAsync();
            await DeleteOrphanApiGatewayLogs();
            await DeleteOrphanApiGatewayV2Logs();

            if ((orphanedLogGroups > 0) || (skippedLogGroups > 0))
            {
                Console.WriteLine();
            }
            Console.WriteLine($"Found {totalLogGroups:N0} log groups. Active {activeLogGroups:N0}. Orphaned {orphanedLogGroups:N0}. Skipped {skippedLogGroups:N0}.");

            // local functions
            async Task DeleteOrphanLambdaLogsAsync()
            {
                // list all lambda functions
                var lambdaClient        = new AmazonLambdaClient(AWSConfigs.RegionEndpoint);
                var request             = new ListFunctionsRequest {
                };
                var lambdaLogGroupNames = new HashSet <string>();

                do
                {
                    var response = await lambdaClient.ListFunctionsAsync(request);

                    foreach (var function in response.Functions)
                    {
                        lambdaLogGroupNames.Add($"/aws/lambda/{function.FunctionName}");
                    }
                    request.Marker = response.NextMarker;
                } while(request.Marker != null);

                // list all log groups for lambda functions
                await DeleteOrphanCloudWatchLogs(
                    "/aws/lambda/",
                    logGroupName => lambdaLogGroupNames.Contains(logGroupName),
                    logGroupName => Regex.IsMatch(logGroupName, @"^\/aws\/lambda\/[a-zA-Z0-9\-_]+$")
                    );
            }

            async Task DeleteOrphanApiGatewayLogs()
            {
                // list all API Gateway V1 instances
                var apiGatewayClient     = new AmazonAPIGatewayClient(AWSConfigs.RegionEndpoint);
                var request              = new GetRestApisRequest {
                };
                var apiGatewayGroupNames = new List <string>();

                do
                {
                    var response = await apiGatewayClient.GetRestApisAsync(request);

                    apiGatewayGroupNames.AddRange(response.Items.Select(item => $"API-Gateway-Execution-Logs_{item.Id}/"));
                    request.Position = response.Position;
                } while(request.Position != null);

                // list all log groups for API Gateway instances
                await DeleteOrphanCloudWatchLogs(
                    "API-Gateway-Execution-Logs_",
                    logGroupName => apiGatewayGroupNames.Any(apiGatewayGroupName => logGroupName.StartsWith(apiGatewayGroupName, StringComparison.Ordinal)),
                    logGroupName => Regex.IsMatch(logGroupName, @"^API-Gateway-Execution-Logs_[a-zA-Z0-9]+/.+$")
                    );
            }

            async Task DeleteOrphanApiGatewayV2Logs()
            {
                // list all API Gateway V2 instances
                var apiGatewayV2Client   = new AmazonApiGatewayV2Client(AWSConfigs.RegionEndpoint);
                var request              = new GetApisRequest {
                };
                var apiGatewayGroupNames = new List <string>();

                do
                {
                    var response = await apiGatewayV2Client.GetApisAsync(request);

                    apiGatewayGroupNames.AddRange(response.Items.Select(item => $"/aws/apigateway/{item.ApiId}/"));
                    request.NextToken = response.NextToken;
                } while(request.NextToken != null);

                // list all log groups for API Gateway instances
                await DeleteOrphanCloudWatchLogs(
                    "/aws/apigateway/",
                    logGroupName => (logGroupName == "/aws/apigateway/welcome") || apiGatewayGroupNames.Any(apiGatewayGroupName => logGroupName.StartsWith(apiGatewayGroupName, StringComparison.Ordinal)),
                    logGroupName => Regex.IsMatch(logGroupName, @"^/aws/apigateway/[a-zA-Z0-9]+/.+$")
                    );
            }

            async Task DeleteOrphanCloudWatchLogs(string logGroupPrefix, Func <string, bool> isActiveLogGroup, Func <string, bool> isValidLogGroup)
            {
                var describeLogGroupsRequest = new DescribeLogGroupsRequest {
                    LogGroupNamePrefix = logGroupPrefix
                };

                do
                {
                    var describeLogGroupsResponse = await logsClient.DescribeLogGroupsAsync(describeLogGroupsRequest);

                    totalLogGroups += describeLogGroupsResponse.LogGroups.Count;
                    foreach (var logGroup in describeLogGroupsResponse.LogGroups)
                    {
                        if (isActiveLogGroup(logGroup.LogGroupName))
                        {
                            // nothing to do
                            ++activeLogGroups;
                        }
                        else if (isValidLogGroup(logGroup.LogGroupName))
                        {
                            // attempt to delete log group
                            if (dryRun)
                            {
                                Console.WriteLine($"* deleted '{logGroup.LogGroupName}' (skipped)");
                                ++orphanedLogGroups;
                            }
                            else
                            {
                                try {
                                    await logsClient.DeleteLogGroupAsync(new DeleteLogGroupRequest {
                                        LogGroupName = logGroup.LogGroupName
                                    });

                                    Console.WriteLine($"* deleted '{logGroup.LogGroupName}'");
                                    ++orphanedLogGroups;
                                } catch {
                                    LogError($"could not delete '{logGroup.LogGroupName}'");
                                    ++skippedLogGroups;
                                }
                            }
                        }
                        else
                        {
                            // log group has an invalid name structure; skip it
                            Console.WriteLine($"SKIPPED '{logGroup.LogGroupName}'");
                            ++skippedLogGroups;
                        }
                    }
                    describeLogGroupsRequest.NextToken = describeLogGroupsResponse.NextToken;
                } while(describeLogGroupsRequest.NextToken != null);
            }
        }