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
87 changes: 73 additions & 14 deletions pkg/resources/database/datasource_uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,15 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re

var uri string
var params map[string]interface{}

const adminUsername = "avnadmin"
var user string

switch data.Type.ValueString() {
case "kafka":
res, err := client.GetDBAASServiceKafka(ctx, data.Name.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Client Error",
fmt.Sprintf("Unable to read Database Service kafka: %s", err),
fmt.Sprintf("Unable to read Database Service Kafka: %s", err),
)
return
}
Expand All @@ -283,9 +282,22 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
return
}

params = res.URIParams
if i, ok := params["user"]; ok {
if s, ok := i.(string); ok {
user = s
}
}
if user == "" {
resp.Diagnostics.AddError(
"Client Error",
"Database Service MySQL user is empty",
)
return
}
data.Schema = types.StringValue("mysql")

creds, err := client.RevealDBAASMysqlUserPassword(ctx, data.Name.ValueString(), adminUsername)
creds, err := client.RevealDBAASMysqlUserPassword(ctx, data.Name.ValueString(), user)
if err != nil {
resp.Diagnostics.AddError(
"Client Error",
Expand All @@ -302,7 +314,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 +330,22 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
return
}

params = res.URIParams
if i, ok := params["user"]; ok {
if s, ok := i.(string); ok {
user = s
}
}
if user == "" {
resp.Diagnostics.AddError(
"Client Error",
"Database Service Postgres user is empty",
)
return
}
data.Schema = types.StringValue("postgres")

creds, err := client.RevealDBAASPostgresUserPassword(ctx, data.Name.ValueString(), adminUsername)
creds, err := client.RevealDBAASPostgresUserPassword(ctx, data.Name.ValueString(), user)
if err != nil {
resp.Diagnostics.AddError(
"Client Error",
Expand All @@ -337,7 +361,7 @@ 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 +378,22 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
return
}

params = res.URIParams
if i, ok := params["user"]; ok {
if s, ok := i.(string); ok {
user = s
}
}
if user == "" {
resp.Diagnostics.AddError(
"Client Error",
"Database Service Redis user is empty",
)
return
}
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(), user)
if err != nil {
resp.Diagnostics.AddError(
"Client Error",
Expand All @@ -373,7 +409,6 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
)
return
}
params = res.URIParams
params["password"] = creds.Password
case "opensearch":
res, err := waitForDBAASService(
Expand All @@ -390,9 +425,22 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
return
}

params = res.URIParams
if i, ok := params["user"]; ok {
if s, ok := i.(string); ok {
user = s
}
}
if user == "" {
resp.Diagnostics.AddError(
"Client Error",
"Database Service Opensearch user is empty",
)
return
}
data.Schema = types.StringValue("https")

creds, err := client.RevealDBAASOpensearchUserPassword(ctx, data.Name.ValueString(), adminUsername)
creds, err := client.RevealDBAASOpensearchUserPassword(ctx, data.Name.ValueString(), user)
if err != nil {
resp.Diagnostics.AddError(
"Client Error",
Expand All @@ -409,7 +457,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 +474,22 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
}

uri = res.URI
params = res.URIParams
if i, ok := params["user"]; ok {
if s, ok := i.(string); ok {
user = s
}
}
if user == "" {
resp.Diagnostics.AddError(
"Client Error",
"Database Service Grafana user is empty",
)
return
}
data.Schema = types.StringValue("https")

creds, err := client.RevealDBAASGrafanaUserPassword(ctx, data.Name.ValueString(), adminUsername)
creds, err := client.RevealDBAASGrafanaUserPassword(ctx, data.Name.ValueString(), user)
if err != nil {
resp.Diagnostics.AddError(
"Client Error",
Expand All @@ -438,7 +498,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