-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow deployment into an externally managed resource group (#35)
* #34 Added the option for the vault to be deployed into an externally managed resource group. * #34 added commentary to explain the slightly quirky behaviour of the resource group. * #34 updated go packages to resolve reported vulnerabilities.
- Loading branch information
1 parent
619b00a
commit 9198ba4
Showing
12 changed files
with
487 additions
and
1,093 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,24 @@ | ||
# The consumer of the module can configure whether the resource group should be created or | ||
# not. If the resource group should be created, the module will create it. If the resource | ||
# group should not be created, the module will look up the existing resource group and | ||
# reference it as data. | ||
# | ||
# The local variable resource_group means the resource group - whether data or resource, | ||
# can be easily referenced in other parts of the module. | ||
########################################################################################### | ||
|
||
resource "azurerm_resource_group" "resource_group" { | ||
count = var.create_resource_group ? 1 : 0 | ||
location = var.resource_group_location | ||
name = var.resource_group_name | ||
tags = var.tags | ||
} | ||
|
||
data "azurerm_resource_group" "resource_group" { | ||
count = !var.create_resource_group ? 1 : 0 | ||
name = var.resource_group_name | ||
} | ||
|
||
locals { | ||
resource_group = var.create_resource_group ? azurerm_resource_group.resource_group[0] : data.azurerm_resource_group.resource_group[0] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package e2e_tests | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azidentity" | ||
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources" | ||
"github.com/gruntwork-io/terratest/modules/random" | ||
"github.com/gruntwork-io/terratest/modules/terraform" | ||
test_structure "github.com/gruntwork-io/terratest/modules/test-structure" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type TestExistingResourceGroupExternalResources struct { | ||
ResourceGroup armresources.ResourceGroup | ||
} | ||
|
||
/* | ||
* Creates resources which are "external" to the az-backup module. | ||
*/ | ||
func setupExternalResourcesForExistingResourceGroupTest(t *testing.T, credential *azidentity.ClientSecretCredential, subscriptionID string, resourceGroupName string, resourceGroupLocation string) *TestExistingResourceGroupExternalResources { | ||
resourceGroup := CreateResourceGroup(t, credential, subscriptionID, resourceGroupName, resourceGroupLocation) | ||
|
||
externalResources := &TestExistingResourceGroupExternalResources{ | ||
ResourceGroup: resourceGroup, | ||
} | ||
|
||
return externalResources | ||
} | ||
|
||
/* | ||
* TestExistingResourceGroup tests the deployment of a backup vault into a pre-existing resource group. | ||
*/ | ||
func TestExistingResourceGroup(t *testing.T) { | ||
t.Parallel() | ||
|
||
environment := GetEnvironmentConfiguration(t) | ||
credential := GetAzureCredential(t, environment) | ||
|
||
uniqueId := random.UniqueId() | ||
resourceGroupName := fmt.Sprintf("rg-nhsbackup-%s", uniqueId) | ||
resourceGroupLocation := "uksouth" | ||
backupVaultName := fmt.Sprintf("bvault-nhsbackup-%s", uniqueId) | ||
|
||
externalResources := setupExternalResourcesForExistingResourceGroupTest(t, credential, environment.SubscriptionID, resourceGroupName, resourceGroupLocation) | ||
|
||
// Teardown stage | ||
// ... | ||
|
||
defer test_structure.RunTestStage(t, "teardown", func() { | ||
terraformOptions := test_structure.LoadTerraformOptions(t, environment.TerraformFolder) | ||
|
||
terraform.Destroy(t, terraformOptions) | ||
|
||
DeleteResourceGroup(t, credential, environment.SubscriptionID, *externalResources.ResourceGroup.Name) | ||
}) | ||
|
||
// Setup stage | ||
// ... | ||
|
||
test_structure.RunTestStage(t, "setup", func() { | ||
terraformOptions := &terraform.Options{ | ||
TerraformDir: environment.TerraformFolder, | ||
|
||
Vars: map[string]interface{}{ | ||
"resource_group_name": resourceGroupName, | ||
"resource_group_location": resourceGroupLocation, | ||
"create_resource_group": false, | ||
"backup_vault_name": backupVaultName, | ||
}, | ||
|
||
BackendConfig: map[string]interface{}{ | ||
"resource_group_name": environment.TerraformStateResourceGroup, | ||
"storage_account_name": environment.TerraformStateStorageAccount, | ||
"container_name": environment.TerraformStateContainer, | ||
"key": backupVaultName + ".tfstate", | ||
}, | ||
} | ||
|
||
// Save options for later test stages | ||
test_structure.SaveTerraformOptions(t, environment.TerraformFolder, terraformOptions) | ||
|
||
terraform.InitAndApply(t, terraformOptions) | ||
}) | ||
|
||
// Validate stage | ||
// ... | ||
|
||
test_structure.RunTestStage(t, "validate", func() { | ||
// Validate resource group | ||
resourceGroup := GetResourceGroup(t, environment.SubscriptionID, credential, resourceGroupName) | ||
assert.NotNil(t, resourceGroup, "Resource group does not exist") | ||
assert.Equal(t, resourceGroupName, *resourceGroup.Name, "Resource group name does not match") | ||
assert.Equal(t, resourceGroupLocation, *resourceGroup.Location, "Resource group location does not match") | ||
}) | ||
} |
Oops, something went wrong.