forked from theaaf/cloudformation-media-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediaconnect_flow_activation.go
77 lines (70 loc) · 2 KB
/
mediaconnect_flow_activation.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
package main
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/mediaconnect"
)
func init() {
RegisterType("Custom::MediaConnectFlowActivation", MediaConnectFlowActivation)
}
func waitForFlowStatus(client *mediaconnect.MediaConnect, flowArn string, status mediaconnect.Status, allowed []mediaconnect.Status) error {
for {
resp, err := client.DescribeFlowRequest(&mediaconnect.DescribeFlowInput{
FlowArn: &flowArn,
}).Send()
if err != nil {
return err
}
if resp.Flow.Status == status {
return nil
}
isUnexpectedState := true
for _, status := range allowed {
if resp.Flow.Status == status {
isUnexpectedState = false
break
}
}
if isUnexpectedState {
return fmt.Errorf("Channel reached unexpected state: " + string(resp.Flow.Status))
}
time.Sleep(5 * time.Second)
}
}
func MediaConnectFlowActivation(request *CustomResourceRequest, cfg aws.Config) (*Success, error) {
client := mediaconnect.New(cfg)
switch request.RequestType {
case "Create", "Update":
var input mediaconnect.StartFlowInput
if err := ReshapeProps(request.ResourceProperties, &input); err != nil {
return nil, err
}
if _, err := client.StartFlowRequest(&input).Send(); err != nil {
return nil, err
}
if err := waitForFlowStatus(client, *input.FlowArn, mediaconnect.StatusActive, []mediaconnect.Status{
mediaconnect.StatusStarting,
}); err != nil {
return nil, err
}
return &Success{
PhysicalResourceId: *input.FlowArn + "/activation",
}, nil
case "Delete":
var input mediaconnect.StopFlowInput
if err := ReshapeProps(request.ResourceProperties, &input); err != nil {
return nil, err
}
if _, err := client.StopFlowRequest(&input).Send(); err != nil {
return nil, err
}
if err := waitForFlowStatus(client, *input.FlowArn, mediaconnect.StatusStandby, []mediaconnect.Status{
mediaconnect.StatusStopping,
}); err != nil {
return nil, err
}
return nil, nil
}
return nil, fmt.Errorf("unexpected request type")
}