Normalize URIs for azurerm Terraform provider

This commit is contained in:
katexochen 2022-10-12 17:00:59 +02:00 committed by Paul Meyer
parent 4c0ef09346
commit 4b2dd1317a
3 changed files with 64 additions and 7 deletions

View File

@ -81,7 +81,7 @@ func (c *Creator) createGCP(ctx context.Context, cl terraformClient, config *con
) (idFile clusterid.File, retErr error) {
defer rollbackOnError(context.Background(), c.out, &retErr, &rollbackerTerraform{client: cl})
vars := &terraform.GCPVariables{
vars := terraform.GCPVariables{
CommonVariables: terraform.CommonVariables{
Name: name,
CountControlPlanes: controlPlaneCount,
@ -98,7 +98,7 @@ func (c *Creator) createGCP(ctx context.Context, cl terraformClient, config *con
Debug: config.IsDebugCluster(),
}
ip, err := cl.CreateCluster(ctx, name, vars)
ip, err := cl.CreateCluster(ctx, name, &vars)
if err != nil {
return clusterid.File{}, err
}
@ -114,7 +114,7 @@ func (c *Creator) createAzure(ctx context.Context, cl terraformClient, config *c
) (idFile clusterid.File, retErr error) {
defer rollbackOnError(context.Background(), c.out, &retErr, &rollbackerTerraform{client: cl})
vars := &terraform.AzureVariables{
vars := terraform.AzureVariables{
CommonVariables: terraform.CommonVariables{
Name: name,
CountControlPlanes: controlPlaneCount,
@ -131,7 +131,9 @@ func (c *Creator) createAzure(ctx context.Context, cl terraformClient, config *c
Debug: config.IsDebugCluster(),
}
ip, err := cl.CreateCluster(ctx, name, vars)
vars = normalizeAzureURIs(vars)
ip, err := cl.CreateCluster(ctx, name, &vars)
if err != nil {
return clusterid.File{}, err
}
@ -142,6 +144,16 @@ func (c *Creator) createAzure(ctx context.Context, cl terraformClient, config *c
}, nil
}
func normalizeAzureURIs(vars terraform.AzureVariables) terraform.AzureVariables {
vars.UserAssignedIdentity = strings.ReplaceAll(vars.UserAssignedIdentity, "resourcegroup", "resourceGroup")
vars.ImageID = strings.ReplaceAll(vars.ImageID, "CommunityGalleries", "communityGalleries")
vars.ImageID = strings.ReplaceAll(vars.ImageID, "Images", "images")
vars.ImageID = strings.ReplaceAll(vars.ImageID, "Versions", "versions")
return vars
}
func (c *Creator) createQEMU(ctx context.Context, cl terraformClient, lv libvirtRunner, name string, config *config.Config,
controlPlaneCount, workerCount int,
) (idFile clusterid.File, retErr error) {
@ -183,7 +195,7 @@ func (c *Creator) createQEMU(ctx context.Context, cl terraformClient, lv libvirt
metadataLibvirtURI = "qemu:///system"
}
vars := &terraform.QEMUVariables{
vars := terraform.QEMUVariables{
CommonVariables: terraform.CommonVariables{
Name: name,
CountControlPlanes: controlPlaneCount,
@ -200,7 +212,7 @@ func (c *Creator) createQEMU(ctx context.Context, cl terraformClient, lv libvirt
MetadataLibvirtURI: metadataLibvirtURI,
}
ip, err := cl.CreateCluster(ctx, name, vars)
ip, err := cl.CreateCluster(ctx, name, &vars)
if err != nil {
return clusterid.File{}, err
}

View File

@ -13,6 +13,7 @@ import (
"runtime"
"testing"
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
"github.com/edgelesssys/constellation/v2/internal/config"
"github.com/stretchr/testify/assert"
@ -121,3 +122,47 @@ func TestCreator(t *testing.T) {
})
}
}
func TestNormalizeAzureURIs(t *testing.T) {
testCases := map[string]struct {
in terraform.AzureVariables
want terraform.AzureVariables
}{
"empty": {
in: terraform.AzureVariables{},
want: terraform.AzureVariables{},
},
"no change": {
in: terraform.AzureVariables{
ImageID: "/communityGalleries/foo/images/constellation/versions/2.1.0",
},
want: terraform.AzureVariables{
ImageID: "/communityGalleries/foo/images/constellation/versions/2.1.0",
},
},
"fix image id": {
in: terraform.AzureVariables{
ImageID: "/CommunityGalleries/foo/Images/constellation/Versions/2.1.0",
},
want: terraform.AzureVariables{
ImageID: "/communityGalleries/foo/images/constellation/versions/2.1.0",
},
},
"fix resource group": {
in: terraform.AzureVariables{
UserAssignedIdentity: "/subscriptions/foo/resourcegroups/test/providers/Microsoft.ManagedIdentity/userAssignedIdentities/uai",
},
want: terraform.AzureVariables{
UserAssignedIdentity: "/subscriptions/foo/resourceGroups/test/providers/Microsoft.ManagedIdentity/userAssignedIdentities/uai",
},
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
out := normalizeAzureURIs(tc.in)
assert.Equal(tc.want, out)
})
}
}

View File

@ -9,6 +9,6 @@ SPDX-License-Identifier: AGPL-3.0-only
package config
const (
DefaultImageAzure = "/CommunityGalleries/ConstellationCVM-b3782fa0-0df7-4f2f-963e-fc7fc42663df/Images/constellation/Versions/2.1.0"
DefaultImageAzure = "/communityGalleries/ConstellationCVM-b3782fa0-0df7-4f2f-963e-fc7fc42663df/images/constellation/versions/2.1.0"
DefaultImageGCP = "projects/constellation-images/global/images/constellation-v2-1-0"
)