diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2e83029..e2612cc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,10 +9,10 @@ jobs: - name: Install Task uses: arduino/setup-task@v1 - - name: Set up Go 1.18 + - name: Set up Go 1.20 uses: actions/setup-go@v1 with: - go-version: 1.18 + go-version: 1.20 id: go - name: Check out code into the Go module directory diff --git a/.gitignore b/.gitignore index d2790ea..db14bdf 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,12 @@ website/node_modules .vagrant/ *.backup ./*.tfstate +examples/**/**/.terraform/ +examples/**/**/*provider.tf +examples/**/**/*.tfstate +examples/**/**/.terraform.lock.hcl +examples/**/**/.terraformrc +examples/.terraformrc .terraform/ *.log *.bak @@ -25,6 +31,7 @@ website/node_modules *.test *.iml *.env +.envrc website/vendor .task/ vendor/ @@ -34,3 +41,4 @@ vendor/ # Keep windows files with windows line endings *.winfile eol=crlf +!/examples/.terraformrc diff --git a/.goreleaser.yml b/.goreleaser.yml index f51b9e6..fb124e1 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -9,7 +9,7 @@ builds: # goreleaser does not work with CGO, it could also complicate # usage by users in CI/CD systems like Terraform Cloud where # they are unable to install libraries. - - CGO_ENABLED=0 + # - CGO_ENABLED=0 mod_timestamp: '{{ .CommitTimestamp }}' flags: - -trimpath diff --git a/Taskfile.yaml b/Taskfile.yaml index 065f330..2993b7f 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -5,8 +5,8 @@ version: '3' vars: BINARY_FOLDER: bin NAME: influxdb-v2 - GOLANGCI_LINT_VERSION: v1.49.0 - VERSION: v0.4.4 + GOLANGCI_LINT_VERSION: v1.52.2 + VERSION: v0.4.5 NAMESPACE: hasanhakkaev dotenv: ['.env'] @@ -66,9 +66,7 @@ tasks: start-influx: desc: Start InfluxDB cmds: - - ./scripts/setup-influxdb.sh - status: - - while ! $(curl -sS 'http://localhost:8086/ready' | grep -q ready); do echo 'Waiting for influx...'; sleep 1; done + - ./scripts/setup_influxdb.sh stop-influx: desc: Stop InfluxDB cmds: diff --git a/docs/data-sources/bucket.md b/docs/data-sources/bucket.md new file mode 100644 index 0000000..6d98a0b --- /dev/null +++ b/docs/data-sources/bucket.md @@ -0,0 +1,62 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "influxdb-v2_bucket Data Source - terraform-provider-influxdb-v2" +subcategory: "" +description: |- + Lookup a Bucket in InfluxDB2. +--- + +# influxdb-v2_bucket (Data Source) + +Lookup a Bucket in InfluxDB2. + +## Example Usage + +```terraform +data "influxdb-v2_bucket" "bucket" { + name = "newName" +} + +output "influxdb-v2_bucket4" { + value = data.influxdb-v2_bucket.bucket +} + +terraform { + required_providers { + influxdb-v2 = { + source = "local/local/influxdb-v2" + version = "0.4.5" + } + } +} +``` + + +## Schema + +### Required + +- `name` (String) Bucket name. + +### Read-Only + +- `created_at` (String) The string time that the Bucket was created. +- `created_timestamp` (Number) The timestamp that the Bucket was created. +- `description` (String) Description of the bucket. +- `id` (String) Bucket id. +- `org_id` (String) ID of organization in which to create a bucket. +- `retention_rules` (Set of Object) Rules to expire or retain data. No rules means data never expires. (see [below for nested schema](#nestedatt--retention_rules)) +- `type` (String) Bucket type. +- `updated_at` (String) The string time that the Bucket was last updated. +- `updated_timestamp` (Number) The timestamp that the Bucket was last updated. + + +### Nested Schema for `retention_rules` + +Read-Only: + +- `every_seconds` (Number) +- `shard_group_duration_seconds` (Number) +- `type` (String) + + diff --git a/docs/data-sources/organization.md b/docs/data-sources/organization.md index 54eae3e..31cc5e4 100644 --- a/docs/data-sources/organization.md +++ b/docs/data-sources/organization.md @@ -1,36 +1,69 @@ --- -layout: "influxdb-v2" -page_title: "InfluxDB V2: influxdb-v2_organization" -sidebar_current: "docs-influxdb-v2-datasource-organization" +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "influxdb-v2_organization Data Source - terraform-provider-influxdb-v2" +subcategory: "" description: |- -The influxdb-v2_organization data source returns influxdb status. + Lookup an Organization in InfluxDB2. --- -# influxdb-v2\_organization (Data Source) +# influxdb-v2_organization (Data Source) -The influxdb-v2_organization data source retrieves influxdb organization information. +Lookup an Organization in InfluxDB2. ## Example Usage -```hcl +```terraform data "influxdb-v2_organization" "organization" { - name = "my-org" + name = "testorg" } output "influxdb-v2_organization_id" { - value = data.influxdb-v2_organization.organization.id + value = data.influxdb-v2_organization.organization.id +} + +output "influxdb-v2_organization_name" { + value = data.influxdb-v2_organization.organization.name +} + +output "influxdb-v2_organization_description" { + value = data.influxdb-v2_organization.organization.description +} + +output "influxdb-v2_organization_created_at" { + value = data.influxdb-v2_organization.organization.created_at +} + +output "influxdb-v2_organization_updated_at" { + value = data.influxdb-v2_organization.organization.updated_at +} + +terraform { + required_providers { + influxdb-v2 = { + source = "local/local/influxdb-v2" + version = "0.4.5" + } + } } ``` -## Argument Reference + +## Schema + +### Required + +- `name` (String) Name of the Organization. + +### Optional -* ``name`` (Required) The organization name +- `id` (String) ID of the Organization. +### Read-Only -## Attributes Reference +- `created_at` (String) The string time that the Organization was created. +- `created_timestamp` (Number) The timestamp that the Organization was created. +- `description` (String) The description of the Organization. +- `updated_at` (String) The string time that the Organization was last updated. +- `updated_timestamp` (Number) The timestamp that the Organization was last updated. -The following attributes are exported: -* ``id`` - The ID of the Influx organization. -* ``name`` - The name of the Influx organization. -* ``description`` - The description of the Influx organization. \ No newline at end of file diff --git a/docs/data-sources/ready.md b/docs/data-sources/ready.md index 4455ce2..818f47e 100644 --- a/docs/data-sources/ready.md +++ b/docs/data-sources/ready.md @@ -1,32 +1,31 @@ --- -layout: "influxdb-v2" -page_title: "InfluxDB V2: influxdb-v2_ready" -sidebar_current: "docs-influxdb-v2-datasource-ready" +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "influxdb-v2_ready Data Source - terraform-provider-influxdb-v2" +subcategory: "" description: |- -The influxdb-v2_ready data source returns influxdb status. + --- -# influxdb-v2\_ready (Data Source) +# influxdb-v2_ready (Data Source) + -The influxdb-v2_ready data source retrieves influxdb instance status information. -If the endpoint server is online, the function will output its URL, otherwise, the field will be empty. ## Example Usage -```hcl +```terraform data "influxdb-v2_ready" "test" {} output "influxdb-v2_ready" { - value = data.influxdb-v2_ready.test.output["url"] + value = data.influxdb-v2_ready.test.output["url"] } ``` -## Argument Reference + +## Schema -This data source doesn't support arguments. +### Read-Only -## Attributes Reference +- `id` (String) The ID of this resource. +- `output` (Map of String) -The following attributes are exported: -* ``url`` - The URL of the influx instance (empty if not ready). \ No newline at end of file diff --git a/docs/index.md b/docs/index.md index 204dcf5..53dc680 100644 --- a/docs/index.md +++ b/docs/index.md @@ -15,7 +15,7 @@ description: |- ```terraform provider "influxdb-v2" { url = "http://localhost:8086" # changeme - token = "super-secret-admin-token" # changeme + token = "WLCq15HS_zugineJalPUqxTxxBKK7IluEseKR0rD3-2CfHBdS0BguLHGEaXRnJ2p080EdTsx9yKq1kFnLnSaxA==" # changeme } ``` diff --git a/docs/resources/authorization.md b/docs/resources/authorization.md index 44a7191..cdd75f9 100644 --- a/docs/resources/authorization.md +++ b/docs/resources/authorization.md @@ -1,9 +1,9 @@ --- -layout: "influxdb-v2" -page_title: "InfluxDB V2: influxdb-v2_authorization" -sidebar_current: "docs-influxdb-v2-resource-authorization" +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "influxdb-v2_authorization Resource - terraform-provider-influxdb-v2" +subcategory: "" description: |- -The influxdb-v2_authorization resource manages influxdb v2 authorizations. + --- # influxdb-v2_authorization (Resource) @@ -41,29 +41,50 @@ resource "influxdb-v2_authorization" "example_authorization" { } ``` -## Argument Reference + +## Schema -The following arguments are supported: +### Required -* ``org_id`` (Required) The organization id to which the authorization will be linked. -* ``permissions`` (Required) Permission array of the authorization. - * ``action`` (Required) Action of the permission, can be "read" or "write". - * ``resource`` (Required) Permission resource - * ``id`` (Required) ID of the resource to which the permission is linked - * ``orgID`` (Required) Organization ID to link to. - * ``type`` (Required) The type of authorization, can be `authorizations` `buckets` `dashboards` `orgs` `sources` `tasks` `telegrafs` `users` `variables` `scrapers` `secrets` `labels` `views` `documents` `notificationRules` `notificationEndpoints` `checks` `dbrp` - * ``name`` (Optional) Name of the resource - * ``org`` (Optional) Name of the organization with orgID. -* ``status`` (Optional) Status of the authorization, can be "active" or "inactive" - Default "active" -* ``description`` (Optional) The description of the bucket. +- `org_id` (String) +- `permissions` (Block Set, Min: 1) (see [below for nested schema](#nestedblock--permissions)) -## Attributes Reference +### Optional -In addition to the above arguments, the following attributes are exported: +- `description` (String) +- `status` (String) + +### Read-Only + +- `id` (String) The ID of this resource. +- `token` (String, Sensitive) +- `user_id` (String) +- `user_org_id` (String) + + +### Nested Schema for `permissions` + +Required: + +- `action` (String) +- `resource` (Block Set, Min: 1) (see [below for nested schema](#nestedblock--permissions--resource)) + + +### Nested Schema for `permissions.resource` + +Required: + +- `org_id` (String) +- `type` (String) + +Optional: + +- `org` (String) + +Read-Only: + +- `id` (String) The ID of this resource. -* ``user_id`` - The user ID which is created with the authorization. -* ``user_org_id`` - The org ID linked to the user of that authorization. -* ``token`` - The token newly created. ## Import Import is supported using the following syntax: diff --git a/docs/resources/bucket.md b/docs/resources/bucket.md index 5b4a5b3..601dedb 100644 --- a/docs/resources/bucket.md +++ b/docs/resources/bucket.md @@ -1,9 +1,9 @@ --- -layout: "influxdb-v2" -page_title: "InfluxDB V2: influxdb-v2_bucket" -sidebar_current: "docs-influxdb-v2-resource-bucket" +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "influxdb-v2_bucket Resource - terraform-provider-influxdb-v2" +subcategory: "" description: |- -The influxdb-v2_authorization resource manages influxdb v2 buckets. + --- # influxdb-v2_bucket (Resource) @@ -14,7 +14,7 @@ The influxdb-v2_authorization resource manages influxdb v2 buckets. ```terraform locals { - org_id = "example_org_id" + org_id = "ed2e6b93c8396828" } resource "influxdb-v2_bucket" "example_bucket" { @@ -25,28 +25,48 @@ resource "influxdb-v2_bucket" "example_bucket" { every_seconds = 0 } } +terraform { + required_providers { + influxdb-v2 = { + source = "local/local/influxdb-v2" + version = "0.4.5" + } + } +} ``` -## Argument Reference +## Schema + +### Required + +- `name` (String) +- `org_id` (String) +- `retention_rules` (Block Set, Min: 1) (see [below for nested schema](#nestedblock--retention_rules)) + +### Optional + +- `description` (String) +- `rp` (String) + +### Read-Only + +- `created_at` (String) +- `id` (String) The ID of this resource. +- `type` (String) +- `updated_at` (String) + + +### Nested Schema for `retention_rules` -The following arguments are supported: +Required: -* ``name`` (Required) The name of the bucket. -* ``org_id`` (Required) The organization id to which the bucket is linked. -* ``retention_rules`` (Required) Retention rules that affect the bucket. - * ``every_seconds`` (Required) How many seconds the rule should be applied. -* ``description`` (Optional) The description of the bucket. -* ``rp`` (Optional) As of now, the influxdb documentation doesn't say what this paramenter is for. +- `every_seconds` (Number) -## Attributes Reference +Optional: -In addition to the above arguments, the following attributes are exported: +- `type` (String) -* ``created_at`` - The date the bucket has been created. -* ``updated_at`` - The date the bucket has been updated. -* ``type`` - The type of bucket. -* ## Import Import is supported using the following syntax: diff --git a/docs/resources/organization.md b/docs/resources/organization.md index a143db1..8081091 100644 --- a/docs/resources/organization.md +++ b/docs/resources/organization.md @@ -1,33 +1,40 @@ --- -layout: "influxdb-v2" -page_title: "InfluxDB V2: influxdb-organization" -sidebar_current: "docs-influxdb-v2-resource-organization" +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "influxdb-v2_organization Resource - terraform-provider-influxdb-v2" +subcategory: "" description: |- -The influxdb-organization resource manages influxdb v2 organizations. + --- +# influxdb-v2_organization (Resource) + + + ## Example Usage -```hcl -resource "influxdb-organization" "organization" { - name = "my_organization" - description = "My organization desciption" +```terraform +resource "influxdb-v2_organization" "example_organization" { + name = "Example organization name" + description = "Example organization description" } ``` -## Argument Reference + +## Schema -The following arguments are supported: +### Required -* ``name`` (Required) The organization name -* ``description`` (Required) A short description for your organization +- `name` (String) -## Attributes Reference +### Optional -In addition to the above arguments, the following attributes are exported: +- `description` (String) -* ``id`` - The organization ID which is created +### Read-Only +- `created_at` (String) +- `id` (String) The ID of this resource. +- `updated_at` (String) ## Import diff --git a/examples/data-sources/influxdb-v2_bucket/data-source.tf b/examples/data-sources/influxdb-v2_bucket/data-source.tf new file mode 100644 index 0000000..7ad2f43 --- /dev/null +++ b/examples/data-sources/influxdb-v2_bucket/data-source.tf @@ -0,0 +1,7 @@ +data "influxdb-v2_bucket" "bucket" { + name = "testbucket" +} + +output "influxdb-v2_bucket" { + value = data.influxdb-v2_bucket.bucket +} diff --git a/go.mod b/go.mod index ba7c3a5..a1d0787 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,11 @@ module github.com/hasanhakkaev/terraform-provider-influxdb-v2 -go 1.18 +go 1.20 require ( github.com/hashicorp/terraform-plugin-docs v0.14.1 github.com/hashicorp/terraform-plugin-sdk/v2 v2.26.1 - github.com/influxdata/influxdb-client-go/v2 v2.4.0 + github.com/influxdata/influxdb-client-go/v2 v2.12.3 ) require ( @@ -16,7 +16,7 @@ require ( github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect github.com/armon/go-radix v1.0.0 // indirect github.com/bgentry/speakeasy v0.1.0 // indirect - github.com/deepmap/oapi-codegen v1.6.0 // indirect + github.com/deepmap/oapi-codegen v1.8.2 // indirect github.com/fatih/color v1.13.0 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/google/go-cmp v0.5.9 // indirect @@ -70,5 +70,4 @@ require ( google.golang.org/genproto v0.0.0-20200711021454-869866162049 // indirect google.golang.org/grpc v1.51.0 // indirect google.golang.org/protobuf v1.28.1 // indirect - gopkg.in/yaml.v2 v2.3.0 // indirect ) diff --git a/go.sum b/go.sum index 911ee09..48a1e5d 100644 --- a/go.sum +++ b/go.sum @@ -35,8 +35,8 @@ github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/deepmap/oapi-codegen v1.6.0 h1:w/d1ntwh91XI0b/8ja7+u5SvA4IFfM0UNNLmiDR1gg0= -github.com/deepmap/oapi-codegen v1.6.0/go.mod h1:ryDa9AgbELGeB+YEXE1dR53yAjHwFvE9iAUlWl9Al3M= +github.com/deepmap/oapi-codegen v1.8.2 h1:SegyeYGcdi0jLLrpbCMoJxnUUn8GBXHsvr4rbzjuhfU= +github.com/deepmap/oapi-codegen v1.8.2/go.mod h1:YLgSKSDv/bZQB7N4ws6luhozi3cEdRktEqrX88CvjIw= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg= github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= @@ -47,7 +47,7 @@ github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE= -github.com/getkin/kin-openapi v0.53.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4= +github.com/getkin/kin-openapi v0.61.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= github.com/go-chi/chi/v5 v5.0.0/go.mod h1:BBug9lr0cqtdAhsu6R4AAdvufI0/XBzAQSsUqJpoZOs= @@ -146,8 +146,8 @@ github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= -github.com/influxdata/influxdb-client-go/v2 v2.4.0 h1:HGBfZYStlx3Kqvsv1h2pJixbCl/jhnFtxpKFAv9Tu5k= -github.com/influxdata/influxdb-client-go/v2 v2.4.0/go.mod h1:vLNHdxTJkIf2mSLvGrpj8TCcISApPoXkaxP8g9uRlW8= +github.com/influxdata/influxdb-client-go/v2 v2.12.3 h1:28nRlNMRIV4QbtIUvxhWqaxn0IpXeMSkY/uJa/O/vC4= +github.com/influxdata/influxdb-client-go/v2 v2.12.3/go.mod h1:IrrLUbCjjfkmRuaCiGQg4m2GbkaeJDcuWoxiWdQEbA0= github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 h1:W9WBk7wlPfJLvMCdtV4zPulc4uCPrlywQOmbFOhgQNU= github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= @@ -235,8 +235,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s= github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= @@ -389,7 +389,6 @@ gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/influxdbv2/data_bucket.go b/influxdbv2/data_bucket.go new file mode 100644 index 0000000..86a165c --- /dev/null +++ b/influxdbv2/data_bucket.go @@ -0,0 +1,200 @@ +package influxdbv2 + +import ( + "context" + "fmt" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/influxdata/influxdb-client-go/v2" + "github.com/influxdata/influxdb-client-go/v2/domain" +) + +func dataSourceBucket() *schema.Resource { + return &schema.Resource{ + Description: "Lookup a Bucket in InfluxDB2.", + ReadContext: dataSourceBucketRead, + + Schema: mergeSchemas(map[string]*schema.Schema{ + "id": { + Description: "Bucket id.", + Type: schema.TypeString, + Computed: true, + }, + "name": { + Description: "Bucket name.", + Type: schema.TypeString, + Required: true, + }, + "org_id": { + Description: "ID of organization in which to create a bucket.", + Type: schema.TypeString, + Computed: true, + }, + "description": { + Description: "Description of the bucket.", + Type: schema.TypeString, + Computed: true, + }, + "retention_rules": { + Description: "Rules to expire or retain data. No rules means data never expires.", + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "every_seconds": { + Description: "Duration in seconds for how long data will be kept in the database. 0 means infinite.", + Type: schema.TypeInt, + Required: true, + }, + "shard_group_duration_seconds": { + Description: "Duration in seconds for how long each shard group will last before it gets rotated out.", + Type: schema.TypeInt, + Optional: true, + }, + "type": { + Description: "Retention rule type.", + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + "type": { + Description: "Bucket type.", + Type: schema.TypeString, + Computed: true, + }, + }, createUpdatedSchema("Bucket")), + } +} + +func dataSourceBucketRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + // use the meta value to retrieve your client from the provider configure method + influx := meta.(influxdb2.Client) + bucketAPI := influx.BucketsAPI() + + // Warning or errors can be collected in a slice type + var ( + diags diag.Diagnostics + bucket *domain.Bucket + err error + ) + + if v, ok := d.GetOk("name"); ok { + bucketName := v.(string) + if bucket, err = bucketAPI.FindBucketByName(ctx, bucketName); err != nil { + diags = append(diags, diag.FromErr(err)...) + diags = append(diags, diag.Diagnostic{ + Severity: diag.Error, + Summary: fmt.Sprintf("Can't find Bucket with name: %s", bucketName), + }) + return diags + } + } + diags = setBucketData(d, bucket) + d.Set("id", bucket.Id) + d.SetId(*bucket.Id) + //id := bucket.Id + //if id == nil { + // diags = append(diags, diag.Diagnostic{ + // Severity: diag.Error, + // Summary: "Bucket not found", + // }) + // return diags + //} + // + //d.SetId(*id) + //err = d.Set("id", *id) + //if err != nil { + // return diag.FromErr(err) + //} + //if bucket.Description != nil { + // err := d.Set("description", *bucket.Description) + // if err != nil { + // return diag.FromErr(err) + // } + //} + //err = d.Set("name", bucket.Name) + //if err != nil { + // return diag.FromErr(err) + //} + // + //err = d.Set("type", bucket.Type) + //if err != nil { + // return diag.FromErr(err) + //} + // + //retentionRules := make([]interface{}, len(bucket.RetentionRules)) + //for i, rule := range bucket.RetentionRules { + // ruleMap := map[string]interface{}{ + // "every_seconds": int(rule.EverySeconds), + // "type": rule.Type, + // } + // if rule.ShardGroupDurationSeconds != nil && *rule.ShardGroupDurationSeconds > rule.EverySeconds { + // ruleMap["shard_group_duration_seconds"] = int(*rule.ShardGroupDurationSeconds) // convert *int64 to int + // } else { + // ruleMap["shard_group_duration_seconds"] = 0 + // } + // retentionRules[i] = ruleMap + //} + //if err := d.Set("retention_rules", schema.NewSet(schema.HashResource(dataSourceBucket().Schema["retention_rules"].Elem.(*schema.Resource)), retentionRules)); err != nil { + // return diag.FromErr(err) + //} + + return diags +} + +func setBucketData(data *schema.ResourceData, bucket *domain.Bucket) diag.Diagnostics { + var ( + err error + ) + err = data.Set("org_id", *bucket.OrgID) + if err != nil { + return diag.FromErr(err) + } + err = data.Set("name", bucket.Name) + if err != nil { + return diag.FromErr(err) + } + err = data.Set("description", bucket.Description) + if err != nil { + return diag.FromErr(err) + } + err = data.Set("created_at", bucket.CreatedAt.Format("2006-01-02T15:04:05.000Z")) + if err != nil { + return diag.FromErr(err) + } + err = data.Set("updated_at", bucket.UpdatedAt.Format("2006-01-02T15:04:05.000Z")) + if err != nil { + return diag.FromErr(err) + } + err = data.Set("updated_timestamp", bucket.UpdatedAt.UnixNano()/1000000) + if err != nil { + return diag.FromErr(err) + } + err = data.Set("created_timestamp", bucket.CreatedAt.UnixNano()/1000000) + if err != nil { + return diag.FromErr(err) + } + err = data.Set("type", bucket.Type) + if err != nil { + return diag.FromErr(err) + } + + var retentionRules []map[string]interface{} + for _, rule := range bucket.RetentionRules { + mapped := map[string]interface{}{ + "every_seconds": rule.EverySeconds, + "shard_group_duration_seconds": rule.ShardGroupDurationSeconds, + "type": rule.Type, + } + retentionRules = append(retentionRules, mapped) + } + + err = data.Set("retention_rules", retentionRules) + if err != nil { + return diag.FromErr(err) + } + + return nil +} diff --git a/influxdbv2/data_bucket_test.go b/influxdbv2/data_bucket_test.go new file mode 100644 index 0000000..7fda3d8 --- /dev/null +++ b/influxdbv2/data_bucket_test.go @@ -0,0 +1,41 @@ +package influxdbv2 + +import ( + "os" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +// TestAccReadBucket tests the read bucket data source +func TestAccReadBucket(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testDataSourceBucketConfig(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.influxdb-v2_bucket.by_name", "name", "AcctestBucket"), + resource.TestCheckResourceAttr("data.influxdb-v2_bucket.by_name", "description", "Desc Acctest"), + ), + }, + }, + }) +} + +func testDataSourceBucketConfig() string { + return `resource "influxdb-v2_bucket" "bucket" { + name = "AcctestBucket" + description = "Desc Acctest" + org_id = "` + os.Getenv("INFLUXDB_V2_ORG_ID") + `" + retention_rules { + every_seconds = "3640" + } + } + data "influxdb-v2_bucket" "by_name" { + name = influxdb-v2_bucket.bucket.name + depends_on = [influxdb-v2_bucket.bucket] + } +` +} diff --git a/influxdbv2/data_orgranization.go b/influxdbv2/data_orgranization.go index edff2b8..21a7a1a 100644 --- a/influxdbv2/data_orgranization.go +++ b/influxdbv2/data_orgranization.go @@ -15,12 +15,11 @@ func dataSourceOrganization() *schema.Resource { return &schema.Resource{ Description: "Lookup an Organization in InfluxDB2.", ReadContext: dataSourceOrganizationRead, - Schema: map[string]*schema.Schema{ - "name": { + Schema: mergeSchemas(map[string]*schema.Schema{ + "description": { Type: schema.TypeString, - Optional: true, - Computed: false, - Description: "Name of the Organization.", + Computed: true, + Description: "The description of the Organization.", }, "id": { Type: schema.TypeString, @@ -28,13 +27,13 @@ func dataSourceOrganization() *schema.Resource { Computed: true, Description: "ID of the Organization.", }, - // Computed outputs - "description": { + "name": { Type: schema.TypeString, - Computed: true, - Description: "The description of the Organization.", + Required: true, + Computed: false, + Description: "Name of the Organization.", }, - }, + }, createUpdatedSchema("Organization")), } } @@ -76,16 +75,31 @@ func dataSourceOrganizationRead(ctx context.Context, d *schema.ResourceData, met if err != nil { return nil } - err = d.Set("name", org.Name) - if err != nil { - return nil - } if org.Description != nil { err := d.Set("description", *org.Description) if err != nil { return nil } } - + err = d.Set("name", org.Name) + if err != nil { + return nil + } + err = d.Set("created_at", org.CreatedAt.Format("2006-01-02T15:04:05.000Z")) + if err != nil { + return nil + } + err = d.Set("updated_at", org.UpdatedAt.Format("2006-01-02T15:04:05.000Z")) + if err != nil { + return nil + } + err = d.Set("created_timestamp", org.CreatedAt.UnixNano()/1000000) + if err != nil { + return nil + } + err = d.Set("updated_timestamp", org.UpdatedAt.UnixNano()/1000000) + if err != nil { + return nil + } return diags } diff --git a/influxdbv2/data_ready.go b/influxdbv2/data_ready.go index 3291e6c..d4adb09 100644 --- a/influxdbv2/data_ready.go +++ b/influxdbv2/data_ready.go @@ -27,7 +27,7 @@ func DataGetReady(d *schema.ResourceData, meta interface{}) error { if err != nil { return fmt.Errorf("server is not ready: %v", err) } - if ready { + if *ready.Status != "ready" { log.Printf("Server is ready !") } diff --git a/influxdbv2/helpers.go b/influxdbv2/helpers.go new file mode 100644 index 0000000..7de5511 --- /dev/null +++ b/influxdbv2/helpers.go @@ -0,0 +1,42 @@ +package influxdbv2 + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func createUpdatedSchema(itemType string) map[string]*schema.Schema { + return map[string]*schema.Schema{ + "created_at": { + Description: fmt.Sprintf("The string time that the %s was created.", itemType), + Type: schema.TypeString, + Computed: true, + }, + "updated_at": { + Description: fmt.Sprintf("The string time that the %s was last updated.", itemType), + Type: schema.TypeString, + Computed: true, + }, + "created_timestamp": { + Description: fmt.Sprintf("The timestamp that the %s was created.", itemType), + Type: schema.TypeInt, + Computed: true, + }, + "updated_timestamp": { + Description: fmt.Sprintf("The timestamp that the %s was last updated.", itemType), + Type: schema.TypeInt, + Computed: true, + }, + } +} + +func mergeSchemas(schemas ...map[string]*schema.Schema) map[string]*schema.Schema { + res := map[string]*schema.Schema{} + for _, s := range schemas { + for k, v := range s { + res[k] = v + } + } + return res +} diff --git a/influxdbv2/provider.go b/influxdbv2/provider.go index 19b6901..5ec4a1e 100644 --- a/influxdbv2/provider.go +++ b/influxdbv2/provider.go @@ -13,6 +13,7 @@ func Provider() *schema.Provider { DataSourcesMap: map[string]*schema.Resource{ "influxdb-v2_ready": DataReady(), "influxdb-v2_organization": dataSourceOrganization(), + "influxdb-v2_bucket": dataSourceBucket(), }, ResourcesMap: map[string]*schema.Resource{ "influxdb-v2_bucket": ResourceBucket(), diff --git a/influxdbv2/resource_create_authorization.go b/influxdbv2/resource_create_authorization.go index 1740c2a..415abd9 100644 --- a/influxdbv2/resource_create_authorization.go +++ b/influxdbv2/resource_create_authorization.go @@ -74,8 +74,9 @@ func ResourceAuthorization() *schema.Resource { Computed: true, }, "token": { - Type: schema.TypeString, - Computed: true, + Type: schema.TypeString, + Computed: true, + Sensitive: true, }, }, } @@ -101,6 +102,10 @@ func resourceAuthorizationCreate(d *schema.ResourceData, meta interface{}) error return fmt.Errorf("error creating authorization: %e", err) } d.SetId(*result.Id) + err = d.Set("token", *result.Token) + if err != nil { + return err + } return resourceAuthorizationRead(d, meta) } @@ -137,9 +142,11 @@ func resourceAuthorizationRead(d *schema.ResourceData, meta interface{}) error { if err != nil { return err } - err = d.Set("token", authorizations.Token) - if err != nil { - return err + if *authorizations.Token != "redacted" { + err = d.Set("token", authorizations.Token) + if err != nil { + return err + } } return nil } diff --git a/influxdbv2/resource_create_buckets.go b/influxdbv2/resource_create_buckets.go index ba3a5de..03f16d7 100644 --- a/influxdbv2/resource_create_buckets.go +++ b/influxdbv2/resource_create_buckets.go @@ -112,6 +112,15 @@ func resourceBucketRead(d *schema.ResourceData, meta interface{}) error { } rr = append(rr, tmp) } + if len(result.RetentionRules) == 0 { + // If no retention rules, there's a default of 0 expiry + // but this isn't returned on the API + tmp := map[string]interface{}{ + "every_seconds": 0, + "type": "expire", + } + rr = append(rr, tmp) + } err = d.Set("name", result.Name) if err != nil { @@ -181,7 +190,7 @@ func getRetentionRules(input interface{}) domain.RetentionRules { for _, retentionRule := range retentionRulesSet { rr, ok := retentionRule.(map[string]interface{}) if ok { - each := domain.RetentionRule{EverySeconds: rr["every_seconds"].(int)} + each := domain.RetentionRule{EverySeconds: int64(rr["every_seconds"].(int))} result = append(result, each) } } diff --git a/scripts/setup_influxdb.sh b/scripts/setup_influxdb.sh index bcbd0e5..293917b 100755 --- a/scripts/setup_influxdb.sh +++ b/scripts/setup_influxdb.sh @@ -1,7 +1,7 @@ -#!/usr/bin/.env bash +#!/usr/bin/env bash echo "1) launching influx" -docker run -d --name tf_acc_tests_influxdb -p 8086:8086 influxdb:2.0.7 +docker run -d --name tf_acc_tests_influxdb -p 8086:8086 influxdb:2.7.1-alpine while ! $(curl -sS 'http://localhost:8086/ready' | grep -q ready); do echo 'Waiting for influx...'; sleep 1; done echo "2) onboarding"