-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_blob.go
70 lines (61 loc) · 1.63 KB
/
data_source_blob.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
// SPDX-FileCopyrightText: 2024 Dominik Wombacher <[email protected]>
// SPDX-FileCopyrightText: 2019 The SourceHut API Contributors
//
// SPDX-License-Identifier: BSD-2-Clause
package main
import (
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
// Datasource Name
blobName = "sourcehut_blob"
// Schema names
contentsKey = "contents"
)
// dataSourceBlob returns a data source for getting information about a file
// (blob) in a paste.
func dataSourceBlob() *schema.Resource {
return &schema.Resource{
Read: dataSourceBlobRead,
Schema: map[string]*schema.Schema{
idKey: {
Type: schema.TypeString,
Required: true,
Description: "The SHA1 hash of the paste.",
},
createdKey: {
Type: schema.TypeString,
Computed: true,
Description: "The date on which the paste was created in RFC3339 format.",
},
createdTimestampKey: {
Type: schema.TypeInt,
Computed: true,
Description: "The date on which the paste was created as a unix timestamp.",
},
contentsKey: {
Type: schema.TypeString,
Computed: true,
Description: "The files contents as a UTF-8 encoded string.",
},
},
}
}
func dataSourceBlobRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(config)
blob, err := config.pasteClient.GetBlob(d.Get("id").(string))
if err != nil {
return err
}
d.SetId(blob.ID)
err = d.Set(createdKey, blob.Created.Format(time.RFC3339))
if err != nil {
return err
}
err = d.Set(createdTimestampKey, blob.Created.Unix())
if err != nil {
return err
}
return d.Set(contentsKey, blob.Contents)
}