forked from kayac/ecspresso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
109 lines (99 loc) · 2.94 KB
/
plugin.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
package ecspresso
import (
"context"
"errors"
"fmt"
"path/filepath"
"strings"
"text/template"
"github.com/fujiwara/cfn-lookup/cfn"
"github.com/fujiwara/tfstate-lookup/tfstate"
"github.com/kayac/ecspresso/v2/secretsmanager"
"github.com/kayac/ecspresso/v2/ssm"
"github.com/samber/lo"
)
var defaultPluginNames = []string{"ssm", "secretsmanager"}
type ConfigPlugin struct {
Name string `yaml:"name" json:"name"`
Config map[string]interface{} `yaml:"config" json:"config"`
FuncPrefix string `yaml:"func_prefix,omitempty" json:"func_prefix,omitempty"`
}
func (p ConfigPlugin) Setup(ctx context.Context, c *Config) error {
switch strings.ToLower(p.Name) {
case "tfstate":
return setupPluginTFState(ctx, p, c)
case "cloudformation":
return setupPluginCFn(ctx, p, c)
case "ssm":
return setupPluginSSM(ctx, p, c)
case "secretsmanager":
return setupPluginSecretsManager(ctx, p, c)
default:
return fmt.Errorf("plugin %s is not available", p.Name)
}
}
func (p ConfigPlugin) AppendFuncMap(c *Config, funcMap template.FuncMap) error {
modified := make(template.FuncMap, len(funcMap))
for funcName, f := range funcMap {
name := p.FuncPrefix + funcName
for _, appendedFuncs := range c.templateFuncs {
if _, exists := appendedFuncs[name]; exists {
if lo.Contains(defaultPluginNames, p.Name) {
Log("[DEBUG] template function %s already exists by default plugins. skip", name)
continue
}
return fmt.Errorf("template function %s already exists. set func_prefix to %s plugin", name, p.Name)
}
}
modified[name] = f
}
c.templateFuncs = append(c.templateFuncs, modified)
return nil
}
func setupPluginTFState(ctx context.Context, p ConfigPlugin, c *Config) error {
var loc string
if p.Config["path"] != nil {
path, ok := p.Config["path"].(string)
if !ok {
return errors.New("tfstate plugin requires path for tfstate file as a string")
}
if !filepath.IsAbs(path) {
path = filepath.Join(c.dir, path)
}
loc = path
} else if p.Config["url"] != nil {
u, ok := p.Config["url"].(string)
if !ok {
return errors.New("tfstate plugin requires url for tfstate URL as a string")
}
loc = u
} else {
return errors.New("tfstate plugin requires path or url for tfstate location")
}
funcs, err := tfstate.FuncMap(ctx, loc)
if err != nil {
return err
}
return p.AppendFuncMap(c, funcs)
}
func setupPluginCFn(ctx context.Context, p ConfigPlugin, c *Config) error {
funcs, err := cfn.FuncMap(ctx, c.awsv2Config)
if err != nil {
return err
}
return p.AppendFuncMap(c, funcs)
}
func setupPluginSSM(ctx context.Context, p ConfigPlugin, c *Config) error {
funcs, err := ssm.FuncMap(ctx, c.awsv2Config)
if err != nil {
return err
}
return p.AppendFuncMap(c, funcs)
}
func setupPluginSecretsManager(ctx context.Context, p ConfigPlugin, c *Config) error {
funcs, err := secretsmanager.FuncMap(ctx, c.awsv2Config)
if err != nil {
return err
}
return p.AppendFuncMap(c, funcs)
}