-
Notifications
You must be signed in to change notification settings - Fork 7
/
data_source_hcs_elb_certificate.go
110 lines (97 loc) · 2.84 KB
/
data_source_hcs_elb_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
package elb
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud/openstack/elb/v3/certificates"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/config"
)
// DataSourceELBCertificateV3 the data source of "hcs_elb_certificate"
// Dedicated ELB
func DataSourceELBCertificateV3() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceELbCertificateV3Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"domain": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"expiration": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceELbCertificateV3Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := config.GetHcsConfig(meta)
client, err := cfg.ElbV3Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating Dedicated ELB Client: %s", err)
}
listOpts := certificates.ListOpts{
Name: d.Get("name").(string),
}
r, err := certificates.List(client, listOpts).AllPages()
if err != nil {
return diag.Errorf("unable to list ELB certificates: %s", err)
}
certs, err := certificates.ExtractCertificates(r)
if err != nil {
return diag.Errorf("unable to retrieve certs from Dedicated ELB: %s", err)
}
log.Printf("[DEBUG] Get certificate list: %#v", certs)
if len(certs) == 0 {
return diag.Errorf("your query returned no results. Please change your search criteria and try again.")
}
err = setCertificateAttributes(d, certs[0])
if err != nil {
return diag.FromErr(err)
}
return nil
}
func setCertificateAttributes(d *schema.ResourceData, c certificates.Certificate) error {
d.SetId(c.ID)
var expiration string
tm, err := time.Parse(time.RFC3339, c.ExpireTime)
if err != nil {
// If the format of ExpireTime is not expected, set the original value directly.
expiration = c.ExpireTime
log.Printf("[WAIN] The format of the ExpireTime field of the Dedicated ELB certificate "+
"is not expected:%s", c.ExpireTime)
} else {
expiration = tm.Format("2006-01-02 15:04:05 MST")
}
mErr := multierror.Append(nil,
d.Set("name", c.Name),
d.Set("domain", c.Domain),
d.Set("description", c.Description),
d.Set("type", c.Type),
d.Set("expiration", expiration),
)
if err := mErr.ErrorOrNil(); err != nil {
return fmt.Errorf("error setting Dedicated ELB Certificate fields: %s", err)
}
return nil
}