Newer
Older
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package device
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
v20250101 "github.com/smallstep/terraform-provider-smallstep/internal/apiclient/v20250101"
"github.com/smallstep/terraform-provider-smallstep/internal/provider/utils"
)
var _ datasource.DataSourceWithConfigure = (*DataSource)(nil)
func NewDataSource() datasource.DataSource {
return &DataSource{}
}
type DataSource struct {
client *v20250101.Client
}
func (a *DataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = typeName
}
// Configure adds the Smallstep API client to the data source.
func (ds *DataSource) 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.(*v20250101.Client)
if !ok {
resp.Diagnostics.AddError(
"Get Smallstep API client from provider",
fmt.Sprintf("Expected *v20250101.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
ds.client = client
}
func (ds *DataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
device, props, err := utils.Describe("device")
if err != nil {
resp.Diagnostics.AddError(
"Parse Smallstep OpenAPI Device Schema",
err.Error(),
)
return
}
deviceUser, userProps, err := utils.Describe("deviceUser")
if err != nil {
resp.Diagnostics.AddError(
"Parse Smallstep OpenAPI Device User Schema",
err.Error(),
)
return
}
resp.Schema = schema.Schema{
MarkdownDescription: device,
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: props["id"],
Required: true,
},
"permanent_identifier": schema.StringAttribute{
MarkdownDescription: props["permanentIdentifier"],
Computed: true,
},
"serial": schema.StringAttribute{
MarkdownDescription: props["serial"],
Computed: true,
},
"display_name": schema.StringAttribute{
MarkdownDescription: props["displayName"],
Computed: true,
},
"display_id": schema.StringAttribute{
MarkdownDescription: props["displayId"],
Computed: true,
},
"os": schema.StringAttribute{
MarkdownDescription: props["os"],
Computed: true,
},
"ownership": schema.StringAttribute{
MarkdownDescription: props["ownership"],
Computed: true,
},
"metadata": schema.MapAttribute{
MarkdownDescription: props["metadata"],
Computed: true,
ElementType: types.StringType,
},
"tags": schema.SetAttribute{
MarkdownDescription: props["tags"],
Computed: true,
ElementType: types.StringType,
},
"user": schema.SingleNestedAttribute{
MarkdownDescription: deviceUser,
Computed: true,
Attributes: map[string]schema.Attribute{
"display_name": schema.StringAttribute{
MarkdownDescription: userProps["displayName"],
Computed: true,
},
"email": schema.StringAttribute{
MarkdownDescription: userProps["email"],
Computed: true,
},
},
},
"connected": schema.BoolAttribute{
MarkdownDescription: props["connected"],
Computed: true,
},
"high_assurance": schema.BoolAttribute{
MarkdownDescription: props["highAssurance"],
Computed: true,
},
"enrolled_at": schema.StringAttribute{
MarkdownDescription: props["enrolledAt"],
Computed: true,
},
"approved_at": schema.StringAttribute{
MarkdownDescription: props["approvedAt"],
Computed: true,
},
"last_seen": schema.StringAttribute{
MarkdownDescription: props["lastSeen"],
Computed: true,
},
},
}
}
func (ds *DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config Model
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
if resp.Diagnostics.HasError() {
return
}
deviceID := config.ID.ValueString()
if deviceID == "" {
resp.Diagnostics.AddError(
"Invalid Device ID",
"Device ID is required",
)
return
}
httpResp, err := ds.client.GetDevice(ctx, deviceID, &v20250101.GetDeviceParams{})
if err != nil {
resp.Diagnostics.AddError(
"Smallstep API Client Error",
fmt.Sprintf("Failed to read device %q: %v", config.ID.ValueString(), err),
)
return
}
defer httpResp.Body.Close()
if httpResp.StatusCode == http.StatusNotFound {
resp.State.RemoveResource(ctx)
return
}
if httpResp.StatusCode != http.StatusOK {
reqID := httpResp.Header.Get("X-Request-Id")
resp.Diagnostics.AddError(
"Smallstep API Response Error",
fmt.Sprintf("Request %q received status %d reading device %s: %s", reqID, httpResp.StatusCode, deviceID, utils.APIErrorMsg(httpResp.Body)),
)
return
}
device := &v20250101.Device{}
if err := json.NewDecoder(httpResp.Body).Decode(device); err != nil {
resp.Diagnostics.AddError(
"Smallstep API Client Error",
fmt.Sprintf("Failed to unmarshal device %s: %v", deviceID, err),
)
return
}
remote, d := fromAPI(ctx, device, req.Config)
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &remote)...)
}