Skip to content

Commit 2d784be

Browse files
committed
add database resource
1 parent eedb158 commit 2d784be

File tree

20 files changed

+2074
-162
lines changed

20 files changed

+2074
-162
lines changed

docs/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ provider "clickhouse" {
6262

6363
- `api_url` (String) API URL of the ClickHouse OpenAPI the provider will interact with. Alternatively, can be configured using the `CLICKHOUSE_API_URL` environment variable. Only specify if you have a specific deployment of the ClickHouse OpenAPI you want to run against.
6464
- `organization_id` (String) ID of the organization the provider will create services under. Alternatively, can be configured using the `CLICKHOUSE_ORG_ID` environment variable.
65+
- `query_api_url` (String) API URL of the ClickHouse Query API the provider will interact with. Alternatively, can be configured using the `CLICKHOUSE_QUERY_API_URL` environment variable. Only specify if you have a specific deployment of the ClickHouse Query API you want to run against.
6566
- `token_key` (String) Token key of the key/secret pair. Used to authenticate with OpenAPI. Alternatively, can be configured using the `CLICKHOUSE_TOKEN_KEY` environment variable.
6667
- `token_secret` (String, Sensitive) Token secret of the key/secret pair. Used to authenticate with OpenAPI. Alternatively, can be configured using the `CLICKHOUSE_TOKEN_SECRET` environment variable.

examples/full/basic/aws/main.tf

-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ resource "clickhouse_service" "service" {
5353
data.clickhouse_api_key_id.self.id,
5454
]
5555
roles = [
56-
"sql_console_read_only",
5756
"sql_console_admin"
5857
]
5958
allowed_origins = null

examples/full/basic/azure/main.tf

+13
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ variable "release_channel" {
2929
}
3030
}
3131

32+
data "clickhouse_api_key_id" "self" {
33+
}
34+
3235
resource "clickhouse_service" "service" {
3336
name = var.service_name
3437
cloud_provider = "azure"
@@ -45,6 +48,16 @@ resource "clickhouse_service" "service" {
4548
}
4649
]
4750

51+
query_api_endpoints = {
52+
api_key_ids = [
53+
data.clickhouse_api_key_id.self.id,
54+
]
55+
roles = [
56+
"sql_console_admin"
57+
]
58+
allowed_origins = null
59+
}
60+
4861
min_replica_memory_gb = 8
4962
max_replica_memory_gb = 120
5063

examples/full/basic/gcp/main.tf

+13
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ variable "release_channel" {
2929
}
3030
}
3131

32+
data "clickhouse_api_key_id" "self" {
33+
}
34+
3235
resource "clickhouse_service" "service" {
3336
name = var.service_name
3437
cloud_provider = "gcp"
@@ -45,6 +48,16 @@ resource "clickhouse_service" "service" {
4548
}
4649
]
4750

51+
query_api_endpoints = {
52+
api_key_ids = [
53+
data.clickhouse_api_key_id.self.id,
54+
]
55+
roles = [
56+
"sql_console_admin"
57+
]
58+
allowed_origins = null
59+
}
60+
4861
min_replica_memory_gb = 8
4962
max_replica_memory_gb = 120
5063

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Database can be imported by specifying the service UUID and the database name, comma separated.
2+
terraform import clickhouse_database.example xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx,mydatabase
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resource "clickhouse_database" "mydatabase" {
2+
service_id = xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
3+
name = "mydatabase"
4+
comment = "This is a test database"
5+
}

go.mod

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/hashicorp/terraform-plugin-framework v1.13.0
1111
github.com/hashicorp/terraform-plugin-framework-validators v0.16.0
1212
github.com/hashicorp/terraform-plugin-log v0.9.0
13+
github.com/huandu/go-sqlbuilder v1.33.1
1314
)
1415

1516
require (
@@ -18,6 +19,7 @@ require (
1819
github.com/Masterminds/goutils v1.1.1 // indirect
1920
github.com/Masterminds/semver/v3 v3.2.0 // indirect
2021
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
22+
github.com/Microsoft/go-winio v0.6.2 // indirect
2123
github.com/ProtonMail/go-crypto v1.1.0-alpha.2 // indirect
2224
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
2325
github.com/armon/go-radix v1.0.0 // indirect
@@ -45,8 +47,9 @@ require (
4547
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
4648
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
4749
github.com/hashicorp/yamux v0.1.1 // indirect
48-
github.com/huandu/xstrings v1.3.3 // indirect
50+
github.com/huandu/xstrings v1.4.0 // indirect
4951
github.com/imdario/mergo v0.3.15 // indirect
52+
github.com/kr/pretty v0.3.1 // indirect
5053
github.com/mattn/go-colorable v0.1.13 // indirect
5154
github.com/mattn/go-isatty v0.0.20 // indirect
5255
github.com/mattn/go-runewidth v0.0.9 // indirect
@@ -56,8 +59,9 @@ require (
5659
github.com/oklog/run v1.0.0 // indirect
5760
github.com/pmezard/go-difflib v1.0.0 // indirect
5861
github.com/posener/complete v1.2.3 // indirect
59-
github.com/shopspring/decimal v1.3.1 // indirect
62+
github.com/shopspring/decimal v1.4.0 // indirect
6063
github.com/spf13/cast v1.5.0 // indirect
64+
github.com/stretchr/testify v1.9.0 // indirect
6165
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
6266
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
6367
github.com/yuin/goldmark v1.7.7 // indirect

go.sum

+17-11
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7Y
1010
github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
1111
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
1212
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
13-
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
14-
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
13+
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
14+
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
1515
github.com/ProtonMail/go-crypto v1.1.0-alpha.2 h1:bkyFVUP+ROOARdgCiJzNQo2V2kiB97LyUpzH9P6Hrlg=
1616
github.com/ProtonMail/go-crypto v1.1.0-alpha.2/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQAosU5td4SnOrE=
1717
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
@@ -28,6 +28,7 @@ github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK3
2828
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
2929
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
3030
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
31+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
3132
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=
3233
github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
3334
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -103,8 +104,13 @@ github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S
103104
github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc=
104105
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
105106
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
106-
github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4=
107+
github.com/huandu/go-assert v1.1.6 h1:oaAfYxq9KNDi9qswn/6aE0EydfxSa+tWZC1KabNitYs=
108+
github.com/huandu/go-assert v1.1.6/go.mod h1:JuIfbmYG9ykwvuxoJ3V8TB5QP+3+ajIA54Y44TmkMxs=
109+
github.com/huandu/go-sqlbuilder v1.33.1 h1:lwLv8Azdi5BUmaG/QgRkzeaxyMjaqp5rj39oBbmTi1o=
110+
github.com/huandu/go-sqlbuilder v1.33.1/go.mod h1:mS0GAtrtW+XL6nM2/gXHRJax2RwSW1TraavWDFAc1JA=
107111
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
112+
github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU=
113+
github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
108114
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
109115
github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM=
110116
github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
@@ -114,8 +120,8 @@ github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgf
114120
github.com/jhump/protoreflect v1.15.1/go.mod h1:jD/2GMKKE6OqX8qTjhADU1e6DShO+gavG9e0Q693nKo=
115121
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
116122
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
117-
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
118-
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
123+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
124+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
119125
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
120126
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
121127
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
@@ -141,17 +147,19 @@ github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
141147
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
142148
github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4=
143149
github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI=
150+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
144151
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
145152
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
146153
github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXqo=
147154
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
155+
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
148156
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
149157
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
150158
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
151159
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
152160
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
153-
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
154-
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
161+
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
162+
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
155163
github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A=
156164
github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo=
157165
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
@@ -162,8 +170,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
162170
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
163171
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
164172
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
165-
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
166-
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
173+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
174+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
167175
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
168176
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
169177
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
@@ -197,8 +205,6 @@ golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
197205
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
198206
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
199207
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
200-
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
201-
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
202208
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
203209
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
204210
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

pkg/internal/api/client.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ import (
66
)
77

88
type ClientImpl struct {
9-
BaseUrl string
10-
HttpClient *http.Client
11-
OrganizationId string
12-
TokenKey string
13-
TokenSecret string
9+
BaseUrl string
10+
QueryAPIBaseUrl string
11+
HttpClient *http.Client
12+
OrganizationId string
13+
TokenKey string
14+
TokenSecret string
1415
}
1516

16-
func NewClient(apiUrl string, organizationId string, tokenKey string, tokenSecret string) (*ClientImpl, error) {
17+
func NewClient(apiUrl string, queryApiBaseUrl string, organizationId string, tokenKey string, tokenSecret string) (Client, error) {
1718
client := &ClientImpl{
18-
BaseUrl: apiUrl,
19+
BaseUrl: apiUrl,
20+
QueryAPIBaseUrl: queryApiBaseUrl,
1921
HttpClient: &http.Client{
2022
Timeout: time.Second * 30,
2123
},

0 commit comments

Comments
 (0)