Skip to content

Commit

Permalink
Added logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jbristowe committed Dec 10, 2020
1 parent 6039bae commit a77b2a7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
39 changes: 32 additions & 7 deletions octopusdeploy/resource_certificate.go
Expand Up @@ -2,6 +2,7 @@ package octopusdeploy

import (
"context"
"log"

"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -23,51 +24,75 @@ func resourceCertificate() *schema.Resource {
func resourceCertificateCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
certificate := expandCertificate(d)

log.Printf("[INFO] creating certificate: %#v", certificate)

client := m.(*octopusdeploy.Client)
createdCertificate, err := client.Certificates.Add(certificate)
if err != nil {
return diag.FromErr(err)
}

if err := setCertificate(ctx, d, createdCertificate); err != nil {
return diag.FromErr(err)
}

d.SetId(createdCertificate.GetID())
return resourceCertificateRead(ctx, d, m)

log.Printf("[INFO] certificate created (%s)", d.Id())
return nil
}

func resourceCertificateDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] deleting certificate (%s)", d.Id())

client := m.(*octopusdeploy.Client)
err := client.Certificates.DeleteByID(d.Id())
if err != nil {
if err := client.Certificates.DeleteByID(d.Id()); err != nil {
return diag.FromErr(err)
}

d.SetId("")

log.Printf("[INFO] certificate deleted")
return nil
}

func resourceCertificateRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] reading certificate (%s)", d.Id())

client := m.(*octopusdeploy.Client)
certificate, err := client.Certificates.GetByID(d.Id())
if err != nil {
apiError := err.(*octopusdeploy.APIError)
if apiError.StatusCode == 404 {
log.Printf("[INFO] certificate (%s) not found; deleting from state", d.Id())
d.SetId("")
return nil
}
return diag.FromErr(err)
}

setCertificate(ctx, d, certificate)
if err := setCertificate(ctx, d, certificate); err != nil {
return diag.FromErr(err)
}

log.Printf("[INFO] certificate read (%s)", d.Id())
return nil
}

func resourceCertificateUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
certificate := expandCertificate(d)
log.Printf("[INFO] updating certificate (%s)", d.Id())

certificate := expandCertificate(d)
client := m.(*octopusdeploy.Client)
_, err := client.Certificates.Update(*certificate)
updatedCertificate, err := client.Certificates.Update(*certificate)
if err != nil {
return diag.FromErr(err)
}

return resourceCertificateRead(ctx, d, m)
if err := setCertificate(ctx, d, updatedCertificate); err != nil {
return diag.FromErr(err)
}

log.Printf("[INFO] certificate updated (%s)", d.Id())
return nil
}
39 changes: 32 additions & 7 deletions octopusdeploy/resource_channel.go
Expand Up @@ -2,6 +2,7 @@ package octopusdeploy

import (
"context"
"log"

"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -23,51 +24,75 @@ func resourceChannel() *schema.Resource {
func resourceChannelCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
channel := expandChannel(d)

log.Printf("[INFO] creating channel: %#v", channel)

client := m.(*octopusdeploy.Client)
createdChannel, err := client.Channels.Add(channel)
if err != nil {
return diag.FromErr(err)
}

if err := setChannel(ctx, d, createdChannel); err != nil {
return diag.FromErr(err)
}

d.SetId(createdChannel.GetID())
return resourceChannelRead(ctx, d, m)

log.Printf("[INFO] channel created (%s)", d.Id())
return nil
}

func resourceChannelDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] deleting channel (%s)", d.Id())

client := m.(*octopusdeploy.Client)
err := client.Channels.DeleteByID(d.Id())
if err != nil {
if err := client.Channels.DeleteByID(d.Id()); err != nil {
return diag.FromErr(err)
}

d.SetId("")

log.Printf("[INFO] channel deleted")
return nil
}

func resourceChannelRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] reading channel (%s)", d.Id())

client := m.(*octopusdeploy.Client)
channel, err := client.Channels.GetByID(d.Id())
if err != nil {
apiError := err.(*octopusdeploy.APIError)
if apiError.StatusCode == 404 {
log.Printf("[INFO] channel (%s) not found; deleting from state", d.Id())
d.SetId("")
return nil
}
return diag.FromErr(err)
}

setChannel(ctx, d, channel)
if err := setChannel(ctx, d, channel); err != nil {
return diag.FromErr(err)
}

log.Printf("[INFO] channel read (%s)", d.Id())
return nil
}

func resourceChannelUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
channel := expandChannel(d)
log.Printf("[INFO] updating channel (%s)", d.Id())

channel := expandChannel(d)
client := m.(*octopusdeploy.Client)
_, err := client.Channels.Update(channel)
updatedChannel, err := client.Channels.Update(channel)
if err != nil {
return diag.FromErr(err)
}

return resourceChannelRead(ctx, d, m)
if err := setChannel(ctx, d, updatedChannel); err != nil {
return diag.FromErr(err)
}

log.Printf("[INFO] channel updated (%s)", d.Id())
return nil
}

0 comments on commit a77b2a7

Please sign in to comment.