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

batch - switch over to hashicorp/go-azure-sdk #20375

Merged
merged 8 commits into from
Feb 9, 2023
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/google/go-cmp v0.5.9
github.com/google/uuid v1.1.2
github.com/hashicorp/go-azure-helpers v0.51.0
github.com/hashicorp/go-azure-sdk v0.20230204.1161343
github.com/hashicorp/go-azure-sdk v0.20230208.1165725
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/go-version v1.6.0
Expand All @@ -26,7 +26,7 @@ require (
github.com/rickb777/date v1.12.5-0.20200422084442-6300e543c4d9
github.com/sergi/go-diff v1.2.0
github.com/tombuildsstuff/giovanni v0.20.0
github.com/tombuildsstuff/kermit v0.20221207.1110610
github.com/tombuildsstuff/kermit v0.20230208.1135849
golang.org/x/crypto v0.5.0
golang.org/x/net v0.5.0
gopkg.in/yaml.v3 v3.0.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
github.com/hashicorp/go-azure-helpers v0.12.0/go.mod h1:Zc3v4DNeX6PDdy7NljlYpnrdac1++qNW0I4U+ofGwpg=
github.com/hashicorp/go-azure-helpers v0.51.0 h1:8KSDGkGnWH6zOT60R3KUqsi0fk1vA7AMunaOUJZMM6k=
github.com/hashicorp/go-azure-helpers v0.51.0/go.mod h1:lsykLR4KjTUO7MiRmNWiTiX8QQtw3ILjyOvT0f5h3rw=
github.com/hashicorp/go-azure-sdk v0.20230204.1161343 h1:s6zwEj43q+8HtRMTw00J/QLLePOQ/82BfLdWugoFTHg=
github.com/hashicorp/go-azure-sdk v0.20230204.1161343/go.mod h1:Rx0agw+UlqcYIWQMH1qkzK5MfxCVmLv9p3HwyHzGdWY=
github.com/hashicorp/go-azure-sdk v0.20230208.1165725 h1:L1iUUz0OZH6Y16c0M1yvl7oxEQ1E/i1o/rLwfzK2mds=
github.com/hashicorp/go-azure-sdk v0.20230208.1165725/go.mod h1:aHinadEuBi04I1i+yvpPMZUxvxRxl5JgBOwlzIIxozU=
github.com/hashicorp/go-checkpoint v0.5.0 h1:MFYpPZCnQqQTE18jFwSII6eUQrD/oxMFp3mlgcqk5mU=
github.com/hashicorp/go-checkpoint v0.5.0/go.mod h1:7nfLNL10NsxqO4iWuW6tWW0HjZuDrwkBuEQsVcpCOgg=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
Expand Down Expand Up @@ -352,8 +352,8 @@ github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/tombuildsstuff/giovanni v0.20.0 h1:IM/I/iNWMXnPYwcSq8uxV7TKDlv7Nejq0bRK9i6O/C0=
github.com/tombuildsstuff/giovanni v0.20.0/go.mod h1:66KVLYma2whJhEdxPSPL3GQHkulhK+C5CluKfHGfPF4=
github.com/tombuildsstuff/kermit v0.20221207.1110610 h1:WzvCKNwvnbCGTEu+YtTDgrOZ6r6Lb6G6CEEziFIPUms=
github.com/tombuildsstuff/kermit v0.20221207.1110610/go.mod h1:rLa19LUGz+w1EbdCcj+X4tK/goaTp1hQtdCHK7sbFz4=
github.com/tombuildsstuff/kermit v0.20230208.1135849 h1:GgoBJqearx3fEtmlqkPVcq1tzgHqQAHVN5L/5X2q+Bc=
github.com/tombuildsstuff/kermit v0.20230208.1135849/go.mod h1:rLa19LUGz+w1EbdCcj+X4tK/goaTp1hQtdCHK7sbFz4=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down
25 changes: 11 additions & 14 deletions internal/services/batch/batch_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,39 @@ package batch
import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2022-01-01/batch" // nolint: staticcheck
"github.com/hashicorp/go-azure-sdk/resource-manager/batch/2022-01-01/batchaccount"
)

// expandBatchAccountKeyVaultReference expands Batch account KeyVault reference
func expandBatchAccountKeyVaultReference(list []interface{}) (*batch.KeyVaultReference, error) {
func expandBatchAccountKeyVaultReference(list []interface{}) (*batchaccount.KeyVaultReference, error) {
if len(list) == 0 || list[0] == nil {
return nil, fmt.Errorf("Error: key vault reference should be defined")
return nil, fmt.Errorf("key vault reference should be defined")
}

keyVaultRef := list[0].(map[string]interface{})

keyVaultRefID := keyVaultRef["id"].(string)
keyVaultRefURL := keyVaultRef["url"].(string)

ref := &batch.KeyVaultReference{
ID: &keyVaultRefID,
URL: &keyVaultRefURL,
ref := &batchaccount.KeyVaultReference{
Id: keyVaultRef["id"].(string),
Url: keyVaultRef["url"].(string),
}

return ref, nil
}

// flattenBatchAccountKeyvaultReference flattens a Batch account keyvault reference
func flattenBatchAccountKeyvaultReference(keyVaultReference *batch.KeyVaultReference) interface{} {
func flattenBatchAccountKeyvaultReference(keyVaultReference *batchaccount.KeyVaultReference) interface{} {
result := make(map[string]interface{})

if keyVaultReference == nil {
return []interface{}{}
}

if keyVaultReference.ID != nil {
result["id"] = *keyVaultReference.ID
if keyVaultReference.Id != "" {
result["id"] = keyVaultReference.Id
}

if keyVaultReference.URL != nil {
result["url"] = *keyVaultReference.URL
if keyVaultReference.Url != "" {
result["url"] = keyVaultReference.Url
}

return []interface{}{result}
Expand Down
74 changes: 39 additions & 35 deletions internal/services/batch/batch_account_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2022-01-01/batch" // nolint: staticcheck
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/batch/2022-01-01/batchaccount"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/batch/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/batch/validate"
keyVaultValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceBatchAccount() *pluginsdk.Resource {
Expand Down Expand Up @@ -87,7 +86,7 @@ func dataSourceBatchAccount() *pluginsdk.Resource {
},
},

"tags": tags.SchemaDataSource(),
"tags": commonschema.TagsDataSource(),
},
}
}
Expand All @@ -98,52 +97,57 @@ func dataSourceBatchAccountRead(d *pluginsdk.ResourceData, meta interface{}) err
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewAccountID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
id := batchaccount.NewBatchAccountID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, id.ResourceGroup, id.BatchAccountName)
resp, err := client.Get(ctx, id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("making Read request on %s: %+v", id, err)
return fmt.Errorf("reading %s: %+v", id, err)
}

d.SetId(id.ID())

d.Set("name", id.BatchAccountName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("resource_group_name", id.ResourceGroupName)

d.Set("account_endpoint", resp.AccountEndpoint)
d.Set("location", location.NormalizeNilable(resp.Location))
if model := resp.Model; model != nil {
d.Set("location", location.NormalizeNilable(model.Location))

if props := resp.AccountProperties; props != nil {
if autoStorage := props.AutoStorage; autoStorage != nil {
d.Set("storage_account_id", autoStorage.StorageAccountID)
}
d.Set("pool_allocation_mode", props.PoolAllocationMode)
poolAllocationMode := d.Get("pool_allocation_mode").(string)

if encryption := props.Encryption; encryption != nil {
d.Set("encryption", flattenEncryption(encryption))
}
if props := model.Properties; props != nil {
d.Set("account_endpoint", props.AccountEndpoint)

if poolAllocationMode == string(batch.PoolAllocationModeBatchService) {
keys, err := client.GetKeys(ctx, id.ResourceGroup, id.BatchAccountName)
if err != nil {
return fmt.Errorf("cannot read keys for %s: %v", id, err)
if autoStorage := props.AutoStorage; autoStorage != nil {
d.Set("storage_account_id", autoStorage.StorageAccountId)
}
d.Set("pool_allocation_mode", props.PoolAllocationMode)
poolAllocationMode := d.Get("pool_allocation_mode").(string)

d.Set("primary_access_key", keys.Primary)
d.Set("secondary_access_key", keys.Secondary)
if encryption := props.Encryption; encryption != nil {
d.Set("encryption", flattenEncryption(encryption))
}

// set empty keyvault reference which is not needed in Batch Service allocation mode.
d.Set("key_vault_reference", []interface{}{})
} else if poolAllocationMode == string(batch.PoolAllocationModeUserSubscription) {
if err := d.Set("key_vault_reference", flattenBatchAccountKeyvaultReference(props.KeyVaultReference)); err != nil {
return fmt.Errorf("flattening `key_vault_reference`: %+v", err)
if poolAllocationMode == string(batchaccount.PoolAllocationModeBatchService) {
keys, err := client.GetKeys(ctx, id)
if err != nil {
return fmt.Errorf("cannot read keys for %s: %v", id, err)
}

if keysModel := keys.Model; keysModel != nil {
d.Set("primary_access_key", keysModel.Primary)
d.Set("secondary_access_key", keysModel.Secondary)
}

// set empty keyvault reference which is not needed in Batch Service allocation mode.
d.Set("key_vault_reference", []interface{}{})
} else if poolAllocationMode == string(batchaccount.PoolAllocationModeUserSubscription) {
if err := d.Set("key_vault_reference", flattenBatchAccountKeyvaultReference(props.KeyVaultReference)); err != nil {
return fmt.Errorf("flattening `key_vault_reference`: %+v", err)
}
}
}
return tags.FlattenAndSet(d, model.Tags)
}

return tags.FlattenAndSet(d, resp.Tags)
return nil
}
Loading