-
Notifications
You must be signed in to change notification settings - Fork 1
/
medialive_channel.go
97 lines (90 loc) · 2.47 KB
/
medialive_channel.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
package main
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/medialive"
)
func init() {
RegisterType("Custom::MediaLiveChannel", MediaLiveChannel)
}
func waitForChannelState(client *medialive.MediaLive, channelId string, state medialive.ChannelState, allowed []medialive.ChannelState) error {
for {
resp, err := client.DescribeChannelRequest(&medialive.DescribeChannelInput{
ChannelId: &channelId,
}).Send()
if err != nil {
return err
}
if resp.State == state {
return nil
}
isUnexpectedState := true
for _, state := range allowed {
if resp.State == state {
isUnexpectedState = false
break
}
}
if isUnexpectedState {
return fmt.Errorf("Channel reached unexpected state: " + string(resp.State))
}
time.Sleep(5 * time.Second)
}
}
func MediaLiveChannel(request *CustomResourceRequest, cfg aws.Config) (*Success, error) {
client := medialive.New(cfg)
switch request.RequestType {
case "Create":
var input medialive.CreateChannelInput
if err := ReshapeProps(request.ResourceProperties, &input); err != nil {
return nil, err
}
resp, err := client.CreateChannelRequest(&input).Send()
if err != nil {
return nil, err
}
if err := waitForChannelState(client, *resp.Channel.Id, medialive.ChannelStateIdle, []medialive.ChannelState{
medialive.ChannelStateCreating,
}); err != nil {
return nil, err
}
return &Success{
PhysicalResourceId: *resp.Channel.Id,
Data: map[string]interface{}{
"Arn": *resp.Channel.Arn,
"Id": *resp.Channel.Id,
},
}, nil
case "Update":
var input medialive.UpdateChannelInput
if err := ReshapeProps(request.ResourceProperties, &input); err != nil {
return nil, err
}
input.ChannelId = &request.PhysicalResourceId
resp, err := client.UpdateChannelRequest(&input).Send()
if err != nil {
return nil, err
}
return &Success{
PhysicalResourceId: *resp.Channel.Id,
Data: map[string]interface{}{
"Arn": *resp.Channel.Arn,
"Id": *resp.Channel.Id,
},
}, nil
case "Delete":
if _, err := client.DeleteChannelRequest(&medialive.DeleteChannelInput{
ChannelId: &request.PhysicalResourceId,
}).Send(); err != nil {
return nil, err
}
if err := waitForChannelState(client, request.PhysicalResourceId, medialive.ChannelStateDeleted, []medialive.ChannelState{
medialive.ChannelStateDeleting,
}); err != nil {
return nil, err
}
return nil, nil
}
return nil, fmt.Errorf("unexpected request type")
}