Skip to content

Commit

Permalink
Merge pull request #10216 from jcanizalez/fix_streaming_endpoint
Browse files Browse the repository at this point in the history
fix error on azurerm_media_streaming_endpoint when destroying a running instance
  • Loading branch information
tombuildsstuff authored Jan 22, 2021
2 parents daced62 + 9cd4dd7 commit 85842b5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,24 @@ func resourceMediaStreamingEndpointDelete(d *schema.ResourceData, meta interface
return err
}

// Stop Streaming Endpoint before we attempt to delete it.
resp, err := client.Get(ctx, id.ResourceGroup, id.MediaserviceName, id.Name)
if err != nil {
return fmt.Errorf("reading %s: %+v", id, err)
}
if props := resp.StreamingEndpointProperties; props != nil {
if props.ResourceState == media.StreamingEndpointResourceStateRunning {
stopFuture, err := client.Stop(ctx, id.ResourceGroup, id.MediaserviceName, id.Name)
if err != nil {
return fmt.Errorf("stopping %s: %+v", id, err)
}

if err = stopFuture.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for %s to stop: %+v", id, err)
}
}
}

future, err := client.Delete(ctx, id.ResourceGroup, id.MediaserviceName, id.Name)
if err != nil {
return fmt.Errorf("Error deleting Streaming Endpoint %q in Media Services Account %q (Resource Group %q): %+v", id.Name, id.MediaserviceName, id.ResourceGroup, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,38 @@ func TestAccMediaStreamingEndpoint_MaxCacheAge(t *testing.T) {
})
}

func TestAccMediaStreamingEndpoint_shouldStopWhenStarted(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_media_streaming_endpoint", "test")
r := MediaStreamingEndpointResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.basic(data),
Check: resource.ComposeAggregateTestCheckFunc(
data.CheckWithClient(r.Start),
),
},
})
}

func (r MediaStreamingEndpointResource) Start(ctx context.Context, client *clients.Client, state *terraform.InstanceState) error {
id, err := parse.StreamingEndpointID(state.ID)
if err != nil {
return err
}

future, err := client.Media.StreamingEndpointsClient.Start(ctx, id.ResourceGroup, id.MediaserviceName, id.Name)
if err != nil {
return fmt.Errorf("starting %s: %+v", id, err)
}

if err := future.WaitForCompletionRef(ctx, client.Media.StreamingEndpointsClient.Client); err != nil {
return fmt.Errorf("waiting for %s to start: %+v", id, err)
}

return nil
}

func (MediaStreamingEndpointResource) Exists(ctx context.Context, clients *clients.Client, state *terraform.InstanceState) (*bool, error) {
id, err := parse.StreamingEndpointID(state.ID)
if err != nil {
Expand Down

0 comments on commit 85842b5

Please sign in to comment.