-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Support Managed Load Balancer as Resources
- Loading branch information
1 parent
2cb1612
commit 6b349bf
Showing
18 changed files
with
6,966 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
package ecl | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
|
||
"github.com/nttcom/eclcloud/v2/ecl/managed_load_balancer/v1/certificates" | ||
) | ||
|
||
func certificateFileSchemaForResource() *schema.Schema { | ||
return &schema.Schema{ | ||
Type: schema.TypeMap, | ||
Required: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"content": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceMLBCertificateV1() *schema.Resource { | ||
var result *schema.Resource | ||
|
||
result = &schema.Resource{ | ||
Create: resourceMLBCertificateV1Create, | ||
Read: reourceMLBCertificateV1Read, | ||
Update: resourceMLBCertificateV1Update, | ||
Delete: resourceMLBCertificateV1Delete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"description": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"tags": &schema.Schema{ | ||
Type: schema.TypeMap, | ||
Optional: true, | ||
}, | ||
"tenant_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
"ca_cert": certificateFileSchemaForResource(), | ||
"ssl_cert": certificateFileSchemaForResource(), | ||
"ssl_key": certificateFileSchemaForResource(), | ||
}, | ||
} | ||
|
||
return result | ||
} | ||
|
||
func resourceMLBCertificateV1Create(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
createOpts := certificates.CreateOpts{ | ||
Name: d.Get("name").(string), | ||
Description: d.Get("description").(string), | ||
Tags: d.Get("tags").(map[string]interface{}), | ||
} | ||
|
||
managedLoadBalancerClient, err := config.managedLoadBalancerV1Client(GetRegion(d, config)) | ||
if err != nil { | ||
return fmt.Errorf("Error creating ECL managed load balancer client: %s", err) | ||
} | ||
|
||
log.Printf("[DEBUG] Creating ECL managed load balancer certificate with options %+v", createOpts) | ||
|
||
certificate, err := certificates.Create(managedLoadBalancerClient, createOpts).Extract() | ||
if err != nil { | ||
return fmt.Errorf("Error creating ECL managed load balancer certificate with options %+v: %s", createOpts, err) | ||
} | ||
|
||
d.SetId(certificate.ID) | ||
log.Printf("[INFO] ECL managed load balancer certificate ID: %s", certificate.ID) | ||
|
||
for _, fileType := range []string{"ca_cert", "ssl_cert", "ssl_key"} { | ||
file := d.Get(fileType).(map[string]interface{}) | ||
uploadFileOpts := certificates.UploadFileOpts{ | ||
Type: fileType, | ||
Content: file["content"].(string), | ||
} | ||
|
||
log.Printf("[DEBUG] Uploading ECL managed load balancer certificate file (%s) with options %+v", d.Id(), uploadFileOpts) | ||
|
||
err = certificates.UploadFile(managedLoadBalancerClient, certificate.ID, uploadFileOpts).ExtractErr() | ||
if err != nil { | ||
return fmt.Errorf("Error uploading ECL managed load balancer certificate file (%s) with options %+v: %s", d.Id(), uploadFileOpts, err) | ||
} | ||
} | ||
|
||
return reourceMLBCertificateV1Read(d, meta) | ||
} | ||
|
||
func reourceMLBCertificateV1Read(d *schema.ResourceData, meta interface{}) error { | ||
var certificate certificates.Certificate | ||
|
||
config := meta.(*Config) | ||
|
||
managedLoadBalancerClient, err := config.managedLoadBalancerV1Client(GetRegion(d, config)) | ||
if err != nil { | ||
return fmt.Errorf("Error creating ECL managed load balancer client: %s", err) | ||
} | ||
|
||
err = certificates.Show(managedLoadBalancerClient, d.Id()).ExtractInto(&certificate) | ||
if err != nil { | ||
return CheckDeleted(d, err, "certificate") | ||
} | ||
|
||
log.Printf("[DEBUG] Retrieved ECL managed load balancer certificate (%s): %+v", d.Id(), certificate) | ||
|
||
d.Set("name", certificate.Name) | ||
d.Set("description", certificate.Description) | ||
d.Set("tags", certificate.Tags) | ||
d.Set("tenant_id", certificate.TenantID) | ||
|
||
return nil | ||
} | ||
|
||
func resourceMLBCertificateV1Update(d *schema.ResourceData, meta interface{}) error { | ||
var isUpdated bool | ||
var updateOpts certificates.UpdateOpts | ||
|
||
config := meta.(*Config) | ||
|
||
managedLoadBalancerClient, err := config.managedLoadBalancerV1Client(GetRegion(d, config)) | ||
if err != nil { | ||
return fmt.Errorf("Error creating ECL managed load balancer client: %s", err) | ||
} | ||
|
||
if d.HasChange("name") { | ||
isUpdated = true | ||
name := d.Get("name").(string) | ||
updateOpts.Name = &name | ||
} | ||
|
||
if d.HasChange("description") { | ||
isUpdated = true | ||
description := d.Get("description").(string) | ||
updateOpts.Description = &description | ||
} | ||
|
||
if d.HasChange("tags") { | ||
isUpdated = true | ||
tags := d.Get("tags").(map[string]interface{}) | ||
updateOpts.Tags = &tags | ||
} | ||
|
||
if isUpdated { | ||
log.Printf("[DEBUG] Updating ECL managed load balancer certificate (%s) with options %+v", d.Id(), updateOpts) | ||
|
||
_, err := certificates.Update(managedLoadBalancerClient, d.Id(), updateOpts).Extract() | ||
if err != nil { | ||
return fmt.Errorf("Error updating ECL managed load balancer certificate (%s) with options %+v: %s", d.Id(), updateOpts, err) | ||
} | ||
} | ||
|
||
return reourceMLBCertificateV1Read(d, meta) | ||
} | ||
|
||
func resourceMLBCertificateV1Delete(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
managedLoadBalancerClient, err := config.managedLoadBalancerV1Client(GetRegion(d, config)) | ||
if err != nil { | ||
return fmt.Errorf("Error creating ECL managed load balancer client: %s", err) | ||
} | ||
|
||
log.Printf("[DEBUG] Deleting ECL managed load balancer certificate (%s)", d.Id()) | ||
|
||
err = certificates.Delete(managedLoadBalancerClient, d.Id()).ExtractErr() | ||
if err != nil { | ||
return fmt.Errorf("Error deleting ECL managed load balancer certificate: %s", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.