Skip to content

Commit

Permalink
Merge pull request #9 from geronimo-iia/master
Browse files Browse the repository at this point in the history
Continuous Query Error Handling
  • Loading branch information
DrFaust92 authored Nov 11, 2023
2 parents 4241ef0 + 9cac3a2 commit dd7445d
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 331 deletions.
11 changes: 8 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ jobs:
-
name: Unshallow
run: git fetch --prune --unshallow
-
-
name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.16
go-version: '1.16'

- name: flux requirements
run: |
sudo apt-get install -y clang pkg-config
go get github.com/influxdata/pkg-config
-
name: Import GPG key
id: import_gpg
Expand Down
12 changes: 10 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: Test suite
on:
pull_request:
types: [opened, synchronize, reopened]
push:
branches:
- 'master'
jobs:
test:
name: Run tests
Expand All @@ -11,9 +14,14 @@ jobs:
uses: actions/[email protected]

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.16
go-version: '1.16'

- name: flux requirements
run: |
sudo apt-get install -y clang pkg-config
go get github.com/influxdata/pkg-config
- name: Test
run: make test build
29 changes: 28 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export TF_ACC_TERRAFORM_VERSION=1.5.0

TEST?=$$(go list ./... |grep -v 'vendor')
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
WEBSITE_REPO=github.com/hashicorp/terraform-website
Expand All @@ -8,13 +10,38 @@ default: build
build: fmtcheck
go install


.PHONY: dev-setup
dev-setup: ## setup development dependencies
@which ./bin/golangci-lint || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ./bin v1.53.3


.PHONY: dev-cleanup
dev-cleanup: ## cleanup development dependencies
rm -rf bin/*

.PHONY: mod
mod: ## add missing and remove unused modules
go mod tidy -compat=1.20

.PHONY: lint-check
lint-check: ## Run static code analysis and check formatting
./bin/golangci-lint run ./... -v

.PHONY: lint-fix
lint-fix: ## Run static code analysis, check formatting and try to fix findings
./bin/golangci-lint run ./... -v --fix


test: fmtcheck
go test -i $(TEST) || exit 1
echo $(TEST) | \
xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4

testacc: fmtcheck
INFLUXDB_USERNAME=test INFLUXDB_PASSWORD=test TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
TF_ACC=1 \
INFLUXDB_USERNAME=test INFLUXDB_PASSWORD=test \
go test -v ./... -timeout 120m

vet:
@echo "go vet ."
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Terraform Provider
Requirements
------------

- [Terraform](https://www.terraform.io/downloads.html) 0.10.x
- [Go](https://golang.org/doc/install) 1.11 (to build the provider plugin)
- [Terraform](https://www.terraform.io/downloads.html) 1.5.x
- [Go](https://golang.org/doc/install) 1.16 (to build the provider plugin)

Building The Provider
---------------------
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/terraform-providers/terraform-provider-influxdb
go 1.16

require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.16.0
github.com/influxdata/influxdb v0.0.0-20170119032824-8e0bf700f82c
)
378 changes: 65 additions & 313 deletions go.sum

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion influxdb/continuous_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,16 @@ func createContinuousQuery(d *schema.ResourceData, meta interface{}) error {

d.SetId(fmt.Sprintf("%s:%s", name, database))

return readContinuousQuery(d, meta)
err = readContinuousQuery(d, meta)
if err != nil {
return err
}
// check that cq is created
if d.Id() == "" {
return fmt.Errorf("Unable to create continuous query '%s', check your sql query.", name)
}

return nil
}

func readContinuousQuery(d *schema.ResourceData, meta interface{}) error {
Expand Down
85 changes: 85 additions & 0 deletions influxdb/continuous_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package influxdb

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
Expand Down Expand Up @@ -51,6 +52,43 @@ func TestAccInfluxDBContiuousQuery_resample(t *testing.T) {
})
}

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

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccContiuousQueryConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckContiuousQueryExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "database", rName),
resource.TestCheckResourceAttr(resourceName, "query", "SELECT count(arrival_time) AS count INTO tooling_rp.:MEASUREMENT FROM raw_default_rp./.*/ WHERE tech_source_id = 'S2' GROUP BY time(1d), project, tech_source_id"),
),
},
},
})
}

func TestAccContiuousWithFailureQueryConfig(t *testing.T) {
//resourceName := "influxdb_continuous_query.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccContiuousQueryWithFailureConfig(rName),
// No check function is given because we expect this configuration
// to fail before any infrastructure is created
ExpectError: regexp.MustCompile("Unable to create continuous query"),
},
},
})
}

func testAccCheckContiuousQueryExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -105,6 +143,53 @@ resource "influxdb_continuous_query" "test" {
`, rName)
}

func testAccContiuousQueryConfig(rName string) string {
return fmt.Sprintf(`
resource "influxdb_database" "test" {
name = %[1]q
retention_policies {
name = "tooling_rp"
duration = "24h0m0s"
default = "false"
}
retention_policies {
name = "raw_default_rp"
duration = "24h0m0s"
default = "false"
}
}
resource "influxdb_continuous_query" "test" {
name = %[1]q
database = influxdb_database.test.name
query = "SELECT count(arrival_time) AS count INTO tooling_rp.:MEASUREMENT FROM raw_default_rp./.*/ WHERE tech_source_id = 'S2' GROUP BY time(1d), project, tech_source_id"
}
`, rName)
}

func testAccContiuousQueryWithFailureConfig(rName string) string {
return fmt.Sprintf(`
resource "influxdb_database" "test" {
name = %[1]q
retention_policies {
name = "raw_default_rp"
duration = "24h0m0s"
default = "false"
}
}
resource "influxdb_continuous_query" "test" {
name = %[1]q
database = influxdb_database.test.name
query = "SELECT count(arrival_time) AS count INTO tooling_rp.:MEASUREMENT FROM raw_default_rp./.*/ WHERE tech_source_id = 'S2' GROUP BY time(1d), project, tech_source_id"
}
`, rName)
}

func testAccContiuousQueryResampleConfig(rName string) string {
return fmt.Sprintf(`
resource "influxdb_database" "test" {
Expand Down
10 changes: 8 additions & 2 deletions influxdb/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,30 @@ func Provider() *schema.Provider {

Schema: map[string]*schema.Schema{
"url": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
Description: "Influxdb connection url",
DefaultFunc: schema.EnvDefaultFunc(
"INFLUXDB_URL", "http://localhost:8086/",
),
},
"username": {
Type: schema.TypeString,
Optional: true,
Description: "Influxdb user name",
DefaultFunc: schema.EnvDefaultFunc("INFLUXDB_USERNAME", ""),
},
"password": {
Type: schema.TypeString,
Optional: true,
Description: "Influxdb password",
Sensitive: true,
StateFunc: hashSum,
DefaultFunc: schema.EnvDefaultFunc("INFLUXDB_PASSWORD", ""),
},
"skip_ssl_verify": {
Type: schema.TypeBool,
Description: "skip ssl verify on connection",
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("INFLUXDB_SKIP_SSL_VERIFY", "0"),
},
Expand Down Expand Up @@ -65,6 +69,8 @@ func configure(d *schema.ResourceData) (interface{}, error) {
return nil, err
}

// assume that an InfluxBD is already provision when using the InfluxDB provider.
// you have to manage dependency between your modules
_, _, err = conn.Ping()
if err != nil {
return nil, fmt.Errorf("error pinging server: %w", err)
Expand Down
5 changes: 4 additions & 1 deletion influxdb/resource_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ func readDatabase(d *schema.ResourceData, meta interface{}) error {
for _, result := range resp.Results[0].Series[0].Values {
if result[0] == name {
d.Set("name", d.Id())
readRetentionPolicies(d, meta)
err := readRetentionPolicies(d, meta)
if err != nil {
return err
}
return nil
}
}
Expand Down
25 changes: 20 additions & 5 deletions influxdb/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,15 @@ func updateUser(d *schema.ResourceData, meta interface{}) error {

if d.HasChange("admin") {
if !d.Get("admin").(bool) {
revokeAllOn(conn, name)
err := revokeAllOn(conn, name)
if err != nil {
return err
}
} else {
grantAllOn(conn, name)
err := grantAllOn(conn, name)
if err != nil {
return err
}
}
}

Expand All @@ -216,10 +222,16 @@ func updateUser(d *schema.ResourceData, meta interface{}) error {
}

if !exists {
revokePrivilegeOn(conn, oldGrant["privilege"].(string), oldGrant["database"].(string), name)
err := revokePrivilegeOn(conn, oldGrant["privilege"].(string), oldGrant["database"].(string), name)
if err != nil {
return err
}
} else {
if privilege != oldGrant["privilege"].(string) {
grantPrivilegeOn(conn, privilege, oldGrant["database"].(string), name)
err := grantPrivilegeOn(conn, privilege, oldGrant["database"].(string), name)
if err != nil {
return err
}
}
}
}
Expand All @@ -235,7 +247,10 @@ func updateUser(d *schema.ResourceData, meta interface{}) error {
}

if !exists {
grantPrivilegeOn(conn, newGrant["privilege"].(string), newGrant["database"].(string), name)
err := grantPrivilegeOn(conn, newGrant["privilege"].(string), newGrant["database"].(string), name)
if err != nil {
return err
}
}
}
}
Expand Down

0 comments on commit dd7445d

Please sign in to comment.