-
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.
Add new resource & data source: azurerm_netapp_snapshot (#5215)
fixes #5216
- Loading branch information
Showing
13 changed files
with
968 additions
and
12 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
89 changes: 89 additions & 0 deletions
89
azurerm/internal/services/netapp/data_source_netapp_snapshot.go
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,89 @@ | ||
package netapp | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func dataSourceArmNetAppSnapshot() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceArmNetAppSnapshotRead, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: ValidateNetAppSnapshotName, | ||
}, | ||
|
||
"location": azure.SchemaLocationForDataSource(), | ||
|
||
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(), | ||
|
||
"account_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: ValidateNetAppAccountName, | ||
}, | ||
|
||
"pool_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: ValidateNetAppPoolName, | ||
}, | ||
|
||
"volume_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: ValidateNetAppVolumeName, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceArmNetAppSnapshotRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).NetApp.SnapshotClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
name := d.Get("name").(string) | ||
accountName := d.Get("account_name").(string) | ||
poolName := d.Get("pool_name").(string) | ||
volumeName := d.Get("volume_name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
|
||
resp, err := client.Get(ctx, resourceGroup, accountName, poolName, volumeName, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Error: NetApp Snapshot %q (Resource Group %q) was not found", name, resourceGroup) | ||
} | ||
return fmt.Errorf("Error reading NetApp Snapshot %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
if resp.ID == nil || *resp.ID == "" { | ||
return fmt.Errorf("Error retrieving NetApp Snapshot %q (Resource Group %q): ID was nil or empty", name, resourceGroup) | ||
} | ||
|
||
d.SetId(*resp.ID) | ||
|
||
d.Set("name", name) | ||
d.Set("resource_group_name", resourceGroup) | ||
d.Set("account_name", accountName) | ||
d.Set("pool_name", poolName) | ||
d.Set("volume_name", volumeName) | ||
if location := resp.Location; location != nil { | ||
d.Set("location", azure.NormalizeLocation(*location)) | ||
} | ||
|
||
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
189 changes: 189 additions & 0 deletions
189
azurerm/internal/services/netapp/resource_arm_netapp_snapshot.go
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,189 @@ | ||
package netapp | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/netapp/mgmt/2019-06-01/netapp" | ||
"github.com/hashicorp/go-azure-helpers/response" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmNetAppSnapshot() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmNetAppSnapshotCreate, | ||
Read: resourceArmNetAppSnapshotRead, | ||
Update: nil, | ||
Delete: resourceArmNetAppSnapshotDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(30 * time.Minute), | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
Update: schema.DefaultTimeout(30 * time.Minute), | ||
Delete: schema.DefaultTimeout(30 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: ValidateNetAppSnapshotName, | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupName(), | ||
|
||
"location": azure.SchemaLocation(), | ||
|
||
"account_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: ValidateNetAppAccountName, | ||
}, | ||
|
||
"pool_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: ValidateNetAppPoolName, | ||
}, | ||
|
||
"volume_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: ValidateNetAppVolumeName, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmNetAppSnapshotCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).NetApp.SnapshotClient | ||
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
accountName := d.Get("account_name").(string) | ||
poolName := d.Get("pool_name").(string) | ||
volumeName := d.Get("volume_name").(string) | ||
|
||
if features.ShouldResourcesBeImported() && d.IsNewResource() { | ||
resp, err := client.Get(ctx, resourceGroup, accountName, poolName, volumeName, name) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Error checking for present of existing NetApp Snapshot %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
} | ||
if !utils.ResponseWasNotFound(resp.Response) { | ||
return tf.ImportAsExistsError("azurerm_netapp_snapshot", *resp.ID) | ||
} | ||
} | ||
|
||
location := azure.NormalizeLocation(d.Get("location").(string)) | ||
|
||
parameters := netapp.Snapshot{ | ||
Location: utils.String(location), | ||
} | ||
|
||
future, err := client.Create(ctx, parameters, resourceGroup, accountName, poolName, volumeName, name) | ||
if err != nil { | ||
return fmt.Errorf("Error creating NetApp Snapshot %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("Error waiting for creation of NetApp Snapshot %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
resp, err := client.Get(ctx, resourceGroup, accountName, poolName, volumeName, name) | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving NetApp Snapshot %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
if resp.ID == nil || *resp.ID == "" { | ||
return fmt.Errorf("Cannot read NetApp Snapshot %q (Resource Group %q) ID", name, resourceGroup) | ||
} | ||
d.SetId(*resp.ID) | ||
|
||
return resourceArmNetAppSnapshotRead(d, meta) | ||
} | ||
|
||
func resourceArmNetAppSnapshotRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).NetApp.SnapshotClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := azure.ParseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resourceGroup := id.ResourceGroup | ||
accountName := id.Path["netAppAccounts"] | ||
poolName := id.Path["capacityPools"] | ||
volumeName := id.Path["volumes"] | ||
name := id.Path["snapshots"] | ||
|
||
resp, err := client.Get(ctx, resourceGroup, accountName, poolName, volumeName, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[INFO] NetApp Snapshots %q does not exist - removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error reading NetApp Snapshots %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
d.Set("name", name) | ||
d.Set("resource_group_name", resourceGroup) | ||
d.Set("account_name", accountName) | ||
d.Set("pool_name", poolName) | ||
d.Set("volume_name", volumeName) | ||
if location := resp.Location; location != nil { | ||
d.Set("location", azure.NormalizeLocation(*location)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmNetAppSnapshotDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).NetApp.SnapshotClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := azure.ParseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resourceGroup := id.ResourceGroup | ||
accountName := id.Path["netAppAccounts"] | ||
poolName := id.Path["capacityPools"] | ||
volumeName := id.Path["volumes"] | ||
name := id.Path["snapshots"] | ||
|
||
future, err := client.Delete(ctx, resourceGroup, accountName, poolName, volumeName, name) | ||
if err != nil { | ||
if response.WasNotFound(future.Response()) { | ||
return nil | ||
} | ||
return fmt.Errorf("Error deleting NetApp Snapshot %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
if !response.WasNotFound(future.Response()) { | ||
return fmt.Errorf("Error waiting for deleting NetApp Snapshot %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
41 changes: 41 additions & 0 deletions
41
azurerm/internal/services/netapp/tests/data_source_netapp_snapshot_test.go
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,41 @@ | ||
package tests | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" | ||
) | ||
|
||
func TestAccDataSourceAzureRMNetAppSnapshot_basic(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "data.azurerm_netapp_snapshot", "test") | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.PreCheck(t) }, | ||
Providers: acceptance.SupportedProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceNetAppSnapshot_basic(data), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet(data.ResourceName, "name"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceNetAppSnapshot_basic(data acceptance.TestData) string { | ||
config := testAccAzureRMNetAppSnapshot_basic(data) | ||
return fmt.Sprintf(` | ||
%s | ||
data "azurerm_netapp_snapshot" "test" { | ||
resource_group_name = "${azurerm_netapp_snapshot.test.resource_group_name}" | ||
account_name = "${azurerm_netapp_snapshot.test.account_name}" | ||
pool_name = "${azurerm_netapp_snapshot.test.pool_name}" | ||
volume_name = "${azurerm_netapp_snapshot.test.volume_name}" | ||
name = "${azurerm_netapp_snapshot.test.name}" | ||
} | ||
`, config) | ||
} |
Oops, something went wrong.