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

Commit

Permalink
Correcting AMI ID being sent from the UI
Browse files Browse the repository at this point in the history
Signed-off-by: Sudarshan <asudarshan@vmware.com>
  • Loading branch information
saji-pivotal committed Sep 28, 2021
1 parent 5b764f3 commit d6e59d9
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
23 changes: 20 additions & 3 deletions pkg/v1/tkg/tkgconfigproviders/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package tkgconfigproviders

import (
"encoding/base64"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/tkgconfigbom"
"strconv"

"github.com/pkg/errors"
Expand Down Expand Up @@ -121,9 +122,7 @@ func (c *client) NewAWSConfig(params *models.AWSRegionalClusterParams, encodedCr

amiID := ""

if val, ok := bomConfiguration.AMI[params.AwsAccountParams.Region]; ok {
amiID = val[0].ID
}
amiID = c.GetAMIId(bomConfiguration, params)

if amiID == "" {
return nil, errors.Errorf("No AMI found in region %s for TKr version %s", params.AwsAccountParams.Region, bomConfiguration.Release.Version)
Expand Down Expand Up @@ -286,6 +285,24 @@ func (c *client) NewAWSConfig(params *models.AWSRegionalClusterParams, encodedCr
return res, nil
}

func (c *client) GetAMIId(bomConfiguration *tkgconfigbom.BOMConfiguration, params *models.AWSRegionalClusterParams) string {
amiID := ""
if amis, ok := bomConfiguration.AMI[params.AwsAccountParams.Region]; ok {
if params.Os != nil && params.Os.OsInfo != nil {
for _, ami := range amis {
if ami.OSInfo.Name == params.Os.OsInfo.Name {
amiID = ami.ID
break
}
}
} else {
amiID = amis[0].ID
}
}

return amiID
}

// AppendSubnets append subnet information in providerConfig to paramsVpc
func appendSubnets(paramsVpc *models.AWSVpc, highAvailability bool) ([]*newSubnetPair, error) {
ProdSubnetCount := 6
Expand Down
1 change: 1 addition & 0 deletions pkg/v1/tkg/tkgconfigproviders/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Client interface {
NewAzureConfig(params *models.AzureRegionalClusterParams) (*AzureConfig, error)
NewVSphereConfig(params *models.VsphereRegionalClusterParams) (*VSphereConfig, error)
NewDockerConfig(params *models.DockerRegionalClusterParams) (*DockerConfig, error)
GetAMIId(bomConfiguration *tkgconfigbom.BOMConfiguration, params *models.AWSRegionalClusterParams) string
}

// TKGConfigReaderWriter returns tkgConfigReaderWriter client
Expand Down
83 changes: 83 additions & 0 deletions pkg/v1/tkg/tkgconfigproviders/tkgconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,89 @@ var _ = Describe("Azure VM Images", func() {
})
})

var _ = FDescribe("Test AWS AMIID", func() {
var (
vpcConfig *models.AWSVpc
params *models.AWSRegionalClusterParams
flavor string
client Client
bomConfiguration *tkgconfigbom.BOMConfiguration
)

JustBeforeEach(func() {
params = &models.AWSRegionalClusterParams{
Vpc: vpcConfig,
AwsAccountParams: &models.AWSAccountParams{
Region: fakeRegion,
},
ControlPlaneFlavor: flavor,
Networking: &models.TKGNetwork{
ClusterPodCIDR: "10.0.0.4/15",
},
KubernetesVersion: "v1.18.0+vmware.1",
IdentityManagement: &models.IdentityManagementConfig{
IdmType: swag.String("oidc"),
OidcClaimMappings: map[string]string{"groups": "group", "username": "usr"},
OidcClientID: "client-id",
OidcClientSecret: "clientsecret",
OidcProviderName: "my-provider",
OidcProviderURL: "http:0.0.0.0",
OidcScope: "email",
OidcSkipVerifyCert: true,
},
}
setupBomFile("../fakes/config/bom/tkr-bom-v1.18.0+vmware.1-tkg.2.yaml", testingDir)
client = newForTesting("../fakes/config/config.yaml", testingDir, defaultBoMFilepath)

bomConfiguration = &tkgconfigbom.BOMConfiguration{
Release: &tkgconfigbom.ReleaseInfo{
Version: "v1.18.0+vmware.1-tkg.2",
},
AMI: map[string][]tkgconfigbom.AMIInfo{
fakeRegion: {
{
ID: "ami-123456",
OSInfo: tkgconfigbom.OSInfo{
Name: "amazon",
Version: "2",
Arch: "amd64",
},
},
{
ID: "ami-567890",
OSInfo: tkgconfigbom.OSInfo{
Name: "ubuntu",
Version: "2",
Arch: "amd64",
},
},
},
},
}

})

It("When OS is specified in the parameters", func() {
os := &models.AWSVirtualMachine{
Name: "ubuntu",
OsInfo: &models.OSInfo{
Arch: "amd64",
Name: "ubuntu",
Version: "2",
},
}

params.Os = os
amiId := client.GetAMIId(bomConfiguration,params)
Expect(amiId).To(Equal("ami-567890"))
})

It("When OS is not specified in the paramters", func() {
amiId := client.GetAMIId(bomConfiguration,params)
Expect(amiId).To(Equal("ami-123456"))
})
})

var _ = Describe("EnsureNewVPCAWSConfig", func() {
var (
err error
Expand Down

0 comments on commit d6e59d9

Please # to comment.