Skip to content

Commit

Permalink
Added new resource azurerm_recovery_services_vault and data source az…
Browse files Browse the repository at this point in the history
…urerm_recovery_services_vault
  • Loading branch information
katbyte committed Mar 19, 2018
1 parent 913bcd7 commit c59daeb
Show file tree
Hide file tree
Showing 24 changed files with 3,062 additions and 0 deletions.
11 changes: 11 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/operationalinsights/mgmt/2015-11-01-preview/operationalinsights"
"github.com/Azure/azure-sdk-for-go/services/operationsmanagement/mgmt/2015-11-01-preview/operationsmanagement"
"github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2017-04-30-preview/postgresql"
"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2016-06-01/recoveryservices"
"github.com/Azure/azure-sdk-for-go/services/redis/mgmt/2016-04-01/redis"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-06-01/subscriptions"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-09-01/locks"
Expand Down Expand Up @@ -157,6 +158,9 @@ type ArmClient struct {
vnetPeeringsClient network.VirtualNetworkPeeringsClient
watcherClient network.WatchersClient

// Recovery Services
recoveryServicesVaultsClient recoveryservices.VaultsClient

// Resources
managementLocksClient locks.ManagementLocksClient
deploymentsClient resources.DeploymentsClient
Expand Down Expand Up @@ -358,6 +362,7 @@ func getArmClient(c *authentication.Config) (*ArmClient, error) {
client.registerMonitorClients(endpoint, c.SubscriptionID, auth, sender)
client.registerNetworkingClients(endpoint, c.SubscriptionID, auth, sender)
client.registerOperationalInsightsClients(endpoint, c.SubscriptionID, auth, sender)
client.registerRecoveryServiceClients(endpoint, c.SubscriptionID, auth)
client.registerRedisClients(endpoint, c.SubscriptionID, auth, sender)
client.registerResourcesClients(endpoint, c.SubscriptionID, auth)
client.registerSearchClients(endpoint, c.SubscriptionID, auth)
Expand Down Expand Up @@ -748,6 +753,12 @@ func (c *ArmClient) registerOperationalInsightsClients(endpoint, subscriptionId
c.solutionsClient = solutionsClient
}

func (c *ArmClient) registerRecoveryServiceClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
vaultsClient := recoveryservices.NewVaultsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&vaultsClient.Client, auth)
c.recoveryServicesVaultsClient = vaultsClient
}

func (c *ArmClient) registerRedisClients(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) {
redisClient := redis.NewClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&redisClient.Client, auth)
Expand Down
68 changes: 68 additions & 0 deletions azurerm/data_source_recovery_services_vault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package azurerm

import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmRecoveryServicesVault() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmRecoveryServicesVaultRead,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"location": locationForDataSourceSchema(),

"resource_group_name": resourceGroupNameForDataSourceSchema(),

"tags": tagsForDataSourceSchema(),

"sku": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceArmRecoveryServicesVaultRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).recoveryServicesVaultsClient
ctx := meta.(*ArmClient).StopContext

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

log.Printf("[DEBUG] Reading Recovery Service Vault %q (resource group %q)", name, resourceGroup)

vault, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(vault.Response) {
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request on Recovery Service Vault %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*vault.ID)
d.Set("name", vault.Name)
d.Set("location", azureRMNormalizeLocation(*vault.Location))
d.Set("resource_group_name", resourceGroup)
flattenAndSetTags(d, &vault.Tags)

if sku := vault.Sku; sku != nil {
d.Set("sku", string(sku.Name))
}

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

import (
"fmt"
"testing"

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

func TestAccDataSourceAzureRMRecoveryServicesVault_basic(t *testing.T) {
dataSourceName := "data.azurerm_recovery_services_vault.test"
ri := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceRecoveryServicesVault_basic(ri, testLocation()),
Check: checkAccAzureRMRecoveryServicesVault_basic(dataSourceName),
},
},
})
}

func testAccDataSourceRecoveryServicesVault_basic(rInt int, location string) string {
return fmt.Sprintf(`
%s
data "azurerm_recovery_services_vault" "test" {
name = "${azurerm_recovery_services_vault.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, testAccAzureRMRecoveryServicesVault_basic(rInt, location))
}
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_platform_image": dataSourceArmPlatformImage(),
"azurerm_public_ip": dataSourceArmPublicIP(),
"azurerm_public_ips": dataSourceArmPublicIPs(),
"azurerm_recovery_services_vault": dataSourceArmRecoveryServicesVault(),
"azurerm_resource_group": dataSourceArmResourceGroup(),
"azurerm_role_definition": dataSourceArmRoleDefinition(),
"azurerm_storage_account": dataSourceArmStorageAccount(),
Expand Down Expand Up @@ -169,6 +170,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_postgresql_firewall_rule": resourceArmPostgreSQLFirewallRule(),
"azurerm_postgresql_server": resourceArmPostgreSQLServer(),
"azurerm_public_ip": resourceArmPublicIp(),
"azurerm_recovery_services_vault": resourceArmRecoveryServicesVault(),
"azurerm_redis_cache": resourceArmRedisCache(),
"azurerm_redis_firewall_rule": resourceArmRedisFirewallRule(),
"azurerm_resource_group": resourceArmResourceGroup(),
Expand Down
146 changes: 146 additions & 0 deletions azurerm/resource_arm_recovery_services_vault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package azurerm

import (
"fmt"
"log"
"regexp"

"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2016-06-01/recoveryservices"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmRecoveryServicesVault() *schema.Resource {
return &schema.Resource{
Create: resourceArmRecoveryServicesVaultCreateUpdate,
Read: resourceArmRecoveryServicesVaultRead,
Update: resourceArmRecoveryServicesVaultCreateUpdate,
Delete: resourceArmRecoveryServicesVaultDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile("^[a-zA-Z][-a-zA-Z0-9]{1,49}$"),
"Recovery Service Vault name must be 2 - 50 characters long, start with a letter, contain only letters, numbers and hyphens.",
),
},

"location": locationSchema(),

"resource_group_name": resourceGroupNameSchema(),

"tags": tagsSchema(),

"sku": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
ValidateFunc: validation.StringInSlice([]string{
string(recoveryservices.RS0),
string(recoveryservices.Standard),
}, true),
},
},
}
}

func resourceArmRecoveryServicesVaultCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).recoveryServicesVaultsClient
ctx := meta.(*ArmClient).StopContext

name := d.Get("name").(string)
location := d.Get("location").(string)
resourceGroup := d.Get("resource_group_name").(string)
tags := d.Get("tags").(map[string]interface{})

log.Printf("[DEBUG] Creating/updating Recovery Service Vault %q (resource group %q)", name, resourceGroup)

//build vault struct
vault := recoveryservices.Vault{
Location: utils.String(location),
Tags: *expandTags(tags),
Sku: &recoveryservices.Sku{
Name: recoveryservices.SkuName(d.Get("sku").(string)),
},
Properties: &recoveryservices.VaultProperties{},
}

//create recovery services vault
collection, err := client.CreateOrUpdate(ctx, resourceGroup, name, vault)
if err != nil {
return fmt.Errorf("Error creating/updating Recovery Service Vault %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*collection.ID)

return resourceArmRecoveryServicesVaultRead(d, meta)
}

func resourceArmRecoveryServicesVaultRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).recoveryServicesVaultsClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}

name := id.Path["vaults"]
resourceGroup := id.ResourceGroup

log.Printf("[DEBUG] Reading Recovery Service Vault %q (resource group %q)", name, resourceGroup)

vault, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(vault.Response) {
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request on Recovery Service Vault %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.Set("name", vault.Name)
d.Set("location", azureRMNormalizeLocation(*vault.Location))
d.Set("resource_group_name", resourceGroup)
flattenAndSetTags(d, &vault.Tags)

if sku := vault.Sku; sku != nil {
d.Set("sku", string(sku.Name))
}

return nil
}

func resourceArmRecoveryServicesVaultDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).recoveryServicesVaultsClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}

name := id.Path["vaults"]
resourceGroup := id.ResourceGroup

log.Printf("[DEBUG] Deleting Recovery Service Vault %q (resource group %q)", name, resourceGroup)

resp, err := client.Delete(ctx, resourceGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error issuing delete request for Recovery Service Vault %q (Resource Group %q): %+v", name, resourceGroup, err)
}
}

return nil
}
Loading

0 comments on commit c59daeb

Please sign in to comment.