Skip to content

Commit d7effa7

Browse files
Remove powervs-utils dependency (#759)
Signed-off-by: Prajyot-Parab <[email protected]>
1 parent 066399e commit d7effa7

File tree

7 files changed

+101
-34
lines changed

7 files changed

+101
-34
lines changed

cloud/scope/powervs_cluster.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/IBM/platform-services-go-sdk/resourcecontrollerv2"
2626
"github.com/go-logr/logr"
2727
"github.com/pkg/errors"
28-
utils "github.com/ppc64le-cloud/powervs-utils"
2928
"k8s.io/klog/v2/klogr"
3029
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
3130
"sigs.k8s.io/cluster-api/util/patch"
@@ -121,23 +120,16 @@ func NewPowerVSClusterScope(params PowerVSClusterScopeParams) (scope *PowerVSClu
121120
return
122121
}
123122

124-
region, err := utils.GetRegion(*res.RegionID)
125-
if err != nil {
126-
err = errors.Wrap(err, "failed to get region")
127-
return
128-
}
129-
130123
options := powervs.ServiceOptions{
131124
IBMPIOptions: &ibmpisession.IBMPIOptions{
132-
Debug: params.Logger.V(DEBUGLEVEL).Enabled(),
133-
Region: region,
134-
Zone: *res.RegionID,
125+
Debug: params.Logger.V(DEBUGLEVEL).Enabled(),
126+
Zone: *res.RegionID,
135127
},
136128
CloudInstanceID: spec.ServiceInstanceID,
137129
}
138130

139131
// Fetch the service endpoint.
140-
if svcEndpoint := endpoints.FetchPVSEndpoint(region, params.ServiceEndpoint); svcEndpoint != "" {
132+
if svcEndpoint := endpoints.FetchPVSEndpoint(endpoints.CostructRegionFromZone(*res.RegionID), params.ServiceEndpoint); svcEndpoint != "" {
141133
options.IBMPIOptions.URL = svcEndpoint
142134
scope.Logger.V(3).Info("overriding the default powervs service endpoint")
143135
}

cloud/scope/powervs_image.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/IBM/platform-services-go-sdk/resourcecontrollerv2"
2727
"github.com/go-logr/logr"
2828
"github.com/pkg/errors"
29-
utils "github.com/ppc64le-cloud/powervs-utils"
3029
"k8s.io/klog/v2/klogr"
3130
"sigs.k8s.io/cluster-api/util/patch"
3231
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -114,17 +113,10 @@ func NewPowerVSImageScope(params PowerVSImageScopeParams) (scope *PowerVSImageSc
114113
return
115114
}
116115

117-
region, err := utils.GetRegion(*res.RegionID)
118-
if err != nil {
119-
err = errors.Wrap(err, "failed to get region")
120-
return
121-
}
122-
123116
options := powervs.ServiceOptions{
124117
IBMPIOptions: &ibmpisession.IBMPIOptions{
125-
Debug: params.Logger.V(DEBUGLEVEL).Enabled(),
126-
Region: region,
127-
Zone: *res.RegionID,
118+
Debug: params.Logger.V(DEBUGLEVEL).Enabled(),
119+
Zone: *res.RegionID,
128120
},
129121
CloudInstanceID: spec.ServiceInstanceID,
130122
}
@@ -136,7 +128,7 @@ func NewPowerVSImageScope(params PowerVSImageScopeParams) (scope *PowerVSImageSc
136128
scope.IBMPowerVSCluster = params.IBMPowerVSCluster
137129

138130
// Fetch the service endpoint.
139-
if svcEndpoint := endpoints.FetchPVSEndpoint(region, params.ServiceEndpoint); svcEndpoint != "" {
131+
if svcEndpoint := endpoints.FetchPVSEndpoint(endpoints.CostructRegionFromZone(*res.RegionID), params.ServiceEndpoint); svcEndpoint != "" {
140132
options.IBMPIOptions.URL = svcEndpoint
141133
scope.Logger.V(3).Info("overriding the default powervs service endpoint")
142134
}

cloud/scope/powervs_machine.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/IBM/platform-services-go-sdk/resourcecontrollerv2"
3131
"github.com/go-logr/logr"
3232
"github.com/pkg/errors"
33-
powerVSUtils "github.com/ppc64le-cloud/powervs-utils"
3433
corev1 "k8s.io/api/core/v1"
3534
"k8s.io/apimachinery/pkg/types"
3635
"k8s.io/client-go/tools/cache"
@@ -144,19 +143,14 @@ func NewPowerVSMachineScope(params PowerVSMachineScopeParams) (scope *PowerVSMac
144143
return
145144
}
146145

147-
region, err := powerVSUtils.GetRegion(*res.RegionID)
148-
if err != nil {
149-
err = errors.Wrap(err, "failed to get region")
150-
return
151-
}
146+
region := endpoints.CostructRegionFromZone(*res.RegionID)
152147
scope.SetRegion(region)
153148
scope.SetZone(*res.RegionID)
154149

155150
serviceOptions := powervs.ServiceOptions{
156151
IBMPIOptions: &ibmpisession.IBMPIOptions{
157-
Debug: params.Logger.V(DEBUGLEVEL).Enabled(),
158-
Region: region,
159-
Zone: *res.RegionID,
152+
Debug: params.Logger.V(DEBUGLEVEL).Enabled(),
153+
Zone: *res.RegionID,
160154
},
161155
CloudInstanceID: m.Spec.ServiceInstanceID,
162156
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ require (
1313
github.com/onsi/ginkgo v1.16.5
1414
github.com/onsi/gomega v1.19.0
1515
github.com/pkg/errors v0.9.1
16-
github.com/ppc64le-cloud/powervs-utils v0.0.0-20220607051746-a593c549605b
1716
github.com/spf13/pflag v1.0.5
1817
github.com/stretchr/testify v1.8.0
1918
golang.org/x/text v0.3.7

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
780780
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
781781
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
782782
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
783-
github.com/ppc64le-cloud/powervs-utils v0.0.0-20220607051746-a593c549605b h1:OljQqJCHS68jU3iKjhpGsD85OKOLCkApmAU3jLYMwIA=
784-
github.com/ppc64le-cloud/powervs-utils v0.0.0-20220607051746-a593c549605b/go.mod h1:KImYgHmvBVtAczNhyDBDSN54PGIdz0+QiPVQMmObEQY=
785783
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=
786784
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
787785
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=

pkg/endpoints/endpoints.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package endpoints
1919
import (
2020
"errors"
2121
"net/url"
22+
"regexp"
2223
"strings"
2324
)
2425

@@ -142,3 +143,18 @@ func FetchRCEndpoint(serviceEndpoint []ServiceEndpoint) string {
142143
}
143144
return ""
144145
}
146+
147+
// CostructRegionFromZone Calculate region based on location/zone.
148+
func CostructRegionFromZone(zone string) string {
149+
var regex string
150+
if strings.Contains(zone, "-") {
151+
// it's a region or AZ
152+
regex = "-[0-9]+$"
153+
} else {
154+
// it's a datacenter
155+
regex = "[0-9]+$"
156+
}
157+
158+
reg, _ := regexp.Compile(regex)
159+
return reg.ReplaceAllString(zone, "")
160+
}

pkg/endpoints/endpoints_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,79 @@ func TestFetchRCEndpoint(t *testing.T) {
255255
})
256256
}
257257
}
258+
259+
func TestCostructRegionFromZone(t *testing.T) {
260+
testCases := []struct {
261+
name string
262+
zone string
263+
expectedRegion string
264+
}{
265+
{
266+
name: "Return region dal",
267+
zone: "dal12",
268+
expectedRegion: "dal",
269+
},
270+
{
271+
name: "Return region eu-de",
272+
zone: "eu-de-1",
273+
expectedRegion: "eu-de",
274+
},
275+
{
276+
name: "Return region lon04",
277+
zone: "lon04",
278+
expectedRegion: "lon",
279+
},
280+
{
281+
name: "Return region mon01",
282+
zone: "mon01",
283+
expectedRegion: "mon",
284+
},
285+
{
286+
name: "Return region osa21",
287+
zone: "osa21",
288+
expectedRegion: "osa",
289+
},
290+
{
291+
name: "Return region sao01",
292+
zone: "sao01",
293+
expectedRegion: "sao",
294+
},
295+
{
296+
name: "Return region syd04",
297+
zone: "syd04",
298+
expectedRegion: "syd",
299+
},
300+
{
301+
name: "Return region tok04",
302+
zone: "tok04",
303+
expectedRegion: "tok",
304+
},
305+
{
306+
name: "Return region tor01",
307+
zone: "tor01",
308+
expectedRegion: "tor",
309+
},
310+
{
311+
name: "Return region wdc06",
312+
zone: "wdc06",
313+
expectedRegion: "wdc",
314+
},
315+
{
316+
name: "Return region us-east",
317+
zone: "us-east",
318+
expectedRegion: "us-east",
319+
},
320+
{
321+
name: "Return region us-south",
322+
zone: "us-south",
323+
expectedRegion: "us-south",
324+
},
325+
}
326+
327+
for _, tc := range testCases {
328+
t.Run(tc.name, func(t *testing.T) {
329+
out := CostructRegionFromZone(tc.zone)
330+
require.Equal(t, tc.expectedRegion, out)
331+
})
332+
}
333+
}

0 commit comments

Comments
 (0)