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_volume #4933

Merged
merged 13 commits into from
Dec 18, 2019
99 changes: 99 additions & 0 deletions azurerm/data_source_netapp_volume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package azurerm

import (
"fmt"

"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/utils"
)

func dataSourceArmNetAppVolume() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmNetAppVolumeRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: aznetapp.ValidateNetAppPoolName,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"location": azure.SchemaLocationForDataSource(),

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

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

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

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

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

"usage_threshold": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}

func dataSourceArmNetAppVolumeRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Netapp.VolumeClient
ctx := meta.(*ArmClient).StopContext

name := d.Get("name").(string)
accountName := d.Get("account_name").(string)
poolName := d.Get("pool_name").(string)
resourceGroup := d.Get("resource_group_name").(string)

resp, err := client.Get(ctx, resourceGroup, accountName, poolName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: NetApp Volume %q (Resource Group %q) was not found", name, resourceGroup)
}
return fmt.Errorf("Error reading NetApp Volume %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
d.Set("account_name", accountName)
d.Set("pool_name", poolName)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
if props := resp.VolumeProperties; props != nil {
d.Set("creation_token", props.CreationToken)
d.Set("service_level", props.ServiceLevel)
d.Set("subnet_id", props.SubnetID)

if props.UsageThreshold != nil {
d.Set("usage_threshold", *props.UsageThreshold/1073741824)
}
}

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

import (
"fmt"
"testing"

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

func TestAccDataSourceAzureRMNetAppVolume_basic(t *testing.T) {
dataSourceName := "data.azurerm_netapp_volume.test"
ri := tf.AccRandTimeInt()
location := testLocation()

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

func testAccDataSourceNetAppVolume_basic(rInt int, location string) string {
config := testAccAzureRMNetAppVolume_basic(rInt, location)
return fmt.Sprintf(`
%s

data "azurerm_netapp_volume" "test" {
resource_group_name = "${azurerm_netapp_volume.test.resource_group_name}"
account_name = "${azurerm_netapp_volume.test.account_name}"
pool_name = "${azurerm_netapp_volume.test.pool_name}"
name = "${azurerm_netapp_volume.test.name}"
}
`, config)
}
5 changes: 5 additions & 0 deletions azurerm/internal/services/netapp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type Client struct {
AccountClient *netapp.AccountsClient
PoolClient *netapp.PoolsClient
VolumeClient *netapp.VolumesClient
}

func BuildClient(o *common.ClientOptions) *Client {
Expand All @@ -17,8 +18,12 @@ func BuildClient(o *common.ClientOptions) *Client {
poolClient := netapp.NewPoolsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&poolClient.Client, o.ResourceManagerAuthorizer)

volumeClient := netapp.NewVolumesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&volumeClient.Client, o.ResourceManagerAuthorizer)

return &Client{
AccountClient: &accountClient,
PoolClient: &poolClient,
VolumeClient: &volumeClient,
}
}
26 changes: 0 additions & 26 deletions azurerm/internal/services/netapp/validate.go

This file was deleted.

66 changes: 66 additions & 0 deletions azurerm/internal/services/netapp/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package netapp

import (
"fmt"
"net"
"regexp"
)

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

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

return warnings, errors
}

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

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

return warnings, errors
}

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

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

return warnings, errors
}

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

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

return warnings, errors
}

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

if !regexp.MustCompile(`^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$`).MatchString(value) {
errors = append(errors, fmt.Errorf("%s must start with IPV4 address and/or slash, number of bits (0-32) as prefix. Example: 127.0.0.1/8. Got %q.", k, value))
}

ip := net.ParseIP(value)
if four := ip.To4(); four == nil {
errors = append(errors, fmt.Errorf("%q is not a valid IPv4 address: %q", k, v))
}

if len(errors) == 2 {
return warnings, errors
} else {
return warnings, nil
}
}
Loading