/
fargate.go
82 lines (69 loc) · 2.06 KB
/
fargate.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
package tf
import (
"context"
"encoding/json"
"errors"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/sysdiglabs/agent-kilt/runtimes/cloudformation/cfnpatcher"
)
type cfnProperties struct {
RequiresCompatibilities []string `json:"RequiresCompatibilities"`
ContainerDefinitions []map[string]interface{} `json:"ContainerDefinitions"`
}
type cfnResource struct {
ResourceType string `json:"Type"`
Properties cfnProperties `json:"Properties"`
}
type cfnStack struct {
Resources map[string]cfnResource `json:"Resources"`
}
func patchFargateTaskDefinition(ctx context.Context, containerDefinitions *types.String, kiltConfig *cfnpatcher.Configuration) (patched types.String, err error) {
var cdefs []map[string]interface{}
err = json.Unmarshal([]byte(containerDefinitions.Value), &cdefs)
if err != nil {
return types.String{Unknown: true}, err
}
stack := cfnStack{
Resources: map[string]cfnResource{
"kilt": {
ResourceType: "AWS::ECS::TaskDefinition",
Properties: cfnProperties{
RequiresCompatibilities: []string{"FARGATE"},
ContainerDefinitions: cdefs,
},
},
},
}
patchedStack, err := json.Marshal(stack)
if err != nil {
return types.String{Unknown: true}, err
}
defer func() {
if r := recover(); r != nil {
patched = types.String{Unknown: true}
switch x := r.(type) {
case string:
err = errors.New(x)
case error:
err = x
default:
// Fallback err (per specs, error strings should be lowercase w/o punctuation
err = errors.New("unknown panic")
}
}
}()
templateParameters := make([]byte, 0)
patchedBytes, err := cfnpatcher.Patch(ctx, kiltConfig, patchedStack, templateParameters)
if err != nil {
return types.String{Unknown: true}, err
}
err = json.Unmarshal(patchedBytes, &stack)
if err != nil {
return types.String{Unknown: true}, err
}
patchedBytes, err = json.Marshal(stack.Resources["kilt"].Properties.ContainerDefinitions)
if err != nil {
return types.String{Unknown: true}, err
}
return types.String{Value: string(patchedBytes)}, nil
}