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

New Resource: azurerm_hpc_cache #5528

Merged
merged 27 commits into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
efe8b6e
wip: add azurerm_hpc_cache resource
aqche Jan 26, 2020
a9c830b
run go mod vendor
aqche Jan 26, 2020
75004c2
formatting fixes for resource_arm_hpc_cache_test.go
aqche Jan 27, 2020
036dbea
more resource_arm_hpc_cache_test.go formatting
aqche Jan 27, 2020
a58691f
wip: create docs for azurerm_hpc_cache
aqche Jan 27, 2020
cd0d8cd
add Storage Cache to allowed-subcategories
aqche Jan 27, 2020
98559fb
Merge branch 'master' of https://github.com/terraform-providers/terra…
aqche Jan 30, 2020
e283c73
hpc cache: cache size valudation, sku name arg, wait for provisioning…
aqche Jan 30, 2020
b765f66
whitespace
aqche Jan 30, 2020
205492c
whitespace
aqche Jan 30, 2020
d795f90
remove tags
aqche Jan 31, 2020
e77d488
Merge branch 'master' of https://github.com/terraform-providers/terra…
aqche Jan 31, 2020
c32a6e3
Merge branch 'master' of https://github.com/terraform-providers/terra…
aqche Feb 4, 2020
6edea9f
Apply suggestions from code review
aqche Feb 11, 2020
c6df54b
Merge branch 'master' of https://github.com/terraform-providers/terra…
aqche Feb 11, 2020
22b634b
Merge branch 'azurerm_hpc_cache' of github.com:aqche/terraform-provid…
aqche Feb 11, 2020
7bbbaba
apply review suggestions
aqche Feb 11, 2020
bfbfa64
small fixes when moving hpc resource
aqche Feb 11, 2020
7f5ff1b
tests fix
aqche Feb 11, 2020
5035ba1
Merge branch 'master' of https://github.com/terraform-providers/terra…
aqche Feb 11, 2020
324f369
merge conflicts
aqche Feb 17, 2020
5c7f4cb
Merge branch 'master' into azurerm_hpc_cache
aqche Feb 24, 2020
df8d3a6
Merge branch 'master' of https://github.com/terraform-providers/terra…
aqche Feb 28, 2020
19cce38
use future.WaitForCompletionRef() for hpc creation
aqche Feb 28, 2020
75547ea
Apply suggestions from code review
aqche Feb 29, 2020
35a5a6b
address review
aqche Feb 29, 2020
a7f9030
fmt
aqche Feb 29, 2020
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
1 change: 1 addition & 0 deletions azurerm/internal/provider/required_resource_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func RequiredResourceProviders() map[string]struct{} {
"Microsoft.ServiceFabric": {},
"Microsoft.Sql": {},
"Microsoft.Storage": {},
"Microsoft.StorageCache": {},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@katbyte i've updated the RequiredResourceProviders func here. let me know if this is correct. thanks!

"Microsoft.StreamAnalytics": {},
"Microsoft.Web": {},
}
Expand Down
6 changes: 6 additions & 0 deletions azurerm/internal/services/storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/datalakestore/filesystems"

"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-04-01/storage"
"github.com/Azure/azure-sdk-for-go/services/storagecache/mgmt/2019-11-01/storagecache"
az "github.com/Azure/go-autorest/autorest/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/accounts"
Expand All @@ -25,6 +26,7 @@ type Client struct {
FileSystemsClient *filesystems.Client
ManagementPoliciesClient storage.ManagementPoliciesClient
BlobServicesClient storage.BlobServicesClient
CachesClient *storagecache.CachesClient
BlobAccountsClient *accounts.Client

environment az.Environment
Expand All @@ -44,6 +46,9 @@ func NewClient(options *common.ClientOptions) *Client {
blobServicesClient := storage.NewBlobServicesClientWithBaseURI(options.ResourceManagerEndpoint, options.SubscriptionId)
options.ConfigureClient(&blobServicesClient.Client, options.ResourceManagerAuthorizer)

cachesClient := storagecache.NewCachesClientWithBaseURI(options.ResourceManagerEndpoint, options.SubscriptionId)
options.ConfigureClient(&cachesClient.Client, options.ResourceManagerAuthorizer)

blobAccountsClient := accounts.NewWithEnvironment(options.Environment)
options.ConfigureClient(&blobAccountsClient.Client, options.StorageAuthorizer)

Expand All @@ -54,6 +59,7 @@ func NewClient(options *common.ClientOptions) *Client {
FileSystemsClient: &fileSystemsClient,
ManagementPoliciesClient: managementPoliciesClient,
BlobServicesClient: blobServicesClient,
CachesClient: &cachesClient,
BlobAccountsClient: &blobAccountsClient,
environment: options.Environment,
}
Expand Down
1 change: 1 addition & 0 deletions azurerm/internal/services/storage/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
// SupportedResources returns the supported Resources supported by this Service
func (r Registration) SupportedResources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_hpc_cache": resourceArmHPCCache(),
"azurerm_storage_account": resourceArmStorageAccount(),
"azurerm_storage_account_customer_managed_key": resourceArmStorageAccountCustomerManagedKey(),
"azurerm_storage_account_network_rules": resourceArmStorageAccountNetworkRules(),
Expand Down
229 changes: 229 additions & 0 deletions azurerm/internal/services/storage/resource_arm_hpc_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
package storage

import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/storagecache/mgmt/2019-11-01/storagecache"
"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/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"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 resourceArmHPCCache() *schema.Resource {
return &schema.Resource{
Create: resourceArmHPCCacheCreate,
Read: resourceArmHPCCacheRead,
Delete: resourceArmHPCCacheDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
},

"resource_group_name": azure.SchemaResourceGroupName(),

"location": azure.SchemaLocation(),

"cache_size": {
aqche marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeInt,
Required: true,
ForceNew: true,
ValidateFunc: validation.IntInSlice([]int{
3072,
6144,
12288,
24576,
49152,
}),
},

"subnet_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceIDOrEmpty,
},

"sku_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"Standard_2G",
"Standard_4G",
"Standard_8G",
}, false),
},
},
}
}

func resourceArmHPCCacheCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Storage.CachesClient
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

log.Printf("[INFO] preparing arguments for Azure HPC Cache creation.")
name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

if features.ShouldResourcesBeImported() && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing HPC Cache %q (Resource Group %q): %s", name, resourceGroup, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_hpc_cache", *existing.ID)
}
}

location := d.Get("location").(string)
cacheSize := d.Get("cache_size").(int)
subnet := d.Get("subnet_id").(string)
skuName := d.Get("sku_name").(string)

cache := &storagecache.Cache{
Name: utils.String(name),
Location: utils.String(location),
CacheProperties: &storagecache.CacheProperties{
CacheSizeGB: utils.Int32(int32(cacheSize)),
Subnet: utils.String(subnet),
},
Sku: &storagecache.CacheSku{
Name: utils.String(skuName),
},
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, name, cache); err != nil {
return fmt.Errorf("Error creating HPC Cache %q (Resource Group %q): %+v", name, resourceGroup, err)
}

stateConf := &resource.StateChangeConf{
Copy link
Collaborator

@magodo magodo Feb 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aqche
I may be dumb here, why not just use future.WaitForCompletionRef() here? Is there some limitation about this API?

Copy link
Contributor Author

@aqche aqche Feb 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@magodo thanks for calling that out, it actually slipped my mind to try future.WaitForCompletionRef() here first. I'll give it a test and report back.

update: worked like a charm!

Pending: []string{string(storagecache.Creating)},
Target: []string{string(storagecache.Succeeded)},
MinTimeout: 30 * time.Second,
Refresh: func() (interface{}, string, error) {
resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return resp, "Error", fmt.Errorf("Error retrieving HPC Cache %q (Resource Group %q): %+v", resourceGroup, name, err)
}

if properties := resp.CacheProperties; properties != nil {
return resp, string(properties.ProvisioningState), nil
}

return resp, "Unknown", nil
},
}

if features.SupportsCustomTimeouts() {
if d.IsNewResource() {
stateConf.Timeout = d.Timeout(schema.TimeoutCreate)
}
} else {
stateConf.Timeout = 30 * time.Minute
}

_, err := stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for HPC Cache %q (Resource Group %q) to finish provisioning: %+v", resourceGroup, name, err)
}

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error retrieving HPC Cache %q (Resource Group %q): %+v", name, resourceGroup, err)
}
if resp.ID == nil {
return fmt.Errorf("Cannot read ID for HPC Cache %q (Resource Group %q): %+v", name, resourceGroup, err)
}
d.SetId(*resp.ID)

return resourceArmHPCCacheRead(d, meta)
}

func resourceArmHPCCacheRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Storage.CachesClient
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
name := id.Path["caches"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are starting to write Parse ID functions for all resources, you can see an example here: #5779


resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] HPC Cache %q was not found in Resource Group %q - removing from state!", name, resourceGroup)
d.SetId("")
return nil
}

return fmt.Errorf("Error retrieving HPC Cache %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
d.Set("location", resp.Location)

if props := resp.CacheProperties; props != nil {
d.Set("cache_size", props.CacheSizeGB)
d.Set("subnet_id", props.Subnet)
}

if sku := resp.Sku; sku != nil {
d.Set("sku_name", sku.Name)
}

return nil
}

func resourceArmHPCCacheDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Storage.CachesClient
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
name := id.Path["caches"]

future, err := client.Delete(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error deleting HPC Cache %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for deletion of HPC Cache %q (Resource Group %q): %+v", name, resourceGroup, err)
}

return nil
}
Loading