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

media: switching to use a Resource ID Formatter for azurerm_media_transform #9802

Merged
merged 7 commits into from
Dec 10, 2020
28 changes: 19 additions & 9 deletions azurerm/internal/services/media/media_services_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"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/helpers/tf"
"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"
Expand Down Expand Up @@ -119,22 +120,32 @@ func resourceMediaServicesAccount() *schema.Resource {

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

accountName := d.Get("name").(string)
resourceId := parse.NewMediaServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, resourceId.ResourceGroup, resourceId.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for existing %s: %+v", resourceId, err)
}
}

if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_media_services_account", resourceId.ID(""))
}
}

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)
if err != nil {
return err
}

parameters := media.Service{
ServiceProperties: &media.ServiceProperties{
StorageAccounts: storageAccounts,
Expand All @@ -151,12 +162,11 @@ func resourceMediaServicesAccountCreateUpdate(d *schema.ResourceData, meta inter
parameters.StorageAuthentication = media.StorageAuthentication(v.(string))
}

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)
if _, err := client.CreateOrUpdate(ctx, resourceId.ResourceGroup, resourceId.Name, parameters); err != nil {
return fmt.Errorf("creating %s: %+v", resourceId, err)
}

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

d.SetId(resourceId.ID(""))
return resourceMediaServicesAccountRead(d, meta)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ func TestAccMediaServicesAccount_basic(t *testing.T) {
})
}

func TestAccMediaServicesAccount_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_media_services_account", "test")
r := MediaServicesAccountResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.basic(data),
Check: resource.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).Key("storage_account.#").HasValue("1"),
),
},
data.RequiresImportErrorStep(r.requiresImport),
})
}

func TestAccMediaServicesAccount_multipleAccounts(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_media_services_account", "test")
r := MediaServicesAccountResource{}
Expand Down Expand Up @@ -63,7 +78,6 @@ func TestAccMediaServicesAccount_multiplePrimaries(t *testing.T) {
Config: r.multiplePrimaries(data),
ExpectError: regexp.MustCompile("Only one Storage Account can be set as Primary"),
},
data.ImportStep(),
})
}

Expand Down Expand Up @@ -96,8 +110,8 @@ func (MediaServicesAccountResource) Exists(ctx context.Context, clients *clients
return utils.Bool(resp.ServiceProperties != nil), nil
}

func (MediaServicesAccountResource) basic(data acceptance.TestData) string {
template := MediaServicesAccountResource{}.template(data)
func (r MediaServicesAccountResource) basic(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%s

Expand All @@ -118,6 +132,28 @@ resource "azurerm_media_services_account" "test" {
`, template, data.RandomString)
}

func (r MediaServicesAccountResource) requiresImport(data acceptance.TestData) string {
template := r.basic(data)
return fmt.Sprintf(`
%s

resource "azurerm_media_services_account" "import" {
name = azurerm_media_services_account.test.name
location = azurerm_media_services_account.test.location
resource_group_name = azurerm_media_services_account.test.resource_group_name

storage_account {
id = azurerm_storage_account.first.id
is_primary = true
}

tags = {
environment = "staging"
}
}
`, template)
}

func (MediaServicesAccountResource) multipleAccounts(data acceptance.TestData) string {
template := MediaServicesAccountResource{}.template(data)
return fmt.Sprintf(`
Expand Down
122 changes: 84 additions & 38 deletions azurerm/internal/services/media/media_transform_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/media/parse"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
Expand Down Expand Up @@ -77,8 +78,9 @@ func resourceMediaTransform() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(media.ContinueJob), string(media.StopProcessingJob),
}, true),
string(media.ContinueJob),
string(media.StopProcessingJob),
}, false),
},
"builtin_preset": {
Type: schema.TypeList,
Expand All @@ -90,13 +92,18 @@ func resourceMediaTransform() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(media.AACGoodQualityAudio), string(media.AdaptiveStreaming),
string(media.ContentAwareEncoding), string(media.ContentAwareEncodingExperimental),
string(media.CopyAllBitrateNonInterleaved), string(media.H264MultipleBitrate1080p),
string(media.H264MultipleBitrate720p), string(media.H264MultipleBitrateSD),
string(media.H264SingleBitrate1080p), string(media.H264SingleBitrate720p),
string(media.AACGoodQualityAudio),
string(media.AdaptiveStreaming),
string(media.ContentAwareEncoding),
string(media.ContentAwareEncodingExperimental),
string(media.CopyAllBitrateNonInterleaved),
string(media.H264MultipleBitrate1080p),
string(media.H264MultipleBitrate720p),
string(media.H264MultipleBitrateSD),
}, true),
string(media.H264SingleBitrate1080p),
string(media.H264SingleBitrate720p),
string(media.H264MultipleBitrateSD),
}, false),
},
},
},
Expand All @@ -111,16 +118,31 @@ func resourceMediaTransform() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"ar-EG", "ar-SY", "de-DE", "en-AU", "en-GB", "en-US", "es-ES", "es-MX",
"fr-FR", "hi-IN", "it-IT", "ja-JP", "ko-KR", "pt-BR", "ru-RU", "zh-CN",
}, true),
"ar-EG",
"ar-SY",
"de-DE",
"en-AU",
"en-GB",
"en-US",
"es-ES",
"es-MX",
"fr-FR",
"hi-IN",
"it-IT",
"ja-JP",
"ko-KR",
"pt-BR",
"ru-RU",
"zh-CN",
}, false),
},
"audio_analysis_mode": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(media.Basic), string(media.Standard),
}, true),
string(media.Basic),
string(media.Standard),
}, false),
},
},
},
Expand All @@ -135,23 +157,40 @@ func resourceMediaTransform() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"ar-EG", "ar-SY", "de-DE", "en-AU", "en-GB", "en-US", "es-ES", "es-MX",
"fr-FR", "hi-IN", "it-IT", "ja-JP", "ko-KR", "pt-BR", "ru-RU", "zh-CN",
}, true),
"ar-EG",
"ar-SY",
"de-DE",
"en-AU",
"en-GB",
"en-US",
"es-ES",
"es-MX",
"fr-FR",
"hi-IN",
"it-IT",
"ja-JP",
"ko-KR",
"pt-BR",
"ru-RU",
"zh-CN",
}, false),
},
"audio_analysis_mode": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(media.Basic), string(media.Standard),
}, true),
string(media.Basic),
string(media.Standard),
}, false),
},
"insights_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(media.AllInsights), string(media.AudioInsightsOnly), string(media.VideoInsightsOnly),
}, true),
string(media.AllInsights),
string(media.AudioInsightsOnly),
string(media.VideoInsightsOnly),
}, false),
},
},
},
Expand All @@ -166,8 +205,9 @@ func resourceMediaTransform() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(media.SourceResolution), string(media.StandardDefinition),
}, true),
string(media.SourceResolution),
string(media.StandardDefinition),
}, false),
},
},
},
Expand All @@ -176,8 +216,10 @@ func resourceMediaTransform() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(media.High), string(media.Normal), string(media.Low),
}, true),
string(media.High),
string(media.Normal),
string(media.Low),
}, false),
},
},
},
Expand All @@ -188,17 +230,27 @@ func resourceMediaTransform() *schema.Resource {

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

transformName := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
accountName := d.Get("media_services_account_name").(string)
description := d.Get("description").(string)
resourceId := parse.NewTransformID(subscriptionId, d.Get("resource_group_name").(string), d.Get("media_services_account_name").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, resourceId.ResourceGroup, resourceId.MediaserviceName, resourceId.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for existing %s: %+v", resourceId, err)
}
}

if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_media_transform", resourceId.ID(""))
}
}

parameters := media.Transform{
TransformProperties: &media.TransformProperties{
Description: utils.String(description),
Description: utils.String(d.Get("description").(string)),
},
}

Expand All @@ -210,17 +262,11 @@ func resourceMediaTransformCreateUpdate(d *schema.ResourceData, meta interface{}
parameters.Outputs = transformOutput
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, accountName, transformName, parameters); err != nil {
return fmt.Errorf("Error creating Transform %q in Media Services Account %q (Resource Group %q): %+v", transformName, accountName, resourceGroup, err)
if _, err := client.CreateOrUpdate(ctx, resourceId.ResourceGroup, resourceId.MediaserviceName, resourceId.Name, parameters); err != nil {
return fmt.Errorf("creating/updating %s: %+v", resourceId, err)
}

transform, err := client.Get(ctx, resourceGroup, accountName, transformName)
if err != nil {
return fmt.Errorf("Error retrieving Transform %q from Media Services Account %q (Resource Group %q): %+v", transformName, accountName, resourceGroup, err)
}

d.SetId(*transform.ID)

d.SetId(resourceId.ID(""))
return resourceMediaTransformRead(d, meta)
}

Expand Down
Loading