generated from hashicorp/terraform-provider-scaffolding-framework
/
data_source.go
145 lines (122 loc) · 4 KB
/
data_source.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
package attestation_authority
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-log/tflog"
v20230301 "github.com/smallstep/terraform-provider-smallstep/internal/apiclient/v20230301"
"github.com/smallstep/terraform-provider-smallstep/internal/provider/utils"
)
var _ datasource.DataSourceWithConfigure = (*DataSource)(nil)
func NewDataSource() datasource.DataSource {
return &DataSource{}
}
// DataSource implements data.smallstep_attestation_authority
type DataSource struct {
client *v20230301.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 (a *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.(*v20230301.Client)
if !ok {
resp.Diagnostics.AddError(
"Get Smallstep API client from provider",
fmt.Sprintf("Expected *v20230301.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
a.client = client
}
func (a *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
}
id := config.ID.ValueString()
httpResp, err := a.client.GetAttestationAuthority(ctx, id, &v20230301.GetAttestationAuthorityParams{})
if err != nil {
resp.Diagnostics.AddError(
"Smallstep API Client Error",
fmt.Sprintf("Failed to read attestation authority %q: %v", id, err),
)
return
}
defer httpResp.Body.Close()
if httpResp.StatusCode != http.StatusOK {
resp.Diagnostics.AddError(
"Smallstep API Response Error",
fmt.Sprintf("Received status %d reading attestation authority %q: %s", httpResp.StatusCode, id, utils.APIErrorMsg(httpResp.Body)),
)
return
}
aa := &v20230301.AttestationAuthority{}
if err := json.NewDecoder(httpResp.Body).Decode(aa); err != nil {
resp.Diagnostics.AddError(
"Smallstep API Client Error",
fmt.Sprintf("Failed to unmarshal attestation authority %q: %v", id, err),
)
return
}
remote, d := fromAPI(ctx, aa, req.Config)
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}
tflog.Trace(ctx, fmt.Sprintf("read attestation authority %q data source", id))
resp.Diagnostics.Append(resp.State.Set(ctx, &remote)...)
}
func (d *DataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
component, props, err := utils.Describe("attestationAuthority")
if err != nil {
resp.Diagnostics.AddError(
"Parse Smallstep OpenAPI spec",
err.Error(),
)
return
}
resp.Schema = schema.Schema{
MarkdownDescription: component,
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: props["id"],
Required: true,
},
"slug": schema.StringAttribute{
MarkdownDescription: props["slug"],
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: props["name"],
Computed: true,
},
"root": schema.StringAttribute{
MarkdownDescription: props["root"],
Computed: true,
},
"attestor_roots": schema.StringAttribute{
MarkdownDescription: props["attestorRoots"],
Computed: true,
},
"attestor_intermediates": schema.StringAttribute{
MarkdownDescription: props["attestorIntermediates"],
Computed: true,
},
"created_at": schema.StringAttribute{
MarkdownDescription: props["createdAt"],
Computed: true,
},
},
}
}