/
data_source_ssl_certificate.go
119 lines (102 loc) · 3.44 KB
/
data_source_ssl_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
107
108
109
110
111
112
113
114
115
116
117
118
119
package cdnetworks
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/myklst/terraform-provider-st-cdnetworks/cdnetworksapi"
)
var (
_ datasource.DataSource = &certDataSource{}
_ datasource.DataSourceWithConfigure = &certDataSource{}
)
type certificate struct {
Id types.String `tfsdk:"certificate_id"`
Name types.String `tfsdk:"name"`
}
type certDataSourceModel struct {
CertNameList types.List `tfsdk:"cert_name_list"`
CertList []*certificate `tfsdk:"cert_list"`
}
type certDataSource struct {
client *cdnetworksapi.Client
}
func NewCertDataSource() datasource.DataSource {
return &certDataSource{}
}
func (d *certDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_ssl_certificate"
}
func (d *certDataSource) Schema(_ context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "This data source provides certificates configured in cdnetworks.",
Attributes: map[string]schema.Attribute{
"cert_name_list": schema.ListAttribute{
Description: "List of certificate name.If cert_name_list is null,retrieve all certificates.If cert_name_list is not null (includes empty), retrive certificates with specific name.",
ElementType: types.StringType,
Optional: true,
},
"cert_list": schema.ListNestedAttribute{
Description: "List of certificate",
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "The name of certificate",
Computed: true,
},
"certificate_id": schema.StringAttribute{
Description: "Certificate ID",
Computed: true,
},
},
},
Computed: true,
},
},
}
}
func (d *certDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
d.client = req.ProviderData.(*cdnetworksapi.Client)
}
func (d *certDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var model, state certDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &model)...)
if resp.Diagnostics.HasError() {
return
}
state.CertNameList = model.CertNameList
queryCertificateListResponse, err := d.client.QueryCertificateList()
if err != nil {
resp.Diagnostics.AddError("[API ERROR] Fail to Query Certificate List", err.Error())
return
}
state.CertList = make([]*certificate, 0)
if state.CertNameList.IsNull() {
for _, ssl := range queryCertificateListResponse.SslCertificates {
state.CertList = append(state.CertList,
&certificate{
Id: types.StringPointerValue(ssl.CertificateId),
Name: types.StringPointerValue(ssl.Name),
},
)
}
} else {
for _, name := range state.CertNameList.Elements() {
var c *certificate
for _, ssl := range queryCertificateListResponse.SslCertificates {
if name.(types.String).ValueString() == *ssl.Name {
c = &certificate{
Id: types.StringPointerValue(ssl.CertificateId),
Name: types.StringPointerValue(ssl.Name),
}
break
}
}
state.CertList = append(state.CertList, c)
}
}
resp.State.Set(ctx, &state)
}