|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 7 | + "github.com/aws/aws-sdk-go-v2/service/batch" |
| 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 tableAwsBatchQueue(_ context.Context) *plugin.Table { |
| 14 | + return &plugin.Table{ |
| 15 | + Name: "aws_batch_queue", |
| 16 | + Description: "AWS Batch Queue", |
| 17 | + Get: &plugin.GetConfig{ |
| 18 | + KeyColumns: plugin.SingleColumn("job_queue_name"), |
| 19 | + IgnoreConfig: &plugin.IgnoreConfig{ |
| 20 | + ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"JobQueueNotFoundException"}), |
| 21 | + }, |
| 22 | + Hydrate: getBatchQueue, |
| 23 | + Tags: map[string]string{"service": "batch", "action": "DescribeJobQueues"}, |
| 24 | + }, |
| 25 | + List: &plugin.ListConfig{ |
| 26 | + Hydrate: listBatchQueues, |
| 27 | + Tags: map[string]string{"service": "batch", "action": "DescribeJobQueues"}, |
| 28 | + }, |
| 29 | + GetMatrixItemFunc: SupportedRegionMatrix(AWS_BATCH_SERVICE_ID), |
| 30 | + Columns: awsRegionalColumns([]*plugin.Column{ |
| 31 | + { |
| 32 | + Name: "job_queue_name", |
| 33 | + Description: "The name of the job queue", |
| 34 | + Type: proto.ColumnType_STRING, |
| 35 | + }, |
| 36 | + { |
| 37 | + Name: "arn", |
| 38 | + Description: "The Amazon Resource Name (ARN) of the job queue", |
| 39 | + Type: proto.ColumnType_STRING, |
| 40 | + Transform: transform.FromField("JobQueueArn"), |
| 41 | + }, |
| 42 | + { |
| 43 | + Name: "state", |
| 44 | + Description: "The state of the job queue (ENABLED or DISABLED)", |
| 45 | + Type: proto.ColumnType_STRING, |
| 46 | + }, |
| 47 | + { |
| 48 | + Name: "status", |
| 49 | + Description: "The status of the job queue (CREATING, UPDATING, DELETING, or DELETED)", |
| 50 | + Type: proto.ColumnType_STRING, |
| 51 | + }, |
| 52 | + { |
| 53 | + Name: "priority", |
| 54 | + Description: "The priority of the job queue", |
| 55 | + Type: proto.ColumnType_INT, |
| 56 | + }, |
| 57 | + { |
| 58 | + Name: "compute_environment_order", |
| 59 | + Description: "The compute environments that are attached to the job queue and the order in which job placement is preferred", |
| 60 | + Type: proto.ColumnType_JSON, |
| 61 | + }, |
| 62 | + { |
| 63 | + Name: "scheduling_policy_arn", |
| 64 | + Description: "The ARN of the scheduling policy", |
| 65 | + Type: proto.ColumnType_STRING, |
| 66 | + }, |
| 67 | + { |
| 68 | + Name: "status_reason", |
| 69 | + Description: "A short, human-readable string to provide additional details about the current status of the job queue", |
| 70 | + Type: proto.ColumnType_STRING, |
| 71 | + }, |
| 72 | + |
| 73 | + // Standard columns for all tables |
| 74 | + { |
| 75 | + Name: "tags", |
| 76 | + Description: "The tags assigned to the job queue", |
| 77 | + Type: proto.ColumnType_JSON, |
| 78 | + }, |
| 79 | + { |
| 80 | + Name: "title", |
| 81 | + Description: resourceInterfaceDescription("title"), |
| 82 | + Type: proto.ColumnType_STRING, |
| 83 | + Transform: transform.FromField("JobQueueName"), |
| 84 | + }, |
| 85 | + { |
| 86 | + Name: "akas", |
| 87 | + Description: resourceInterfaceDescription("akas"), |
| 88 | + Type: proto.ColumnType_JSON, |
| 89 | + Transform: transform.FromField("JobQueueArn").Transform(transform.EnsureStringArray), |
| 90 | + }, |
| 91 | + }), |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func listBatchQueues(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { |
| 96 | + // Create service client |
| 97 | + svc, err := BatchClient(ctx, d) |
| 98 | + if err != nil { |
| 99 | + plugin.Logger(ctx).Error("aws_batch_queue.listBatchQueues", "client_error", err) |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + |
| 103 | + // Unsupported region check |
| 104 | + if svc == nil { |
| 105 | + return nil, nil |
| 106 | + } |
| 107 | + |
| 108 | + // Limiting the results |
| 109 | + maxLimit := int32(100) |
| 110 | + if d.QueryContext.Limit != nil { |
| 111 | + limit := int32(*d.QueryContext.Limit) |
| 112 | + if limit < maxLimit { |
| 113 | + maxLimit = limit |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + input := &batch.DescribeJobQueuesInput{ |
| 118 | + MaxResults: aws.Int32(maxLimit), |
| 119 | + } |
| 120 | + |
| 121 | + paginator := batch.NewDescribeJobQueuesPaginator(svc, input) |
| 122 | + for paginator.HasMorePages() { |
| 123 | + output, err := paginator.NextPage(ctx) |
| 124 | + if err != nil { |
| 125 | + plugin.Logger(ctx).Error("aws_batch_queue.listBatchQueues", "api_error", err) |
| 126 | + return nil, err |
| 127 | + } |
| 128 | + |
| 129 | + for _, queue := range output.JobQueues { |
| 130 | + d.StreamListItem(ctx, queue) |
| 131 | + |
| 132 | + // Context can be cancelled due to manual cancellation or the limit has been hit |
| 133 | + if d.RowsRemaining(ctx) == 0 { |
| 134 | + return nil, nil |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return nil, nil |
| 140 | +} |
| 141 | + |
| 142 | +func getBatchQueue(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { |
| 143 | + queueName := d.EqualsQualString("job_queue_name") |
| 144 | + if queueName == "" { |
| 145 | + return nil, nil |
| 146 | + } |
| 147 | + |
| 148 | + // Create service client |
| 149 | + svc, err := BatchClient(ctx, d) |
| 150 | + if err != nil { |
| 151 | + plugin.Logger(ctx).Error("aws_batch_queue.getBatchQueue", "client_error", err) |
| 152 | + return nil, err |
| 153 | + } |
| 154 | + |
| 155 | + // Unsupported region check |
| 156 | + if svc == nil { |
| 157 | + return nil, nil |
| 158 | + } |
| 159 | + |
| 160 | + input := &batch.DescribeJobQueuesInput{ |
| 161 | + JobQueues: []string{queueName}, |
| 162 | + } |
| 163 | + |
| 164 | + output, err := svc.DescribeJobQueues(ctx, input) |
| 165 | + if err != nil { |
| 166 | + plugin.Logger(ctx).Error("aws_batch_queue.getBatchQueue", "api_error", err) |
| 167 | + return nil, err |
| 168 | + } |
| 169 | + |
| 170 | + if len(output.JobQueues) == 0 { |
| 171 | + return nil, nil |
| 172 | + } |
| 173 | + |
| 174 | + return output.JobQueues[0], nil |
| 175 | +} |
0 commit comments