Skip to content

Commit

Permalink
New Resource & Data Source: azurerm_netapp_pool (#4889)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Ye authored and mbfrahry committed Nov 19, 2019
1 parent 93b8313 commit 43f35dd
Show file tree
Hide file tree
Showing 14 changed files with 775 additions and 15 deletions.
12 changes: 4 additions & 8 deletions azurerm/data_source_netapp_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package azurerm

import (
"fmt"
"regexp"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"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"
)

Expand All @@ -16,12 +15,9 @@ func dataSourceArmNetAppAccount() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`(^[\da-zA-Z])([-\da-zA-Z]{1,62})([\da-zA-Z]$)`),
`The name must be between 3 and 64 characters in length and begin with a letter or number, end with a letter or number and may contain only letters, numbers or hyphens.`,
),
Type: schema.TypeString,
Required: true,
ValidateFunc: aznetapp.ValidateNetAppAccountName,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
Expand Down
78 changes: 78 additions & 0 deletions azurerm/data_source_netapp_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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 dataSourceArmNetAppPool() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmNetAppPoolRead,

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,
},

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

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

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

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

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

d.SetId(*resp.ID)

d.Set("name", name)
d.Set("account_name", accountName)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
if poolProperties := resp.PoolProperties; poolProperties != nil {
d.Set("service_level", poolProperties.ServiceLevel)
if poolProperties.Size != nil {
d.Set("size_in_4_tb", *poolProperties.Size/4398046511104)
}
}

return nil
}
45 changes: 45 additions & 0 deletions azurerm/data_source_netapp_pool_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 TestAccDataSourceAzureRMNetAppPool_basic(t *testing.T) {
dataSourceName := "data.azurerm_netapp_pool.test"
ri := tf.AccRandTimeInt()
location := testLocation()

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

func testAccDataSourceNetAppPool_basic(rInt int, location string) string {
config := testAccAzureRMNetAppPool_basic(rInt, location)
return fmt.Sprintf(`
%s
data "azurerm_netapp_pool" "test" {
resource_group_name = "${azurerm_netapp_pool.test.resource_group_name}"
account_name = "${azurerm_netapp_pool.test.account_name}"
name = "${azurerm_netapp_pool.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 @@ -7,13 +7,18 @@ import (

type Client struct {
AccountClient *netapp.AccountsClient
PoolClient *netapp.PoolsClient
}

func BuildClient(o *common.ClientOptions) *Client {
accountClient := netapp.NewAccountsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&accountClient.Client, o.ResourceManagerAuthorizer)

poolClient := netapp.NewPoolsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&poolClient.Client, o.ResourceManagerAuthorizer)

return &Client{
AccountClient: &accountClient,
PoolClient: &poolClient,
}
}
26 changes: 26 additions & 0 deletions azurerm/internal/services/netapp/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package netapp

import (
"fmt"
"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
}
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_monitor_log_profile": dataSourceArmMonitorLogProfile(),
"azurerm_mssql_elasticpool": dataSourceArmMsSqlElasticpool(),
"azurerm_netapp_account": dataSourceArmNetAppAccount(),
"azurerm_netapp_pool": dataSourceArmNetAppPool(),
"azurerm_network_ddos_protection_plan": dataSourceNetworkDDoSProtectionPlan(),
"azurerm_network_interface": dataSourceArmNetworkInterface(),
"azurerm_network_security_group": dataSourceArmNetworkSecurityGroup(),
Expand Down Expand Up @@ -371,6 +372,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_network_security_rule": resourceArmNetworkSecurityRule(),
"azurerm_network_watcher": resourceArmNetworkWatcher(),
"azurerm_netapp_account": resourceArmNetAppAccount(),
"azurerm_netapp_pool": resourceArmNetAppPool(),
"azurerm_notification_hub_authorization_rule": resourceArmNotificationHubAuthorizationRule(),
"azurerm_notification_hub_namespace": resourceArmNotificationHubNamespace(),
"azurerm_notification_hub": resourceArmNotificationHub(),
Expand Down
12 changes: 5 additions & 7 deletions azurerm/resource_arm_netapp_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
aznetapp "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/netapp"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand All @@ -30,13 +31,10 @@ func resourceArmNetAppAccount() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`(^[\da-zA-Z])([-\da-zA-Z]{1,62})([\da-zA-Z]$)`),
`The name must be between 3 and 64 characters in length and begin with a letter or number, end with a letter or number and may contain only letters, numbers or hyphens.`,
),
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: aznetapp.ValidateNetAppAccountName,
},

"resource_group_name": azure.SchemaResourceGroupName(),
Expand Down
Loading

0 comments on commit 43f35dd

Please sign in to comment.