diff --git a/influxdb/provider.go b/influxdb/provider.go index c8ebe00..09735b5 100644 --- a/influxdb/provider.go +++ b/influxdb/provider.go @@ -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{ diff --git a/influxdb/resource_database.go b/influxdb/resource_database.go index 7676c73..a8860ec 100644 --- a/influxdb/resource_database.go +++ b/influxdb/resource_database.go @@ -2,6 +2,7 @@ package influxdb import ( "fmt" + "reflect" "encoding/json" @@ -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) } } diff --git a/influxdb/resource_database_test.go b/influxdb/resource_database_test.go index c79d959..8febb55 100644 --- a/influxdb/resource_database_test.go +++ b/influxdb/resource_database_test.go @@ -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"), ), }, @@ -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", }), ), }, @@ -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", @@ -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", @@ -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) +}