/
bucket_objects_data_source.go
153 lines (126 loc) · 3.72 KB
/
bucket_objects_data_source.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
package s3
// WARNING: This code is DEPRECATED and will be removed in a future release!!
// DO NOT apply fixes or enhancements to the data source in this file.
// INSTEAD, apply fixes and enhancements to the data source in "objects_data_source.go".
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ATenderholt/rainbow-test/terraform/conns"
)
func DataSourceBucketObjects() *schema.Resource {
return &schema.Resource{
Read: dataSourceBucketObjectsRead,
Schema: map[string]*schema.Schema{
"bucket": {
Deprecated: "Use the aws_s3_objects data source instead",
Type: schema.TypeString,
Required: true,
},
"prefix": {
Type: schema.TypeString,
Optional: true,
},
"delimiter": {
Type: schema.TypeString,
Optional: true,
},
"encoding_type": {
Type: schema.TypeString,
Optional: true,
},
"max_keys": {
Type: schema.TypeInt,
Optional: true,
Default: 1000,
},
"start_after": {
Type: schema.TypeString,
Optional: true,
},
"fetch_owner": {
Type: schema.TypeBool,
Optional: true,
},
"keys": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"common_prefixes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"owners": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceBucketObjectsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).S3Conn
bucket := d.Get("bucket").(string)
prefix := d.Get("prefix").(string)
listInput := s3.ListObjectsV2Input{
Bucket: aws.String(bucket),
}
if prefix != "" {
listInput.Prefix = aws.String(prefix)
}
if s, ok := d.GetOk("delimiter"); ok {
listInput.Delimiter = aws.String(s.(string))
}
if s, ok := d.GetOk("encoding_type"); ok {
listInput.EncodingType = aws.String(s.(string))
}
// "listInput.MaxKeys" refers to max keys returned in a single request
// (i.e., page size), not the total number of keys returned if you page
// through the results. "maxKeys" does refer to total keys returned.
maxKeys := int64(d.Get("max_keys").(int))
if maxKeys <= keyRequestPageSize {
listInput.MaxKeys = aws.Int64(maxKeys)
}
if s, ok := d.GetOk("start_after"); ok {
listInput.StartAfter = aws.String(s.(string))
}
if b, ok := d.GetOk("fetch_owner"); ok {
listInput.FetchOwner = aws.Bool(b.(bool))
}
var commonPrefixes []string
var keys []string
var owners []string
err := conn.ListObjectsV2Pages(&listInput, func(page *s3.ListObjectsV2Output, lastPage bool) bool {
for _, commonPrefix := range page.CommonPrefixes {
commonPrefixes = append(commonPrefixes, aws.StringValue(commonPrefix.Prefix))
}
for _, object := range page.Contents {
keys = append(keys, aws.StringValue(object.Key))
if object.Owner != nil {
owners = append(owners, aws.StringValue(object.Owner.ID))
}
}
maxKeys = maxKeys - aws.Int64Value(page.KeyCount)
if maxKeys <= keyRequestPageSize {
listInput.MaxKeys = aws.Int64(maxKeys)
}
return !lastPage
})
if err != nil {
return fmt.Errorf("error listing S3 Bucket (%s) Objects: %w", bucket, err)
}
d.SetId(bucket)
if err := d.Set("common_prefixes", commonPrefixes); err != nil {
return fmt.Errorf("error setting common_prefixes: %w", err)
}
if err := d.Set("keys", keys); err != nil {
return fmt.Errorf("error setting keys: %w", err)
}
if err := d.Set("owners", owners); err != nil {
return fmt.Errorf("error setting owners: %w", err)
}
return nil
}