-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_source_ssl_certificate.go
112 lines (96 loc) · 3.34 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
package ucloud
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-ucloud/ucloud/api"
"github.com/ucloud/ucloud-sdk-go/services/ucdn"
)
var (
_ datasource.DataSource = &certDataSource{}
_ datasource.DataSourceWithConfigure = &certDataSource{}
)
type certificate struct {
CertName types.String `tfsdk:"cert_name"`
Domains types.List `tfsdk:"domains"`
}
type certDataSourceModel struct {
CertNameList types.List `tfsdk:"cert_name_list"`
CertList []*certificate `tfsdk:"cert_list"`
}
type certDataSource struct {
client *ucdn.UCDNClient
}
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 ucloud, including certificate name,domains associated with the certificate,etc.",
Attributes: map[string]schema.Attribute{
"cert_name_list": schema.ListAttribute{
Description: "List of cert_name.If `cert_name_list` is null,retrieve all certificates.If `cert_name_list` is not null,retrieve 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{
"cert_name": schema.StringAttribute{
Description: "The name of certificate",
Computed: true,
},
"domains": schema.ListAttribute{
Description: "Domain associcated with this certificate.",
ElementType: types.StringType,
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.(ucloudClients).cdnClient
}
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
var queryList []string
resp.Diagnostics.Append(state.CertNameList.ElementsAs(ctx, &queryList, false)...)
if resp.Diagnostics.HasError() {
return
}
certs, err := api.GetCertificates(d.client, queryList...)
if err != nil {
resp.Diagnostics.AddError("[API ERROR]Fail to get ssl status", err.Error())
return
}
state.CertList = make([]*certificate, len(queryList))
for _, cert := range certs {
if cert == nil {
continue
}
domains, _ := types.ListValueFrom(ctx, types.StringType, cert.Domains)
c := &certificate{
CertName: types.StringValue(cert.CertName),
Domains: domains,
}
state.CertList = append(state.CertList, c)
}
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}