Skip to content

Commit

Permalink
Support http server mainly for tests
Browse files Browse the repository at this point in the history
Signed-off-by: Yussuf Shaikh <yussuf.shaikh@ibm.com>
  • Loading branch information
yussufsh committed Jan 14, 2022
1 parent b0182c0 commit 03c4a61
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
18 changes: 14 additions & 4 deletions ibmpisession/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,23 @@ func NewIBMPISession(o *IBMPIOptions) (*IBMPISession, error) {
}

// We need just the server host from the URL
serviceURL = strings.TrimPrefix(serviceURL, "https://")
serviceURL = strings.TrimPrefix(serviceURL, "http://")
var host, scheme string
if strings.HasPrefix(serviceURL, "https://") {
scheme = SCHEME_HTTPS
host = strings.TrimPrefix(serviceURL, "https://")
} else if strings.HasPrefix(serviceURL, "http://") {
scheme = SCHEME_HTTP
host = strings.TrimPrefix(serviceURL, "http://")
} else {
// by default we use "https"
scheme = SCHEME_HTTPS
host = serviceURL
}

return &IBMPISession{
CRNFormat: crnBuilder(o.UserAccount, o.Zone, serviceURL),
CRNFormat: crnBuilder(o.UserAccount, o.Zone, host),
Options: o,
Power: getPIClient(o.Debug, serviceURL),
Power: getPIClient(o.Debug, host, scheme),
}, nil
}

Expand Down
36 changes: 36 additions & 0 deletions ibmpisession/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,42 @@ func TestNewIBMPISession(t *testing.T) {
CRNFormat: "crn:v1:bluemix:public:power-iaas:dal12:a/1234:%s::",
},
},
{
name: "Simple URL with https",
args: args{
o: &IBMPIOptions{
Authenticator: &core.NoAuthAuthenticator{},
UserAccount: "1234",
Region: "dal",
Zone: "dal12",
URL: "https://dal.power-iaas.test.cloud.ibm.com",
},
},
want: &IBMPISession{
Options: &IBMPIOptions{
Authenticator: &core.NoAuthAuthenticator{},
},
CRNFormat: "crn:v1:staging:public:power-iaas:dal12:a/1234:%s::",
},
},
{
name: "Simple URL with http",
args: args{
o: &IBMPIOptions{
Authenticator: &core.NoAuthAuthenticator{},
UserAccount: "1234",
Region: "dal",
Zone: "dal12",
URL: "http://dal.power-iaas.test.cloud.ibm.com",
},
},
want: &IBMPISession{
Options: &IBMPIOptions{
Authenticator: &core.NoAuthAuthenticator{},
},
CRNFormat: "crn:v1:staging:public:power-iaas:dal12:a/1234:%s::",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
12 changes: 10 additions & 2 deletions ibmpisession/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import (
httptransport "github.com/go-openapi/runtime/client"
)

const (
SCHEME_HTTPS = "https"
SCHEME_HTTP = "http"
)

// fetchAuthorizationData Fetch Authorization token using the Authenticator
func fetchAuthorizationData(a core.Authenticator) (string, error) {
req := &http.Request{
Expand Down Expand Up @@ -56,9 +61,12 @@ func powerJSONConsumer() runtime.Consumer {
}

// getPIClient generates a PowerIaas client
func getPIClient(debug bool, host string) *client.PowerIaasAPI {
func getPIClient(debug bool, host string, scheme string) *client.PowerIaasAPI {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: false}
transport := httptransport.New(host, "/", []string{"https"})
if scheme == "" {
scheme = SCHEME_HTTPS
}
transport := httptransport.New(host, "/", []string{scheme})
transport.Debug = debug
transport.SetLogger(IBMPILogger{})
transport.Consumers[runtime.JSONMime] = powerJSONConsumer()
Expand Down
32 changes: 32 additions & 0 deletions ibmpisession/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,35 @@ func Test_powerJSONConsumer(t *testing.T) {
})
}
}

func Test_getPIClient(t *testing.T) {
type args struct {
debug bool
host string
scheme string
}
tests := []struct {
name string
args args
}{
{
name: "Scheme HTTTP",
args: args{debug: true, host: "", scheme: "http"},
},
{
name: "Scheme HTTTPS",
args: args{debug: true, host: "", scheme: "https"},
},
{
name: "Scheme Empty",
args: args{debug: true, host: "", scheme: ""},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getPIClient(tt.args.debug, tt.args.host, tt.args.scheme); got == nil {
t.Errorf("getPIClient() = %v", got)
}
})
}
}

0 comments on commit 03c4a61

Please sign in to comment.