Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

normalizeazureregion added in scale, upgrade, and deploy #2764

Merged
merged 13 commits into from
Apr 27, 2018
3 changes: 3 additions & 0 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/Azure/acs-engine/pkg/acsengine/transform"
"github.com/Azure/acs-engine/pkg/api"
"github.com/Azure/acs-engine/pkg/armhelpers"
"github.com/Azure/acs-engine/pkg/helpers"
"github.com/Azure/acs-engine/pkg/i18n"
)

Expand Down Expand Up @@ -116,6 +117,8 @@ func (dc *deployCmd) validate(cmd *cobra.Command, args []string) error {
if dc.location == "" {
return fmt.Errorf(fmt.Sprintf("--location must be specified"))
}
dc.location = helpers.NormalizeAzureRegion(dc.location)

// skip validating the model fields for now
dc.containerService, dc.apiVersion, err = apiloader.LoadContainerServiceFromFile(dc.apimodelPath, false, false, nil)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/Azure/acs-engine/pkg/api"
"github.com/Azure/acs-engine/pkg/armhelpers"
"github.com/Azure/acs-engine/pkg/armhelpers/utils"
"github.com/Azure/acs-engine/pkg/helpers"
"github.com/Azure/acs-engine/pkg/i18n"
"github.com/Azure/acs-engine/pkg/operations"
"github.com/leonelquinteros/gotext"
Expand Down Expand Up @@ -99,6 +100,8 @@ func (sc *scaleCmd) validate(cmd *cobra.Command, args []string) {
if sc.location == "" {
cmd.Usage()
log.Fatal("--location must be specified")
} else {
sc.location = helpers.NormalizeAzureRegion(sc.location)
}

if sc.newDesiredAgentCount == 0 {
Expand Down
3 changes: 3 additions & 0 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Azure/acs-engine/pkg/acsengine"
"github.com/Azure/acs-engine/pkg/api"
"github.com/Azure/acs-engine/pkg/armhelpers"
"github.com/Azure/acs-engine/pkg/helpers"
"github.com/Azure/acs-engine/pkg/i18n"
"github.com/Azure/acs-engine/pkg/operations/kubernetesupgrade"
"github.com/leonelquinteros/gotext"
Expand Down Expand Up @@ -87,6 +88,8 @@ func (uc *upgradeCmd) validate(cmd *cobra.Command, args []string) {
if uc.location == "" {
cmd.Usage()
log.Fatal("--location must be specified")
} else {
uc.location = helpers.NormalizeAzureRegion(uc.location)
}

if uc.timeoutInMinutes != -1 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/convertertoagentpoolonlyapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
func ConvertV20170831AgentPoolOnly(v20170831 *v20170831.ManagedCluster) *ContainerService {
c := &ContainerService{}
c.ID = v20170831.ID
c.Location = NormalizeAzureRegion(v20170831.Location)
c.Location = helpers.NormalizeAzureRegion(v20170831.Location)
c.Name = v20170831.Name
if v20170831.Plan != nil {
c.Plan = convertv20170831AgentPoolOnlyResourcePurchasePlan(v20170831.Plan)
Expand All @@ -54,7 +54,7 @@ func ConvertV20170831AgentPoolOnly(v20170831 *v20170831.ManagedCluster) *Contain
func ConvertV20180331AgentPoolOnly(v20180331 *v20180331.ManagedCluster) *ContainerService {
c := &ContainerService{}
c.ID = v20180331.ID
c.Location = NormalizeAzureRegion(v20180331.Location)
c.Location = helpers.NormalizeAzureRegion(v20180331.Location)
c.Name = v20180331.Name
if v20180331.Plan != nil {
c.Plan = convertv20180331AgentPoolOnlyResourcePurchasePlan(v20180331.Plan)
Expand Down Expand Up @@ -111,7 +111,7 @@ func convertV20170831AgentPoolOnlyProperties(obj *v20170831.Properties) *Propert
func ConvertVLabsAgentPoolOnly(vlabs *vlabs.ManagedCluster) *ContainerService {
c := &ContainerService{}
c.ID = vlabs.ID
c.Location = NormalizeAzureRegion(vlabs.Location)
c.Location = helpers.NormalizeAzureRegion(vlabs.Location)
c.Name = vlabs.Name
if vlabs.Plan != nil {
c.Plan = &ResourcePurchasePlan{}
Expand Down
18 changes: 6 additions & 12 deletions pkg/api/convertertoapi.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package api

import (
"strings"

"github.com/Azure/acs-engine/pkg/api/common"
"github.com/Azure/acs-engine/pkg/api/v20160330"
"github.com/Azure/acs-engine/pkg/api/v20160930"
"github.com/Azure/acs-engine/pkg/api/v20170131"
"github.com/Azure/acs-engine/pkg/api/v20170701"
"github.com/Azure/acs-engine/pkg/api/vlabs"
"github.com/Azure/acs-engine/pkg/helpers"
)

///////////////////////////////////////////////////////////
Expand All @@ -19,16 +18,11 @@ import (
// for converting.
///////////////////////////////////////////////////////////

// NormalizeAzureRegion returns a normalized Azure region with whilte spaces removed and converted to lower case
func NormalizeAzureRegion(name string) string {
return strings.ToLower(strings.Replace(name, " ", "", -1))
}

// ConvertV20160930ContainerService converts a v20160930 ContainerService to an unversioned ContainerService
func ConvertV20160930ContainerService(v20160930 *v20160930.ContainerService) *ContainerService {
c := &ContainerService{}
c.ID = v20160930.ID
c.Location = NormalizeAzureRegion(v20160930.Location)
c.Location = helpers.NormalizeAzureRegion(v20160930.Location)
c.Name = v20160930.Name
if v20160930.Plan != nil {
c.Plan = &ResourcePurchasePlan{}
Expand All @@ -48,7 +42,7 @@ func ConvertV20160930ContainerService(v20160930 *v20160930.ContainerService) *Co
func ConvertV20160330ContainerService(v20160330 *v20160330.ContainerService) *ContainerService {
c := &ContainerService{}
c.ID = v20160330.ID
c.Location = NormalizeAzureRegion(v20160330.Location)
c.Location = helpers.NormalizeAzureRegion(v20160330.Location)
c.Name = v20160330.Name
if v20160330.Plan != nil {
c.Plan = &ResourcePurchasePlan{}
Expand All @@ -68,7 +62,7 @@ func ConvertV20160330ContainerService(v20160330 *v20160330.ContainerService) *Co
func ConvertV20170131ContainerService(v20170131 *v20170131.ContainerService) *ContainerService {
c := &ContainerService{}
c.ID = v20170131.ID
c.Location = NormalizeAzureRegion(v20170131.Location)
c.Location = helpers.NormalizeAzureRegion(v20170131.Location)
c.Name = v20170131.Name
if v20170131.Plan != nil {
c.Plan = &ResourcePurchasePlan{}
Expand All @@ -88,7 +82,7 @@ func ConvertV20170131ContainerService(v20170131 *v20170131.ContainerService) *Co
func ConvertV20170701ContainerService(v20170701 *v20170701.ContainerService) *ContainerService {
c := &ContainerService{}
c.ID = v20170701.ID
c.Location = NormalizeAzureRegion(v20170701.Location)
c.Location = helpers.NormalizeAzureRegion(v20170701.Location)
c.Name = v20170701.Name
if v20170701.Plan != nil {
c.Plan = &ResourcePurchasePlan{}
Expand All @@ -108,7 +102,7 @@ func ConvertV20170701ContainerService(v20170701 *v20170701.ContainerService) *Co
func ConvertVLabsContainerService(vlabs *vlabs.ContainerService) *ContainerService {
c := &ContainerService{}
c.ID = vlabs.ID
c.Location = NormalizeAzureRegion(vlabs.Location)
c.Location = helpers.NormalizeAzureRegion(vlabs.Location)
c.Name = vlabs.Name
if vlabs.Plan != nil {
c.Plan = &ResourcePurchasePlan{}
Expand Down
6 changes: 6 additions & 0 deletions pkg/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/rsa"
"encoding/json"
"io"
"strings"

"github.com/Azure/acs-engine/pkg/i18n"
"golang.org/x/crypto/ssh"
Expand All @@ -16,6 +17,11 @@ const (
SSHKeySize = 4096
)

// NormalizeAzureRegion returns a normalized Azure region with white spaces removed and converted to lower case
func NormalizeAzureRegion(name string) string {
return strings.ToLower(strings.Replace(name, " ", "", -1))
}

// JSONMarshalIndent marshals formatted JSON w/ optional SetEscapeHTML
func JSONMarshalIndent(content interface{}, prefix, indent string, escape bool) ([]byte, error) {
b, err := JSONMarshal(content, escape)
Expand Down
31 changes: 31 additions & 0 deletions pkg/helpers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,34 @@ func TestPointerToBool(t *testing.T) {
t.Fatalf("expected PointerToBool(true) to return *true, instead returned %#v", ret)
}
}

func TestIsRegionNormalized(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

cases := []struct {
input string
expectedResult string
}{
{
input: "westus",
expectedResult: "westus",
},
{
input: "West US",
expectedResult: "westus",
},
{
input: "Eastern Africa",
expectedResult: "easternafrica",
},
{
input: "",
expectedResult: "",
},
}

for _, c := range cases {
result := NormalizeAzureRegion(c.input)
if c.expectedResult != result {
t.Fatalf("NormalizeAzureRegion returned unexpected result: expected %s but got %s", c.expectedResult, result)
}
}
}