Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Resource/Data Source: azurerm_recovery_services_vault #995

Merged
merged 5 commits into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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/2018-03-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 @@ -166,6 +167,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 @@ -373,6 +377,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 @@ -786,6 +791,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
69 changes: 69 additions & 0 deletions azurerm/data_source_recovery_services_vault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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,
ForceNew: 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)

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

flattenAndSetTags(d, vault.Tags)
return nil
}
44 changes: 44 additions & 0 deletions azurerm/data_source_recovery_services_vault_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package azurerm

import (
"fmt"
"testing"

"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2016-06-01/recoveryservices"
"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: resource.ComposeTestCheckFunc(
testCheckAzureRMRecoveryServicesVaultExists(dataSourceName),
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
resource.TestCheckResourceAttrSet(dataSourceName, "location"),
resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"),
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"),
resource.TestCheckResourceAttr(dataSourceName, "sku", string(recoveryservices.Standard)),
),
},
},
})
}

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 @@ -93,6 +93,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_scheduler_job_collection": dataSourceArmSchedulerJobCollection(),
Expand Down Expand Up @@ -179,6 +180,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
149 changes: 149 additions & 0 deletions azurerm/resource_arm_recovery_services_vault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
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
vault, 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(*vault.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)

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.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", resp.Name)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}

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

flattenAndSetTags(d, resp.Tags)

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