Skip to content

Commit

Permalink
Support for Key Vault Keys
Browse files Browse the repository at this point in the history
Also fixes an issue where the Key Vault Secret Import Tests weren't running properly
  • Loading branch information
tombuildsstuff committed Sep 26, 2017
1 parent 2506354 commit 742605a
Show file tree
Hide file tree
Showing 10 changed files with 951 additions and 19 deletions.
26 changes: 15 additions & 11 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,21 +688,25 @@ func (c *Config) getArmClient() (*ArmClient, error) {
aschc.Sender = sender
client.automationScheduleClient = aschc

kvc := keyvault.NewVaultsClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&kvc.Client)
kvc.Authorizer = auth
kvc.Sender = sender
client.keyVaultClient = kvc

kvmc := keyVault.New()
setUserAgent(&kvmc.Client)
kvmc.Authorizer = keyVaultAuth
kvmc.Sender = sender
client.keyVaultManagementClient = kvmc
client.registerKeyVaultClients(endpoint, c.SubscriptionID, auth, keyVaultAuth, sender)

return &client, nil
}

func (c *ArmClient) registerKeyVaultClients(endpoint, subscriptionId string, auth autorest.Authorizer, keyVaultAuth autorest.Authorizer, sender autorest.Sender) {
keyVaultClient := keyvault.NewVaultsClientWithBaseURI(endpoint, subscriptionId)
setUserAgent(&keyVaultClient.Client)
keyVaultClient.Authorizer = auth
keyVaultClient.Sender = sender
c.keyVaultClient = keyVaultClient

keyVaultManagementClient := keyVault.New()
setUserAgent(&keyVaultManagementClient.Client)
keyVaultManagementClient.Authorizer = keyVaultAuth
keyVaultManagementClient.Sender = sender
c.keyVaultManagementClient = keyVaultManagementClient
}

func (armClient *ArmClient) getKeyForStorageAccount(resourceGroupName, storageAccountName string) (string, bool, error) {
accountKeys, err := armClient.storageServiceClient.ListKeys(resourceGroupName, storageAccountName)
if accountKeys.StatusCode == http.StatusNotFound {
Expand Down
104 changes: 104 additions & 0 deletions azurerm/import_arm_key_vault_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package azurerm

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMKeyVaultKey_importBasicEC(t *testing.T) {
resourceName := "azurerm_key_vault_key.test"

rs := acctest.RandString(6)
config := testAccAzureRMKeyVaultKey_basicEC(rs, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKeyVaultKeyDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"key_size"},
},
},
})
}

func TestAccAzureRMKeyVaultKey_importBasicRSA(t *testing.T) {
resourceName := "azurerm_key_vault_key.test"

rs := acctest.RandString(6)
config := testAccAzureRMKeyVaultKey_basicRSA(rs, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKeyVaultKeyDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"key_size"},
},
},
})
}

func TestAccAzureRMKeyVaultKey_importBasicRSAHSM(t *testing.T) {
resourceName := "azurerm_key_vault_key.test"

rs := acctest.RandString(6)
config := testAccAzureRMKeyVaultKey_basicRSAHSM(rs, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKeyVaultKeyDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"key_size"},
},
},
})
}

func TestAccAzureRMKeyVaultKey_importComplete(t *testing.T) {
resourceName := "azurerm_key_vault_key.test"

rs := acctest.RandString(6)
config := testAccAzureRMKeyVaultKey_complete(rs, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKeyVaultKeyDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"key_size"},
},
},
})
}
4 changes: 2 additions & 2 deletions azurerm/import_arm_key_vault_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestAccAzureRMKeyVaultSecret_importBasic(t *testing.T) {
resourceName := "azurerm_key_vault.test"
resourceName := "azurerm_key_vault_secret.test"

rs := acctest.RandString(6)
config := testAccAzureRMKeyVaultSecret_basic(rs, testLocation())
Expand All @@ -31,7 +31,7 @@ func TestAccAzureRMKeyVaultSecret_importBasic(t *testing.T) {
}

func TestAccAzureRMKeyVaultSecret_importComplete(t *testing.T) {
resourceName := "azurerm_key_vault.test"
resourceName := "azurerm_key_vault_secret.test"

rs := acctest.RandString(6)
config := testAccAzureRMKeyVaultSecret_complete(rs, testLocation())
Expand Down
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_express_route_circuit": resourceArmExpressRouteCircuit(),
"azurerm_image": resourceArmImage(),
"azurerm_key_vault": resourceArmKeyVault(),
"azurerm_key_vault_key": resourceArmKeyVaultKey(),
"azurerm_key_vault_secret": resourceArmKeyVaultSecret(),
"azurerm_lb": resourceArmLoadBalancer(),
"azurerm_lb_backend_address_pool": resourceArmLoadBalancerBackendAddressPool(),
Expand Down
Loading

0 comments on commit 742605a

Please sign in to comment.