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

Azure batch expose keys and account url #3071

Merged
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
27 changes: 27 additions & 0 deletions azurerm/data_source_batch_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package azurerm
import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2017-09-01/batch"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand All @@ -28,6 +29,20 @@ func dataSourceArmBatchAccount() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"primary_access_key": {
Type: schema.TypeString,
Sensitive: true,
Computed: true,
},
"secondary_access_key": {
Type: schema.TypeString,
Sensitive: true,
Computed: true,
},
"account_endpoint": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsForDataSourceSchema(),
},
}
Expand All @@ -52,6 +67,7 @@ func dataSourceArmBatchAccountRead(d *schema.ResourceData, meta interface{}) err

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
d.Set("account_endpoint", resp.AccountEndpoint)

if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
Expand All @@ -64,6 +80,17 @@ func dataSourceArmBatchAccountRead(d *schema.ResourceData, meta interface{}) err
d.Set("pool_allocation_mode", props.PoolAllocationMode)
}

if d.Get("pool_allocation_mode").(string) == string(batch.BatchService) {
keys, err := client.GetKeys(ctx, resourceGroup, name)

if err != nil {
return fmt.Errorf("Cannot read keys for Batch account %q (resource group %q): %v", name, resourceGroup, err)
}

d.Set("primary_access_key", keys.Primary)
d.Set("secondary_access_key", keys.Secondary)
}

flattenAndSetTags(d, resp.Tags)

return nil
Expand Down
72 changes: 49 additions & 23 deletions azurerm/resource_arm_batch_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ func resourceArmBatchAccount() *schema.Resource {
string(batch.UserSubscription),
}, false),
},
"primary_access_key": {
Type: schema.TypeString,
Sensitive: true,
Computed: true,
},
"secondary_access_key": {
Type: schema.TypeString,
Sensitive: true,
Computed: true,
},
"account_endpoint": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
},
}
Expand All @@ -59,18 +73,18 @@ func resourceArmBatchAccountCreate(d *schema.ResourceData, meta interface{}) err

log.Printf("[INFO] preparing arguments for Azure Batch account creation.")

resourceGroupName := d.Get("resource_group_name").(string)
resourceGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)
location := azureRMNormalizeLocation(d.Get("location").(string))
storageAccountId := d.Get("storage_account_id").(string)
poolAllocationMode := d.Get("pool_allocation_mode").(string)
tags := d.Get("tags").(map[string]interface{})

if requireResourcesToBeImported && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroupName, name)
existing, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Batch Account %q (Resource Group %q): %s", name, resourceGroupName, err)
return fmt.Errorf("Error checking for presence of existing Batch Account %q (Resource Group %q): %s", name, resourceGroup, err)
}
}

Expand All @@ -93,22 +107,22 @@ func resourceArmBatchAccountCreate(d *schema.ResourceData, meta interface{}) err
}
}

future, err := client.Create(ctx, resourceGroupName, name, parameters)
future, err := client.Create(ctx, resourceGroup, name, parameters)
if err != nil {
return fmt.Errorf("Error creating Batch account %q (Resource Group %q): %+v", name, resourceGroupName, err)
return fmt.Errorf("Error creating Batch account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for creation of Batch account %q (Resource Group %q): %+v", name, resourceGroupName, err)
return fmt.Errorf("Error waiting for creation of Batch account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

read, err := client.Get(ctx, resourceGroupName, name)
read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error retrieving Batch account %q (Resource Group %q): %+v", name, resourceGroupName, err)
return fmt.Errorf("Error retrieving Batch account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if read.ID == nil {
return fmt.Errorf("Cannot read Batch account %q (resource group %q) ID", name, resourceGroupName)
return fmt.Errorf("Cannot read Batch account %q (resource group %q) ID", name, resourceGroup)
}

d.SetId(*read.ID)
Expand All @@ -125,20 +139,21 @@ func resourceArmBatchAccountRead(d *schema.ResourceData, meta interface{}) error
return err
}
name := id.Path["batchAccounts"]
resourceGroupName := id.ResourceGroup
resourceGroup := id.ResourceGroup

resp, err := client.Get(ctx, resourceGroupName, name)
resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
log.Printf("[DEBUG] Batch Account %q was not found in Resource Group %q - removing from state!", name, resourceGroupName)
log.Printf("[DEBUG] Batch Account %q was not found in Resource Group %q - removing from state!", name, resourceGroup)
return nil
}
return fmt.Errorf("Error reading the state of Batch account %q: %+v", name, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroupName)
d.Set("resource_group_name", resourceGroup)
d.Set("account_endpoint", resp.AccountEndpoint)

if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
Expand All @@ -151,6 +166,17 @@ func resourceArmBatchAccountRead(d *schema.ResourceData, meta interface{}) error
d.Set("pool_allocation_mode", props.PoolAllocationMode)
}

if d.Get("pool_allocation_mode").(string) == string(batch.BatchService) {
keys, err := client.GetKeys(ctx, resourceGroup, name)

if err != nil {
return fmt.Errorf("Cannot read keys for Batch account %q (resource group %q): %v", name, resourceGroup, err)
}

d.Set("primary_access_key", keys.Primary)
d.Set("secondary_access_key", keys.Secondary)
}

flattenAndSetTags(d, resp.Tags)

return nil
Expand All @@ -167,7 +193,7 @@ func resourceArmBatchAccountUpdate(d *schema.ResourceData, meta interface{}) err
return err
}
name := id.Path["batchAccounts"]
resourceGroupName := id.ResourceGroup
resourceGroup := id.ResourceGroup

storageAccountId := d.Get("storage_account_id").(string)
tags := d.Get("tags").(map[string]interface{})
Expand All @@ -181,17 +207,17 @@ func resourceArmBatchAccountUpdate(d *schema.ResourceData, meta interface{}) err
Tags: expandTags(tags),
}

if _, err = client.Update(ctx, resourceGroupName, name, parameters); err != nil {
return fmt.Errorf("Error updating Batch account %q (Resource Group %q): %+v", name, resourceGroupName, err)
if _, err = client.Update(ctx, resourceGroup, name, parameters); err != nil {
return fmt.Errorf("Error updating Batch account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

read, err := client.Get(ctx, resourceGroupName, name)
read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error retrieving Batch account %q (Resource Group %q): %+v", name, resourceGroupName, err)
return fmt.Errorf("Error retrieving Batch account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if read.ID == nil {
return fmt.Errorf("Cannot read Batch account %q (resource group %q) ID", name, resourceGroupName)
return fmt.Errorf("Cannot read Batch account %q (resource group %q) ID", name, resourceGroup)
}

d.SetId(*read.ID)
Expand All @@ -208,16 +234,16 @@ func resourceArmBatchAccountDelete(d *schema.ResourceData, meta interface{}) err
return err
}
name := id.Path["batchAccounts"]
resourceGroupName := id.ResourceGroup
resourceGroup := id.ResourceGroup

future, err := client.Delete(ctx, resourceGroupName, name)
future, err := client.Delete(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error deleting Batch account %q (Resource Group %q): %+v", name, resourceGroupName, err)
return fmt.Errorf("Error deleting Batch account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
if !response.WasNotFound(future.Response()) {
return fmt.Errorf("Error waiting for deletion of Batch account %q (Resource Group %q): %+v", name, resourceGroupName, err)
return fmt.Errorf("Error waiting for deletion of Batch account %q (Resource Group %q): %+v", name, resourceGroup, err)
}
}

Expand Down
6 changes: 3 additions & 3 deletions azurerm/resource_arm_eventhub_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func TestAccAzureRMEventHubNamespace_BasicWithTagsUpdate(t *testing.T) {
func TestAccAzureRMEventHubNamespace_BasicWithCapacity(t *testing.T) {
resourceName := "azurerm_eventhub_namespace.test"
ri := tf.AccRandTimeInt()
config := testAccAzureRMEventHubNamespace_capacity(ri, testLocation(), 20)
config := testAccAzureRMEventHubNamespace_capacity(ri, testLocation(), 100)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -238,7 +238,7 @@ func TestAccAzureRMEventHubNamespace_BasicWithCapacity(t *testing.T) {
func TestAccAzureRMEventHubNamespace_BasicWithCapacityUpdate(t *testing.T) {
resourceName := "azurerm_eventhub_namespace.test"
ri := tf.AccRandTimeInt()
preConfig := testAccAzureRMEventHubNamespace_capacity(ri, testLocation(), 20)
preConfig := testAccAzureRMEventHubNamespace_capacity(ri, testLocation(), 100)
postConfig := testAccAzureRMEventHubNamespace_capacity(ri, testLocation(), 2)

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -494,7 +494,7 @@ resource "azurerm_eventhub_namespace" "test" {
sku = "Standard"
capacity = "2"
auto_inflate_enabled = true
maximum_throughput_units = 20
maximum_throughput_units = 100
}
`, rInt, location, rInt)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway v1.6.3 // indirect
github.com/hashicorp/go-azure-helpers v0.0.0-20181211121309-38db96513363
github.com/hashicorp/go-cleanhttp v0.0.0-20170211013415-3573b8b52aa7 // indirect
github.com/hashicorp/go-getter v0.0.0-20180226183729-64040d90d4ab // indirect
github.com/hashicorp/go-getter v0.0.0-20180226183729-64040d90d4ab
github.com/hashicorp/go-hclog v0.0.0-20170903163258-8105cc0a3736 // indirect
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/go-plugin v0.0.0-20170816151819-a5174f84d7f8 // indirect
Expand Down
8 changes: 8 additions & 0 deletions website/docs/d/batch_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ The following attributes are exported:

* `storage_account_id` - The ID of the Storage Account used for this Batch account.

* `primary_access_key` - The Batch account primary access key.

* `secondary_access_key` - The Batch account secondary access key.

* `account_endpoint` - The account endpoint used to interact with the Batch service.

* `tags` - A map of tags assigned to the Batch account.

~> **NOTE:** Primary and secondary access keys are only available when `pool_allocation_mode` is set to `BatchService`. See [documentation](https://docs.microsoft.com/en-us/azure/batch/batch-api-basics) for more information.
8 changes: 8 additions & 0 deletions website/docs/r/batch_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,11 @@ The following arguments are supported:
The following attributes are exported:

* `id` - The Batch account ID.

* `primary_access_key` - The Batch account primary access key.

* `secondary_access_key` - The Batch account secondary access key.

* `account_endpoint` - The account endpoint used to interact with the Batch service.

~> **NOTE:** Primary and secondary access keys are only available when `pool_allocation_mode` is set to `BatchService`. See [documentation](https://docs.microsoft.com/en-us/azure/batch/batch-api-basics) for more information.