/
datasource-certificate.go
106 lines (86 loc) · 3.38 KB
/
datasource-certificate.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
// Code generated by "terraform-provider-keyhub-generator"; DO NOT EDIT.
// Copyright (c) Topicus Security B.V.
// SPDX-License-Identifier: APSL-2.0
package provider
import (
"context"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
keyhubreq "github.com/topicuskeyhub/sdk-go/certificate"
keyhubmodels "github.com/topicuskeyhub/sdk-go/models"
)
// Ensure provider defined types fully satisfy framework interfaces.
var (
_ datasource.DataSource = &certificateDataSource{}
_ datasource.DataSourceWithConfigure = &certificateDataSource{}
)
func NewCertificateDataSource() datasource.DataSource {
return &certificateDataSource{}
}
type certificateDataSource struct {
providerData *KeyHubProviderData
}
func (d *certificateDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = ProviderName + "_certificate"
log.Printf("Registered data source %s", resp.TypeName)
}
func (d *certificateDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: dataSourceSchemaAttrsCertificateCertificate(true),
}
}
func (d *certificateDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
providerData, ok := req.ProviderData.(*KeyHubProviderData)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *keyhub.KeyHubClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.providerData = providerData
}
func (d *certificateDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data certificateCertificateDataDS
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Reading certificate from Topicus KeyHub by UUID")
additionalBackup := data.Additional
additional, _ := tfToSlice(data.Additional, func(val attr.Value, diags *diag.Diagnostics) string {
return val.(basetypes.StringValue).ValueString()
})
uuid := data.UUID.ValueString()
d.providerData.Mutex.RLock()
defer d.providerData.Mutex.RUnlock()
wrapper, err := d.providerData.Client.Certificate().Get(ctx, &keyhubreq.CertificateRequestBuilderGetRequestConfiguration{
QueryParameters: &keyhubreq.CertificateRequestBuilderGetQueryParameters{
Uuid: []string{uuid},
Additional: additional,
},
})
tkh, diags := findFirst[keyhubmodels.CertificateCertificateable](ctx, wrapper, "certificate", &uuid, false, err)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tf, diags := tkhToTFObjectDSCertificateCertificate(true, tkh)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
fillDataStructFromTFObjectDSCertificateCertificate(&data, tf)
data.Additional = additionalBackup
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}