Skip to content

Commit

Permalink
d/api_management: switching to use a separate identity block flatte…
Browse files Browse the repository at this point in the history
…n func for the Data Source
  • Loading branch information
tombuildsstuff committed Feb 2, 2021
1 parent f783609 commit eac2da0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/apimanagement/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/apimanagement/schemaz"
msiparse "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/msi/parse"

"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2019-12-01/apimanagement"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -237,7 +238,7 @@ func dataSourceApiManagementRead(d *schema.ResourceData, meta interface{}) error
d.Set("location", azure.NormalizeLocation(*location))
}

identity, err := flattenAzureRmApiManagementMachineIdentity(resp.Identity)
identity, err := flattenApiManagementDataSourceIdentity(resp.Identity)
if err != nil {
return err
}
Expand Down Expand Up @@ -363,6 +364,37 @@ func flattenDataSourceApiManagementAdditionalLocations(input *[]apimanagement.Ad
return results
}

func flattenApiManagementDataSourceIdentity(identity *apimanagement.ServiceIdentity) ([]interface{}, error) {
if identity == nil || identity.Type == apimanagement.None {
return make([]interface{}, 0), nil
}

result := make(map[string]interface{})
result["type"] = string(identity.Type)

if identity.PrincipalID != nil {
result["principal_id"] = identity.PrincipalID.String()
}

if identity.TenantID != nil {
result["tenant_id"] = identity.TenantID.String()
}

identityIds := make([]interface{}, 0)
if identity.UserAssignedIdentities != nil {
for key := range identity.UserAssignedIdentities {
parsedId, err := msiparse.UserAssignedIdentityID(key)
if err != nil {
return nil, err
}
identityIds = append(identityIds, parsedId.ID())
}
result["identity_ids"] = identityIds
}

return []interface{}{result}, nil
}

func apiManagementDataSourceHostnameSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"host_name": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1203,8 +1203,7 @@ func flattenAzureRmApiManagementMachineIdentity(identity *apimanagement.ServiceI
}
identityIds = append(identityIds, parsedId.ID())
}
// not casting to a Set since this is a List in the DS and a Set in the Resource
result["identity_ids"] = identityIds
result["identity_ids"] = schema.NewSet(schema.HashString, identityIds)
}

return []interface{}{result}, nil
Expand Down

0 comments on commit eac2da0

Please sign in to comment.