Skip to content

Commit

Permalink
New Resource: azurerm_data_lake_firewall_rule
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Jul 5, 2018
1 parent 9c1edb0 commit fc73946
Show file tree
Hide file tree
Showing 9 changed files with 510 additions and 23 deletions.
17 changes: 11 additions & 6 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ type ArmClient struct {
sqlVirtualNetworkRulesClient sql.VirtualNetworkRulesClient

// Data Lake Store
dataLakeStoreAccountClient account.AccountsClient
dataLakeStoreAccountClient account.AccountsClient
dataLakeStoreFirewallRulesClient account.FirewallRulesClient

// KeyVault
keyVaultClient keyvault.VaultsClient
Expand Down Expand Up @@ -383,7 +384,7 @@ func getArmClient(c *authentication.Config) (*ArmClient, error) {
client.registerContainerServicesClients(endpoint, c.SubscriptionID, auth)
client.registerCosmosDBClients(endpoint, c.SubscriptionID, auth, sender)
client.registerDatabases(endpoint, c.SubscriptionID, auth, sender)
client.registerDataLakeStoreAccountClients(endpoint, c.SubscriptionID, auth, sender)
client.registerDataLakeStoreClients(endpoint, c.SubscriptionID, auth, sender)
client.registerDeviceClients(endpoint, c.SubscriptionID, auth, sender)
client.registerDNSClients(endpoint, c.SubscriptionID, auth, sender)
client.registerEventGridClients(endpoint, c.SubscriptionID, auth, sender)
Expand Down Expand Up @@ -643,10 +644,14 @@ func (c *ArmClient) registerDatabases(endpoint, subscriptionId string, auth auto
c.sqlVirtualNetworkRulesClient = sqlVNRClient
}

func (c *ArmClient) registerDataLakeStoreAccountClients(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) {
dataLakeStoreAccountClient := account.NewAccountsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&dataLakeStoreAccountClient.Client, auth)
c.dataLakeStoreAccountClient = dataLakeStoreAccountClient
func (c *ArmClient) registerDataLakeStoreClients(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) {
accountClient := account.NewAccountsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&accountClient.Client, auth)
c.dataLakeStoreAccountClient = accountClient

firewallRulesClient := account.NewFirewallRulesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&firewallRulesClient.Client, auth)
c.dataLakeStoreFirewallRulesClient = firewallRulesClient
}

func (c *ArmClient) registerDeviceClients(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) {
Expand Down
17 changes: 17 additions & 0 deletions azurerm/helpers/validate/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ func IPv4AddressOrEmpty(i interface{}, k string) (_ []string, errors []error) {
return validateIpv4Address(i, k, true)
}

func IPv4OrIPv6Address(i interface{}, k string) (_ []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return
}

ip := net.ParseIP(v)
four := ip.To4()
six := ip.To16()
if four == nil && six == nil {
errors = append(errors, fmt.Errorf("%q is not a valid IPv4 or IPv6 address: %q", k, v))
}

return
}

func validateIpv4Address(i interface{}, k string, allowEmpty bool) (_ []string, errors []error) {
v, ok := i.(string)
if !ok {
Expand Down
54 changes: 54 additions & 0 deletions azurerm/helpers/validate/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,60 @@ func TestIPv4AddressOrEmpty(t *testing.T) {
}
}

func TestIPv4OrIPv6(t *testing.T) {
cases := []struct {
IP string
Errors int
}{
{
IP: "",
Errors: 1,
},
{
IP: "0.0.0.0",
Errors: 0,
},
{
IP: "1.2.3.no",
Errors: 1,
},
{
IP: "text",
Errors: 1,
},
{
IP: "1.2.3.4",
Errors: 0,
},
{
IP: "12.34.43.21",
Errors: 0,
},
{
IP: "100.123.199.0",
Errors: 0,
},
{
IP: "255.255.255.255",
Errors: 0,
},
{
IP: "2001:db8:85a3:8d3:1319:8a2e:370:7348",
Errors: 0,
},
}

for _, tc := range cases {
t.Run(tc.IP, func(t *testing.T) {
_, errors := IPv4OrIPv6Address(tc.IP, "test")

if len(errors) != tc.Errors {
t.Fatalf("Expected IPv4OrIPv6Address to return %d error(s) not %d", len(errors), tc.Errors)
}
})
}
}

func TestMACAddress(t *testing.T) {
cases := []struct {
MAC string
Expand Down
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_container_group": resourceArmContainerGroup(),
"azurerm_cosmosdb_account": resourceArmCosmosDBAccount(),
"azurerm_data_lake_store": resourceArmDataLakeStore(),
"azurerm_data_lake_store_firewall_rule": resourceArmDataLakeStoreFirewallRule(),
"azurerm_dns_a_record": resourceArmDnsARecord(),
"azurerm_dns_aaaa_record": resourceArmDnsAAAARecord(),
"azurerm_dns_caa_record": resourceArmDnsCaaRecord(),
Expand Down
146 changes: 146 additions & 0 deletions azurerm/resource_arm_data_lake_store_firewall_rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package azurerm

import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/services/datalake/store/mgmt/2016-11-01/account"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmDataLakeStoreFirewallRule() *schema.Resource {
return &schema.Resource{
Create: resourceArmDateLakeStoreAccountFirewallRuleCreateUpdate,
Read: resourceArmDateLakeStoreAccountFirewallRuleRead,
Update: resourceArmDateLakeStoreAccountFirewallRuleCreateUpdate,
Delete: resourceArmDateLakeStoreAccountFirewallRuleDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

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

"account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"resource_group_name": resourceGroupNameSchema(),

"start_ip_address": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.IPv4OrIPv6Address,
},

"end_ip_address": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.IPv4OrIPv6Address,
},
},
}
}

func resourceArmDateLakeStoreAccountFirewallRuleCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).dataLakeStoreFirewallRulesClient
ctx := meta.(*ArmClient).StopContext

log.Printf("[INFO] preparing arguments for Date Lake Store Firewall Rule creation.")

name := d.Get("name").(string)
accountName := d.Get("account_name").(string)
resourceGroup := d.Get("resource_group_name").(string)
startIPAddress := d.Get("start_ip_address").(string)
endIPAddress := d.Get("end_ip_address").(string)

dateLakeStore := account.CreateOrUpdateFirewallRuleParameters{
CreateOrUpdateFirewallRuleProperties: &account.CreateOrUpdateFirewallRuleProperties{
StartIPAddress: utils.String(startIPAddress),
EndIPAddress: utils.String(endIPAddress),
},
}

_, err := client.CreateOrUpdate(ctx, resourceGroup, accountName, name, dateLakeStore)
if err != nil {
return fmt.Errorf("Error issuing create request for Data Lake Store %q (Resource Group %q): %+v", name, resourceGroup, err)
}

read, err := client.Get(ctx, resourceGroup, accountName, name)
if err != nil {
return fmt.Errorf("Error retrieving Data Lake Store Firewall Rule %q (Account %q / Resource Group %q): %+v", name, accountName, resourceGroup, err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read Data Lake Store %q (Account %q / Resource Group %q) ID", name, accountName, resourceGroup)
}

d.SetId(*read.ID)

return resourceArmDateLakeStoreAccountFirewallRuleRead(d, meta)
}

func resourceArmDateLakeStoreAccountFirewallRuleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).dataLakeStoreFirewallRulesClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
accountName := id.Path["accounts"]
name := id.Path["firewallRules"]

resp, err := client.Get(ctx, resourceGroup, accountName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[WARN] Data Lake Store Firewall Rule %q was not found (Account %q / Resource Group %q)", name, accountName, resourceGroup)
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Azure Data Lake Store Firewall Rule %q (Account %q / Resource Group %q): %+v", name, accountName, resourceGroup, err)
}

d.Set("name", name)
d.Set("account_name", accountName)
d.Set("resource_group_name", resourceGroup)

if props := resp.FirewallRuleProperties; props != nil {
d.Set("start_ip_address", props.StartIPAddress)
d.Set("end_ip_address", props.EndIPAddress)
}

return nil
}

func resourceArmDateLakeStoreAccountFirewallRuleDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).dataLakeStoreFirewallRulesClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}

resourceGroup := id.ResourceGroup
accountName := id.Path["accounts"]
name := id.Path["firewallRules"]
resp, err := client.Delete(ctx, resourceGroup, accountName, name)
if err != nil {
if response.WasNotFound(resp.Response) {
return nil
}
return fmt.Errorf("Error issuing delete request for Data Lake Store Firewall Rule %q (Account %q / Resource Group %q): %+v", name, accountName, resourceGroup, err)
}

return nil
}
Loading

0 comments on commit fc73946

Please sign in to comment.