Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit ecb467a

Browse files
authored
Merge pull request hashicorp#9478 from BedeGaming/azurerm-keyvault
provider/azurerm: key_vault resource and client_config datasource
2 parents c59794b + e7d64b2 commit ecb467a

19 files changed

Lines changed: 2126 additions & 3 deletions

File tree

builtin/providers/azurerm/config.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/Azure/azure-sdk-for-go/arm/cdn"
1010
"github.com/Azure/azure-sdk-for-go/arm/compute"
11+
"github.com/Azure/azure-sdk-for-go/arm/keyvault"
1112
"github.com/Azure/azure-sdk-for-go/arm/network"
1213
"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
1314
"github.com/Azure/azure-sdk-for-go/arm/scheduler"
@@ -24,6 +25,10 @@ import (
2425
// ArmClient contains the handles to all the specific Azure Resource Manager
2526
// resource classes' respective clients.
2627
type ArmClient struct {
28+
clientId string
29+
tenantId string
30+
subscriptionId string
31+
2732
rivieraClient *riviera.Client
2833

2934
availSetClient compute.AvailabilitySetsClient
@@ -71,6 +76,8 @@ type ArmClient struct {
7176
serviceBusNamespacesClient servicebus.NamespacesClient
7277
serviceBusTopicsClient servicebus.TopicsClient
7378
serviceBusSubscriptionsClient servicebus.SubscriptionsClient
79+
80+
keyVaultClient keyvault.VaultsClient
7481
}
7582

7683
func withRequestLogging() autorest.SendDecorator {
@@ -110,7 +117,11 @@ func setUserAgent(client *autorest.Client) {
110117
// *ArmClient based on the Config's current settings.
111118
func (c *Config) getArmClient() (*ArmClient, error) {
112119
// client declarations:
113-
client := ArmClient{}
120+
client := ArmClient{
121+
clientId: c.ClientID,
122+
tenantId: c.TenantID,
123+
subscriptionId: c.SubscriptionID,
124+
}
114125

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

380+
kvc := keyvault.NewVaultsClient(c.SubscriptionID)
381+
setUserAgent(&kvc.Client)
382+
kvc.Authorizer = spt
383+
kvc.Sender = autorest.CreateSender(withRequestLogging())
384+
client.keyVaultClient = kvc
385+
369386
return &client, nil
370387
}
371388

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package azurerm
2+
3+
import (
4+
"time"
5+
6+
"github.com/hashicorp/terraform/helper/schema"
7+
)
8+
9+
func dataSourceArmClientConfig() *schema.Resource {
10+
return &schema.Resource{
11+
Read: dataSourceArmClientConfigRead,
12+
13+
Schema: map[string]*schema.Schema{
14+
"client_id": {
15+
Type: schema.TypeString,
16+
Computed: true,
17+
},
18+
"tenant_id": {
19+
Type: schema.TypeString,
20+
Computed: true,
21+
},
22+
"subscription_id": {
23+
Type: schema.TypeString,
24+
Computed: true,
25+
},
26+
},
27+
}
28+
}
29+
30+
func dataSourceArmClientConfigRead(d *schema.ResourceData, meta interface{}) error {
31+
client := meta.(*ArmClient)
32+
33+
d.SetId(time.Now().UTC().String())
34+
d.Set("client_id", client.clientId)
35+
d.Set("tenant_id", client.tenantId)
36+
d.Set("subscription_id", client.subscriptionId)
37+
38+
return nil
39+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package azurerm
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/resource"
8+
"github.com/hashicorp/terraform/terraform"
9+
)
10+
11+
func TestAccAzureRMClientConfig_basic(t *testing.T) {
12+
clientId := os.Getenv("ARM_CLIENT_ID")
13+
tenantId := os.Getenv("ARM_TENANT_ID")
14+
subscriptionId := os.Getenv("ARM_SUBSCRIPTION_ID")
15+
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
Providers: testAccProviders,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccCheckArmClientConfig_basic,
22+
Check: resource.ComposeTestCheckFunc(
23+
testAzureRMClientConfigAttr("data.azurerm_client_config.current", "client_id", clientId),
24+
testAzureRMClientConfigAttr("data.azurerm_client_config.current", "tenant_id", tenantId),
25+
testAzureRMClientConfigAttr("data.azurerm_client_config.current", "subscription_id", subscriptionId),
26+
),
27+
},
28+
},
29+
})
30+
}
31+
32+
// Wraps resource.TestCheckResourceAttr to prevent leaking values to console
33+
// in case of mismatch
34+
func testAzureRMClientConfigAttr(name, key, value string) resource.TestCheckFunc {
35+
return func(s *terraform.State) error {
36+
err := resource.TestCheckResourceAttr(name, key, value)(s)
37+
if err != nil {
38+
// return fmt.Errorf("%s: Attribute '%s', failed check (values hidden)", name, key)
39+
return err
40+
}
41+
42+
return nil
43+
}
44+
}
45+
46+
const testAccCheckArmClientConfig_basic = `
47+
data "azurerm_client_config" "current" { }
48+
`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/acctest"
8+
"github.com/hashicorp/terraform/helper/resource"
9+
)
10+
11+
func TestAccAzureRMKeyVault_importBasic(t *testing.T) {
12+
resourceName := "azurerm_key_vault.test"
13+
14+
ri := acctest.RandInt()
15+
config := fmt.Sprintf(testAccAzureRMKeyVault_basic, ri, ri)
16+
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
19+
Providers: testAccProviders,
20+
CheckDestroy: testCheckAzureRMKeyVaultDestroy,
21+
Steps: []resource.TestStep{
22+
resource.TestStep{
23+
Config: config,
24+
},
25+
26+
resource.TestStep{
27+
ResourceName: resourceName,
28+
ImportState: true,
29+
ImportStateVerify: true,
30+
},
31+
},
32+
})
33+
}

builtin/providers/azurerm/provider.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ func Provider() terraform.ResourceProvider {
4444
},
4545
},
4646

47+
DataSourcesMap: map[string]*schema.Resource{
48+
"azurerm_client_config": dataSourceArmClientConfig(),
49+
},
50+
4751
ResourcesMap: map[string]*schema.Resource{
4852
// These resources use the Azure ARM SDK
4953
"azurerm_availability_set": resourceArmAvailabilitySet(),
@@ -57,6 +61,7 @@ func Provider() terraform.ResourceProvider {
5761
"azurerm_lb_probe": resourceArmLoadBalancerProbe(),
5862
"azurerm_lb_rule": resourceArmLoadBalancerRule(),
5963

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

192197
var wg sync.WaitGroup
193198
wg.Add(len(providers))

0 commit comments

Comments
 (0)