Skip to content

Commit 44fad61

Browse files
authored
Merge pull request #12 from IvanOfThings/refactoring-code-improving-behavior-for-cluster-misuse
Refactoring code improving behavior for cluster misuse
2 parents 01cc8f6 + d93fef2 commit 44fad61

File tree

24 files changed

+132
-89
lines changed

24 files changed

+132
-89
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ jobs:
116116
timeout-minutes: 10
117117
env:
118118
TF_ACC: "1"
119-
TF_CLICKHOUSE_URL: "127.0.0.1"
119+
TF_CLICKHOUSE_HOST: "127.0.0.1"
120120
TF_CLICKHOUSE_USERNAME: "default"
121121
TF_CLICKHOUSE_PASSWORD: ""
122122
TF_CLICKHOUSE_PORT: "8123"

GNUmakefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
TEST?=$$(go list ./... | grep -v 'vendor')
22
HOSTNAME=hashicorp.com
3-
NAMESPACE=edu
3+
NAMESPACE=ivanofthings
44
NAME=clickhouse
55
BINARY=terraform-provider-${NAME}
6-
VERSION=0.1.0
6+
VERSION=2.0.0
77
OS_ARCH=linux_amd64
88

99
default: testacc
1010

1111
# Run acceptance tests
1212
.PHONY: testacc
1313
testacc:
14-
TF_ACC=1 TF_CLICKHOUSE_URL="127.0.0.1" TF_CLICKHOUSE_USERNAME="default" TF_CLICKHOUSE_PASSWORD="" TF_CLICKHOUSE_PORT=8923 go test -tags testing ./... -v $(TESTARGS) -timeout 120m
14+
TF_ACC=1 TF_CLICKHOUSE_HOST="127.0.0.1" TF_CLICKHOUSE_USERNAME="default" TF_CLICKHOUSE_PASSWORD="" TF_CLICKHOUSE_PORT=8923 go test -tags testing ./... -v $(TESTARGS) -timeout 120m
1515

1616
build:
1717
go build -o ${BINARY}

README.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Definining provider
4545
```hcl
4646
provider "clickhouse" {
4747
port = 8123
48-
clickhouse_url = "127.0.0.1"
48+
host = "127.0.0.1"
4949
username = "default"
5050
password = ""
5151
}
@@ -56,13 +56,13 @@ In order to definte url, username and password in a safety way it is possible to
5656
```config
5757
TF_CLICKHOUSE_USERNAME=default
5858
TF_CLICKHOUSE_PASSWORD=""
59-
TF_CLICKHOUSE_URL="127.0.0.1"
59+
TF_CLICKHOUSE_HOST="127.0.0.1"
6060
TF_CLICKHOUSE_PORT=8123
6161
```
6262

6363
```hcl
6464
resource "clickhouse_db" "test_db_clusterd" {
65-
db_name = "database_test_clustered"
65+
name = "database_test_clustered"
6666
comment = "This is a test database"
6767
}
6868
```
@@ -74,7 +74,7 @@ Configuring provider
7474
```hcl
7575
provider "clickhouse" {
7676
port = 8923
77-
clickhouse_url = "127.0.0.1"
77+
host = "127.0.0.1"
7878
username = "default"
7979
password = ""
8080
default_cluster ="cluster"
@@ -85,7 +85,7 @@ Creating a Database
8585

8686
```hcl
8787
resource "clickhouse_db" "test_db_clusterd" {
88-
db_name = "database_test_clustered"
88+
name = "database_test_clustered"
8989
comment = "This is a test database"
9090
cluster = "cluster"
9191
}
@@ -98,7 +98,7 @@ I is possible to use macros defined for cluster, databases, installation names i
9898
```hcl
9999
provider "clickhouse" {
100100
port = 8123
101-
clickhouse_url = "127.0.0.1"
101+
host = "127.0.0.1"
102102
username = "default"
103103
password = ""
104104
default_cluster ="'{cluster}'"
@@ -107,7 +107,7 @@ provider "clickhouse" {
107107

108108
```hcl
109109
resource "clickhouse_db" "test_db_clusterd" {
110-
db_name = "database_test_clustered"
110+
name = "database_test_clustered"
111111
comment = "This is a test database"
112112
cluster = "'{cluster}'"
113113
}
@@ -117,8 +117,8 @@ Creating tables
117117

118118
```hcl
119119
resource "clickhouse_table" "replicated_table" {
120-
database = clickhouse_db.test_db_clustered.db_name
121-
table_name = "replicated_table"
120+
database = clickhouse_db.test_db_clustered.name
121+
name = "replicated_table"
122122
cluster = clickhouse_db.test_db_clustered.cluster
123123
engine = "ReplicatedMergeTree"
124124
engine_params = ["'/clickhouse/{installation}/clickhouse_db.test_db_clustered.cluster/tables/{shard}/{database}/{table}'", "'{replica}'"]
@@ -150,11 +150,11 @@ resource "clickhouse_table" "replicated_table" {
150150
151151
152152
resource "clickhouse_table" "distributed_table" {
153-
database = clickhouse_db.test_db_clustered.db_name
154-
table_name = "distributed_table"
153+
database = clickhouse_db.test_db_clustered.name
154+
name = "distributed_table"
155155
cluster = clickhouse_db.test_db_clustered.cluster
156156
engine = "Distributed"
157-
engine_params = [clickhouse_db.test_db_clustered.cluster, clickhouse_db.test_db_clustered.db_name, clickhouse_table.replicated_table.table_name, "rand()"]
157+
engine_params = [clickhouse_db.test_db_clustered.cluster, clickhouse_db.test_db_clustered.name, clickhouse_table.replicated_table.name, "rand()"]
158158
}
159159
```
160160

docs/data-sources/dbs.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ Read-Only:
2727

2828
- `comment` (String)
2929
- `data_path` (String)
30-
- `db_name` (String)
3130
- `engine` (String)
3231
- `metadata_path` (String)
32+
- `name` (String)
3333
- `uuid` (String)
3434

3535

docs/index.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ description: |-
1616
terraform {
1717
required_providers {
1818
clickhouse = {
19-
version = "0.1.0"
20-
source = "hashicorp.com/edu/clickhouse"
19+
version = "2.0.0"
20+
source = "hashicorp.com/ivanofthings/clickhouse"
2121
}
2222
}
2323
}
@@ -36,8 +36,8 @@ provider "clickhouse" {
3636

3737
### Optional
3838

39-
- `clickhouse_url` (String, Sensitive) Clickhouse server url
4039
- `default_cluster` (String) Default cluster, if provided will be used when no cluster is provided
40+
- `host` (String, Sensitive) Clickhouse server url
4141
- `password` (String, Sensitive) Clickhouse user password with admin privileges
4242
- `port` (Number) Clickhouse server port
4343
- `username` (String) Clickhouse username with admin privileges

docs/resources/db.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Resource to handle clickhouse databases.
1717

1818
### Required
1919

20-
- `db_name` (String) Database name
20+
- `name` (String) Database name
2121

2222
### Optional
2323

docs/resources/table.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Resource to manage tables
2020
- `database` (String) DB Name where the table will bellow
2121
- `engine` (String) Table engine type (Supported types so far: Distributed, ReplicatedReplacingMergeTree, ReplacingMergeTree)
2222
- `engine_params` (List of String) Engine params in case the engine type requires them
23-
- `table_name` (String) Table Name
23+
- `name` (String) Table Name
2424

2525
### Optional
2626

examples/data-sources/clickhouse_dbs/main.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
terraform {
22
required_providers {
33
clickhouse = {
4-
version = "0.1.0"
5-
source = "hashicorp.com/edu/clickhouse"
4+
version = "2.0.0"
5+
source = "hashicorp.com/ivanofthings/clickhouse"
66
}
77
}
88
}

examples/main.tf

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
terraform {
22
required_providers {
33
clickhouse = {
4-
version = "0.1.0"
5-
source = "hashicorp.com/edu/clickhouse"
4+
version = "2.0.0"
5+
source = "hashicorp.com/ivanofthings/clickhouse"
66
}
77
}
88
}
99

1010
provider "clickhouse" {
11-
port = 8923
12-
clickhouse_url = "127.0.0.1"
13-
username = "default"
14-
password = ""
11+
port = 8123
12+
host = "127.0.0.1"
13+
username = "default"
14+
password = ""
1515
}
1616

1717
module "databases" {

examples/provider/provider.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
terraform {
22
required_providers {
33
clickhouse = {
4-
version = "0.1.0"
5-
source = "hashicorp.com/edu/clickhouse"
4+
version = "2.0.0"
5+
source = "hashicorp.com/ivanofthings/clickhouse"
66
}
77
}
88
}
+7-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
terraform {
22
required_providers {
33
clickhouse = {
4-
version = "0.1.0"
5-
source = "hashicorp.com/edu/clickhouse"
4+
version = "2.0.0"
5+
source = "hashicorp.com/ivanofthings/clickhouse"
66
}
77
}
88
}
@@ -11,19 +11,19 @@ provider "clickhouse" {
1111
port = 8123
1212
}
1313

14-
/*
14+
1515
resource "clickhouse_db" "test_db" {
16-
db_name = "database_test_3"
16+
name = "database_test"
1717
comment = "This is a test database"
1818
}
19-
*/
19+
2020

2121
// '{cluster}' is tha way to refer cluster name using macros provided by altinity clickhouse ok8s operator for clickhouse
2222
// Should provide cluster name in case of using this into a clickhouse cluster
2323
// see: https://github.com/Altinity/clickhouse-operator/blob/master/docs/replication_setup.md
2424
resource "clickhouse_db" "test_db_clusterd" {
25-
db_name = "database_test_clustered_2"
26-
comment = "This is a test database"
25+
name = "clustered_test_database"
26+
comment = "This is a culstered test database"
2727
cluster = "'{cluster}'"
2828
}
2929

examples/resources/clickhouse_table/main.tf

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
terraform {
22
required_providers {
33
clickhouse = {
4-
version = "0.1.0"
5-
source = "hashicorp.com/edu/clickhouse"
4+
version = "2.0.0"
5+
source = "hashicorp.com/ivanofthings/clickhouse"
66
}
77
}
88
}
@@ -12,15 +12,15 @@ provider "clickhouse" {
1212
}
1313

1414
resource "clickhouse_db" "test_db_clustered" {
15-
db_name = "database_test_10"
16-
comment = "This is a test database"
15+
name = "awesome_database"
16+
comment = "This is an awesome database"
1717
cluster = "'{cluster}'"
1818
}
1919

2020

2121
resource "clickhouse_table" "replicated_table" {
22-
database = clickhouse_db.test_db_clustered.db_name
23-
table_name = "replicated_table"
22+
database = clickhouse_db.test_db_clustered.name
23+
name = "replicated_table"
2424
cluster = "'{cluster}'"
2525
engine = "ReplicatedMergeTree"
2626
engine_params = ["'/clickhouse/{installation}/{cluster}/tables/{shard}/{database}/{table}'", "'{replica}'"]
@@ -52,11 +52,15 @@ resource "clickhouse_table" "replicated_table" {
5252

5353

5454
resource "clickhouse_table" "distributed_table" {
55-
database = clickhouse_db.test_db_clustered.db_name
56-
table_name = "t1_dist_6"
57-
cluster = "'{cluster}'"
58-
engine = "Distributed"
59-
engine_params = ["'{cluster}'", clickhouse_db.test_db_clustered.db_name, clickhouse_table.replicated_table.table_name, "rand()"]
55+
database = clickhouse_db.test_db_clustered.name
56+
name = "distributed_table"
57+
cluster = "'{cluster}'"
58+
engine = "Distributed"
59+
engine_params = [
60+
"'{cluster}'",
61+
clickhouse_db.test_db_clustered.name,
62+
clickhouse_table.replicated_table.name,
63+
"rand()"]
6064
}
6165

6266

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func main() {
3636
Debug: debugMode,
3737

3838
// TODO: update this string with the full name of your provider as used in your configs
39-
ProviderAddr: "registry.terraform.io/hashicorp/clickhouse",
39+
ProviderAddr: "registry.terraform.io/ivanofthings/clickhouse",
4040

4141
ProviderFunc: provider.New(version),
4242
}

pkg/common/db_sql_helpers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
type DataSourceDbsItem struct {
10-
DbName string `json:"db_name"`
10+
DbName string `json:"name"`
1111
Engine string `json:"engine"`
1212
DataPath string `json:"data_path"`
1313
MetadataPath string `json:"metadata_path"`

pkg/common/sql_helpers.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func GetResourceNamesOnDataBases(conn *ch.Conn, databaseName string) (resources
123123
func GetTables(conn *ch.Conn, data *CHDataBase, errors *[]error) []clickhouseTable {
124124
var query string
125125
if data != nil {
126-
query = fmt.Sprintf("SELECT `database`, `name`, `engine_full`, `engine`, `comment` FROM system.tables where database = '%v' AND name = '%v'", data.Database, data.Table_name)
126+
query = fmt.Sprintf("SELECT `database`, `name`, `engine_full`, `engine`, `comment` FROM system.tables where database = '%v' AND name = '%v'", data.Database, data.Name)
127127
} else {
128128
query = fmt.Sprintf("SELECT `database`, `name`, `engine_full`, `engine`, `comment` FROM system.tables")
129129

@@ -140,14 +140,14 @@ func GetTables(conn *ch.Conn, data *CHDataBase, errors *[]error) []clickhouseTab
140140
result := iter.Result
141141

142142
database := *toString(result, "database", errors)
143-
name := *toString(result, "name", errors)
143+
table_name := *toString(result, "name", errors)
144144

145145
var columns []clickhouseTablesColumn
146-
columns = getColumns(conn, database, name, errors)
146+
columns = getColumns(conn, database, table_name, errors)
147147

148148
table := clickhouseTable{
149149
database: database,
150-
table_name: name,
150+
name: table_name,
151151
engine_full: *toString(result, "engine_full", errors),
152152
engine: *toString(result, "engine", errors),
153153
comment: *toString(result, "comment", errors),

pkg/common/table_mappers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func MapTableToDatasource(table clickhouseTable) (*DataSourceCHTable, error) {
8484

8585
return &DataSourceCHTable{
8686
Database: table.database,
87-
Table_name: table.table_name,
87+
Name: table.name,
8888
Engine_full: table.engine_full,
8989
Engine: table.engine,
9090
Cluster: &cluster,

pkg/common/types.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ type clickhouseTablesColumn struct {
3737

3838
type clickhouseTable struct {
3939
database string
40-
table_name string
40+
name string
4141
engine string
4242
engine_full string
4343
comment string
4444
columns []clickhouseTablesColumn
4545
}
4646

4747
type CHDataBase struct {
48-
Database string
49-
Table_name string
48+
Database string
49+
Name string
5050
}
5151

5252
type dataSourceClickhouseColumn struct {
@@ -56,7 +56,7 @@ type dataSourceClickhouseColumn struct {
5656

5757
type DataSourceCHTable struct {
5858
Database string
59-
Table_name string
59+
Name string
6060
Engine_full string
6161
Engine string
6262
Cluster *string

pkg/datasources/data_source_dbs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func DataSourceDbs() *schema.Resource {
2121
Computed: true,
2222
Elem: &schema.Resource{
2323
Schema: map[string]*schema.Schema{
24-
"db_name": &schema.Schema{
24+
"name": &schema.Schema{
2525
Description: "DB Name",
2626
Type: schema.TypeString,
2727
Computed: true,

0 commit comments

Comments
 (0)