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

Add new resource & data source: azurerm_netapp_snapshot #5215

Merged
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
17 changes: 11 additions & 6 deletions azurerm/internal/services/netapp/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

type Client struct {
AccountClient *netapp.AccountsClient
PoolClient *netapp.PoolsClient
VolumeClient *netapp.VolumesClient
AccountClient *netapp.AccountsClient
PoolClient *netapp.PoolsClient
VolumeClient *netapp.VolumesClient
SnapshotClient *netapp.SnapshotsClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand All @@ -21,9 +22,13 @@ func NewClient(o *common.ClientOptions) *Client {
volumeClient := netapp.NewVolumesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&volumeClient.Client, o.ResourceManagerAuthorizer)

snapshotClient := netapp.NewSnapshotsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&snapshotClient.Client, o.ResourceManagerAuthorizer)

return &Client{
AccountClient: &accountClient,
PoolClient: &poolClient,
VolumeClient: &volumeClient,
AccountClient: &accountClient,
PoolClient: &poolClient,
VolumeClient: &volumeClient,
SnapshotClient: &snapshotClient,
}
}
89 changes: 89 additions & 0 deletions azurerm/internal/services/netapp/data_source_netapp_snapshot.go
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
}
14 changes: 8 additions & 6 deletions azurerm/internal/services/netapp/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ func (r Registration) Name() string {
// SupportedDataSources returns the supported Data Sources supported by this Service
func (r Registration) SupportedDataSources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_netapp_account": dataSourceArmNetAppAccount(),
"azurerm_netapp_pool": dataSourceArmNetAppPool(),
"azurerm_netapp_volume": dataSourceArmNetAppVolume()}
"azurerm_netapp_account": dataSourceArmNetAppAccount(),
"azurerm_netapp_pool": dataSourceArmNetAppPool(),
"azurerm_netapp_volume": dataSourceArmNetAppVolume(),
"azurerm_netapp_snapshot": dataSourceArmNetAppSnapshot()}
}

// SupportedResources returns the supported Resources supported by this Service
func (r Registration) SupportedResources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_netapp_account": resourceArmNetAppAccount(),
"azurerm_netapp_pool": resourceArmNetAppPool(),
"azurerm_netapp_volume": resourceArmNetAppVolume()}
"azurerm_netapp_account": resourceArmNetAppAccount(),
"azurerm_netapp_pool": resourceArmNetAppPool(),
"azurerm_netapp_volume": resourceArmNetAppVolume(),
"azurerm_netapp_snapshot": resourceArmNetAppSnapshot()}
}
189 changes: 189 additions & 0 deletions azurerm/internal/services/netapp/resource_arm_netapp_snapshot.go
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
}
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)
}
Loading