Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbaas: datasource username fix #396

Merged
merged 16 commits into from
Nov 22, 2024
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

BUG FIXES:

- Fix for allowing admin_username for dbaas #396

## 0.61.1

BUG FIXES:
Expand All @@ -10,7 +14,7 @@ BUG FIXES:

IMPROVEMENTS:

- Fetching database credentials via dedicated API endpoints
- Fetching database credentials via dedicated API endpoints #395

## 0.61.0

Expand Down
22 changes: 10 additions & 12 deletions pkg/resources/database/datasource_uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,6 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
var uri string
var params map[string]interface{}

const adminUsername = "avnadmin"

switch data.Type.ValueString() {
case "kafka":
res, err := client.GetDBAASServiceKafka(ctx, data.Name.ValueString())
Expand Down Expand Up @@ -283,9 +281,10 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
return
}

params = res.URIParams
data.Schema = types.StringValue("mysql")

creds, err := client.RevealDBAASMysqlUserPassword(ctx, data.Name.ValueString(), adminUsername)
creds, err := client.RevealDBAASMysqlUserPassword(ctx, data.Name.ValueString(), params["user"].(string))
kobajagi marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
resp.Diagnostics.AddError(
"Client Error",
Expand All @@ -302,7 +301,6 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
return
}

params = res.URIParams
params["password"] = creds.Password
case "pg":
res, err := waitForDBAASService(
Expand All @@ -319,9 +317,10 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
return
}

params = res.URIParams
data.Schema = types.StringValue("postgres")

creds, err := client.RevealDBAASPostgresUserPassword(ctx, data.Name.ValueString(), adminUsername)
creds, err := client.RevealDBAASPostgresUserPassword(ctx, data.Name.ValueString(), params["user"].(string))
if err != nil {
resp.Diagnostics.AddError(
"Client Error",
Expand All @@ -337,7 +336,6 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
)
return
}
params = res.URIParams
params["password"] = creds.Password
case "redis":
res, err := waitForDBAASService(
Expand All @@ -354,10 +352,10 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
return
}

params = res.URIParams
data.Schema = types.StringValue("rediss")
const redisDefaultUsername = "default"

creds, err := client.RevealDBAASRedisUserPassword(ctx, data.Name.ValueString(), redisDefaultUsername)
creds, err := client.RevealDBAASRedisUserPassword(ctx, data.Name.ValueString(), params["user"].(string))
if err != nil {
resp.Diagnostics.AddError(
"Client Error",
Expand Down Expand Up @@ -390,9 +388,10 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
return
}

params = res.URIParams
data.Schema = types.StringValue("https")

creds, err := client.RevealDBAASOpensearchUserPassword(ctx, data.Name.ValueString(), adminUsername)
creds, err := client.RevealDBAASOpensearchUserPassword(ctx, data.Name.ValueString(), params["user"].(string))
if err != nil {
resp.Diagnostics.AddError(
"Client Error",
Expand All @@ -409,7 +408,6 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
return
}

params = res.URIParams
params["password"] = creds.Password
case "grafana":
res, err := waitForDBAASService(
Expand All @@ -427,9 +425,10 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
}

uri = res.URI
params = res.URIParams
data.Schema = types.StringValue("https")

creds, err := client.RevealDBAASGrafanaUserPassword(ctx, data.Name.ValueString(), adminUsername)
creds, err := client.RevealDBAASGrafanaUserPassword(ctx, data.Name.ValueString(), params["user"].(string))
if err != nil {
resp.Diagnostics.AddError(
"Client Error",
Expand All @@ -438,7 +437,6 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
return
}

params = res.URIParams
params["password"] = creds.Password
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/resources/database/datasource_uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func testDataSourceURI(t *testing.T) {
}

fullResourceName := "data.exoscale_database_uri.test"
pgUsername := acctest.RandomWithPrefix(testutils.TestUsername)
data := DataSourceURIModel{
ResourceName: "test",
Name: "exoscale_database.test.name",
Expand All @@ -44,6 +45,7 @@ func testDataSourceURI(t *testing.T) {
Plan: "hobbyist-2",
Zone: testutils.TestZoneName,
TerminationProtection: false,
AdminUsername: pgUsername,
}
buf := &bytes.Buffer{}
err = tplResourcePg.Execute(buf, &resourcePg)
Expand All @@ -70,7 +72,7 @@ func testDataSourceURI(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(fullResourceName, "uri"),
resource.TestCheckResourceAttr(fullResourceName, "schema", "postgres"),
resource.TestCheckResourceAttr(fullResourceName, "username", "avnadmin"),
resource.TestCheckResourceAttr(fullResourceName, "username", pgUsername),
resource.TestCheckResourceAttrSet(fullResourceName, "password"),
resource.TestCheckResourceAttrSet(fullResourceName, "host"),
resource.TestCheckResourceAttrSet(fullResourceName, "port"),
Expand Down
1 change: 1 addition & 0 deletions pkg/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

const (
Prefix = "test-terraform-exoscale"
TestUsername = "test-exo-username"
TestDescription = "Created by the terraform-exoscale provider"
TestZoneName = "bg-sof-1"
TestInstanceTemplateName = "Linux Ubuntu 20.04 LTS 64-bit"
Expand Down
Loading