/
data_source_projects.go
153 lines (132 loc) · 4.85 KB
/
data_source_projects.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package provider
import (
"context"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/oxidecomputer/oxide.go/oxide"
)
var _ datasource.DataSource = (*projectsDataSource)(nil)
var _ datasource.DataSourceWithConfigure = (*projectsDataSource)(nil)
type projectsDataSource struct {
client *oxide.Client
}
type projectsDataSourceModel struct {
ID types.String `tfsdk:"id"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
Projects []projectModel `tfsdk:"projects"`
}
type projectModel struct {
Description types.String `tfsdk:"description"`
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
TimeCreated types.String `tfsdk:"time_created"`
TimeModified types.String `tfsdk:"time_modified"`
}
// NewProjectsDataSource initialises a projects datasource
func NewProjectsDataSource() datasource.DataSource {
return &projectsDataSource{}
}
func (d *projectsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "oxide_projects"
}
// Configure adds the provider configured client to the data source.
func (d *projectsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
d.client = req.ProviderData.(*oxide.Client)
}
func (d *projectsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"timeouts": timeouts.Attributes(ctx),
"projects": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"description": schema.StringAttribute{
Computed: true,
Description: "Description for the project.",
},
"id": schema.StringAttribute{
Computed: true,
Description: "Unique, immutable, system-controlled identifier of the project.",
},
"name": schema.StringAttribute{
Computed: true,
Description: "Name of the project.",
},
"time_created": schema.StringAttribute{
Computed: true,
Description: "Timestamp of when this project was created.",
},
"time_modified": schema.StringAttribute{
Computed: true,
Description: "Timestamp of when this project was last modified.",
},
},
},
},
},
}
}
func (d *projectsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state projectsDataSourceModel
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
readTimeout, diags := state.Timeouts.Read(ctx, defaultTimeout())
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx, cancel := context.WithTimeout(ctx, readTimeout)
defer cancel()
// TODO: It would be preferable to us the client.Projects.ListAllPages method instead.
// Unfortunately, currently that method has a bug where it returns twice as many results
// as there are in reality. For now I'll use the List method with a limit of 1,000,000,000 results.
// Seems unlikely anyone will have more than one billion projects.
params := oxide.ProjectListParams{
Limit: 1000000000,
SortBy: oxide.NameOrIdSortModeNameDescending,
}
projects, err := d.client.ProjectList(ctx, params)
if err != nil {
resp.Diagnostics.AddError(
"Unable to read projects:",
err.Error(),
)
return
}
tflog.Trace(ctx, "read all projects", map[string]any{"success": true})
// Set a unique ID for the datasource payload
state.ID = types.StringValue(uuid.New().String())
// Map response body to model
for _, project := range projects.Items {
projectState := projectModel{
Description: types.StringValue(project.Description),
ID: types.StringValue(project.Id),
Name: types.StringValue(string(project.Name)),
TimeCreated: types.StringValue(project.TimeCreated.String()),
TimeModified: types.StringValue(project.TimeCreated.String()),
}
state.Projects = append(state.Projects, projectState)
}
// Save state into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
}