Skip to content

Commit

Permalink
added reveal calls and URI func
Browse files Browse the repository at this point in the history
  • Loading branch information
simisoft-exo committed Oct 24, 2024
1 parent 6713f21 commit 3fd76ba
Showing 1 changed file with 87 additions and 9 deletions.
96 changes: 87 additions & 9 deletions pkg/resources/database/datasource_uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database
import (
"context"
"fmt"
"regexp"
"strconv"

exoscale "github.com/exoscale/egoscale/v3"
Expand Down Expand Up @@ -35,6 +36,25 @@ type DataSourceURI struct {
client *exoscale.Client
}

func uriWithPassword(uri string, username string, password string) (string, error) {
if uri == "" {
return "", fmt.Errorf("empty URI provided")
}

re := regexp.MustCompile(`(.*)://(.*)@(.*)`)
matches := re.FindStringSubmatch(uri)

if len(matches) != 4 {
return "", fmt.Errorf("URI must contain username (format: protocol://[email protected])")
}

return fmt.Sprintf("%s://%s:%s@%s",
matches[1],
username,
password,
matches[3]), nil
}

// NewDataSourceURI creates instance of DataSourceURI.
func NewDataSourceURI() datasource.DataSource {
return &DataSourceURI{}
Expand Down Expand Up @@ -193,6 +213,8 @@ 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": // kafka has: schema, host & port
res, err := client.GetDBAASServiceKafka(ctx, data.Name.ValueString())
Expand All @@ -204,50 +226,87 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
return
}

uri = res.URI
creds, err := client.RevealDBAASKafkaUserPassword(ctx, data.Name.ValueString(), adminUsername)

Check failure on line 229 in pkg/resources/database/datasource_uri.go

View workflow job for this annotation

GitHub Actions / test

this value of err is never used (SA4006)
uri, err = uriWithPassword(res.URI, creds.Username, creds.Password)

if err != nil {
resp.Diagnostics.AddError("Client Error",
fmt.Sprintf("Unable to get Database Service kafka secret: %s", err),
)
return
}

params = res.URIParams
params["password"] = creds.Password
case "mysql":
res, err := client.GetDBAASServiceMysql(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 MySQL: %s", err),
)
return
}

data.Schema = types.StringValue("mysql")

uri = res.URI
creds, err := client.RevealDBAASMysqlUserPassword(ctx, data.Name.ValueString(), adminUsername)

Check failure on line 253 in pkg/resources/database/datasource_uri.go

View workflow job for this annotation

GitHub Actions / test

this value of err is never used (SA4006)
uri, err = uriWithPassword(res.URI, creds.Username, creds.Password)

if err != nil {
resp.Diagnostics.AddError("Client Error",
fmt.Sprintf("Unable to get Database Service MySQL secret: %s", err),
)
return
}
params = res.URIParams
params["password"] = creds.Password
case "pg":
res, err := client.GetDBAASServicePG(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 Postgres: %s", err),
)
return
}

data.Schema = types.StringValue("postgres")

uri = res.URI
creds, err := client.RevealDBAASPostgresUserPassword(ctx, data.Name.ValueString(), adminUsername)

Check failure on line 276 in pkg/resources/database/datasource_uri.go

View workflow job for this annotation

GitHub Actions / test

this value of err is never used (SA4006)
uri, err = uriWithPassword(res.URI, creds.Username, creds.Password)

if err != nil {
resp.Diagnostics.AddError("Client Error",
fmt.Sprintf("Unable to get Database Service Postgres secret: %s", err),
)
return
}
params = res.URIParams
params["password"] = creds.Password
case "redis":
res, err := client.GetDBAASServiceRedis(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 Redis: %s", err),
)
return
}

data.Schema = types.StringValue("rediss")

uri = res.URI
creds, err := client.RevealDBAASRedisUserPassword(ctx, data.Name.ValueString(), adminUsername)

Check failure on line 299 in pkg/resources/database/datasource_uri.go

View workflow job for this annotation

GitHub Actions / test

this value of err is never used (SA4006)
uri, err = uriWithPassword(res.URI, creds.Username, creds.Password)

if err != nil {
resp.Diagnostics.AddError("Client Error",
fmt.Sprintf("Unable to get Database Service Redis secret: %s", err),
)
return
}
params = res.URIParams
params["password"] = creds.Password
case "opensearch":
res, err := client.GetDBAASServiceOpensearch(ctx, data.Name.ValueString())
if err != nil {
Expand All @@ -261,21 +320,40 @@ func (d *DataSourceURI) Read(ctx context.Context, req datasource.ReadRequest, re
data.Schema = types.StringValue("https")

uri = res.URI

Check failure on line 322 in pkg/resources/database/datasource_uri.go

View workflow job for this annotation

GitHub Actions / test

this value of uri is never used (SA4006)
creds, err := client.RevealDBAASOpensearchUserPassword(ctx, data.Name.ValueString(), adminUsername)

Check failure on line 323 in pkg/resources/database/datasource_uri.go

View workflow job for this annotation

GitHub Actions / test

this value of err is never used (SA4006)
uri, err = uriWithPassword(res.URI, creds.Username, creds.Password)

if err != nil {
resp.Diagnostics.AddError("Client Error",
fmt.Sprintf("Unable to get Database Service OpenSearch secret: %s", err),
)
return
}
params = res.URIParams
params["password"] = creds.Password
case "grafana":
res, err := client.GetDBAASServiceGrafana(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 Grafana: %s", err),
)
return
}

data.Schema = types.StringValue("https")

uri = res.URI
creds, err := client.RevealDBAASGrafanaUserPassword(ctx, data.Name.ValueString(), adminUsername)

Check failure on line 346 in pkg/resources/database/datasource_uri.go

View workflow job for this annotation

GitHub Actions / test

this value of err is never used (SA4006)
uri, err = uriWithPassword(res.URI, creds.Username, creds.Password)

if err != nil {
resp.Diagnostics.AddError("Client Error",
fmt.Sprintf("Unable to get Database Service Grafana secret: %s", err),
)
return
}
params = res.URIParams
params["password"] = creds.Password
}

data.URI = types.StringValue(uri)
Expand Down

0 comments on commit 3fd76ba

Please sign in to comment.