|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs" |
| 7 | + "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types" |
| 8 | + "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" |
| 9 | + "github.com/turbot/steampipe-plugin-sdk/v5/plugin" |
| 10 | + "github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform" |
| 11 | +) |
| 12 | + |
| 13 | +func tableAwsCloudWatchLogDeliveryDestination(_ context.Context) *plugin.Table { |
| 14 | + return &plugin.Table{ |
| 15 | + Name: "aws_cloudwatch_log_delivery_destination", |
| 16 | + Description: "AWS CloudWatch Log Delivery Destination represents a destination configuration for vended log delivery.", |
| 17 | + Get: &plugin.GetConfig{ |
| 18 | + KeyColumns: plugin.SingleColumn("name"), |
| 19 | + Hydrate: getCloudWatchLogDeliveryDestination, |
| 20 | + Tags: map[string]string{"service": "logs", "action": "GetDeliveryDestination"}, |
| 21 | + }, |
| 22 | + List: &plugin.ListConfig{ |
| 23 | + Hydrate: listCloudWatchLogDeliveryDestinations, |
| 24 | + Tags: map[string]string{"service": "logs", "action": "ListDeliveryDestinations"}, |
| 25 | + }, |
| 26 | + GetMatrixItemFunc: CloudWatchRegionsMatrix, |
| 27 | + Columns: []*plugin.Column{ |
| 28 | + { |
| 29 | + Name: "name", |
| 30 | + Description: "The name of the delivery destination.", |
| 31 | + Type: proto.ColumnType_STRING, |
| 32 | + }, |
| 33 | + { |
| 34 | + Name: "arn", |
| 35 | + Description: "The Amazon Resource Name (ARN) that uniquely identifies this delivery destination.", |
| 36 | + Type: proto.ColumnType_STRING, |
| 37 | + }, |
| 38 | + { |
| 39 | + Name: "destination_resource_arn", |
| 40 | + Description: "The ARN of the Amazon Web Services destination that this delivery destination represents.", |
| 41 | + Type: proto.ColumnType_STRING, |
| 42 | + Transform: transform.FromField("DeliveryDestinationConfiguration.DestinationResourceArn"), |
| 43 | + }, |
| 44 | + { |
| 45 | + Name: "delivery_destination_type", |
| 46 | + Description: "Displays whether this delivery destination is CloudWatch Logs, Amazon S3, or Firehose.", |
| 47 | + Type: proto.ColumnType_STRING, |
| 48 | + }, |
| 49 | + { |
| 50 | + Name: "output_format", |
| 51 | + Description: "The format of the logs that are sent to this delivery destination.", |
| 52 | + Type: proto.ColumnType_STRING, |
| 53 | + }, |
| 54 | + { |
| 55 | + Name: "policy", |
| 56 | + Description: "The policy of the delivery destination.", |
| 57 | + Type: proto.ColumnType_JSON, |
| 58 | + Hydrate: getCloudWatchLogDeliveryDestinationPolicy, |
| 59 | + Transform: transform.FromValue(), |
| 60 | + }, |
| 61 | + |
| 62 | + // Standard columns |
| 63 | + { |
| 64 | + Name: "title", |
| 65 | + Description: resourceInterfaceDescription("title"), |
| 66 | + Type: proto.ColumnType_STRING, |
| 67 | + Transform: transform.FromField("Name"), |
| 68 | + }, |
| 69 | + { |
| 70 | + Name: "tags", |
| 71 | + Description: resourceInterfaceDescription("tags"), |
| 72 | + Type: proto.ColumnType_JSON, |
| 73 | + }, |
| 74 | + { |
| 75 | + Name: "akas", |
| 76 | + Description: resourceInterfaceDescription("akas"), |
| 77 | + Type: proto.ColumnType_JSON, |
| 78 | + Transform: transform.FromField("Arn").Transform(transform.EnsureStringArray), |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +//// LIST FUNCTION |
| 85 | + |
| 86 | +func listCloudWatchLogDeliveryDestinations(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { |
| 87 | + // Create session |
| 88 | + svc, err := CloudWatchLogsClient(ctx, d) |
| 89 | + if err != nil { |
| 90 | + plugin.Logger(ctx).Error("aws_cloudwatch_log_delivery_destination.listCloudWatchLogDeliveryDestinations", "connection_error", err) |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + |
| 94 | + // Limiting the results |
| 95 | + maxLimit := int32(50) |
| 96 | + if d.QueryContext.Limit != nil { |
| 97 | + limit := int32(*d.QueryContext.Limit) |
| 98 | + if limit < maxLimit { |
| 99 | + maxLimit = limit |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + input := &cloudwatchlogs.DescribeDeliveryDestinationsInput{ |
| 104 | + Limit: &maxLimit, |
| 105 | + } |
| 106 | + |
| 107 | + paginator := cloudwatchlogs.NewDescribeDeliveryDestinationsPaginator(svc, input, func(o *cloudwatchlogs.DescribeDeliveryDestinationsPaginatorOptions) { |
| 108 | + o.Limit = maxLimit |
| 109 | + o.StopOnDuplicateToken = true |
| 110 | + }) |
| 111 | + |
| 112 | + for paginator.HasMorePages() { |
| 113 | + // apply rate limiting |
| 114 | + d.WaitForListRateLimit(ctx) |
| 115 | + |
| 116 | + output, err := paginator.NextPage(ctx) |
| 117 | + if err != nil { |
| 118 | + plugin.Logger(ctx).Error("aws_cloudwatch_log_delivery_destination.listCloudWatchLogDeliveryDestinations", "api_error", err) |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + |
| 122 | + for _, destination := range output.DeliveryDestinations { |
| 123 | + d.StreamListItem(ctx, destination) |
| 124 | + |
| 125 | + // Context may get cancelled due to manual cancellation or if the limit has been reached |
| 126 | + if d.RowsRemaining(ctx) == 0 { |
| 127 | + return nil, nil |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return nil, nil |
| 133 | +} |
| 134 | + |
| 135 | +//// HYDRATE FUNCTIONS |
| 136 | + |
| 137 | +func getCloudWatchLogDeliveryDestination(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { |
| 138 | + destinationName := d.EqualsQuals["name"].GetStringValue() |
| 139 | + |
| 140 | + // Create session |
| 141 | + svc, err := CloudWatchLogsClient(ctx, d) |
| 142 | + if err != nil { |
| 143 | + plugin.Logger(ctx).Error("aws_cloudwatch_log_delivery_destination.getCloudWatchLogDeliveryDestination", "connection_error", err) |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + |
| 147 | + params := &cloudwatchlogs.GetDeliveryDestinationInput{ |
| 148 | + Name: &destinationName, |
| 149 | + } |
| 150 | + |
| 151 | + op, err := svc.GetDeliveryDestination(ctx, params) |
| 152 | + if err != nil { |
| 153 | + plugin.Logger(ctx).Error("aws_cloudwatch_log_delivery_destination.getCloudWatchLogDeliveryDestination", "api_error", err) |
| 154 | + return nil, err |
| 155 | + } |
| 156 | + |
| 157 | + if op.DeliveryDestination != nil { |
| 158 | + return *op.DeliveryDestination, nil |
| 159 | + } |
| 160 | + |
| 161 | + return nil, nil |
| 162 | +} |
| 163 | + |
| 164 | +func getCloudWatchLogDeliveryDestinationPolicy(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { |
| 165 | + item := h.Item.(types.DeliveryDestination) |
| 166 | + |
| 167 | + // Create session |
| 168 | + svc, err := CloudWatchLogsClient(ctx, d) |
| 169 | + if err != nil { |
| 170 | + plugin.Logger(ctx).Error("aws_cloudwatch_log_delivery_destination.getCloudWatchLogDeliveryDestination", "connection_error", err) |
| 171 | + return nil, err |
| 172 | + } |
| 173 | + |
| 174 | + params := &cloudwatchlogs.GetDeliveryDestinationPolicyInput{ |
| 175 | + DeliveryDestinationName: item.Name, |
| 176 | + } |
| 177 | + |
| 178 | + op, err := svc.GetDeliveryDestinationPolicy(ctx, params) |
| 179 | + if err != nil { |
| 180 | + plugin.Logger(ctx).Error("aws_cloudwatch_log_delivery_destination.getCloudWatchLogDeliveryDestination", "api_error", err) |
| 181 | + return nil, err |
| 182 | + } |
| 183 | + |
| 184 | + if op.Policy != nil { |
| 185 | + return op.Policy, nil |
| 186 | + } |
| 187 | + |
| 188 | + return nil, nil |
| 189 | +} |
0 commit comments