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

Code generation from magic-module-specs#82 for Terraform #78

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
90 changes: 52 additions & 38 deletions azurerm/data_source_batch_account.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file at
// https://github.com/Azure/magic-module-specs
//
// ----------------------------------------------------------------------------

package azurerm

import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2018-12-01/batch"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand All @@ -17,81 +30,82 @@ func dataSourceArmBatchAccount() *schema.Resource {
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateAzureRMBatchAccountName,
},

"location": azure.SchemaLocationForDataSource(),

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
"location": azure.SchemaLocationForDataSource(),
"storage_account_id": {

"account_endpoint": {
Type: schema.TypeString,
Computed: true,
},

"key_vault_reference": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"url": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"pool_allocation_mode": {
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": {

"storage_account_id": {
Type: schema.TypeString,
Computed: true,
},

"tags": tagsForDataSourceSchema(),
},
}
}

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

resourceGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

ctx := meta.(*ArmClient).StopContext
resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Batch account %q (Resource Group %q) was not found", name, resourceGroup)
return fmt.Errorf("Error: Batch Account %q (Resource Group %q) was not found", name, resourceGroup)
}
return fmt.Errorf("Error making Read request on AzureRM Batch account %q: %+v", name, err)
return fmt.Errorf("Error reading Batch Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)

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

if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}

if props := resp.AccountProperties; props != nil {
if autoStorage := props.AutoStorage; autoStorage != nil {
d.Set("storage_account_id", autoStorage.StorageAccountID)
if properties := resp.AccountProperties; properties != nil {
d.Set("account_endpoint", properties.AccountEndpoint)
if err := d.Set("key_vault_reference", flattenArmBatchAccountKeyVaultReference(properties.KeyVaultReference)); err != nil {
return fmt.Errorf("Error setting `key_vault_reference`: %+v", 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("pool_allocation_mode", string(properties.PoolAllocationMode))
if autoStorage := properties.AutoStorage; autoStorage != nil {
d.Set("storage_account_id", autoStorage.StorageAccountID)
}

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

flattenAndSetTags(d, resp.Tags)

return nil
Expand Down
102 changes: 23 additions & 79 deletions azurerm/data_source_batch_account_test.go
Original file line number Diff line number Diff line change
@@ -1,114 +1,58 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file at
// https://github.com/Azure/magic-module-specs
//
// ----------------------------------------------------------------------------

package azurerm

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
)

func TestAccDataSourceAzureRMBatchAccount_basic(t *testing.T) {
dataSourceName := "data.azurerm_batch_account.test"
ri := tf.AccRandTimeInt()
rs := acctest.RandString(4)
location := testLocation()
config := testAccDataSourceAzureRMBatchAccount_basic(ri, rs, location)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "name", fmt.Sprintf("testaccbatch%s", rs)),
resource.TestCheckResourceAttr(dataSourceName, "location", azure.NormalizeLocation(location)),
resource.TestCheckResourceAttr(dataSourceName, "pool_allocation_mode", "BatchService"),
),
},
},
})
}

func TestAccDataSourceAzureRMBatchAccount_complete(t *testing.T) {
dataSourceName := "data.azurerm_batch_account.test"
ri := tf.AccRandTimeInt()
rs := acctest.RandString(4)
location := testLocation()
config := testAccDataSourceAzureRMBatchAccount_complete(ri, rs, location)
rs := strings.ToLower(acctest.RandString(11))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Config: testAccDataSourceBatchAccount_basic(ri, location, rs),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "name", fmt.Sprintf("testaccbatch%s", rs)),
resource.TestCheckResourceAttr(dataSourceName, "location", azure.NormalizeLocation(location)),
resource.TestCheckResourceAttr(dataSourceName, "pool_allocation_mode", "BatchService"),
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(dataSourceName, "tags.env", "test"),
resource.TestCheckResourceAttrSet(dataSourceName, "storage_account_id"),
),
},
},
})
}

func testAccDataSourceAzureRMBatchAccount_basic(rInt int, rString string, location string) string {
func testAccDataSourceBatchAccount_basic(rInt int, location string, rString string) string {
config := testAccAzureRMBatchAccount_basic(rInt, location, rString)
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "testaccRG-%d-batch"
location = "%s"
}

resource "azurerm_batch_account" "test" {
name = "testaccbatch%s"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
pool_allocation_mode = "BatchService"
}

data "azurerm_batch_account" "test" {
name = "${azurerm_batch_account.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, rInt, location, rString)
}

func testAccDataSourceAzureRMBatchAccount_complete(rInt int, rString string, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "testaccRG-%d-batch"
location = "%s"
}

resource "azurerm_storage_account" "test" {
name = "testaccsa%s"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_batch_account" "test" {
name = "testaccbatch%s"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
pool_allocation_mode = "BatchService"
storage_account_id = "${azurerm_storage_account.test.id}"

tags = {
env = "test"
}
}
%s

data "azurerm_batch_account" "test" {
name = "${azurerm_batch_account.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
resource_group_name = "${azurerm_batch_account.test.resource_group_name}"
}
`, rInt, location, rString, rString)
`, config)
}
Loading