Skip to content

Commit

Permalink
Merge pull request #15243 from hashicorp/b/identity
Browse files Browse the repository at this point in the history
identity: working around the use of legacy identity types
  • Loading branch information
tombuildsstuff authored Feb 3, 2022
2 parents 4b1ea44 + 1bd19be commit cadbba5
Show file tree
Hide file tree
Showing 8 changed files with 462 additions and 20 deletions.
95 changes: 87 additions & 8 deletions internal/services/storage/storage_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
azautorest "github.com/Azure/go-autorest/autorest"
autorestAzure "github.com/Azure/go-autorest/autorest/azure"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-getter/helper/url"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -347,7 +349,53 @@ func resourceStorageAccount() *pluginsdk.Resource {
},
},

"identity": commonschema.SystemAssignedUserAssignedIdentityOptional(),
"identity": func() *schema.Schema {
if features.ThreePointOhBeta() {
return commonschema.SystemAssignedUserAssignedIdentityOptional()
}

return &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(identity.TypeUserAssigned),
string(identity.TypeSystemAssigned),
string(identity.TypeSystemAssignedUserAssigned),
"SystemAssigned,UserAssigned", // defined in the Swagger but should be normalized as above
}, false),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
// handle `SystemAssigned, UserAssigned` with and without the spaces being the same
oldWithoutSpaces := strings.ReplaceAll(old, " ", "")
newWithoutSpaces := strings.ReplaceAll(new, " ", "")
return oldWithoutSpaces == newWithoutSpaces
},
},
"identity_ids": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: commonids.ValidateUserAssignedIdentityID,
},
},
"principal_id": {
Type: schema.TypeString,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
}
}(),

"blob_properties": {
Type: pluginsdk.TypeList,
Expand Down Expand Up @@ -2850,20 +2898,45 @@ func flattenStorageAccountBypass(input storage.Bypass) []interface{} {
}

func expandAzureRmStorageAccountIdentity(input []interface{}) (*storage.Identity, error) {
if !features.ThreePointOhBeta() {
// work around the Swagger defining `SystemAssigned,UserAssigned` rather than `SystemAssigned, UserAssigned`
if len(input) > 0 {
raw := input[0].(map[string]interface{})
if identityType := raw["type"].(string); strings.EqualFold("SystemAssigned,UserAssigned", identityType) {
raw["type"] = "SystemAssigned, UserAssigned"
}
input[0] = raw
}
}

expanded, err := identity.ExpandSystemAndUserAssignedMap(input)
if err != nil {
return nil, err
}

userAssignedIdentities := make(map[string]*storage.UserAssignedIdentity)
for id := range expanded.IdentityIds {
userAssignedIdentities[id] = &storage.UserAssignedIdentity{}
out := storage.Identity{
Type: storage.IdentityType(string(expanded.Type)),
}

return &storage.Identity{
Type: storage.IdentityType(string(expanded.Type)),
UserAssignedIdentities: userAssignedIdentities,
}, nil
// work around the Swagger defining `SystemAssigned,UserAssigned` rather than `SystemAssigned, UserAssigned`
if expanded.Type == identity.TypeSystemAssignedUserAssigned {
out.Type = storage.IdentityTypeSystemAssignedUserAssigned
}

// 'Failed to perform resource identity operation. Status: 'BadRequest'. Response:
// {"error":{"code":"BadRequest",
// "message":"The request format was unexpected, a non-UserAssigned identity type should not contain: userAssignedIdentities"
// }}
// Upstream issue: https://github.com/Azure/azure-rest-api-specs/issues/17650
if len(expanded.IdentityIds) > 0 {
userAssignedIdentities := make(map[string]*storage.UserAssignedIdentity)
for id := range expanded.IdentityIds {
userAssignedIdentities[id] = &storage.UserAssignedIdentity{}
}
out.UserAssignedIdentities = userAssignedIdentities
}

return &out, nil
}

func flattenAzureRmStorageAccountIdentity(input *storage.Identity) (*[]interface{}, error) {
Expand All @@ -2874,6 +2947,12 @@ func flattenAzureRmStorageAccountIdentity(input *storage.Identity) (*[]interface
Type: identity.Type(string(input.Type)),
IdentityIds: nil,
}

// work around the Swagger defining `SystemAssigned,UserAssigned` rather than `SystemAssigned, UserAssigned`
if input.Type == storage.IdentityTypeSystemAssignedUserAssigned {
config.Type = identity.TypeSystemAssignedUserAssigned
}

if input.PrincipalID != nil {
config.PrincipalId = *input.PrincipalID
}
Expand Down
24 changes: 23 additions & 1 deletion internal/services/storage/storage_account_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1899,7 +1899,8 @@ resource "azurerm_storage_account" "test" {
}

func (r StorageAccountResource) systemAssignedUserAssignedIdentity(data acceptance.TestData) string {
return fmt.Sprintf(`
if !features.ThreePointOhBeta() {
return fmt.Sprintf(`
%s
resource "azurerm_storage_account" "test" {
Expand All @@ -1917,6 +1918,27 @@ resource "azurerm_storage_account" "test" {
]
}
}
`, r.identityTemplate(data), data.RandomString)
}

return fmt.Sprintf(`
%s
resource "azurerm_storage_account" "test" {
name = "unlikely23exst2acct%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
identity {
type = "SystemAssigned, UserAssigned"
identity_ids = [
azurerm_user_assigned_identity.test.id,
]
}
}
`, r.identityTemplate(data), data.RandomString)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,15 @@ func expandStreamAnalyticsJobIdentity(input []interface{}) (*streamanalytics.Ide
return nil, err
}

// Otherwise we get:
// Code="BadRequest"
// Message="The JSON provided in the request body is invalid. Cannot convert value 'None' to
// type 'System.Nullable`1[Microsoft.Streaming.Service.Contracts.CSMResourceProvider.IdentityType]"
// Upstream issue: https://github.com/Azure/azure-rest-api-specs/issues/17649
if expanded.Type == identity.TypeNone {
return nil, nil
}

return &streamanalytics.Identity{
Type: utils.String(string(expanded.Type)),
}, nil
Expand Down
5 changes: 2 additions & 3 deletions internal/services/synapse/synapse_workspace_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/Azure/azure-sdk-for-go/services/synapse/mgmt/2021-03-01/synapse"
"github.com/gofrs/uuid"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down
Loading

0 comments on commit cadbba5

Please sign in to comment.