From 9de5f67a87f63a875d2b789c49c7842c22566898 Mon Sep 17 00:00:00 2001 From: Karthik K N Date: Fri, 3 Mar 2023 12:45:21 +0530 Subject: [PATCH] Add Power VS region to IBM VPC and IBM COS region mapping functions --- region.go | 122 +++++++++++++++++++++++++++++++++++++++++++++++++ region_test.go | 40 ++++++++++++++++ 2 files changed, 162 insertions(+) diff --git a/region.go b/region.go index b95e9f6..cbef76e 100644 --- a/region.go +++ b/region.go @@ -35,3 +35,125 @@ func GetRegion(zone string) (region string, err error) { } return } + +// Region describes respective IBM Cloud COS region, VPC region and Zones associated with a region in Power VS. +type Region struct { + Description string + VPCRegion string + COSRegion string + Zones []string +} + +// Regions provides a mapping between Power VS and IBM Cloud VPC and IBM COS regions. +var Regions = map[string]Region{ + "dal": { + Description: "Dallas, USA", + VPCRegion: "us-south", + COSRegion: "us-south", + Zones: []string{"dal12"}, + }, + "eu-de": { + Description: "Frankfurt, Germany", + VPCRegion: "eu-de", + COSRegion: "eu-de", + Zones: []string{ + "eu-de-1", + "eu-de-2", + }, + }, + "lon": { + Description: "London, UK.", + VPCRegion: "eu-gb", + COSRegion: "eu-gb", + Zones: []string{ + "lon04", + "lon06", + }, + }, + "mon": { + Description: "Montreal, Canada", + VPCRegion: "ca-tor", + COSRegion: "ca-tor", + Zones: []string{"mon01"}, + }, + "osa": { + Description: "Osaka, Japan", + VPCRegion: "jp-osa", + COSRegion: "jp-osa", + Zones: []string{"osa21"}, + }, + "syd": { + Description: "Sydney, Australia", + VPCRegion: "au-syd", + COSRegion: "au-syd", + Zones: []string{ + "syd04", + "syd05", + }, + }, + "sao": { + Description: "São Paulo, Brazil", + VPCRegion: "br-sao", + COSRegion: "br-sao", + Zones: []string{"sao01"}, + }, + "tok": { + Description: "Tokyo, Japan", + VPCRegion: "jp-tok", + COSRegion: "jp-tok", + Zones: []string{"tok04"}, + }, + "us-east": { + Description: "Washington DC, USA", + VPCRegion: "us-east", + COSRegion: "us-east", + Zones: []string{"us-east"}, + }, +} + +// VPCRegionForPowerVSRegion returns the VPC region for the specified PowerVS region. +func VPCRegionForPowerVSRegion(region string) (string, error) { + if r, ok := Regions[region]; ok { + return r.VPCRegion, nil + } + + return "", fmt.Errorf("VPC region corresponding to a PowerVS region %s not found ", region) +} + +// COSRegionForPowerVSRegion returns the IBM COS region for the specified PowerVS region. +func COSRegionForPowerVSRegion(region string) (string, error) { + if r, ok := Regions[region]; ok { + return r.COSRegion, nil + } + + return "", fmt.Errorf("COS region corresponding to a PowerVS region %s not found ", region) +} + +// ValidateVPCRegion validates that given VPC region is known/tested. +func ValidateVPCRegion(region string) bool { + for r := range Regions { + if region == Regions[r].VPCRegion { + return true + } + } + return false +} + +// ValidateCOSRegion validates that given COS region is known/tested. +func ValidateCOSRegion(region string) bool { + for r := range Regions { + if region == Regions[r].COSRegion { + return true + } + } + return false +} + +// RegionShortNames returns the list of region names +func RegionShortNames() []string { + var keys []string + for r := range Regions { + keys = append(keys, r) + } + return keys +} diff --git a/region_test.go b/region_test.go index 655e31a..71b1fc8 100644 --- a/region_test.go +++ b/region_test.go @@ -100,3 +100,43 @@ func TestGetRegion(t *testing.T) { }) } } + +func TestVPCRegionForPowerVSRegion(t *testing.T) { + type args struct { + region string + } + tests := []struct { + name string + args args + wantRegion string + wantErr bool + }{ + { + "Dallas", + args{"dal"}, + "us-south", + false, + }, + { + "Osaka", + args{"eu-de1"}, + "eu-de", + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + vpcRegion, err := VPCRegionForPowerVSRegion(tt.args.region) + + if err != nil { + if !tt.wantErr { + t.Errorf("VPCRegionForPowerVSRegion() error = %v, wantErr %v", err, tt.wantErr) + } + return + } + if vpcRegion != tt.wantRegion { + t.Errorf("VPCRegionForPowerVSRegion() gotRegion = %v, want %v", vpcRegion, tt.wantRegion) + } + }) + } +}