Skip to content

Commit 0645c4c

Browse files
committed
Add support for load balancer certificate imports
This requires a new composite ID for load balancer certificates
1 parent 3d915f7 commit 0645c4c

File tree

5 files changed

+78
-9
lines changed

5 files changed

+78
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
- Support for specifying network_type in `launch_options` for the `core_instance` resource
66
- Support for `home_region` and `time_created` attributes in health_checks resources and datasources
77
- Support for custom scheduled backup policies in Block Storage
8+
- Support for importing `oci_load_balancer_certificate` resource
9+
10+
### Notes
11+
Starting with this version, newly created load balancer certificates will have an `id` in the form of `loadBalancers/{loadBalancerId}/certificates/{certificateName}`.
12+
Load balancer certificates created with previous versions and upgrading to this version will continue to store `id` in the form of `{certificateName}`.
813

914
## 3.46.0 (October 02, 2019)
1015

examples/load_balancer/lb_full/lb_full.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ resource "oci_load_balancer_rule_set" "test_rule_set" {
349349

350350
conditions {
351351
attribute_name = "SOURCE_VCN_ID"
352-
attribute_value = "${oci_core_virtual_network.vcn1.id}"
352+
attribute_value = "${oci_core_vcn.vcn1.id}"
353353
}
354354

355355
conditions {

oci/load_balancer_certificate_resource.go

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ package provider
55
import (
66
"context"
77
"errors"
8+
"fmt"
9+
"log"
10+
"net/url"
11+
"regexp"
12+
"strings"
813

914
"github.com/hashicorp/terraform/helper/schema"
1015

@@ -13,6 +18,9 @@ import (
1318

1419
func LoadBalancerCertificateResource() *schema.Resource {
1520
return &schema.Resource{
21+
Importer: &schema.ResourceImporter{
22+
State: schema.ImportStatePassthrough,
23+
},
1624
Timeouts: DefaultTimeout,
1725
Create: createLoadBalancerCertificate,
1826
Read: readLoadBalancerCertificate,
@@ -102,12 +110,12 @@ type LoadBalancerCertificateResourceCrud struct {
102110
}
103111

104112
func (s *LoadBalancerCertificateResourceCrud) ID() string {
105-
id, workSuccess := LoadBalancerResourceID(s.Res, s.WorkRequest)
106-
if id != nil {
107-
return *id
108-
}
109-
if workSuccess {
110-
return s.D.Get("certificate_name").(string)
113+
if s.WorkRequest != nil {
114+
if s.WorkRequest.LifecycleState == oci_load_balancer.WorkRequestLifecycleStateSucceeded {
115+
return getCertificateCompositeId(s.D.Get("certificate_name").(string), s.D.Get("load_balancer_id").(string))
116+
} else {
117+
return *s.WorkRequest.Id
118+
}
111119
}
112120
return ""
113121
}
@@ -211,14 +219,25 @@ func (s *LoadBalancerCertificateResourceCrud) Get() error {
211219
request.LoadBalancerId = &tmp
212220
}
213221

222+
certificateName := s.D.Get("certificate_name").(string)
223+
224+
if !strings.HasPrefix(s.D.Id(), "ocid1.loadbalancerworkrequest.") {
225+
certNameFromId, loadBalancerId, err := parseCertificateCompositeId(s.D.Id())
226+
if err == nil {
227+
certificateName = certNameFromId
228+
request.LoadBalancerId = &loadBalancerId
229+
} else {
230+
log.Printf("[WARN] Get() unable to parse current ID: %s", s.D.Id())
231+
}
232+
}
233+
214234
request.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "load_balancer")
215235

216236
response, err := s.Client.ListCertificates(context.Background(), request)
217237
if err != nil {
218238
return err
219239
}
220240

221-
certificateName := s.D.Get("certificate_name").(string)
222241
for _, item := range response.Items {
223242
if *item.CertificateName == certificateName {
224243
s.Res = &item
@@ -269,6 +288,15 @@ func (s *LoadBalancerCertificateResourceCrud) SetData() error {
269288
if s.Res == nil {
270289
return nil
271290
}
291+
292+
certificateName, loadBalancerId, err := parseCertificateCompositeId(s.D.Id())
293+
if err == nil {
294+
s.D.Set("certificate_name", &certificateName)
295+
s.D.Set("load_balancer_id", &loadBalancerId)
296+
} else {
297+
log.Printf("[WARN] SetData() unable to parse current ID: %s", s.D.Id())
298+
}
299+
272300
if s.Res.CaCertificate != nil {
273301
s.D.Set("ca_certificate", *s.Res.CaCertificate)
274302
}
@@ -283,3 +311,23 @@ func (s *LoadBalancerCertificateResourceCrud) SetData() error {
283311

284312
return nil
285313
}
314+
315+
func getCertificateCompositeId(certificateName string, loadBalancerId string) string {
316+
certificateName = url.PathEscape(certificateName)
317+
loadBalancerId = url.PathEscape(loadBalancerId)
318+
compositeId := "loadBalancers/" + loadBalancerId + "/certificates/" + certificateName
319+
return compositeId
320+
}
321+
322+
func parseCertificateCompositeId(compositeId string) (certificateName string, loadBalancerId string, err error) {
323+
parts := strings.Split(compositeId, "/")
324+
match, _ := regexp.MatchString("loadBalancers/.*/certificates/.*", compositeId)
325+
if !match || len(parts) != 4 {
326+
err = fmt.Errorf("illegal compositeId %s encountered", compositeId)
327+
return
328+
}
329+
loadBalancerId, _ = url.PathUnescape(parts[1])
330+
certificateName, _ = url.PathUnescape(parts[3])
331+
332+
return
333+
}

oci/load_balancer_certificate_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ func TestLoadBalancerCertificateResource_basic(t *testing.T) {
104104
resource.TestCheckResourceAttr(datasourceName, "certificates.0.public_certificate", "-----BEGIN CERTIFICATE-----\nMIIC9jCCAd4CCQD2rPUVJETHGzANBgkqhkiG9w0BAQsFADA9MQswCQYDVQQGEwJV\nUzELMAkGA1UECAwCV0ExEDAOBgNVBAcMB1NlYXR0bGUxDzANBgNVBAoMBk9yYWNs\nZTAeFw0xOTAxMTcyMjU4MDVaFw0yMTAxMTYyMjU4MDVaMD0xCzAJBgNVBAYTAlVT\nMQswCQYDVQQIDAJXQTEQMA4GA1UEBwwHU2VhdHRsZTEPMA0GA1UECgwGT3JhY2xl\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA30+wt7OlUB/YpmWbTRkx\nnLG0lKWiV+oupNKj8luXmC5jvOFTUejt1pQhpA47nCqywlOAfk2N8hJWTyJZUmKU\n+DWVV2So2B/obYxpiiyWF2tcF/cYi1kBYeAIu5JkVFwDe4ITK/oQUFEhIn3Qg/oC\nMQ2985/MTdCXONgnbmePU64GrJwfvOeJcQB3VIL1BBfISj4pPw5708qTRv5MJBOO\njLKRM68KXC5us4879IrSA77NQr1KwjGnQlykyCgGvvgwgrUTd5c/dH8EKrZVcFi6\nytM66P/1CTpk1YpbI4gqiG0HBbuXG4JRIjyzW4GT4JXeSjgvrkIYL8k/M4Az1WEc\n2wIDAQABMA0GCSqGSIb3DQEBCwUAA4IBAQAuI53m8Va6EafDi6GQdQrzNNQFCAVQ\nxIABAB0uaSYCs3H+pqTktHzOrOluSUEogXRl0UU5/OuvxAz4idA4cfBdId4i7AcY\nqZsBjA/xqH/rxR3pcgfaGyxQzrUsJFf0ZwnzqYJs7fUvuatHJYi/cRBxrKR2+4Oj\nlUbb9TSmezlzHK5CaD5XzN+lZqbsSvN3OQbOryJCbtjZVQFGZ1SmL6OLrwpbBKuP\nn2ob+gaP57YSzO3zk1NDXMlQPHRsdSOqocyKx8y+7J0g6MqPvBzIe+wI3QW85MQY\nj1/IHmj84LNGp7pHCyiYx/oI+00gRch04H2pJv0TP3sAQ37gplBwDrUo\n-----END CERTIFICATE-----"),
105105
),
106106
},
107+
// verify resource import
108+
{
109+
Config: config,
110+
ImportState: true,
111+
ImportStateVerify: true,
112+
ImportStateVerifyIgnore: []string{
113+
"passphrase",
114+
"private_key",
115+
"state",
116+
},
117+
ResourceName: resourceName,
118+
},
107119
},
108120
})
109121
}

website/docs/r/load_balancer_certificate.html.markdown

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,9 @@ The following attributes are exported:
117117

118118
## Import
119119

120-
Import is not supported for this resource.
120+
Certificates can be imported using the `id`, e.g.
121+
122+
```
123+
$ terraform import oci_load_balancer_certificate.test_certificate "loadBalancers/{loadBalancerId}/certificates/{certificateName}"
124+
```
121125

0 commit comments

Comments
 (0)