Skip to content

Commit

Permalink
db
Browse files Browse the repository at this point in the history
  • Loading branch information
DrFaust92 committed Jan 25, 2022
1 parent 127a594 commit d00a75b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 49 deletions.
3 changes: 0 additions & 3 deletions influxdb/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ package influxdb
import (
"fmt"
"net/url"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/influxdata/influxdb/client"
)

var quoteReplacer = strings.NewReplacer(`"`, `\"`)

func Provider() *schema.Provider {
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
Expand Down
22 changes: 15 additions & 7 deletions influxdb/resource_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package influxdb

import (
"fmt"
"reflect"

"encoding/json"

Expand Down Expand Up @@ -175,29 +176,36 @@ func readRetentionPolicies(d *schema.ResourceData, meta interface{}) error {
return resp.Err
}

defaultRetentionPolicy := map[string]interface{}{
"name": "autogen",
"duration": "0s",
"shardgroupduration": "168h0m0s",
"replication": 1,
"default": true,
}

retentionPolicies := []interface{}{}

if resp.Results[0].Err == nil {
for _, result := range resp.Results[0].Series[0].Values {

name := result[0].(string)
if name == "autogen" {
continue
}

replication, err := result[3].(json.Number).Int64()
if err != nil {
return err
}

retentionPolicy := map[string]interface{}{
"name": name,
"name": result[0].(string),
"duration": result[1].(string),
"shardgroupduration": result[2].(string),
"replication": replication,
"replication": int(replication),
"default": result[4].(bool),
}

if reflect.DeepEqual(retentionPolicy, defaultRetentionPolicy) {
continue
}

retentionPolicies = append(retentionPolicies, retentionPolicy)
}
}
Expand Down
85 changes: 46 additions & 39 deletions influxdb/resource_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/influxdata/influxdb/client"
)

func TestAccInfluxDBDatabase_basic(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")

resourceName := "influxdb_database.test"
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDatabaseConfig,
Config: testAccDatabaseConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDatabaseExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", "terraform-test"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "retention_policies.#", "0"),
),
},
Expand All @@ -33,21 +35,22 @@ func TestAccInfluxDBDatabase_basic(t *testing.T) {
}

func TestAccInfluxDBDatabase_retention(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")

resourceName := "influxdb_database.test"
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDatabaseWithRPSConfig,
Config: testAccDatabaseWithRPSConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDatabaseExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", "terraform-rp-test"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "retention_policies.#", "1"),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "retention_policies.*", map[string]string{
"name": "1day",
"duration": "24h0m0s",
"default": "true",
"default": "false",
}),
),
},
Expand All @@ -57,10 +60,10 @@ func TestAccInfluxDBDatabase_retention(t *testing.T) {
ImportStateVerify: true,
},
{
Config: testAccDatabaseWithRPSUpdateConfig,
Config: testAccDatabaseWithRPSUpdateConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDatabaseExists(resourceName),
resource.TestCheckResourceAttr("influxdb_database.test", "name", "terraform-rp-test"),
resource.TestCheckResourceAttr("influxdb_database.test", "name", rName),
resource.TestCheckResourceAttr(resourceName, "retention_policies.#", "3"),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "retention_policies.*", map[string]string{
"name": "2days",
Expand All @@ -70,7 +73,7 @@ func TestAccInfluxDBDatabase_retention(t *testing.T) {
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "retention_policies.*", map[string]string{
"name": "12weeks",
"duration": "2016h0m0s",
"default": "true",
"default": "false",
}),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "retention_policies.*", map[string]string{
"name": "1week",
Expand Down Expand Up @@ -120,44 +123,48 @@ func testAccCheckDatabaseExists(n string) resource.TestCheckFunc {
}
}

var testAccDatabaseConfig = `
func testAccDatabaseConfig(rName string) string {
return fmt.Sprintf(`
resource "influxdb_database" "test" {
name = "terraform-test"
name = %[1]q
}
`, rName)
}

`

var testAccDatabaseWithRPSConfig = `
func testAccDatabaseWithRPSConfig(rName string) string {
return fmt.Sprintf(`
resource "influxdb_database" "test" {
name = "terraform-rp-test"
retention_policies {
name = "1day"
duration = "24h0m0s"
default = "true"
}
name = %[1]q
retention_policies {
name = "1day"
duration = "24h0m0s"
default = "false"
}
}
`, rName)
}
`

var testAccDatabaseWithRPSUpdateConfig = `
func testAccDatabaseWithRPSUpdateConfig(rName string) string {
return fmt.Sprintf(`
resource "influxdb_database" "test" {
name = "terraform-rp-test"
name = %[1]q
retention_policies {
name = "2days"
duration = "48h0m0s"
}
retention_policies {
name = "2days"
duration = "48h0m0s"
}
retention_policies {
name = "12weeks"
duration = "2016h0m0s"
default = "true"
}
retention_policies {
name = "1week"
duration = "168h0m0s"
shardgroupduration = "2h0m0s"
}
retention_policies {
name = "12weeks"
duration = "2016h0m0s"
default = "false"
}
`
retention_policies {
name = "1week"
duration = "168h0m0s"
shardgroupduration = "2h0m0s"
}
}
`, rName)
}

0 comments on commit d00a75b

Please sign in to comment.