/
connect-backup-lambda.go
111 lines (93 loc) · 3.32 KB
/
connect-backup-lambda.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
package main
import (
"context"
"log"
"net/url"
"os"
"strconv"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/service/connect"
"github.com/sethkor/connect-backup"
)
type Response struct {
Answer string `json:"Response" yaml:"Response"`
}
type Request struct {
ConnectInstanceId string `json:"ConnectInstanceId" yaml:"ConnectInstanceId"`
S3DestURL string `json:"S3DestURL" yaml:"S3DestURL"`
FlowsRaw *bool `json:"FlowsRaw" yaml:"FlowsRaw"`
}
func HandleRequest(ctx context.Context, backupRequest Request) (Response, error) {
log.Print(backupRequest)
sess := connect_backup.GetAwsSession("", "")
svc := connect.New(sess)
instanceId := backupRequest.ConnectInstanceId
if instanceId == "" {
instanceId = os.Getenv("CONNECT_INSTANCE_ID")
if instanceId == "" {
log.Println("No Connect instance ID passed in either the event or as an environment variable CONNECT_INSTANCE_ID")
}
log.Println("The ConnectInstanceId in the event was blank, using the environment var CONNECT_INSTANCE_ID")
}
if instanceId != "" {
log.Println("Connect Instance: " + instanceId)
}
s3destination := backupRequest.S3DestURL
if s3destination == "" {
s3destination = os.Getenv("S3_DEST_URL")
if s3destination == "" {
log.Fatalln("No S3DestURL passed in either the event or as an environment variable S3_DEST_URL")
return Response{Answer: "No S3DestURL passed in either the event or as an environment variable S3_DEST_URL"}, nil
}
log.Println("The S3DestURL in the event was blank, using the environment var S3_DEST_URL")
}
s3Url, err := url.Parse(s3destination)
log.Println("S3 URL: " + s3Url.String())
if err != nil || s3Url.Scheme != "s3" {
log.Println("There was an error parsing the S3 URL")
return Response{Answer: "The S3 URL passed in the environment variable S3_DEST_URL was malformed"}, err
}
var flowsRaw = false
if backupRequest.FlowsRaw != nil {
flowsRaw = *backupRequest.FlowsRaw
} else {
log.Println("The FlowsRaw in the event was blank, using the environment var FLOWS_RAW")
flowsRawEnvString := os.Getenv("FLOWS_RAW")
if flowsRawEnvString == "" {
log.Println("No FlowsRaw passed in either the event or as an environment variable FLOWS_RAW, I am setting this to false and continuing")
} else {
var err error
flowsRaw, err = strconv.ParseBool(flowsRawEnvString)
if err != nil {
log.Println("The FLOWS_RAW env variable was not a bool that can be parsed, I am setting this to false and continuing")
flowsRaw = false
}
}
}
log.Println("FlowsRaw : " + strconv.FormatBool(flowsRaw))
//connectSvc := connect.New(sess)
//result, err := connectSvc.DescribeInstance(&connect.DescribeInstanceInput{
// InstanceId: &instanceId,
//})
//
//if err != nil {
// log.Println("Could not fetch the instance specified")
// return Response{Answer: "Could not fetch the instance specified"}, err
//}
cb := connect_backup.ConnectBackup{ConnectInstance: connect.Instance{
Id: &instanceId,
},
Svc: svc,
TheWriter: &connect_backup.S3Writer{Destination: *s3Url, Sess: sess},
RawFlow: flowsRaw,
}
err = cb.Backup()
if err != nil {
log.Println("There was an error performing the backup")
return Response{Answer: "There was an error performing the backup"}, err
}
return Response{Answer: "Processing Successful"}, nil
}
func main() {
lambda.Start(HandleRequest)
}