-
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: azurerm_storage_account_network_rules
#5082
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c4cec07
Start of storage account network rule set
mbfrahry fa0f4ab
Finalizing storage account network rules
mbfrahry 779d9d8
Docs changes
mbfrahry 7e8925d
Addressing review
mbfrahry 8b6dc05
goimports
mbfrahry e5a7fb4
Fmt
mbfrahry 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,297 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-04-01/storage" | ||
"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/features" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/locks" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
var storageAccountResourceName = "azurerm_storage_account" | ||
|
||
func resourceArmStorageAccountNetworkRules() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmStorageAccountNetworkRulesCreateUpdate, | ||
Read: resourceArmStorageAccountNetworkRulesRead, | ||
Update: resourceArmStorageAccountNetworkRulesCreateUpdate, | ||
Delete: resourceArmStorageAccountNetworkRulesDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(60 * time.Minute), | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
Update: schema.DefaultTimeout(60 * time.Minute), | ||
Delete: schema.DefaultTimeout(60 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"resource_group_name": azure.SchemaResourceGroupName(), | ||
|
||
"storage_account_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateArmStorageAccountName, | ||
}, | ||
|
||
"bypass": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Computed: true, | ||
ConfigMode: schema.SchemaConfigModeAttr, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
string(storage.AzureServices), | ||
string(storage.Logging), | ||
string(storage.Metrics), | ||
string(storage.None), | ||
}, false), | ||
}, | ||
Set: schema.HashString, | ||
}, | ||
|
||
"ip_rules": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Computed: true, | ||
ConfigMode: schema.SchemaConfigModeAttr, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
ValidateFunc: validate.IPv4Address, | ||
}, | ||
Set: schema.HashString, | ||
}, | ||
|
||
"virtual_network_subnet_ids": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Computed: true, | ||
ConfigMode: schema.SchemaConfigModeAttr, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
ValidateFunc: azure.ValidateResourceID, | ||
}, | ||
Set: schema.HashString, | ||
}, | ||
|
||
"default_action": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
string(storage.DefaultActionAllow), | ||
string(storage.DefaultActionDeny), | ||
}, false), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmStorageAccountNetworkRulesCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).Storage.AccountsClient | ||
ctx, cancel := timeouts.ForCreateUpdate(meta.(*ArmClient).StopContext, d) | ||
defer cancel() | ||
|
||
storageAccountName := d.Get("storage_account_name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
|
||
locks.ByName(storageAccountName, storageAccountResourceName) | ||
defer locks.UnlockByName(storageAccountName, storageAccountResourceName) | ||
|
||
storageAccount, err := client.GetProperties(ctx, resourceGroup, storageAccountName, "") | ||
if err != nil { | ||
if utils.ResponseWasNotFound(storageAccount.Response) { | ||
return fmt.Errorf("Storage Account %q (Resource Group %q) was not found", storageAccountName, resourceGroup) | ||
} | ||
|
||
return fmt.Errorf("Error loading Storage Account %q (Resource Group %q): %+v", storageAccountName, resourceGroup, err) | ||
} | ||
|
||
if features.ShouldResourcesBeImported() { | ||
if checkForNonDefaultStorageAccountNetworkRule(storageAccount.NetworkRuleSet) { | ||
return tf.ImportAsExistsError("azurerm_storage_account_network_rule", *storageAccount.ID) | ||
} | ||
} | ||
|
||
rules := storageAccount.NetworkRuleSet | ||
if rules == nil { | ||
rules = &storage.NetworkRuleSet{} | ||
} | ||
|
||
rules.DefaultAction = storage.DefaultAction(d.Get("default_action").(string)) | ||
|
||
if v, ok := d.GetOk("bypass"); ok { | ||
rules.Bypass = expandStorageAccountNetworkRuleBypass(v.(*schema.Set).List()) | ||
} | ||
|
||
if v, ok := d.GetOk("ip_rules"); ok { | ||
rules.IPRules = expandStorageAccountNetworkRuleIpRules(v.(*schema.Set).List()) | ||
} | ||
|
||
if v, ok := d.GetOk("virtual_network_subnet_ids"); ok { | ||
rules.VirtualNetworkRules = expandStorageAccountNetworkRuleVirtualRules(v.(*schema.Set).List()) | ||
} | ||
|
||
opts := storage.AccountUpdateParameters{ | ||
AccountPropertiesUpdateParameters: &storage.AccountPropertiesUpdateParameters{ | ||
NetworkRuleSet: rules, | ||
}, | ||
} | ||
|
||
if _, err := client.Update(ctx, resourceGroup, storageAccountName, opts); err != nil { | ||
return fmt.Errorf("Error updating Azure Storage Account Network Rules %q (Resource Group %q): %+v", storageAccountName, resourceGroup, err) | ||
} | ||
|
||
d.SetId(*storageAccount.ID) | ||
|
||
return resourceArmStorageAccountNetworkRulesRead(d, meta) | ||
} | ||
|
||
func resourceArmStorageAccountNetworkRulesRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).Storage.AccountsClient | ||
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := azure.ParseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resourceGroup := id.ResourceGroup | ||
storageAccountName := id.Path["storageAccounts"] | ||
|
||
storageAccount, err := client.GetProperties(ctx, resourceGroup, storageAccountName, "") | ||
if err != nil { | ||
return fmt.Errorf("Error reading Storage Account Network Rules %q (Resource Group %q): %+v", storageAccountName, resourceGroup, err) | ||
} | ||
|
||
d.Set("storage_account_name", storageAccountName) | ||
d.Set("resource_group_name", resourceGroup) | ||
|
||
if rules := storageAccount.NetworkRuleSet; rules != nil { | ||
if err := d.Set("ip_rules", schema.NewSet(schema.HashString, flattenStorageAccountIPRules(rules.IPRules))); err != nil { | ||
return fmt.Errorf("Error setting `ip_rules`: %+v", err) | ||
} | ||
if err := d.Set("virtual_network_subnet_ids", schema.NewSet(schema.HashString, flattenStorageAccountVirtualNetworks(rules.VirtualNetworkRules))); err != nil { | ||
return fmt.Errorf("Error setting `virtual_network_subnet_ids`: %+v", err) | ||
} | ||
if err := d.Set("bypass", schema.NewSet(schema.HashString, flattenStorageAccountBypass(rules.Bypass))); err != nil { | ||
return fmt.Errorf("Error setting `bypass`: %+v", err) | ||
} | ||
d.Set("default_action", string(rules.DefaultAction)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmStorageAccountNetworkRulesDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).Storage.AccountsClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*ArmClient).StopContext, d) | ||
defer cancel() | ||
|
||
parsedStorageAccountNetworkRuleId, err := azure.ParseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resourceGroup := parsedStorageAccountNetworkRuleId.ResourceGroup | ||
storageAccountName := parsedStorageAccountNetworkRuleId.Path["storageAccounts"] | ||
|
||
locks.ByName(storageAccountName, storageAccountResourceName) | ||
defer locks.UnlockByName(storageAccountName, storageAccountResourceName) | ||
|
||
storageAccount, err := client.GetProperties(ctx, resourceGroup, storageAccountName, "") | ||
if err != nil { | ||
if utils.ResponseWasNotFound(storageAccount.Response) { | ||
return fmt.Errorf("Storage Account %q (Resource Group %q) was not found", storageAccountName, resourceGroup) | ||
} | ||
|
||
return fmt.Errorf("Error loading Storage Account %q (Resource Group %q): %+v", storageAccountName, resourceGroup, err) | ||
} | ||
|
||
if storageAccount.NetworkRuleSet == nil { | ||
return nil | ||
} | ||
|
||
// We can't delete a network rule set so we'll just update it back to the default instead | ||
opts := storage.AccountUpdateParameters{ | ||
AccountPropertiesUpdateParameters: &storage.AccountPropertiesUpdateParameters{ | ||
NetworkRuleSet: &storage.NetworkRuleSet{ | ||
Bypass: storage.AzureServices, | ||
DefaultAction: storage.DefaultActionDeny, | ||
}, | ||
}, | ||
} | ||
|
||
if _, err := client.Update(ctx, resourceGroup, storageAccountName, opts); err != nil { | ||
return fmt.Errorf("Error deleting Azure Storage Account Network Rule %q (Resource Group %q): %+v", storageAccountName, resourceGroup, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// To make sure that someone isn't overriding their existing network rules, we'll check for a non default network rule | ||
func checkForNonDefaultStorageAccountNetworkRule(rule *storage.NetworkRuleSet) bool { | ||
if rule == nil { | ||
return false | ||
} | ||
|
||
if rule.IPRules != nil || len(*rule.IPRules) != 0 || | ||
rule.VirtualNetworkRules != nil || len(*rule.VirtualNetworkRules) == 0 || | ||
rule.Bypass != "AzureServices" || rule.DefaultAction != "Allow" { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
func expandStorageAccountNetworkRuleBypass(bypass []interface{}) storage.Bypass { | ||
var bypassValues []string | ||
for _, bypassConfig := range bypass { | ||
bypassValues = append(bypassValues, bypassConfig.(string)) | ||
} | ||
|
||
return storage.Bypass(strings.Join(bypassValues, ", ")) | ||
} | ||
|
||
func expandStorageAccountNetworkRuleIpRules(ipRulesInfo []interface{}) *[]storage.IPRule { | ||
ipRules := make([]storage.IPRule, len(ipRulesInfo)) | ||
|
||
for i, ipRuleConfig := range ipRulesInfo { | ||
attrs := ipRuleConfig.(string) | ||
ipRule := storage.IPRule{ | ||
IPAddressOrRange: utils.String(attrs), | ||
Action: storage.Allow, | ||
} | ||
ipRules[i] = ipRule | ||
} | ||
|
||
return &ipRules | ||
} | ||
|
||
func expandStorageAccountNetworkRuleVirtualRules(virtualNetworkInfo []interface{}) *[]storage.VirtualNetworkRule { | ||
virtualNetworks := make([]storage.VirtualNetworkRule, len(virtualNetworkInfo)) | ||
|
||
for i, virtualNetworkConfig := range virtualNetworkInfo { | ||
attrs := virtualNetworkConfig.(string) | ||
virtualNetwork := storage.VirtualNetworkRule{ | ||
VirtualNetworkResourceID: utils.String(attrs), | ||
Action: storage.Allow, | ||
} | ||
virtualNetworks[i] = virtualNetwork | ||
} | ||
|
||
return &virtualNetworks | ||
} |
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.
I think this wants to be Allow?