Skip to content

Commit

Permalink
azurerm_media_services_account - support for 'identity', 'tags' and '…
Browse files Browse the repository at this point in the history
…storage_authentication' (#9457)

Including the support for the properties Identity and Storage Authentication in Azure Media Services Account. In order to support new fields was required to update the API version to new version 2020-05-01, but I have already validated that the Acceptance test still works. Additional take advantage of including support for tags. This PR resolves #9404
  • Loading branch information
jcanizalez authored Dec 3, 2020
1 parent e5ae301 commit 7c5e954
Show file tree
Hide file tree
Showing 25 changed files with 1,924 additions and 769 deletions.
2 changes: 1 addition & 1 deletion azurerm/internal/services/media/client/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/mediaservices/mgmt/2018-07-01/media"
"github.com/Azure/azure-sdk-for-go/services/mediaservices/mgmt/2020-05-01/media"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

Expand Down
111 changes: 93 additions & 18 deletions azurerm/internal/services/media/media_services_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"regexp"
"time"

"github.com/Azure/azure-sdk-for-go/services/mediaservices/mgmt/2018-07-01/media"
"github.com/Azure/azure-sdk-for-go/services/mediaservices/mgmt/2020-05-01/media"
"github.com/hashicorp/go-azure-helpers/response"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/media/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -72,21 +74,60 @@ func resourceArmMediaServicesAccount() *schema.Resource {
},
},

// TODO: support Tags when this bug is fixed:
// https://github.com/Azure/azure-rest-api-specs/issues/5249
// "tags": tags.Schema(),
"identity": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"principal_id": {
Type: schema.TypeString,
Computed: true,
},

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

"type": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice([]string{
"SystemAssigned",
}, true),
},
},
},
},

"storage_authentication_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"ManagedIdentity",
}, true),
},

"tags": tags.Schema(),
},
}
}

func resourceArmMediaServicesAccountCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Media.ServicesClient
subscription := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

accountName := d.Get("name").(string)
location := azure.NormalizeLocation(d.Get("location").(string))
t := d.Get("tags").(map[string]interface{})
resourceGroup := d.Get("resource_group_name").(string)
id := parse.NewMediaServiceID(subscription, resourceGroup, accountName)

storageAccountsRaw := d.Get("storage_account").(*schema.Set).List()
storageAccounts, err := expandMediaServicesAccountStorageAccounts(storageAccountsRaw)
Expand All @@ -99,17 +140,22 @@ func resourceArmMediaServicesAccountCreateUpdate(d *schema.ResourceData, meta in
StorageAccounts: storageAccounts,
},
Location: utils.String(location),
Tags: tags.Expand(t),
}

if _, e := client.CreateOrUpdate(ctx, resourceGroup, accountName, parameters); e != nil {
return fmt.Errorf("Error creating Media Service Account %q (Resource Group %q): %+v", accountName, resourceGroup, e)
if _, ok := d.GetOk("identity"); ok {
parameters.Identity = expandAzureRmMediaServiceIdentity(d)
}

service, err := client.Get(ctx, resourceGroup, accountName)
if err != nil {
return fmt.Errorf("Error retrieving Media Service Account %q (Resource Group %q): %+v", accountName, resourceGroup, err)
if v, ok := d.GetOk("storage_authentication"); ok {
parameters.StorageAuthentication = media.StorageAuthentication(v.(string))
}
d.SetId(*service.ID)

if _, e := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, parameters); e != nil {
return fmt.Errorf("creating Media Service Account %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, e)
}

d.SetId(id.ID(""))

return resourceArmMediaServicesAccountRead(d, meta)
}
Expand All @@ -132,7 +178,7 @@ func resourceArmMediaServicesAccountRead(d *schema.ResourceData, meta interface{
return nil
}

return fmt.Errorf("Error retrieving Media Services Account %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
return fmt.Errorf("retrieving Media Services Account %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

d.Set("name", id.Name)
Expand All @@ -141,17 +187,20 @@ func resourceArmMediaServicesAccountRead(d *schema.ResourceData, meta interface{
d.Set("location", azure.NormalizeLocation(*location))
}

if props := resp.ServiceProperties; props != nil {
props := resp.ServiceProperties
if props != nil {
accounts := flattenMediaServicesAccountStorageAccounts(props.StorageAccounts)
if e := d.Set("storage_account", accounts); e != nil {
return fmt.Errorf("Error flattening `storage_account`: %s", e)
return fmt.Errorf("flattening `storage_account`: %s", e)
}
d.Set("storage_authentication_type", string(props.StorageAuthentication))
}

// TODO: support Tags when this bug is fixed:
// https://github.com/Azure/azure-rest-api-specs/issues/5249
// return tags.FlattenAndSet(d, resp.Tags)
return nil
if err := d.Set("identity", flattenAzureRmMediaServicedentity(resp.Identity)); err != nil {
return fmt.Errorf("flattening `identity`: %s", err)
}

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

func resourceArmMediaServicesAccountDelete(d *schema.ResourceData, meta interface{}) error {
Expand All @@ -169,7 +218,7 @@ func resourceArmMediaServicesAccountDelete(d *schema.ResourceData, meta interfac
if response.WasNotFound(resp.Response) {
return nil
}
return fmt.Errorf("Error issuing AzureRM delete request for Media Services Account '%s': %+v", id.Name, err)
return fmt.Errorf("issuing AzureRM delete request for Media Services Account '%s': %+v", id.Name, err)
}

return nil
Expand Down Expand Up @@ -225,3 +274,29 @@ func flattenMediaServicesAccountStorageAccounts(input *[]media.StorageAccount) [

return results
}

func expandAzureRmMediaServiceIdentity(d *schema.ResourceData) *media.ServiceIdentity {
identities := d.Get("identity").([]interface{})
identity := identities[0].(map[string]interface{})
identityType := identity["type"].(string)
return &media.ServiceIdentity{
Type: media.ManagedIdentityType(identityType),
}
}

func flattenAzureRmMediaServicedentity(identity *media.ServiceIdentity) []interface{} {
if identity == nil {
return make([]interface{}, 0)
}

result := make(map[string]interface{})
result["type"] = string(identity.Type)
if identity.PrincipalID != nil {
result["principal_id"] = *identity.PrincipalID
}
if identity.TenantID != nil {
result["tenant_id"] = *identity.TenantID
}

return []interface{}{result}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ func TestAccAzureRMMediaServicesAccount_multiplePrimaries(t *testing.T) {
})
}

func TestAccAzureRMMediaServicesAccount_identitySystemAssigned(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_media_services_account", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMMediaServicesAccountDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMMediaServicesAccount_identitySystemAssigned(data),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(data.ResourceName, "identity.0.type", "SystemAssigned"),
),
},
data.ImportStep(),
},
})
}

func testCheckAzureRMMediaServicesAccountExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acceptance.AzureProvider.Meta().(*clients.Client).Media.ServicesClient
Expand Down Expand Up @@ -140,6 +159,10 @@ resource "azurerm_media_services_account" "test" {
id = azurerm_storage_account.first.id
is_primary = true
}
tags = {
environment = "staging"
}
}
`, template, data.RandomString)
}
Expand Down Expand Up @@ -237,6 +260,28 @@ resource "azurerm_media_services_account" "test" {
`, template, data.RandomString, data.RandomString)
}

func testAccAzureRMMediaServicesAccount_identitySystemAssigned(data acceptance.TestData) string {
template := testAccAzureRMMediaServicesAccount_template(data)
return fmt.Sprintf(`
%s
resource "azurerm_media_services_account" "test" {
name = "acctestmsa%s"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
storage_account {
id = azurerm_storage_account.first.id
is_primary = true
}
identity {
type = "SystemAssigned"
}
}
`, template, data.RandomString)
}

func testAccAzureRMMediaServicesAccount_template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7c5e954

Please sign in to comment.