Skip to content

Commit

Permalink
feat: add new resource for keys api
Browse files Browse the repository at this point in the history
Introduces a new logdna_key resource to interact with the keys api to
manage service and ingestion keys.

This change includes some additional upkeep changes:

* Update github.com/hashicorp/terraform-plugin-sdk/v2 to v2.15.0
* Remove duplicate code around existence testing
* Have requests use the http client attached to the provider instead of creating
  a new one each time
* Ensure all tests respect the env API_URL
* Fix memory reference error when testing existence of non-existent resource

Ref: LOG-12483
Signed-off-by: Jacob Hull [email protected]
  • Loading branch information
jakedipity committed Jun 1, 2022
1 parent 44e0f20 commit 51a328a
Show file tree
Hide file tree
Showing 17 changed files with 428 additions and 214 deletions.
61 changes: 61 additions & 0 deletions docs/resources/logdna_key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Resource: `logdna_key`

This resource allows you to manage ingestion and service keys.

## Example

```hcl
provider "logdna" {
servicekey = "xxxxxxxxxxxxxxxxxxxxxxxx"
}
resource "logdna_key" "service-key" {
type = "service"
lifecycle {
create_before_destroy = true
}
}
resource "logdna_key" "ingestion-key" {
type = "ingestion"
lifecycle {
create_before_destroy = true
}
}
```

The `create_before_destroy` and `lifecycle` meta-argument are not required, but ensure a valid key is always available so there's no disruption of service.

## Key Rotation

This resource can be used in conjuction with automated scripts to perform automatic key rotations, e.g.,

```sh
# Run this every time you want to rotate the key
$ terraform apply -replace="logdna_key.my_key"
```

## Argument Reference

The following arguments are supported:

- `type`: **string** _(Required)_ The type of key to be used. Should be either `service` or `ingestion`.

## Attributes Reference

In addition to all the arguments above, the following attributes are exported:

- `id`: **string** The unique identifier of this key.
- `key`: **string** The actual key value.
- `type`: **string** The type of key.
- `created`: **int** The date the key was created in Unix time milliseconds.

## Import

A key can be imported using the `id`, e.g.,

```sh
$ terraform import logdna_key.my_key <id>
```
19 changes: 19 additions & 0 deletions logdna/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"
"regexp"
"strings"

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

const tmplPc = `provider "logdna" {
Expand Down Expand Up @@ -148,3 +151,19 @@ func fmtBlockArgs(nstLvl int, opts map[string]string) string {
}
return blkCfg.String()
}

func testResourceExists(rsType string, rsName string) resource.TestCheckFunc {
identifier := fmt.Sprintf("logdna_%s.%s", rsType, rsName)

return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[identifier]
if !ok {
return fmt.Errorf("Not found: %s", identifier)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No ID set")
}

return nil
}
}
10 changes: 5 additions & 5 deletions logdna/data_source_alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ func TestDataAlert_BulkChannels(t *testing.T) {
"email": cloneDefaults(chnlDefaults["email"]),
"email1": cloneDefaults(chnlDefaults["email"]),
}
emsCfg := fmtTestConfigResource("alert", "test", nilLst, alertDefaults, emArgs, nilLst)
emsCfg := fmtTestConfigResource("alert", "test", globalPcArgs, alertDefaults, emArgs, nilLst)

pdArgs := map[string]map[string]string{
"pagerduty": cloneDefaults(chnlDefaults["pagerduty"]),
"pagerduty1": cloneDefaults(chnlDefaults["pagerduty"]),
}
pdsCfg := fmtTestConfigResource("alert", "test", nilLst, alertDefaults, pdArgs, nilLst)
pdsCfg := fmtTestConfigResource("alert", "test", globalPcArgs, alertDefaults, pdArgs, nilLst)

slArgs := map[string]map[string]string{
"slack": cloneDefaults(chnlDefaults["slack"]),
"slack1": cloneDefaults(chnlDefaults["slack"]),
}
slsCfg := fmtTestConfigResource("alert", "test", nilLst, alertDefaults, slArgs, nilLst)
slsCfg := fmtTestConfigResource("alert", "test", globalPcArgs, alertDefaults, slArgs, nilLst)

wbArgs := map[string]map[string]string{
"webhook": cloneDefaults(chnlDefaults["webhook"]),
"webhook1": cloneDefaults(chnlDefaults["webhook"]),
}
wbsCfg := fmtTestConfigResource("alert", "test", nilLst, alertDefaults, wbArgs, nilLst)
wbsCfg := fmtTestConfigResource("alert", "test", globalPcArgs, alertDefaults, wbArgs, nilLst)

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestDataSourceAlert_MultipleChannels(t *testing.T) {
"slack": cloneDefaults(chnlDefaults["slack"]),
"webhook": cloneDefaults(chnlDefaults["webhook"]),
}
fmtCfg := fmt.Sprintf("%s\n%s", fmtTestConfigResource("alert", "test", nilLst, alertDefaults, chArgs, nilLst), ds)
fmtCfg := fmt.Sprintf("%s\n%s", fmtTestConfigResource("alert", "test", globalPcArgs, alertDefaults, chArgs, nilLst), ds)

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Expand Down
1 change: 1 addition & 0 deletions logdna/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func Provider() *schema.Provider {
"logdna_stream_exclusion": resourceStreamExclusion(),
"logdna_ingestion_exclusion": resourceIngestionExclusion(),
"logdna_archive": resourceArchiveConfig(),
"logdna_key": resourceKey(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
1 change: 1 addition & 0 deletions logdna/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

var serviceKey = os.Getenv("SERVICE_KEY")
var apiHostUrl = os.Getenv("API_URL")
var globalPcArgs = []string {serviceKey, apiHostUrl}
var testAccProviders map[string]*schema.Provider
var testAccProvider *schema.Provider

Expand Down
5 changes: 2 additions & 3 deletions logdna/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"io/ioutil"
"net/http"
"time"
)

type httpRequest func(string, string, io.Reader) (*http.Request, error)
Expand All @@ -32,8 +31,8 @@ type requestConfig struct {
// newRequestConfig abstracts the struct creation to allow for mocking
func newRequestConfig(pc *providerConfig, method string, uri string, body interface{}, mutators ...func(*requestConfig)) *requestConfig {
rc := &requestConfig{
serviceKey: pc.serviceKey,
httpClient: &http.Client{Timeout: 15 * time.Second},
serviceKey: pc.serviceKey,
httpClient: pc.httpClient,
apiURL: fmt.Sprintf("%s%s", pc.baseURL, uri), // uri should have a preceding slash (/)
method: method,
body: body,
Expand Down
3 changes: 2 additions & 1 deletion logdna/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -40,7 +41,7 @@ func setJSONMarshal(customMarshaller jsonMarshal) func(*requestConfig) {

func TestRequest_MakeRequest(t *testing.T) {
assert := assert.New(t)
pc := providerConfig{serviceKey: "abc123"}
pc := providerConfig{serviceKey: "abc123", httpClient: &http.Client{Timeout: 15 * time.Second}}
resourceID := "test123456"

t.Run("Server receives proper method, URL, and headers", func(t *testing.T) {
Expand Down
Loading

0 comments on commit 51a328a

Please sign in to comment.