Skip to content

Commit

Permalink
Deprecate region, auto generate based on zone
Browse files Browse the repository at this point in the history
Signed-off-by: Yussuf Shaikh <[email protected]>
  • Loading branch information
yussufsh committed Mar 10, 2022
1 parent b199e83 commit f8fa6ec
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
22 changes: 19 additions & 3 deletions ibmpisession/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ibmpisession

import (
"fmt"
"regexp"
"strings"

"github.com/go-openapi/runtime"
Expand Down Expand Up @@ -31,13 +32,12 @@ type IBMPIOptions struct {

// Region of the Power Cloud Service Instance
// For generating the default endpoint
// Required when URL or env `IBMCLOUD_POWER_API_ENDPOINT` is not set
// Deprecated: Region is deprecated, the URL is auto generated based on Zone when not provided.
Region string

// Power Virtual Server host or URL endpoint
// This will be used instead of generating the default host
// eg: dal.power-iaas.cloud.ibm.com
// Required when Region is not provided
URL string

// Account id of the Power Cloud Service Instance
Expand Down Expand Up @@ -80,7 +80,9 @@ func NewIBMPISession(o *IBMPIOptions) (*IBMPISession, error) {
if o.Region != "" {
serviceURL = o.Region + ".power-iaas.cloud.ibm.com"
} else {
return nil, fmt.Errorf("atleast one of Region or URL is required")
region := costructRegionFromZone(o.Zone)
serviceURL = region + ".power-iaas.cloud.ibm.com"

}
}
}
Expand Down Expand Up @@ -119,3 +121,17 @@ func (s *IBMPISession) AuthInfo(cloudInstanceID string) runtime.ClientAuthInfoWr
return r.SetHeaderParam("CRN", fmt.Sprintf(s.CRNFormat, cloudInstanceID))
})
}

func costructRegionFromZone(zone string) string {
var regex string
if strings.Contains(zone, "-") {
// it's a region or AZ
regex = "-[0-9]+$"
} else {
// it's a datacenter
regex = "[0-9]+$"
}

reg, _ := regexp.Compile(regex)
return reg.ReplaceAllString(zone, "")
}
45 changes: 44 additions & 1 deletion ibmpisession/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ func TestNewIBMPISession(t *testing.T) {
Zone: "dal12",
},
},
wantErr: true,
want: &IBMPISession{
Options: o1,
CRNFormat: "crn:v1:bluemix:public:power-iaas:dal12:a/1234:%s::",
},
},
{
name: "Without URL but with region",
Expand Down Expand Up @@ -253,3 +256,43 @@ func TestIBMPISession_AuthInfo(t *testing.T) {
})
}
}

func Test_costructRegionFromZone(t *testing.T) {
type args struct {
zone string
}
tests := []struct {
name string
args args
want string
}{
{
name: "DC Zone",
args: args{
zone: "dal12",
},
want: "dal",
},
{
name: "AZ Zone",
args: args{
zone: "eu-de-1",
},
want: "eu-de",
},
{
name: "Region Zone",
args: args{
zone: "us-south",
},
want: "us-south",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := costructRegionFromZone(tt.args.zone); got != tt.want {
t.Errorf("costructRegionFromZone() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit f8fa6ec

Please sign in to comment.