Skip to content

Commit

Permalink
Merge functionality with IPI
Browse files Browse the repository at this point in the history
The IPI installer has a file (pkg/types/powervs/powervs_regions.go) which
contains similiar data and functions.  We should merge the two and then use
this library in IPI.
  • Loading branch information
hamzy committed Jan 25, 2024
1 parent 7588e95 commit c431c89
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21

- name: Build
run: go build -v ./...
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module github.com/ppc64le-cloud/powervs-utils

go 1.15
go 1.21

toolchain go1.21.4

require k8s.io/apimachinery v0.29.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
k8s.io/apimachinery v0.29.1 h1:KY4/E6km/wLBguvCZv8cKTeOwwOBqFNjwJIdMkMbbRc=
k8s.io/apimachinery v0.29.1/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU=
85 changes: 83 additions & 2 deletions region.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package utils
import (
"fmt"
"strings"

"k8s.io/apimachinery/pkg/util/sets"
)

func GetRegion(zone string) (region string, err error) {
Expand Down Expand Up @@ -46,6 +48,7 @@ type Region struct {
VPCRegion string
COSRegion string
Zones []string
SysTypes []string
}

// Regions provides a mapping between Power VS and IBM Cloud VPC and IBM COS regions.
Expand All @@ -54,7 +57,11 @@ var Regions = map[string]Region{
Description: "Dallas, USA",
VPCRegion: "us-south",
COSRegion: "us-south",
Zones: []string{"dal12"},
Zones: []string{
"dal10",
"dal12",
},
SysTypes: []string{"s922", "e980"},
},
"eu-de": {
Description: "Frankfurt, Germany",
Expand All @@ -64,6 +71,7 @@ var Regions = map[string]Region{
"eu-de-1",
"eu-de-2",
},
SysTypes: []string{"s922", "e980"},
},
"lon": {
Description: "London, UK.",
Expand All @@ -73,27 +81,31 @@ var Regions = map[string]Region{
"lon04",
"lon06",
},
SysTypes: []string{"s922", "e980"},
},
"mad": {
Description: "Madrid, Spain",
VPCRegion: "eu-es",
COSRegion: "eu-es",
COSRegion: "eu-de", // @HACK - PowerVS says COS not supported in this region
Zones: []string{
"mad02",
"mad04",
},
SysTypes: []string{"s1022"},
},
"mon": {
Description: "Montreal, Canada",
VPCRegion: "ca-tor",
COSRegion: "ca-tor",
Zones: []string{"mon01"},
SysTypes: []string{"s922", "e980"},
},
"osa": {
Description: "Osaka, Japan",
VPCRegion: "jp-osa",
COSRegion: "jp-osa",
Zones: []string{"osa21"},
SysTypes: []string{"s922", "e980"},
},
"syd": {
Description: "Sydney, Australia",
Expand All @@ -103,6 +115,7 @@ var Regions = map[string]Region{
"syd04",
"syd05",
},
SysTypes: []string{"s922", "e980"},
},
"sao": {
Description: "São Paulo, Brazil",
Expand All @@ -112,18 +125,21 @@ var Regions = map[string]Region{
"sao01",
"sao04",
},
SysTypes: []string{"s922", "e980"},
},
"tok": {
Description: "Tokyo, Japan",
VPCRegion: "jp-tok",
COSRegion: "jp-tok",
Zones: []string{"tok04"},
SysTypes: []string{"s922", "e980"},
},
"us-east": {
Description: "Washington DC, USA",
VPCRegion: "us-east",
COSRegion: "us-east",
Zones: []string{"us-east"},
SysTypes: []string{}, // Missing
},
"wdc": {
Description: "Washington DC, USA",
Expand All @@ -133,9 +149,21 @@ var Regions = map[string]Region{
"wdc06",
"wdc07",
},
SysTypes: []string{"s922", "e980"},
},
}

// COSRegionForVPCRegion returns the corresponding COS region for the given VPC region
func COSRegionForVPCRegion(vpcRegion string) (string, error) {
for r := range Regions {
if vpcRegion == Regions[r].VPCRegion {
return Regions[r].COSRegion, nil
}
}

return "", fmt.Errorf("COS region corresponding to a VPC region %s not found ", vpcRegion)
}

// VPCRegionForPowerVSRegion returns the VPC region for the specified PowerVS region.
func VPCRegionForPowerVSRegion(region string) (string, error) {
if r, ok := Regions[region]; ok {
Expand Down Expand Up @@ -182,3 +210,56 @@ func RegionShortNames() []string {
}
return keys
}

// ValidateZone validates that the given zone is known/tested.
func ValidateZone(zone string) bool {
for r := range Regions {
for z := range Regions[r].Zones {
if zone == Regions[r].Zones[z] {
return true
}
}
}
return false
}

// ZoneNames returns the list of zone names.
func ZoneNames() []string {
zones := []string{}
for r := range Regions {
for z := range Regions[r].Zones {
zones = append(zones, Regions[r].Zones[z])
}
}
return zones
}

// RegionFromZone returns the region name for a given zone name.
func RegionFromZone(zone string) string {
for r := range Regions {
for z := range Regions[r].Zones {
if zone == Regions[r].Zones[z] {
return r
}
}
}
return ""
}

// AvailableSysTypes returns the default system type for the zone.
func AvailableSysTypes(region string) ([]string, error) {
knownRegion, ok := Regions[region]
if !ok {
return nil, fmt.Errorf("unknown region name provided")
}
return knownRegion.SysTypes, nil
}

// AllKnownSysTypes returns aggregated known system types from all regions.
func AllKnownSysTypes() sets.Set[string] {
sysTypes := sets.New[string]()
for _, region := range Regions {
sysTypes.Insert(region.SysTypes...)
}
return sysTypes
}

0 comments on commit c431c89

Please sign in to comment.