-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
New Resource & Data Source: azurerm_netapp_pool
#4889
Changes from 4 commits
10b1e86
e02258f
98d45ee
49ccba4
f5f1a1b
361ae8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
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" | ||
"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: validation.StringMatch( | ||
regexp.MustCompile(`^[\da-zA-Z_-]{4,64}$`), | ||
`The name must be between 4 and 64 characters in length and contain only letters, numbers, underscore or hyphens.`, | ||
), | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(), | ||
|
||
"location": azure.SchemaLocationForDataSource(), | ||
|
||
"account_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringMatch( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we make this a shared function in the netapp package and share it with the resource and netapp account resource? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. Moved this validation to netapp package as shared function. |
||
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.`, | ||
), | ||
}, | ||
|
||
"service_level": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"size": { | ||
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 { | ||
if err := d.Set("service_level", poolProperties.ServiceLevel); err != nil { | ||
return fmt.Errorf("Error setting `service_level`: %+v", err) | ||
} | ||
|
||
if err := d.Set("size", poolProperties.Size); err != nil { | ||
return fmt.Errorf("Error setting `size`: %+v", err) | ||
} | ||
neil-yechenwei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return nil | ||
} |
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"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
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) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
package azurerm | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"regexp" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/netapp/mgmt/2019-06-01/netapp" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"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" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmNetAppPool() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmNetAppPoolCreateUpdate, | ||
Read: resourceArmNetAppPoolRead, | ||
Update: resourceArmNetAppPoolCreateUpdate, | ||
Delete: resourceArmNetAppPoolDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringMatch( | ||
regexp.MustCompile(`^[\da-zA-Z_-]{4,64}$`), | ||
`The name must be between 4 and 64 characters in length and contain only letters, numbers, underscore or hyphens.`, | ||
), | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupName(), | ||
|
||
"location": azure.SchemaLocation(), | ||
|
||
"account_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.`, | ||
), | ||
}, | ||
|
||
"service_level": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
string(netapp.Premium), | ||
string(netapp.Standard), | ||
string(netapp.Ultra), | ||
}, false), | ||
DiffSuppressFunc: suppress.CaseDifference, | ||
neil-yechenwei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
|
||
"size": { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
ValidateFunc: validation.IntBetween(4398046511104, 549755813888000), | ||
neil-yechenwei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmNetAppPoolCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).Netapp.PoolClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
accountName := d.Get("account_name").(string) | ||
|
||
if features.ShouldResourcesBeImported() && d.IsNewResource() { | ||
existing, err := client.Get(ctx, resourceGroup, accountName, name) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(existing.Response) { | ||
return fmt.Errorf("Error checking for present of existing NetApp Pool %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
} | ||
if existing.ID != nil && *existing.ID != "" { | ||
return tf.ImportAsExistsError("azurerm_netapp_pool", *existing.ID) | ||
} | ||
} | ||
|
||
location := azure.NormalizeLocation(d.Get("location").(string)) | ||
serviceLevel := d.Get("service_level").(string) | ||
size := int64(d.Get("size").(int)) | ||
|
||
capacityPoolParameters := netapp.CapacityPool{ | ||
Location: utils.String(location), | ||
PoolProperties: &netapp.PoolProperties{ | ||
ServiceLevel: netapp.ServiceLevel(serviceLevel), | ||
Size: utils.Int64(size), | ||
}, | ||
} | ||
|
||
future, err := client.CreateOrUpdate(ctx, capacityPoolParameters, resourceGroup, accountName, name) | ||
if err != nil { | ||
return fmt.Errorf("Error creating NetApp Pool %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 Pool %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
resp, err := client.Get(ctx, resourceGroup, accountName, name) | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving NetApp Pool %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
if resp.ID == nil { | ||
return fmt.Errorf("Cannot read NetApp Pool %q (Resource Group %q) ID", name, resourceGroup) | ||
} | ||
d.SetId(*resp.ID) | ||
|
||
return resourceArmNetAppPoolRead(d, meta) | ||
} | ||
|
||
func resourceArmNetAppPoolRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).Netapp.PoolClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
id, err := azure.ParseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resourceGroup := id.ResourceGroup | ||
accountName := id.Path["netAppAccounts"] | ||
name := id.Path["capacityPools"] | ||
|
||
resp, err := client.Get(ctx, resourceGroup, accountName, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[INFO] NetApp Pools %q does not exist - removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error reading NetApp Pools %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
d.Set("name", name) | ||
d.Set("resource_group_name", resourceGroup) | ||
d.Set("account_name", accountName) | ||
if location := resp.Location; location != nil { | ||
d.Set("location", azure.NormalizeLocation(*location)) | ||
} | ||
if poolProperties := resp.PoolProperties; poolProperties != nil { | ||
if err := d.Set("service_level", poolProperties.ServiceLevel); err != nil { | ||
return fmt.Errorf("Error setting `service_level`: %+v", err) | ||
} | ||
|
||
if err := d.Set("size", poolProperties.Size); err != nil { | ||
return fmt.Errorf("Error setting `size`: %+v", err) | ||
} | ||
neil-yechenwei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmNetAppPoolDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).Netapp.PoolClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
id, err := azure.ParseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resourceGroup := id.ResourceGroup | ||
accountName := id.Path["netAppAccounts"] | ||
name := id.Path["capacityPools"] | ||
|
||
_, err = client.Delete(ctx, resourceGroup, accountName, name) | ||
neil-yechenwei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return fmt.Errorf("Error deleting NetApp Pool %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
return waitForNetAppPoolToBeDeleted(ctx, client, resourceGroup, accountName, name) | ||
} | ||
|
||
func waitForNetAppPoolToBeDeleted(ctx context.Context, client *netapp.PoolsClient, resourceGroup, accountName, name string) error { | ||
log.Printf("[DEBUG] Waiting for NetApp Pool Provisioning Service %q (Resource Group %q) to be deleted", name, resourceGroup) | ||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{"200", "202"}, | ||
Target: []string{"404"}, | ||
Refresh: netappPoolDeleteStateRefreshFunc(ctx, client, resourceGroup, accountName, name), | ||
Timeout: 20 * time.Minute, | ||
} | ||
if _, err := stateConf.WaitForState(); err != nil { | ||
return fmt.Errorf("Error waiting for NetApp Pool Provisioning Service %q (Resource Group %q) to be deleted: %+v", name, resourceGroup, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func netappPoolDeleteStateRefreshFunc(ctx context.Context, client *netapp.PoolsClient, resourceGroupName string, accountName string, name string) resource.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
res, err := client.Get(ctx, resourceGroupName, accountName, name) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(res.Response) { | ||
return nil, "", fmt.Errorf("Error retrieving NetApp Pool %q (Resource Group %q): %s", name, resourceGroupName, err) | ||
} | ||
} | ||
|
||
if _, err := client.Delete(ctx, resourceGroupName, accountName, name); err != nil { | ||
neil-yechenwei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
log.Printf("Error reissuing NetApp Pool %q delete request (Resource Group %q): %+v", name, resourceGroupName, err) | ||
} | ||
|
||
return res, strconv.Itoa(res.StatusCode), nil | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we make this a shared function in the netapp package and share it with the resource?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Moved this validation to netapp package as shared function.