/
datasource_artifactory_fileinfo.go
127 lines (113 loc) · 2.89 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
116
117
118
119
120
121
122
123
124
125
126
127
package artifact
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jfrog/terraform-provider-shared/util"
utilsdk "github.com/jfrog/terraform-provider-shared/util/sdk"
"github.com/jfrog/terraform-provider-shared/validator"
)
func ArtifactoryFileInfo() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceFileInfoRead,
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validator.RepoKey,
},
"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(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
repo := d.Get("repository").(string)
path := d.Get("path").(string)
fileInfo := FileInfo{}
resp, err := m.(util.ProviderMetadata).Client.R().
SetResult(&fileInfo).
SetPathParams(map[string]string{
"repoKey": repo,
"path": path,
}).
Get("artifactory/api/storage/{repoKey}/{path}")
if err != nil {
return diag.FromErr(err)
}
if resp.IsError() {
return diag.Errorf("%s", resp.String())
}
return packFileInfo(fileInfo, d)
}
func packFileInfo(fileInfo FileInfo, d *schema.ResourceData) diag.Diagnostics {
setValue := utilsdk.MkLens(d)
d.SetId(fileInfo.Id())
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 len(errors) > 0 {
return diag.Errorf("failed to pack fileInfo %q", errors)
}
return nil
}