Skip to content

Commit

Permalink
add tflogs to rpaas_certificate + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tcarreira committed Aug 18, 2022
1 parent 8b92c87 commit 86f547c
Showing 1 changed file with 65 additions and 16 deletions.
81 changes: 65 additions & 16 deletions internal/provider/resource_rpaas_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ package provider
import (
"context"
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

Expand All @@ -18,9 +18,9 @@ import (

func resourceRpaasCertificate() *schema.Resource {
return &schema.Resource{
CreateContext: resourceRpaasCertificateUpsert,
CreateContext: resourceRpaasCertificateCreate,
ReadContext: resourceRpaasCertificateRead,
UpdateContext: resourceRpaasCertificateUpsert,
UpdateContext: resourceRpaasCertificateUpdate,
DeleteContext: resourceRpaasCertificateDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
Expand Down Expand Up @@ -60,11 +60,12 @@ func resourceRpaasCertificate() *schema.Resource {
}
}

func resourceRpaasCertificateUpsert(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func resourceRpaasCertificateCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
provider := meta.(*rpaasProvider)

instance := d.Get("instance").(string)
serviceName := d.Get("service_name").(string)
certName := d.Get("name").(string)

rpaasClient, err := provider.RpaasClient.SetService(serviceName)
if err != nil {
Expand All @@ -73,36 +74,41 @@ func resourceRpaasCertificateUpsert(ctx context.Context, d *schema.ResourceData,

args := rpaas_client.UpdateCertificateArgs{
Instance: instance,
Name: d.Get("name").(string),
Name: certName,
Certificate: d.Get("certificate").(string),
Key: d.Get("key").(string),
}
log.Printf("[DEBUG] creating certificate for instance %s, certificate name %s", args.Instance, args.Certificate)

tflog.Info(ctx, "Create rpaas_certificate", map[string]interface{}{
"service": serviceName,
"instance": instance,
"name": certName,
})

err = rpaasRetry(ctx, d, func() error {
return rpaasClient.UpdateCertificate(ctx, args)
return rpaasClient.UpdateCertificate(ctx, args) // UpdateCertificate is really an upsert
})

if err != nil {
return diag.Errorf("Unable to create/update certificate %s for instance %s: %v", args.Certificate, instance, err)
return diag.Errorf("Unable to create certificate %s for instance %s: %v", certName, instance, err)
}

d.SetId(fmt.Sprintf("%s::%s::%s", serviceName, instance, args.Name))
d.SetId(fmt.Sprintf("%s::%s::%s", serviceName, instance, certName))
return resourceRpaasCertificateRead(ctx, d, meta)
}

func resourceRpaasCertificateRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
provider := meta.(*rpaasProvider)

serviceName, instance, name, err := parseRpaasCertificateID(d.Id())
serviceName, instance, certName, err := parseRpaasCertificateID(d.Id())
if err != nil {
return diag.Errorf("Unable to parse Certificate ID: %v", err)
}

d.SetId(fmt.Sprintf("%s::%s::%s", serviceName, instance, name))
d.SetId(fmt.Sprintf("%s::%s::%s", serviceName, instance, certName))
d.Set("service_name", serviceName)
d.Set("instance", instance)
d.Set("name", name)
d.Set("name", certName)

rpaasClient, err := provider.RpaasClient.SetService(serviceName)
if err != nil {
Expand All @@ -119,7 +125,7 @@ func resourceRpaasCertificateRead(ctx context.Context, d *schema.ResourceData, m
}

for _, certificate := range info.Certificates {
if certificate.Name == name {
if certificate.Name == certName {
return nil
}
}
Expand All @@ -129,26 +135,69 @@ func resourceRpaasCertificateRead(ctx context.Context, d *schema.ResourceData, m
return nil
}

func resourceRpaasCertificateUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
provider := meta.(*rpaasProvider)

serviceName, instance, certName, err := parseRpaasCertificateID(d.Id())
if err != nil {
return diag.Errorf("Unable to parse Certificate ID: %v", err)
}

rpaasClient, err := provider.RpaasClient.SetService(serviceName)
if err != nil {
return diag.Errorf("Unable to create client for service %s: %v", serviceName, err)
}

args := rpaas_client.UpdateCertificateArgs{
Instance: instance,
Name: certName,
Certificate: d.Get("certificate").(string),
Key: d.Get("key").(string),
}

tflog.Info(ctx, "Update rpaas_certificate", map[string]interface{}{
"service": serviceName,
"instance": instance,
"name": certName,
})

err = rpaasRetry(ctx, d, func() error {
return rpaasClient.UpdateCertificate(ctx, args)
})

if err != nil {
return diag.Errorf("Unable to update certificate %s for instance %s: %v", certName, instance, err)
}

return resourceRpaasCertificateRead(ctx, d, meta)
}

func resourceRpaasCertificateDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
provider := meta.(*rpaasProvider)

instance := d.Get("instance").(string)
serviceName := d.Get("service_name").(string)
name := d.Get("name").(string)
certName := d.Get("name").(string)
rpaasClient, err := provider.RpaasClient.SetService(serviceName)
if err != nil {
return diag.Errorf("Unable to create client for service %s: %v", serviceName, err)
}

tflog.Info(ctx, "Delete rpaas_certificate", map[string]interface{}{
"service": serviceName,
"instance": instance,
"name": certName,
})

err = rpaasRetry(ctx, d, func() error {
return rpaasClient.DeleteCertificate(ctx, rpaas_client.DeleteCertificateArgs{
Instance: instance,
Name: name,
Name: certName,
})
})

if err != nil {
return diag.Errorf("Unable to remove certificate for instance %s: %v", instance, err)
return diag.Errorf("Unable to remove certificate %s for instance %s: %v", certName, instance, err)
}
return nil
}
Expand Down

0 comments on commit 86f547c

Please sign in to comment.