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 resources: azurerm_data_lake_analytics_account & azurerm_data_lake_analytics_firewall_rule #1618

Merged
merged 4 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
49 changes: 31 additions & 18 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
"github.com/Azure/azure-sdk-for-go/services/containerregistry/mgmt/2017-10-01/containerregistry"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2018-03-31/containerservice"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2015-04-08/documentdb"
"github.com/Azure/azure-sdk-for-go/services/datalake/store/mgmt/2016-11-01/account"
analyticsAccount "github.com/Azure/azure-sdk-for-go/services/datalake/analytics/mgmt/2016-11-01/account"
storeAccount "github.com/Azure/azure-sdk-for-go/services/datalake/store/mgmt/2016-11-01/account"
"github.com/Azure/azure-sdk-for-go/services/eventgrid/mgmt/2018-01-01/eventgrid"
"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
Expand Down Expand Up @@ -143,8 +144,12 @@ type ArmClient struct {
sqlVirtualNetworkRulesClient sql.VirtualNetworkRulesClient

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

// Data Lake Store
dataLakeAnalyticsAccountClient analyticsAccount.AccountsClient
dataLakeAnalyticsFirewallRulesClient analyticsAccount.FirewallRulesClient

// KeyVault
keyVaultClient keyvault.VaultsClient
Expand Down Expand Up @@ -664,13 +669,21 @@ func (c *ArmClient) registerDatabases(endpoint, subscriptionId string, auth auto
}

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
storeAccountClient := storeAccount.NewAccountsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&storeAccountClient.Client, auth)
c.dataLakeStoreAccountClient = storeAccountClient

storeFirewallRulesClient := storeAccount.NewFirewallRulesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&storeFirewallRulesClient.Client, auth)
c.dataLakeStoreFirewallRulesClient = storeFirewallRulesClient

analyticsAccountClient := analyticsAccount.NewAccountsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&analyticsAccountClient.Client, auth)
c.dataLakeAnalyticsAccountClient = analyticsAccountClient

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

func (c *ArmClient) registerDeviceClients(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) {
Expand Down Expand Up @@ -1006,22 +1019,22 @@ func (armClient *ArmClient) getKeyForStorageAccount(ctx context.Context, resourc
}
if err != nil {
// We assume this is a transient error rather than a 404 (which is caught above), so assume the
// account still exists.
return "", true, fmt.Errorf("Error retrieving keys for storage account %q: %s", storageAccountName, err)
// storeAccount still exists.
return "", true, fmt.Errorf("Error retrieving keys for storage storeAccount %q: %s", storageAccountName, err)
}

if accountKeys.Keys == nil {
return "", false, fmt.Errorf("Nil key returned for storage account %q", storageAccountName)
return "", false, fmt.Errorf("Nil key returned for storage storeAccount %q", storageAccountName)
}

keys := *accountKeys.Keys
if len(keys) <= 0 {
return "", false, fmt.Errorf("No keys returned for storage account %q", storageAccountName)
return "", false, fmt.Errorf("No keys returned for storage storeAccount %q", storageAccountName)
}

keyPtr := keys[0].Value
if keyPtr == nil {
return "", false, fmt.Errorf("The first key returned is nil for storage account %q", storageAccountName)
return "", false, fmt.Errorf("The first key returned is nil for storage storeAccount %q", storageAccountName)
}

key = *keyPtr
Expand All @@ -1043,7 +1056,7 @@ func (armClient *ArmClient) getBlobStorageClientForStorageAccount(ctx context.Co
storageClient, err := mainStorage.NewClient(storageAccountName, key, armClient.environment.StorageEndpointSuffix,
mainStorage.DefaultAPIVersion, true)
if err != nil {
return nil, true, fmt.Errorf("Error creating storage client for storage account %q: %s", storageAccountName, err)
return nil, true, fmt.Errorf("Error creating storage client for storage storeAccount %q: %s", storageAccountName, err)
}

blobClient := storageClient.GetBlobService()
Expand All @@ -1062,7 +1075,7 @@ func (armClient *ArmClient) getFileServiceClientForStorageAccount(ctx context.Co
storageClient, err := mainStorage.NewClient(storageAccountName, key, armClient.environment.StorageEndpointSuffix,
mainStorage.DefaultAPIVersion, true)
if err != nil {
return nil, true, fmt.Errorf("Error creating storage client for storage account %q: %s", storageAccountName, err)
return nil, true, fmt.Errorf("Error creating storage client for storage storeAccount %q: %s", storageAccountName, err)
}

fileClient := storageClient.GetFileService()
Expand All @@ -1081,7 +1094,7 @@ func (armClient *ArmClient) getTableServiceClientForStorageAccount(ctx context.C
storageClient, err := mainStorage.NewClient(storageAccountName, key, armClient.environment.StorageEndpointSuffix,
mainStorage.DefaultAPIVersion, true)
if err != nil {
return nil, true, fmt.Errorf("Error creating storage client for storage account %q: %s", storageAccountName, err)
return nil, true, fmt.Errorf("Error creating storage client for storage storeAccount %q: %s", storageAccountName, err)
}

tableClient := storageClient.GetTableService()
Expand All @@ -1100,7 +1113,7 @@ func (armClient *ArmClient) getQueueServiceClientForStorageAccount(ctx context.C
storageClient, err := mainStorage.NewClient(storageAccountName, key, armClient.environment.StorageEndpointSuffix,
mainStorage.DefaultAPIVersion, true)
if err != nil {
return nil, true, fmt.Errorf("Error creating storage client for storage account %q: %s", storageAccountName, err)
return nil, true, fmt.Errorf("Error creating storage client for storage storeAccount %q: %s", storageAccountName, err)
}

queueClient := storageClient.GetQueueService()
Expand Down
23 changes: 23 additions & 0 deletions azurerm/helpers/azure/datalake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package azure

import (
"regexp"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

//store and analytic account names are the same
func ValidateDataLakeAccountName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile(`\A([a-z0-9]{3,24})\z`),
"Name can only consist of lowercase letters and numbers and must be between 3 and 24 characters long",
)
}

func ValidateDataLakeFirewallRuleName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile(`\A([-_a-zA-Z0-9]{3,50})\z`),
"Name can only consist of letters, numbers, underscores and hyphens and must be between 3 and 50 characters long",
)
}
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ func Provider() terraform.ResourceProvider {
"azurerm_container_service": resourceArmContainerService(),
"azurerm_container_group": resourceArmContainerGroup(),
"azurerm_cosmosdb_account": resourceArmCosmosDBAccount(),
"azurerm_data_lake_analytics_account": resourceArmDataLakeAnalyticsAccount(),
"azurerm_data_lake_analytics_firewall_rule": resourceArmDataLakeAnalyticsFirewallRule(),
"azurerm_data_lake_store": resourceArmDataLakeStore(),
"azurerm_data_lake_store_firewall_rule": resourceArmDataLakeStoreFirewallRule(),
"azurerm_dns_a_record": resourceArmDnsARecord(),
Expand Down
222 changes: 222 additions & 0 deletions azurerm/resource_arm_data_lake_analytics_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
package azurerm

import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/services/datalake/analytics/mgmt/2016-11-01/account"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceArmDataLakeAnalyticsAccount() *schema.Resource {
return &schema.Resource{
Create: resourceArmDateLakeAnalyticsAccountCreate,
Read: resourceArmDateLakeAnalyticsAccountRead,
Update: resourceArmDateLakeAnalyticsAccountUpdate,
Delete: resourceArmDateLakeAnalyticsAccountDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateDataLakeAccountName(),
},

"location": locationSchema(),

"resource_group_name": resourceGroupNameSchema(),

"tier": {
Type: schema.TypeString,
Optional: true,
Default: string(account.Consumption),
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice([]string{
string(account.Consumption),
string(account.Commitment100000AUHours),
string(account.Commitment10000AUHours),
string(account.Commitment1000AUHours),
string(account.Commitment100AUHours),
string(account.Commitment500000AUHours),
string(account.Commitment50000AUHours),
string(account.Commitment5000AUHours),
string(account.Commitment500AUHours),
}, true),
},

"default_store_account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateDataLakeAccountName(),
},

"tags": tagsSchema(),
},
}
}

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

name := d.Get("name").(string)
location := azureRMNormalizeLocation(d.Get("location").(string))
resourceGroup := d.Get("resource_group_name").(string)
storeAccountName := d.Get("default_store_account_name").(string)
tier := d.Get("tier").(string)
tags := d.Get("tags").(map[string]interface{})

log.Printf("[INFO] preparing arguments for Azure ARM Date Lake Store creation %q (Resource Group %q)", name, resourceGroup)

dateLakeAnalyticsAccount := account.CreateDataLakeAnalyticsAccountParameters{
Location: &location,
Tags: expandTags(tags),
CreateDataLakeAnalyticsAccountProperties: &account.CreateDataLakeAnalyticsAccountProperties{
NewTier: account.TierType(tier),
DefaultDataLakeStoreAccount: &storeAccountName,
DataLakeStoreAccounts: &[]account.AddDataLakeStoreWithAccountParameters{
{
Name: &storeAccountName,
},
},
},
}

future, err := client.Create(ctx, resourceGroup, name, dateLakeAnalyticsAccount)
if err != nil {
return fmt.Errorf("Error issuing create request for Data Lake Analytics Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

err = future.WaitForCompletion(ctx, client.Client)
if err != nil {
return fmt.Errorf("Error creating Data Lake Analytics Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error retrieving Data Lake Analytics Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read Data Lake Analytics Account %s (resource group %s) ID", name, resourceGroup)
}

d.SetId(*read.ID)

return resourceArmDateLakeAnalyticsAccountRead(d, meta)
}

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

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
storeAccountName := d.Get("default_store_account_name").(string)
newTier := d.Get("tier").(string)
newTags := d.Get("tags").(map[string]interface{})

props := &account.UpdateDataLakeAnalyticsAccountParameters{
Tags: expandTags(newTags),
UpdateDataLakeAnalyticsAccountProperties: &account.UpdateDataLakeAnalyticsAccountProperties{
NewTier: account.TierType(newTier),
DataLakeStoreAccounts: &[]account.UpdateDataLakeStoreWithAccountParameters{
{
Name: &storeAccountName,
},
},
},
}

future, err := client.Update(ctx, resourceGroup, name, props)
if err != nil {
return fmt.Errorf("Error issuing update request for Data Lake Analytics Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

err = future.WaitForCompletion(ctx, client.Client)
if err != nil {
return fmt.Errorf("Error waiting for the update of Data Lake Analytics Account %q (Resource Group %q) to commplete: %+v", name, resourceGroup, err)
}

return resourceArmDateLakeAnalyticsAccountRead(d, meta)
}

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

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

resourceGroup := id.ResourceGroup
name := id.Path["accounts"]

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

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}

if properties := resp.DataLakeAnalyticsAccountProperties; properties != nil {
d.Set("tier", string(properties.CurrentTier))
d.Set("default_store_account_name", properties.DefaultDataLakeStoreAccount)
}

flattenAndSetTags(d, resp.Tags)

return nil
}

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

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

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

err = future.WaitForCompletion(ctx, client.Client)
if err != nil {
if response.WasNotFound(future.Response()) {
return nil
}
return fmt.Errorf("Error deleting Data Lake Analytics Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

return nil
}
Loading