/
receiveSqsMessage.go
123 lines (111 loc) · 4.17 KB
/
receiveSqsMessage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2022 Antrea Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"context"
"fmt"
"io"
"os"
"time"
awsarn "github.com/aws/aws-sdk-go-v2/aws/arn"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/spf13/cobra"
sqsclient "antrea.io/theia/snowflake/pkg/aws/client/sqs"
)
// receiveSqsMessageCmd represents the receive-sqs-message command
var receiveSqsMessageCmd = &cobra.Command{
Use: "receive-sqs-message",
Short: "Receive a message from an AWS SQS queue",
Long: `This command can be used to receive a message from an SQS queue
in your AWS account. Snowflake data ingestion errors are sent to an SQS queue,
whose ARN is displayed when running the "onboard" command. While ingestion
errors are not expected when using the Antrea Flow Aggregator to export flows,
this command can prove useful if flow records don't get ingested into your
Snowflake account as expected.
To receive a message without deleting it (message will remain in the queue and
become available to consumers again after a short time interval):
"theia-sf receive-sqs-message --queue-arn <ARN>"
To receive a message and delete it from the queue:
"theia-sf receive-sqs-message --queue-arn <ARN> --delete"
Note that this command will not block: if no message is available in the queue,
it will return immediately.`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
region, _ := cmd.Flags().GetString("region")
sqsQueueARN, _ := cmd.Flags().GetString("queue-arn")
delete, _ := cmd.Flags().GetBool("delete")
arn, err := awsarn.Parse(sqsQueueARN)
if err != nil {
return fmt.Errorf("invalid ARN '%s': %w", sqsQueueARN, err)
}
if region != "" && arn.Region != region {
return fmt.Errorf("region conflict between --region flag and ARN region")
}
region = arn.Region
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
awsCfg, err := awsconfig.LoadDefaultConfig(ctx, awsconfig.WithRegion(region))
if err != nil {
return fmt.Errorf("unable to load AWS SDK config: %w", err)
}
sqsClient := sqsclient.GetClient(awsCfg)
return receiveSQSMessage(ctx, sqsClient, arn.Resource, delete, os.Stdout)
},
}
func receiveSQSMessage(ctx context.Context, sqsClient sqsclient.Interface, queueName string, delete bool, w io.Writer) error {
queueURL, err := func() (string, error) {
output, err := sqsClient.GetQueueUrl(ctx, &sqs.GetQueueUrlInput{
QueueName: &queueName,
})
if err != nil {
return "", err
}
return *output.QueueUrl, nil
}()
if err != nil {
return fmt.Errorf("error when retrieving SQS queue URL: %v", err)
}
output, err := sqsClient.ReceiveMessage(ctx, &sqs.ReceiveMessageInput{
QueueUrl: &queueURL,
MaxNumberOfMessages: int32(1),
WaitTimeSeconds: int32(0),
})
if err != nil {
return fmt.Errorf("error when receiving message from SQS queue: %v", err)
}
if len(output.Messages) == 0 {
return nil
}
message := output.Messages[0]
fmt.Fprintln(w, *message.Body)
if !delete {
return nil
}
_, err = sqsClient.DeleteMessage(ctx, &sqs.DeleteMessageInput{
QueueUrl: &queueURL,
ReceiptHandle: message.ReceiptHandle,
})
if err != nil {
return fmt.Errorf("error when deleting message from SQS queue: %v", err)
}
return nil
}
func init() {
rootCmd.AddCommand(receiveSqsMessageCmd)
receiveSqsMessageCmd.Flags().String("region", GetEnv("AWS_REGION", defaultRegion), "region of the SQS queue")
receiveSqsMessageCmd.Flags().String("queue-arn", "", "ARN of the SQS queue")
receiveSqsMessageCmd.MarkFlagRequired("queue-arn")
receiveSqsMessageCmd.Flags().Bool("delete", false, "delete received message from SQS queue")
}