-
Notifications
You must be signed in to change notification settings - Fork 7
/
resource_hcs_elb_certificate.go
206 lines (174 loc) · 5.81 KB
/
resource_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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package elb
import (
"context"
"log"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud/openstack/elb/v3/certificates"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/common"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/config"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/utils"
)
func ResourceCertificateV3() *schema.Resource {
return &schema.Resource{
CreateContext: resourceCertificateV3Create,
ReadContext: resourceCertificateV3Read,
UpdateContext: resourceCertificateV3Update,
DeleteContext: resourceCertificateV3Delete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "server",
ValidateFunc: validation.StringInSlice([]string{
"server", "client",
}, true),
},
"domain": {
Type: schema.TypeString,
Optional: true,
},
"private_key": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: utils.SuppressNewLineDiffs,
},
"certificate": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: utils.SuppressNewLineDiffs,
},
"expire_time": {
Type: schema.TypeString,
Computed: true,
},
"create_time": {
Type: schema.TypeString,
Computed: true,
},
"update_time": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceCertificateV3Create(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := config.GetHcsConfig(meta)
elbClient, err := cfg.ElbV3Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating ELB client: %s", err)
}
createOpts := certificates.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Type: d.Get("type").(string),
Domain: d.Get("domain").(string),
PrivateKey: d.Get("private_key").(string),
Certificate: d.Get("certificate").(string),
EnterpriseProjectID: cfg.GetEnterpriseProjectID(d),
}
log.Printf("[DEBUG] Create ELB certificate options: %#v", createOpts)
certificate, err := certificates.Create(elbClient, createOpts).Extract()
if err != nil {
return diag.Errorf("error creating Certificate: %s", err)
}
// If all has been successful, set the ID on the resource
d.SetId(certificate.ID)
return resourceCertificateV3Read(ctx, d, meta)
}
func resourceCertificateV3Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := config.GetHcsConfig(meta)
elbClient, err := cfg.ElbV3Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating ELB client: %s", err)
}
certificate, err := certificates.Get(elbClient, d.Id()).Extract()
if err != nil {
return common.CheckDeletedDiag(d, err, "error querying certificate")
}
log.Printf("[DEBUG] Retrieved certificate %s: %#v", d.Id(), certificate)
mErr := multierror.Append(nil,
d.Set("name", certificate.Name),
d.Set("description", certificate.Description),
d.Set("type", certificate.Type),
d.Set("domain", certificate.Domain),
d.Set("certificate", certificate.Certificate),
d.Set("private_key", certificate.PrivateKey),
d.Set("create_time", certificate.CreateTime),
d.Set("update_time", certificate.UpdateTime),
d.Set("expire_time", certificate.ExpireTime),
d.Set("region", cfg.GetRegion(d)),
)
if err := mErr.ErrorOrNil(); err != nil {
return diag.Errorf("error setting Dedicated ELB Certificate fields: %s", err)
}
return nil
}
func resourceCertificateV3Update(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := config.GetHcsConfig(meta)
elbClient, err := cfg.ElbV3Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating ELB client: %s", err)
}
var updateOpts certificates.UpdateOpts
if d.HasChange("name") {
updateOpts.Name = d.Get("name").(string)
}
if d.HasChange("description") {
desc := d.Get("description").(string)
updateOpts.Description = &desc
}
if d.HasChange("domain") {
updateOpts.Domain = d.Get("domain").(string)
}
if d.HasChange("private_key") {
updateOpts.PrivateKey = d.Get("private_key").(string)
}
if d.HasChange("certificate") {
updateOpts.Certificate = d.Get("certificate").(string)
}
log.Printf("[DEBUG] Updating certificate %s with options: %#v", d.Id(), updateOpts)
_, err = certificates.Update(elbClient, d.Id(), updateOpts).Extract()
if err != nil {
return diag.Errorf("error updating certificate %s: %s", d.Id(), err)
}
return resourceCertificateV3Read(ctx, d, meta)
}
func resourceCertificateV3Delete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := config.GetHcsConfig(meta)
elbClient, err := cfg.ElbV3Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating ELB client: %s", err)
}
log.Printf("[DEBUG] Deleting certificate %s", d.Id())
err = certificates.Delete(elbClient, d.Id()).ExtractErr()
if err != nil {
if utils.IsResourceNotFound(err) {
log.Printf("[INFO] deleting an unavailable certificate: %s", d.Id())
return nil
}
return diag.Errorf("error deleting certificate %s: %s", d.Id(), err)
}
return nil
}