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 all 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
31 changes: 31 additions & 0 deletions azurerm/internal/services/storage/parsers/hpc_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package parsers

import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type HPCCacheID struct {
Name string
ResourceGroup string
}

func ParseHPCCacheID(input string) (*HPCCacheID, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

cache := HPCCacheID{
ResourceGroup: id.ResourceGroup,
}

if cache.Name, err = id.PopSegment("caches"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &cache, nil
}
73 changes: 73 additions & 0 deletions azurerm/internal/services/storage/parsers/hpc_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package parsers

import (
"testing"
)

func TestParseHPCCacheID(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *HPCCacheID
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Expected: nil,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Expected: nil,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Expected: nil,
},
{
Name: "Missing Cache Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.StorageCache/caches/",
Expected: nil,
},
{
Name: "Cache ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.StorageCache/caches/mycache1",
Expected: &HPCCacheID{
ResourceGroup: "resGroup1",
Name: "mycache1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.StorageCache/CACHES/mycache1",
Expected: nil,
},
}

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

actual, err := ParseHPCCacheID(v.Input)
if err != nil {
if v.Expected == nil {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup)
}
}
}
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
201 changes: 201 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,201 @@
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/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/services/storage/parsers"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"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,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},
Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parsers.ParseHPCCacheID(id)
return err
}),

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_in_gb": {
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_in_gb").(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),
},
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, name, cache)
if err != nil {
return fmt.Errorf("Error creating 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 HPC Cache %q (Resource Group %q) to finish provisioning: %+v", name, resourceGroup, 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 := parsers.ParseHPCCacheID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.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!", id.Name, id.ResourceGroup)
d.SetId("")
return nil
}

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

d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("location", resp.Location)

if props := resp.CacheProperties; props != nil {
d.Set("cache_size_in_gb", 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 := parsers.ParseHPCCacheID(d.Id())
if err != nil {
return err
}

future, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("Error deleting HPC Cache %q (Resource Group %q): %+v", id.Name, id.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", id.Name, id.ResourceGroup, err)
}

return nil
}
Loading