-
Notifications
You must be signed in to change notification settings - Fork 102
/
datasource_artifactory_fileinfo.go
115 lines (101 loc) · 2.52 KB
/
datasource_artifactory_fileinfo.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
package artifactory
import (
"fmt"
"github.com/go-resty/resty/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceArtifactoryFileInfo() *schema.Resource {
return &schema.Resource{
Read: dataSourceFileInfoRead,
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
ValidateFunc: repoKeyValidator,
},
"path": {
Type: schema.TypeString,
Required: true,
},
"created": {
Type: schema.TypeString,
Computed: true,
},
"created_by": {
Type: schema.TypeString,
Computed: true,
},
"last_modified": {
Type: schema.TypeString,
Computed: true,
},
"modified_by": {
Type: schema.TypeString,
Computed: true,
},
"last_updated": {
Type: schema.TypeString,
Computed: true,
},
"download_uri": {
Type: schema.TypeString,
Computed: true,
},
"mimetype": {
Type: schema.TypeString,
Computed: true,
},
"size": {
Type: schema.TypeInt,
Computed: true,
},
"md5": {
Type: schema.TypeString,
Computed: true,
},
"sha1": {
Type: schema.TypeString,
Computed: true,
},
"sha256": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceFileInfoRead(d *schema.ResourceData, m interface{}) error {
repository := d.Get("repository").(string)
path := d.Get("path").(string)
fileInfo := FileInfo{}
_, err := m.(*resty.Client).R().SetResult(&fileInfo).Get(fmt.Sprintf("artifactory/api/storage/%s/%s", repository, path))
if err != nil {
return err
}
return packFileInfo(fileInfo, d)
}
func packFileInfo(fileInfo FileInfo, d *schema.ResourceData) error {
setValue := mkLens(d)
d.SetId(fileInfo.DownloadUri)
setValue("created", fileInfo.Created)
setValue("created_by", fileInfo.CreatedBy)
setValue("last_modified", fileInfo.LastModified)
setValue("modified_by", fileInfo.ModifiedBy)
setValue("last_updated", fileInfo.LastUpdated)
setValue("download_uri", fileInfo.DownloadUri)
setValue("mimetype", fileInfo.MimeType)
errors := setValue("size", fileInfo.Size)
if fileInfo.Checksums.Md5 != "" {
errors = setValue("md5", fileInfo.Checksums.Md5)
}
if fileInfo.Checksums.Sha1 != "" {
errors = setValue("sha1", fileInfo.Checksums.Sha1)
}
if fileInfo.Checksums.Sha256 != "" {
errors = setValue("sha256", fileInfo.Checksums.Sha256)
}
if errors != nil && len(errors) > 0 {
return fmt.Errorf("failed to pack fileInfo %q", errors)
}
return nil
}