/
data_source_amper_fc.go
164 lines (139 loc) · 4.11 KB
/
data_source_amper_fc.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package provider
import (
"bytes"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/go-homedir"
"github.com/spirius/fc"
"io"
"os"
)
func dataSourceAmperFc() *schema.Resource {
return &schema.Resource{
Read: dataSourceAmperFcRead,
Schema: map[string]*schema.Schema{
"input": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"input_file"},
},
"input_file": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"input"},
},
"filter": &schema.Schema{
Type: schema.TypeList,
Required: true,
ForceNew: true,
MinItems: 2,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"args": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"output": {
Type: schema.TypeString,
Computed: true,
ForceNew: true,
StateFunc: func(in interface{}) string {
return getContentSha256Base64(in.(string))
},
},
"output_size": {
Type: schema.TypeInt,
Computed: true,
ForceNew: true,
},
"output_sha": &schema.Schema{
Type: schema.TypeString,
Computed: true,
ForceNew: true,
Description: "SHA1 checksum of output",
},
"output_base64sha256": &schema.Schema{
Type: schema.TypeString,
Computed: true,
ForceNew: true,
Description: "Base64 Encoded SHA256 checksum of output",
},
"output_md5": &schema.Schema{
Type: schema.TypeString,
Computed: true,
ForceNew: true,
Description: "MD5 of output",
},
},
}
}
type ByFilterIndex []interface{}
func (a ByFilterIndex) Len() int { return len(a) }
func (a ByFilterIndex) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByFilterIndex) Less(i, j int) bool {
return a[i].(map[string]interface{})["index"].(int) < a[j].(map[string]interface{})["index"].(int)
}
func dataSourceAmperFcRead(d *schema.ResourceData, meta interface{}) (err error) {
var input io.Reader
if attr, ok := d.GetOk("input"); ok {
buf := &bytes.Buffer{}
buf.WriteString(attr.(string))
input = buf
} else if attr, ok := d.GetOk("input_file"); ok {
source := attr.(string)
path, err := homedir.Expand(source)
if err != nil {
return fmt.Errorf("Error expanding homedir in source (%s): %s", source, err)
}
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("Error opening S3 bucket object source (%s): %s", source, err)
}
input = file
} else {
return fmt.Errorf("one of 'input', 'input_file' must be specified")
}
pipeline := fc.DefaultFC.NewPipeline()
filters := d.Get("filter").([]interface{})
if len(filters) < 2 {
return fmt.Errorf("at least 2 filters must be specified")
}
input_filter := filters[0].(map[string]interface{})
err = pipeline.SetInputFilter(input_filter["name"].(string), resourceGetStringListFromList(input_filter["args"].([]interface{}))...)
if err != nil {
return fmt.Errorf("cannot set input filter: %s", err)
}
output_filter := filters[len(filters)-1].(map[string]interface{})
err = pipeline.SetOutputFilter(output_filter["name"].(string), resourceGetStringListFromList(output_filter["args"].([]interface{}))...)
if err != nil {
return fmt.Errorf("cannot set input filter: %s", err)
}
for i := 1; i < len(filters)-1; i++ {
filter := filters[i].(map[string]interface{})
pipeline.AddFilter(filter["name"].(string), resourceGetStringListFromList(filter["args"].([]interface{}))...)
}
var output bytes.Buffer
if err = pipeline.Process(input, &output); err != nil {
return fmt.Errorf("cannot process: %s", err)
}
sha1, base64sha256, md5 := getContentShas(output.Bytes())
d.Set("output_sha", sha1)
d.Set("output_base64sha256", base64sha256)
d.Set("output_md5", md5)
d.Set("output_size", output.Len())
d.Set("output", output.String())
d.SetId(sha1)
return nil
}