Skip to content

Commit

Permalink
feat: add new "logdna_enterprise_key" resource
Browse files Browse the repository at this point in the history
This new resource allows creation and management of enterprise-level
keys. This resource is separate from "logdna_key" which controls the
management of non-enterpise accounts.

Ref: LOG-15052
Signed-off-by: Jacob Hull <[email protected]>
  • Loading branch information
jakedipity committed Jan 6, 2023
1 parent 70f58e7 commit 12fb46d
Showing 1 changed file with 156 additions and 0 deletions.
156 changes: 156 additions & 0 deletions logdna/resource_enterprise_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package logdna

import (
"context"
"encoding/json"
"fmt"
"log"

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

var _ = registerTerraform(TerraformInfo{
name: "logdna_enterprise_key",
orgType: OrgTypeEnterprise,
terraformType: TerraformTypeResource,
schema: resourceEnterpriseKey(),
})

func resourceEnterpriseKeyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
pc := m.(*providerConfig)

key := keyRequest{}

if diags = key.CreateRequestBody(d); diags.HasError() {
return diags
}

req := newRequestConfig(
pc,
"POST",
"/v1/enterprise/keys",
nil,
)

body, err := req.MakeRequest()
log.Printf("[DEBUG] %s %s, payload is: %s", req.method, req.apiURL, body)

if err != nil {
return diag.FromErr(err)
}

createdKey := keyResponse{}
err = json.Unmarshal(body, &createdKey)
if err != nil {
return diag.FromErr(err)
}
log.Printf("[DEBUG] After %s key, the created key is %+v", req.method, createdKey)

d.SetId(createdKey.KeyID)

return resourceEnterpriseKeyRead(ctx, d, m)
}

func resourceEnterpriseKeyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics

pc := m.(*providerConfig)
keyID := d.Id()

req := newRequestConfig(
pc,
"GET",
fmt.Sprintf("/v1/enterprise/keys/%s", keyID),
nil,
)

body, err := req.MakeRequest()

log.Printf("[DEBUG] GET key raw response body %s\n", body)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Cannot read the remote key resource",
Detail: err.Error(),
})
return diags
}

key := keyResponse{}
err = json.Unmarshal(body, &key)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Cannot unmarshal response from the remote key resource",
Detail: err.Error(),
})
return diags
}
log.Printf("[DEBUG] The GET key structure is as follows: %+v\n", key)

// Top level keys can be set directly
appendError(d.Set("name", key.Name), &diags)
appendError(d.Set("id", key.KeyID), &diags)
appendError(d.Set("key", key.Key), &diags)
appendError(d.Set("created", key.Created), &diags)

return diags
}

func resourceEnterpriseKeyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
pc := m.(*providerConfig)
keyID := d.Id()

req := newRequestConfig(
pc,
"DELETE",
fmt.Sprintf("/v1/enterprise/keys/%s", keyID),
nil,
)

body, err := req.MakeRequest()
log.Printf("[DEBUG] %s %s key %s", req.method, req.apiURL, body)

if err != nil {
return diag.FromErr(err)
}

d.SetId("")
return nil
}

func resourceEnterpriseKey() *schema.Resource {
return &schema.Resource{
CreateContext: resourceEnterpriseKeyCreate,
ReadContext: resourceEnterpriseKeyRead,
DeleteContext: resourceEnterpriseKeyDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
ForceNew: true,
Computed: true,
},
"key": {
Type: schema.TypeString,
ForceNew: true,
Sensitive: true,
Computed: true,
},
"created": {
Type: schema.TypeInt,
ForceNew: true,
Computed: true,
},
},
}
}

0 comments on commit 12fb46d

Please sign in to comment.