Skip to content

Commit

Permalink
Merge pull request #9478 from BedeGaming/azurerm-keyvault
Browse files Browse the repository at this point in the history
provider/azurerm: key_vault resource and client_config datasource
  • Loading branch information
stack72 authored Oct 25, 2016
2 parents c59794b + e7d64b2 commit ecb467a
Show file tree
Hide file tree
Showing 19 changed files with 2,126 additions and 3 deletions.
19 changes: 18 additions & 1 deletion builtin/providers/azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/Azure/azure-sdk-for-go/arm/cdn"
"github.com/Azure/azure-sdk-for-go/arm/compute"
"github.com/Azure/azure-sdk-for-go/arm/keyvault"
"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
"github.com/Azure/azure-sdk-for-go/arm/scheduler"
Expand All @@ -24,6 +25,10 @@ import (
// ArmClient contains the handles to all the specific Azure Resource Manager
// resource classes' respective clients.
type ArmClient struct {
clientId string
tenantId string
subscriptionId string

rivieraClient *riviera.Client

availSetClient compute.AvailabilitySetsClient
Expand Down Expand Up @@ -71,6 +76,8 @@ type ArmClient struct {
serviceBusNamespacesClient servicebus.NamespacesClient
serviceBusTopicsClient servicebus.TopicsClient
serviceBusSubscriptionsClient servicebus.SubscriptionsClient

keyVaultClient keyvault.VaultsClient
}

func withRequestLogging() autorest.SendDecorator {
Expand Down Expand Up @@ -110,7 +117,11 @@ func setUserAgent(client *autorest.Client) {
// *ArmClient based on the Config's current settings.
func (c *Config) getArmClient() (*ArmClient, error) {
// client declarations:
client := ArmClient{}
client := ArmClient{
clientId: c.ClientID,
tenantId: c.TenantID,
subscriptionId: c.SubscriptionID,
}

rivieraClient, err := riviera.NewClient(&riviera.AzureResourceManagerCredentials{
ClientID: c.ClientID,
Expand Down Expand Up @@ -366,6 +377,12 @@ func (c *Config) getArmClient() (*ArmClient, error) {
sbsc.Sender = autorest.CreateSender(withRequestLogging())
client.serviceBusSubscriptionsClient = sbsc

kvc := keyvault.NewVaultsClient(c.SubscriptionID)
setUserAgent(&kvc.Client)
kvc.Authorizer = spt
kvc.Sender = autorest.CreateSender(withRequestLogging())
client.keyVaultClient = kvc

return &client, nil
}

Expand Down
39 changes: 39 additions & 0 deletions builtin/providers/azurerm/data_source_arm_client_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package azurerm

import (
"time"

"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceArmClientConfig() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmClientConfigRead,

Schema: map[string]*schema.Schema{
"client_id": {
Type: schema.TypeString,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Computed: true,
},
"subscription_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceArmClientConfigRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)

d.SetId(time.Now().UTC().String())
d.Set("client_id", client.clientId)
d.Set("tenant_id", client.tenantId)
d.Set("subscription_id", client.subscriptionId)

return nil
}
48 changes: 48 additions & 0 deletions builtin/providers/azurerm/data_source_arm_client_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package azurerm

import (
"os"
"testing"

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

func TestAccAzureRMClientConfig_basic(t *testing.T) {
clientId := os.Getenv("ARM_CLIENT_ID")
tenantId := os.Getenv("ARM_TENANT_ID")
subscriptionId := os.Getenv("ARM_SUBSCRIPTION_ID")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckArmClientConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAzureRMClientConfigAttr("data.azurerm_client_config.current", "client_id", clientId),
testAzureRMClientConfigAttr("data.azurerm_client_config.current", "tenant_id", tenantId),
testAzureRMClientConfigAttr("data.azurerm_client_config.current", "subscription_id", subscriptionId),
),
},
},
})
}

// Wraps resource.TestCheckResourceAttr to prevent leaking values to console
// in case of mismatch
func testAzureRMClientConfigAttr(name, key, value string) resource.TestCheckFunc {
return func(s *terraform.State) error {
err := resource.TestCheckResourceAttr(name, key, value)(s)
if err != nil {
// return fmt.Errorf("%s: Attribute '%s', failed check (values hidden)", name, key)
return err
}

return nil
}
}

const testAccCheckArmClientConfig_basic = `
data "azurerm_client_config" "current" { }
`
33 changes: 33 additions & 0 deletions builtin/providers/azurerm/import_arm_key_vault_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package azurerm

import (
"fmt"
"testing"

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

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

ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMKeyVault_basic, ri, ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKeyVaultDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
7 changes: 6 additions & 1 deletion builtin/providers/azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func Provider() terraform.ResourceProvider {
},
},

DataSourcesMap: map[string]*schema.Resource{
"azurerm_client_config": dataSourceArmClientConfig(),
},

ResourcesMap: map[string]*schema.Resource{
// These resources use the Azure ARM SDK
"azurerm_availability_set": resourceArmAvailabilitySet(),
Expand All @@ -57,6 +61,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_lb_probe": resourceArmLoadBalancerProbe(),
"azurerm_lb_rule": resourceArmLoadBalancerRule(),

"azurerm_key_vault": resourceArmKeyVault(),
"azurerm_local_network_gateway": resourceArmLocalNetworkGateway(),
"azurerm_network_interface": resourceArmNetworkInterface(),
"azurerm_network_security_group": resourceArmNetworkSecurityGroup(),
Expand Down Expand Up @@ -187,7 +192,7 @@ func registerAzureResourceProvidersWithSubscription(client *riviera.Client) erro
var err error
providerRegistrationOnce.Do(func() {
// We register Microsoft.Compute during client initialization
providers := []string{"Microsoft.Network", "Microsoft.Cdn", "Microsoft.Storage", "Microsoft.Sql", "Microsoft.Search", "Microsoft.Resources", "Microsoft.ServiceBus"}
providers := []string{"Microsoft.Network", "Microsoft.Cdn", "Microsoft.Storage", "Microsoft.Sql", "Microsoft.Search", "Microsoft.Resources", "Microsoft.ServiceBus", "Microsoft.KeyVault"}

var wg sync.WaitGroup
wg.Add(len(providers))
Expand Down
Loading

0 comments on commit ecb467a

Please sign in to comment.