This repository has been archived by the owner on Mar 12, 2022. It is now read-only.
forked from oracle/terraform-provider-oci
/
object_storage_object_data_source.go
182 lines (150 loc) · 4.19 KB
/
object_storage_object_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
package provider
import (
"context"
"fmt"
"io/ioutil"
"log"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
oci_object_storage "github.com/oracle/oci-go-sdk/objectstorage"
)
func ObjectStorageObjectDataSource() *schema.Resource {
return &schema.Resource{
Read: readSingularObjectStorageObject,
Schema: map[string]*schema.Schema{
"bucket": {
Type: schema.TypeString,
Required: true,
},
"namespace": {
Type: schema.TypeString,
Required: true,
},
"object": {
Type: schema.TypeString,
Required: true,
},
"content_length_limit": {
Type: schema.TypeInt,
Optional: true,
//default value is 1MB
Default: 1048576,
},
// Computed
"content": {
Type: schema.TypeString,
Computed: true,
},
"content_encoding": {
Type: schema.TypeString,
Computed: true,
},
"content_language": {
Type: schema.TypeString,
Computed: true,
},
"content_length": {
Type: schema.TypeString,
Computed: true,
},
"content_md5": {
Type: schema.TypeString,
Computed: true,
},
"content_type": {
Type: schema.TypeString,
Computed: true,
},
"metadata": {
Type: schema.TypeMap,
Elem: schema.TypeString,
Computed: true,
},
},
}
}
func readSingularObjectStorageObject(d *schema.ResourceData, m interface{}) error {
sync := &ObjectStorageObjectDataSourceCrud{}
sync.D = d
sync.Client = m.(*OracleClients).objectStorageClient
return ReadResource(sync)
}
type ObjectStorageObjectDataSourceCrud struct {
D *schema.ResourceData
Client *oci_object_storage.ObjectStorageClient
Res *oci_object_storage.GetObjectResponse
}
func (s *ObjectStorageObjectDataSourceCrud) VoidState() {
s.D.SetId("")
}
func (s *ObjectStorageObjectDataSourceCrud) Get() error {
headObjectRequest := &oci_object_storage.HeadObjectRequest{}
if bucket, ok := s.D.GetOkExists("bucket"); ok {
bucketName := bucket.(string)
headObjectRequest.BucketName = &bucketName
}
if namespace, ok := s.D.GetOkExists("namespace"); ok {
namespaceName := namespace.(string)
headObjectRequest.NamespaceName = &namespaceName
}
if object, ok := s.D.GetOkExists("object"); ok {
objectName := object.(string)
headObjectRequest.ObjectName = &objectName
}
headObjectRequest.RequestMetadata.RetryPolicy = getRetryPolicy(false, "object_storage")
headObjectResponse, err := s.Client.HeadObject(context.Background(), *headObjectRequest)
if err != nil {
return err
}
if contentLengthLimit, ok := s.D.GetOkExists("content_length_limit"); ok {
tmpInt64 := int64(contentLengthLimit.(int))
if tmpInt64 < *headObjectResponse.ContentLength {
return fmt.Errorf("the requested object's content length is %v the limit is set to %v", *headObjectResponse.ContentLength, tmpInt64)
}
}
request := oci_object_storage.GetObjectRequest{}
request.NamespaceName = headObjectRequest.NamespaceName
request.BucketName = headObjectRequest.BucketName
request.ObjectName = headObjectRequest.ObjectName
response, err := s.Client.GetObject(context.Background(), request)
if err != nil {
return err
}
s.Res = &response
return nil
}
func (s *ObjectStorageObjectDataSourceCrud) SetData() error {
if s.Res == nil {
return nil
}
s.D.SetId(GenerateDataSourceID())
contentReader := s.Res.Content
contentArray, err := ioutil.ReadAll(contentReader)
if err != nil {
log.Printf("unable to read 'content' from response. Error: %v", err)
} else {
s.D.Set("content", string(contentArray))
}
if s.Res.ContentEncoding != nil {
s.D.Set("content_encoding", *s.Res.ContentEncoding)
}
if s.Res.ContentLanguage != nil {
s.D.Set("content_language", *s.Res.ContentLanguage)
}
if s.Res.ContentLength != nil {
s.D.Set("content_length", strconv.FormatInt(*s.Res.ContentLength, 10))
}
if s.Res.ContentMd5 != nil {
s.D.Set("content_md5", *s.Res.ContentMd5)
}
if s.Res.ContentType != nil {
s.D.Set("content_type", *s.Res.ContentType)
}
if s.Res.OpcMeta != nil {
if err := s.D.Set("metadata", s.Res.OpcMeta); err != nil {
log.Printf("unable to set 'metadata'. Error: %v", err)
}
}
return nil
}