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 2 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
97 changes: 97 additions & 0 deletions azurerm/data_source_netapp_snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package azurerm

import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
aznetapp "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/netapp"
"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: aznetapp.ValidateNetAppSnapshotName,
},

"location": azure.SchemaLocationForDataSource(),

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"account_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: aznetapp.ValidateNetAppAccountName,
},

"pool_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: aznetapp.ValidateNetAppPoolName,
},

"volume_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: aznetapp.ValidateNetAppVolumeName,
},

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

func dataSourceArmNetAppSnapshotRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).NetApp.SnapshotClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).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))
}
if props := resp.SnapshotProperties; props != nil {
d.Set("file_system_id", props.FileSystemID)
}

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

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
)

func TestAccDataSourceAzureRMNetAppSnapshot_basic(t *testing.T) {
dataSourceName := "data.azurerm_netapp_snapshot.test"
ri := tf.AccRandTimeInt()
location := testLocation()
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceNetAppSnapshot_basic(ri, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
),
},
},
})
}

func testAccDataSourceNetAppSnapshot_basic(rInt int, location string) string {
config := testAccAzureRMNetAppSnapshot_basic(rInt, location)
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)
}
6 changes: 6 additions & 0 deletions azurerm/data_source_netapp_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func dataSourceArmNetAppVolume() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},

"file_system_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -100,6 +105,7 @@ func dataSourceArmNetAppVolumeRead(d *schema.ResourceData, meta interface{}) err
d.Set("volume_path", props.CreationToken)
d.Set("service_level", props.ServiceLevel)
d.Set("subnet_id", props.SubnetID)
d.Set("file_system_id", props.FileSystemID)

if props.UsageThreshold != nil {
d.Set("storage_quota_in_gb", *props.UsageThreshold/1073741824)
Expand Down
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,
}
}
10 changes: 10 additions & 0 deletions azurerm/internal/services/netapp/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ func ValidateNetAppVolumeVolumePath(v interface{}, k string) (warnings []string,

return warnings, errors
}

func ValidateNetAppSnapshotName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

if !regexp.MustCompile(`^[\da-zA-Z][-_\da-zA-Z]{3,63}$`).MatchString(value) {
errors = append(errors, fmt.Errorf("%q must be between 4 and 64 characters in length and start with letters or numbers and contains only letters, numbers, underscore or hyphens.", k))
}

return warnings, errors
}
68 changes: 68 additions & 0 deletions azurerm/internal/services/netapp/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,71 @@ func TestValidateNetAppVolumeVolumePath(t *testing.T) {
}
}
}

func TestValidateNetAppSnapshotName(t *testing.T) {
testData := []struct {
input string
expected bool
}{
{
// empty
input: "",
expected: false,
},
{
// basic example
input: "hello",
expected: true,
},
{
// can't start with an underscore
input: "_hello",
expected: false,
},
{
// can't end with a dash
input: "hello-",
expected: true,
},
{
// can't contain an exclamation mark
input: "hello!",
expected: false,
},
{
// dash in the middle
input: "malcolm-in-the-middle",
expected: true,
},
{
// can't end with a period
input: "hello.",
expected: false,
},
{
// 63 chars
input: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk",
expected: true,
},
{
// 64 chars
input: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkj",
expected: true,
},
{
// 65 chars
input: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkja",
expected: false,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q..", v.input)

_, errors := ValidateNetAppSnapshotName(v.input, "name")
actual := len(errors) == 0
if v.expected != actual {
t.Fatalf("Expected %t but got %t", v.expected, actual)
}
}
}
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_netapp_account": dataSourceArmNetAppAccount(),
"azurerm_netapp_pool": dataSourceArmNetAppPool(),
"azurerm_netapp_volume": dataSourceArmNetAppVolume(),
"azurerm_netapp_snapshot": dataSourceArmNetAppSnapshot(),
"azurerm_network_ddos_protection_plan": dataSourceNetworkDDoSProtectionPlan(),
"azurerm_network_interface": dataSourceArmNetworkInterface(),
"azurerm_network_security_group": dataSourceArmNetworkSecurityGroup(),
Expand Down Expand Up @@ -389,6 +390,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_netapp_account": resourceArmNetAppAccount(),
"azurerm_netapp_pool": resourceArmNetAppPool(),
"azurerm_netapp_volume": resourceArmNetAppVolume(),
"azurerm_netapp_snapshot": resourceArmNetAppSnapshot(),
"azurerm_notification_hub_authorization_rule": resourceArmNotificationHubAuthorizationRule(),
"azurerm_notification_hub_namespace": resourceArmNotificationHubNamespace(),
"azurerm_notification_hub": resourceArmNotificationHub(),
Expand Down
Loading