Skip to content

Commit

Permalink
[WIP] Support Managed Load Balancer as Resources
Browse files Browse the repository at this point in the history
  • Loading branch information
hico-horiuchi committed May 2, 2024
1 parent 2cb1612 commit 6b349bf
Show file tree
Hide file tree
Showing 18 changed files with 6,966 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ecl/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ func Provider() terraform.ResourceProvider {
"ecl_imagestorages_image_v2": resourceImageStoragesImageV2(),
"ecl_imagestorages_member_accepter_v2": resourceImageStoragesMemberAccepterV2(),
"ecl_imagestorages_member_v2": resourceImageStoragesMemberV2(),
"ecl_mlb_certificate_v1": resourceMLBCertificateV1(),
"ecl_mlb_health_monitor_v1": resourceMLBHealthMonitorV1(),
"ecl_mlb_listener_v1": resourceMLBListenerV1(),
"ecl_mlb_load_balancer_v1": resourceMLBLoadBalancerV1(),
"ecl_mlb_policy_v1": resourceMLBPolicyV1(),
"ecl_mlb_route_v1": resourceMLBRouteV1(),
"ecl_mlb_rule_v1": resourceMLBRuleV1(),
"ecl_mlb_target_group_v1": resourceMLBTargetGroupV1(),
"ecl_network_common_function_gateway_v2": resourceNetworkCommonFunctionGatewayV2(),
"ecl_network_gateway_interface_v2": resourceNetworkGatewayInterfaceV2(),
"ecl_network_internet_gateway_v2": resourceNetworkInternetGatewayV2(),
Expand Down
190 changes: 190 additions & 0 deletions ecl/resource_ecl_mlb_certificate_v1.go
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
}
Loading

0 comments on commit 6b349bf

Please sign in to comment.