Skip to content

Commit

Permalink
chore: add http if it is missing on dbaas operator endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Jul 15, 2022
1 parent b0df7c7 commit 821bc77
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func NewGenerator(
}

// get the dbaas operator http endpoint or fall back to the default
buildValues.DBaaSOperatorEndpoint = helpers.GetEnv("DBAAS_OPERATOR_HTTP", "dbaas.lagoon.svc:5000", debug)
buildValues.DBaaSOperatorEndpoint = helpers.GetEnv("DBAAS_OPERATOR_HTTP", "http://dbaas.lagoon.svc:5000", debug)

// get the project and environment variables
projectVariables = helpers.GetEnv("LAGOON_PROJECT_VARIABLES", projectVariables, debug)
Expand Down
12 changes: 12 additions & 0 deletions internal/helpers/helpers_dbaas.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"time"
)

var httpClient = &http.Client{Timeout: 10 * time.Second}

func addProtocol(url string) string {
if !strings.Contains(url, "https://") {
if !strings.Contains(url, "http://") {
return fmt.Sprintf("http://%s", url)
}
}
return url
}

func CheckDBaaSHealth(dbaasEndpoint string) error {
// curl --write-out "%{http_code}\n" --silent --output /dev/null "http://dbaas/healthz"
dbaasEndpoint = addProtocol(dbaasEndpoint)
resp, err := httpClient.Get(fmt.Sprintf("%s/healthz", dbaasEndpoint))
if err != nil {
return err
Expand All @@ -30,6 +41,7 @@ type DBaaSProviderResponse struct {
// check the dbaas provider exists, will return true or false without error if it can talk to the dbaas-operator
// will return error if there an issue with the dbaas-operator or the specified endpoint
func CheckDBaaSProvider(dbaasEndpoint, dbaasType, dbaasEnvironment string) (bool, error) {
dbaasEndpoint = addProtocol(dbaasEndpoint)
// curl --silent "http://dbaas/type/env"
resp, err := httpClient.Get(fmt.Sprintf("%s/%s/%s", dbaasEndpoint, dbaasType, dbaasEnvironment))
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions internal/helpers/helpers_dbaas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,43 @@ func TestCheckDBaaSHealth(t *testing.T) {
})
}
}

func Test_addProtocol(t *testing.T) {
type args struct {
url string
}
tests := []struct {
name string
args args
want string
}{
{
name: "test1",
args: args{
url: "dbaas.local.svc:5000",
},
want: "http://dbaas.local.svc:5000",
},
{
name: "test2",
args: args{
url: "https://dbaas.local.svc:5000",
},
want: "https://dbaas.local.svc:5000",
},
{
name: "test3",
args: args{
url: "http://dbaas.local.svc:5000",
},
want: "http://dbaas.local.svc:5000",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := addProtocol(tt.args.url); got != tt.want {
t.Errorf("addProtocol() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 821bc77

Please sign in to comment.