-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new resource azurerm_recovery_services_vault and data source az…
…urerm_recovery_services_vault
- Loading branch information
Showing
24 changed files
with
3,062 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.