generated from hashicorp/terraform-provider-scaffolding
/
data_source_remote_file.go
131 lines (113 loc) · 3.37 KB
/
data_source_remote_file.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
package provider
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceRemoteFile() *schema.Resource {
return &schema.Resource{
Description: "File on remote host.",
ReadContext: dataSourceRemoteFileRead,
Schema: map[string]*schema.Schema{
"conn": {
Type: schema.TypeList,
MinItems: 0,
MaxItems: 1,
Optional: true,
Description: "Connection to host where files are located.",
Elem: connectionSchemaResource,
},
"path": {
Description: "Path to file on remote host.",
Type: schema.TypeString,
Required: true,
},
"content": {
Description: "Content of file.",
Type: schema.TypeString,
Computed: true,
},
"permissions": {
Description: "Permissions of file (in octal form).",
Type: schema.TypeString,
Computed: true,
},
"group": {
Description: "Group ID (GID) of file owner.",
Type: schema.TypeString,
Computed: true,
},
"group_name": {
Description: "Group name of file owner.",
Type: schema.TypeString,
Computed: true,
},
"owner": {
Description: "User ID (UID) of file owner.",
Type: schema.TypeString,
Computed: true,
},
"owner_name": {
Description: "User name of file owner.",
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceRemoteFileRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn, err := meta.(*apiClient).getConnWithDefault(d)
if err != nil {
return diag.Errorf(err.Error())
}
setResourceID(d, conn)
client, err := meta.(*apiClient).getRemoteClient(ctx, conn)
if err != nil {
return diag.Errorf("unable to open remote client: %s", err.Error())
}
conn_sudo, ok := conn.GetOk("conn.0.sudo")
sudo := ok && conn_sudo.(bool)
path := d.Get("path").(string)
exists, err := client.FileExists(path, sudo)
if err != nil {
return diag.Errorf("unable to check if remote file exists: %s", err.Error())
}
if !exists {
return diag.Errorf("cannot read file, it does not exist")
}
content, err := client.ReadFile(path, sudo)
if err != nil {
return diag.Errorf("unable to read remote file: %s", err.Error())
}
d.Set("content", content)
permissions, err := client.ReadFilePermissions(path, sudo)
if err != nil {
return diag.Errorf("unable to read remote file permissions: %s", err.Error())
}
d.Set("permissions", permissions)
owner, err := client.ReadFileOwner(path, sudo)
if err != nil {
return diag.Errorf("unable to read remote file owner: %s", err.Error())
}
d.Set("owner", owner)
owner_name, err := client.ReadFileOwnerName(path, sudo)
if err != nil {
return diag.Errorf("unable to read remote file owner_name: %s", err.Error())
}
d.Set("owner_name", owner_name)
group, err := client.ReadFileGroup(path, sudo)
if err != nil {
return diag.Errorf("unable to read remote file group: %s", err.Error())
}
d.Set("group", group)
group_name, err := client.ReadFileGroupName(path, sudo)
if err != nil {
return diag.Errorf("unable to read remote file group_name: %s", err.Error())
}
d.Set("group_name", group_name)
err = meta.(*apiClient).closeRemoteClient(conn)
if err != nil {
return diag.Errorf("unable to close remote client: %s", err.Error())
}
return diag.Diagnostics{}
}