-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
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 a9c830b
run go mod vendor
aqche 75004c2
formatting fixes for resource_arm_hpc_cache_test.go
aqche 036dbea
more resource_arm_hpc_cache_test.go formatting
aqche a58691f
wip: create docs for azurerm_hpc_cache
aqche cd0d8cd
add Storage Cache to allowed-subcategories
aqche 98559fb
Merge branch 'master' of https://github.com/terraform-providers/terra…
aqche e283c73
hpc cache: cache size valudation, sku name arg, wait for provisioning…
aqche b765f66
whitespace
aqche 205492c
whitespace
aqche d795f90
remove tags
aqche e77d488
Merge branch 'master' of https://github.com/terraform-providers/terra…
aqche c32a6e3
Merge branch 'master' of https://github.com/terraform-providers/terra…
aqche 6edea9f
Apply suggestions from code review
aqche c6df54b
Merge branch 'master' of https://github.com/terraform-providers/terra…
aqche 22b634b
Merge branch 'azurerm_hpc_cache' of github.com:aqche/terraform-provid…
aqche 7bbbaba
apply review suggestions
aqche bfbfa64
small fixes when moving hpc resource
aqche 7f5ff1b
tests fix
aqche 5035ba1
Merge branch 'master' of https://github.com/terraform-providers/terra…
aqche 324f369
merge conflicts
aqche 5c7f4cb
Merge branch 'master' into azurerm_hpc_cache
aqche df8d3a6
Merge branch 'master' of https://github.com/terraform-providers/terra…
aqche 19cce38
use future.WaitForCompletionRef() for hpc creation
aqche 75547ea
Apply suggestions from code review
aqche 35a5a6b
address review
aqche a7f9030
fmt
aqche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
73
azurerm/internal/services/storage/parsers/hpc_cache_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
azurerm/internal/services/storage/resource_arm_hpc_cache.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@katbyte i've updated the
RequiredResourceProviders
func here. let me know if this is correct. thanks!