summaryrefslogtreecommitdiff
path: root/plugins/jobs/oooold/broker/sqs/config.go
blob: d0c2f2b28abbc370694e47ff1483933faa6fbd61 (plain)
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
package sqs

import (
	"fmt"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/sqs"
	"github.com/spiral/roadrunner/service"
	"time"
)

// Config defines sqs broker configuration.
type Config struct {
	// Region defined SQS region, not required when endpoint is not empty.
	Region string

	// Region defined AWS API key, not required when endpoint is not empty.
	Key string

	// Region defined AWS API secret, not required when endpoint is not empty.
	Secret string

	// Endpoint can be used to re-define SQS endpoint to custom location. Only for local development.
	Endpoint string

	// Timeout to allocate the connection. Default 10 seconds.
	Timeout int
}

// Hydrate config values.
func (c *Config) Hydrate(cfg service.Config) error {
	if err := cfg.Unmarshal(c); err != nil {
		return err
	}

	if c.Region == "" {
		return fmt.Errorf("SQS region is missing")
	}

	if c.Key == "" {
		return fmt.Errorf("SQS key is missing")
	}

	if c.Secret == "" {
		return fmt.Errorf("SQS secret is missing")
	}

	return nil
}

// TimeoutDuration returns number of seconds allowed to allocate the connection.
func (c *Config) TimeoutDuration() time.Duration {
	timeout := c.Timeout
	if timeout == 0 {
		timeout = 10
	}

	return time.Duration(timeout) * time.Second
}

// Session returns new AWS session.
func (c *Config) Session() (*session.Session, error) {
	return session.NewSession(&aws.Config{
		Region:      aws.String(c.Region),
		Credentials: credentials.NewStaticCredentials(c.Key, c.Secret, ""),
	})
}

// SQS returns new SQS instance or error.
func (c *Config) SQS() (*sqs.SQS, error) {
	sess, err := c.Session()
	if err != nil {
		return nil, err
	}

	if c.Endpoint == "" {
		return sqs.New(sess), nil
	}

	return sqs.New(sess, &aws.Config{Endpoint: aws.String(c.Endpoint)}), nil
}