/
data_source_files.go
188 lines (165 loc) · 6.68 KB
/
data_source_files.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
183
184
185
186
187
188
/*
Copyright © 2021-2023 Hannes Hayashi
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package provider
import (
"context"
"fmt"
"net/http"
"github.com/hanneshayashi/gsm/gsmdrive"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &filesDataSource{}
func newFilesDataSource() datasource.DataSource {
return &filesDataSource{}
}
// filesDataSource defines the data source implementation.
type filesDataSource struct {
client *http.Client
}
// gdriveDriveResourceModelV1 describes the resource data model V1.
type gdriveFilesDataSourceFileModel struct {
Name types.String `tfsdk:"name"`
Parent types.String `tfsdk:"parent"`
FileId types.String `tfsdk:"file_id"`
Id types.String `tfsdk:"id"`
DriveId types.String `tfsdk:"drive_id"`
MimeType types.String `tfsdk:"mime_type"`
}
// gdriveDriveResourceModelV1 describes the resource data model V1.
type gdriveFilesDataSourceModel struct {
Id types.String `tfsdk:"id"`
Query types.String `tfsdk:"query"`
Spaces types.String `tfsdk:"spaces"`
Corpora types.String `tfsdk:"corpora"`
DriveId types.String `tfsdk:"drive_id"`
Files []*gdriveFilesDataSourceFileModel `tfsdk:"files"`
IncludeItemsFromAllDrives types.Bool `tfsdk:"include_items_from_all_drives"`
}
func (d *filesDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_files"
}
func (d *filesDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Returns a list of files that match the given query.",
Attributes: map[string]schema.Attribute{
"id": dsId(),
"query": schema.StringAttribute{
Required: true,
MarkdownDescription: `A query for filtering the file results.
See the https://developers.google.com/drive/api/v3/search-files for the supported syntax.`,
},
"spaces": schema.StringAttribute{
Optional: true,
MarkdownDescription: `A comma-separated list of spaces to query within the corpus.
Supported values are 'drive', 'appDataFolder' and 'photos'.`,
},
"corpora": schema.StringAttribute{
Optional: true,
MarkdownDescription: `Groupings of files to which the query applies.
Supported groupings are:
* 'user' (files created by, opened by, or shared directly with the user)
* 'drive' (files in the specified shared drive as indicated by the 'driveId')
* 'domain' (files shared to the user's domain)
* 'allDrives' (A combination of 'user' and 'drive' for all drives where the user is a member).
When able, use 'user' or 'drive', instead of 'allDrives', for efficiency.`,
},
"drive_id": schema.StringAttribute{
Optional: true,
MarkdownDescription: `ID of the shared drive.`,
},
"include_items_from_all_drives": schema.BoolAttribute{
Optional: true,
MarkdownDescription: `Whether both My Drive and shared drive items should be included in results.`,
},
"files": schema.SetNestedAttribute{
Computed: true,
MarkdownDescription: "A set of files that match the specified query.",
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": dsId(),
"file_id": schema.StringAttribute{
MarkdownDescription: "The ID of the file.",
Computed: true,
},
"drive_id": schema.StringAttribute{
MarkdownDescription: "The ID of the Shared Drive the file is located in. Only present if the file is located in a Shared Drive.",
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the file.",
Computed: true,
},
"mime_type": schema.StringAttribute{
MarkdownDescription: "The MIME type of the file.",
Computed: true,
},
"parent": schema.StringAttribute{
MarkdownDescription: "The ID of the file's parent.",
Computed: true,
},
},
},
},
},
}
}
func (d *filesDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*http.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}
func (ds *filesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
config := &gdriveFilesDataSourceModel{}
resp.Diagnostics.Append(req.Config.Get(ctx, config)...)
if resp.Diagnostics.HasError() {
return
}
query := config.Query.ValueString()
r, err := gsmdrive.ListFiles(query, config.DriveId.ValueString(), config.Corpora.ValueString(), "", "", config.Spaces.ValueString(), fmt.Sprintf("files(%s),nextPageToken", fieldsFile), config.IncludeItemsFromAllDrives.ValueBool(), 1)
for f := range r {
fileModel := &gdriveFilesDataSourceFileModel{
Name: types.StringValue(f.Name),
Id: types.StringValue(f.Id),
FileId: types.StringValue(f.Id),
MimeType: types.StringValue(f.MimeType),
}
if len(f.Parents) > 0 {
fileModel.Parent = types.StringValue(f.Parents[0])
}
if f.DriveId != "" {
fileModel.DriveId = types.StringValue(f.DriveId)
}
config.Files = append(config.Files, fileModel)
}
e := <-err
if e != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to list files, got error: %s", e))
return
}
config.Id = config.Query
resp.Diagnostics.Append(resp.State.Set(ctx, &config)...)
}