Skip to content

Commit b19e8db

Browse files
OpenAPI: Fix API calls pointing to subpath (#1145)
Closes #1137 Currently, the test suite is pointing to a subpath BUT Grafana redirects from the root to the subpath The provider is badly configured but the tests still pass because of the redirect To fix the test suite, I add a nginx proxy in this PR, so that calling the host directly fails and you have to go through the subpath I've also reworked the Makefile so that it has more common args (less repetition of configs), so it's harder to make a mistake
1 parent 6892869 commit b19e8db

File tree

4 files changed

+74
-38
lines changed

4 files changed

+74
-38
lines changed

GNUmakefile

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
GRAFANA_VERSION ?= 10.1.5
2+
DOCKER_COMPOSE_ARGS ?= --force-recreate --detach --remove-orphans --wait
23

34
testacc:
45
TF_ACC=1 go test ./... -v $(TESTARGS) -timeout 120m
56

67
# Test OSS features
78
testacc-oss:
8-
TF_ACC_OSS=true make testacc
9+
GRAFANA_AUTH="$${GRAFANA_AUTH:-admin:admin}" TF_ACC_OSS=true make testacc
910

1011
# Test Enterprise features
1112
testacc-enterprise:
12-
TF_ACC_ENTERPRISE=true make testacc
13+
GRAFANA_AUTH="$${GRAFANA_AUTH:-admin:admin}" TF_ACC_ENTERPRISE=true make testacc
1314

1415
# Test Cloud API features
1516
testacc-cloud-api:
@@ -20,48 +21,35 @@ testacc-cloud-instance:
2021
TF_ACC_CLOUD_INSTANCE=true make testacc
2122

2223
testacc-oss-docker:
23-
GRAFANA_VERSION=$(GRAFANA_VERSION) docker compose up --force-recreate --detach --remove-orphans --wait
24-
25-
GRAFANA_VERSION=$(GRAFANA_VERSION) \
26-
GRAFANA_URL="http://0.0.0.0:3000" \
27-
GRAFANA_AUTH="admin:admin" \
28-
make testacc-oss
29-
24+
export GRAFANA_URL=http://0.0.0.0:3000 && \
25+
export GRAFANA_VERSION=$(GRAFANA_VERSION) && \
26+
docker compose up $(DOCKER_COMPOSE_ARGS) && \
27+
make testacc-oss && \
3028
docker compose down
3129

3230
testacc-enterprise-docker:
33-
GRAFANA_IMAGE=grafana/grafana-enterprise GRAFANA_VERSION=$(GRAFANA_VERSION) docker compose up --force-recreate --detach --remove-orphans --wait
34-
35-
GRAFANA_VERSION=$(GRAFANA_VERSION) \
36-
GRAFANA_URL="http://0.0.0.0:3000" \
37-
GRAFANA_AUTH="admin:admin" \
38-
make testacc-enterprise
39-
31+
export GRAFANA_URL=http://0.0.0.0:3000 && \
32+
export GRAFANA_VERSION=$(GRAFANA_VERSION) && \
33+
GRAFANA_IMAGE=grafana/grafana-enterprise docker compose up $(DOCKER_COMPOSE_ARGS) && \
34+
make testacc-enterprise && \
4035
docker compose down
4136

4237
testacc-tls-docker:
43-
make -C testdata generate
44-
GRAFANA_VERSION=$(GRAFANA_VERSION) docker compose --profile tls up --force-recreate --detach --remove-orphans --wait
45-
46-
GRAFANA_VERSION=$(GRAFANA_VERSION) \
47-
GRAFANA_URL="http://0.0.0.0:3000" \
48-
GRAFANA_AUTH="admin:admin" \
49-
GRAFANA_TLS_KEY=$$(pwd)/testdata/client.key \
50-
GRAFANA_TLS_CERT=$$(pwd)/testdata/client.crt \
51-
GRAFANA_CA_CERT=$$(pwd)/testdata/ca.crt \
52-
make testacc-oss
53-
38+
export GRAFANA_URL=https://0.0.0.0:3001 && \
39+
export GRAFANA_VERSION=$(GRAFANA_VERSION) && \
40+
make -C testdata generate && \
41+
docker compose --profile tls up $(DOCKER_COMPOSE_ARGS) && \
42+
GRAFANA_TLS_KEY=$$(pwd)/testdata/client.key GRAFANA_TLS_CERT=$$(pwd)/testdata/client.crt GRAFANA_CA_CERT=$$(pwd)/testdata/ca.crt make testacc-oss && \
5443
docker compose --profile tls down
5544

5645
testacc-subpath-docker:
57-
GRAFANA_VERSION=$(GRAFANA_VERSION) GRAFANA_SUBPATH=/grafana/ GF_SERVER_SERVE_FROM_SUB_PATH=true docker compose up --force-recreate --detach --remove-orphans --wait
58-
59-
GRAFANA_VERSION=$(GRAFANA_VERSION) \
60-
GRAFANA_URL="http://0.0.0.0:3000/grafana" \
61-
GRAFANA_AUTH="admin:admin" \
62-
make testacc-oss
63-
64-
docker compose down
46+
export GRAFANA_SUBPATH=/grafana/ && \
47+
export GF_SERVER_SERVE_FROM_SUB_PATH=true && \
48+
export GRAFANA_URL=http://0.0.0.0:3001$${GRAFANA_SUBPATH} && \
49+
export GRAFANA_VERSION=$(GRAFANA_VERSION) && \
50+
docker compose --profile proxy up $(DOCKER_COMPOSE_ARGS) && \
51+
make testacc-oss && \
52+
docker compose --profile proxy down
6553

6654
release:
6755
@test $${RELEASE_VERSION?Please set environment variable RELEASE_VERSION}

docker-compose.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ services:
55
- 3000:3000
66
image: ${GRAFANA_IMAGE:-grafana/grafana}:${GRAFANA_VERSION}
77
environment:
8-
- GF_SERVER_ROOT_URL=http://0.0.0.0:3000${GRAFANA_SUBPATH:-}
8+
- GF_SERVER_ROOT_URL=${GRAFANA_URL}
99
- GF_ENTERPRISE_LICENSE_TEXT=${GF_ENTERPRISE_LICENSE_TEXT:-}
1010
- GF_SERVER_SERVE_FROM_SUB_PATH=${GF_SERVER_SERVE_FROM_SUB_PATH:-}
1111
healthcheck:
12-
test: wget --no-verbose --tries=1 --spider http://0.0.0.0:3000${GRAFANA_SUBPATH:-}/api/health || exit 1 # Use wget because older versions of Grafana don't have curl
12+
test: wget --no-verbose --tries=1 --spider http://0.0.0.0:3000/api/health || exit 1 # Use wget because older versions of Grafana don't have curl
1313
interval: 10s
1414
retries: 10
1515
start_period: 10s
@@ -32,3 +32,13 @@ services:
3232
- ./testdata:/certs
3333
ports:
3434
- 3001:3001
35+
nginx:
36+
profiles:
37+
- "proxy"
38+
depends_on:
39+
- grafana
40+
image: nginx:latest
41+
ports:
42+
- 3001:3001
43+
volumes:
44+
- ./testdata/nginx.conf:/etc/nginx/nginx.conf

internal/provider/provider.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ func createGrafanaOAPIClient(apiURL string, d *schema.ResourceData) (*goapi.Graf
430430
if err != nil {
431431
return nil, fmt.Errorf("failed to parse API url: %v", err.Error())
432432
}
433+
apiPath, err := url.JoinPath(u.Path, "api")
434+
if err != nil {
435+
return nil, fmt.Errorf("failed to join API path: %v", err.Error())
436+
}
433437

434438
userInfo, orgID, APIKey, err := parseAuth(d)
435439
if err != nil {
@@ -438,7 +442,7 @@ func createGrafanaOAPIClient(apiURL string, d *schema.ResourceData) (*goapi.Graf
438442

439443
cfg := goapi.TransportConfig{
440444
Host: u.Host,
441-
BasePath: "/api",
445+
BasePath: apiPath,
442446
Schemes: []string{u.Scheme},
443447
NumRetries: d.Get("retries").(int),
444448
RetryTimeout: time.Second * time.Duration(d.Get("retry_wait").(int)),

testdata/nginx.conf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
events{}
2+
3+
http {
4+
# this is required to proxy Grafana Live WebSocket connections.
5+
map $http_upgrade $connection_upgrade {
6+
default upgrade;
7+
'' close;
8+
}
9+
10+
upstream grafana {
11+
server grafana:3000;
12+
}
13+
14+
server {
15+
listen 3001;
16+
root /usr/share/nginx/html;
17+
index index.html index.htm;
18+
server_name 0.0.0.0;
19+
20+
location /grafana/ {
21+
proxy_set_header Host $host;
22+
proxy_pass http://grafana;
23+
}
24+
25+
# Proxy Grafana Live WebSocket connections.
26+
location /api/live/ {
27+
proxy_http_version 1.1;
28+
proxy_set_header Upgrade $http_upgrade;
29+
proxy_set_header Connection $connection_upgrade;
30+
proxy_set_header Host $host;
31+
proxy_pass http://grafana;
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)