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

azurerm_servicebus_subscription - support status property #7852

Merged
merged 3 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions azurerm/internal/services/servicebus/helper/entity_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package helper

import (
"strings"

"github.com/Azure/azure-sdk-for-go/services/servicebus/mgmt/2017-04-01/servicebus"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func ExpandEntityStatus(input interface{}) servicebus.EntityStatus {
entityStatus := servicebus.Unknown

if status, ok := input.(string); ok {
entityStatus = servicebus.EntityStatus(strings.Title(status))
}

return entityStatus
}

func FlattenEntityStatus(status servicebus.EntityStatus) *string {
return utils.String(string(status))
}
75 changes: 75 additions & 0 deletions azurerm/internal/services/servicebus/helper/entity_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package helper

import (
"testing"

"github.com/Azure/azure-sdk-for-go/services/servicebus/mgmt/2017-04-01/servicebus"
)

func TestExpandEntityStatus(t *testing.T) {
testData := []struct {
Name string
Input string
Expected servicebus.EntityStatus
}{
{
Name: "Active",
Input: "Active",
Expected: servicebus.Active,
},
{
Name: "ReceiveDisabled",
Input: "ReceiveDisabled",
Expected: servicebus.ReceiveDisabled,
},
{
Name: "Disabled",
Input: "Disabled",
Expected: servicebus.Disabled,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual := ExpandEntityStatus(v.Input)

if actual != v.Expected {
t.Fatalf("Expected %q but got %q", v.Expected, actual)
}
}
}

func TestFlattenEntityStatus(t *testing.T) {
testData := []struct {
Name string
Input servicebus.EntityStatus
Expected string
}{
{
Name: "Active",
Input: servicebus.Active,
Expected: "Active",
},
{
Name: "Disabled",
Input: servicebus.Disabled,
Expected: "Disabled",
},
{
Name: "ReceiveDisabled",
Input: servicebus.ReceiveDisabled,
Expected: "ReceiveDisabled",
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual := FlattenEntityStatus(v.Input)

if *actual != v.Expected {
t.Fatalf("Expected %q but got %q", v.Expected, *actual)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (

"github.com/Azure/azure-sdk-for-go/services/servicebus/mgmt/2017-04-01/servicebus"
"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/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/servicebus/helper"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/servicebus/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -106,6 +108,17 @@ func resourceArmServiceBusSubscription() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},

"status": {
Type: schema.TypeString,
Optional: true,
Default: string(servicebus.Active),
ValidateFunc: validation.StringInSlice([]string{
string(servicebus.Active),
string(servicebus.Disabled),
string(servicebus.ReceiveDisabled),
}, false),
},
},
}
}
Expand Down Expand Up @@ -145,6 +158,7 @@ func resourceArmServiceBusSubscriptionCreateUpdate(d *schema.ResourceData, meta
EnableBatchedOperations: &enableBatchedOps,
MaxDeliveryCount: &maxDeliveryCount,
RequiresSession: &requiresSession,
Status: helper.ExpandEntityStatus(d.Get("status")),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate all the work you put into making the helper function to get the status but we should aim for simplicity. We know that status can only be servicebus.EntityStatus because of what we allow in the ValidateFunc so we can be confident that we can get away with a single line versus the helper function + tests that you wrote

Suggested change
Status: helper.ExpandEntityStatus(d.Get("status")),
Status: servicebus.EntityStatus(d.Get("status").(string)),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this to a one-liner in 87b1115

},
}

Expand Down Expand Up @@ -222,6 +236,7 @@ func resourceArmServiceBusSubscriptionRead(d *schema.ResourceData, meta interfac
d.Set("requires_session", props.RequiresSession)
d.Set("forward_to", props.ForwardTo)
d.Set("forward_dead_lettered_messages_to", props.ForwardDeadLetteredMessagesTo)
d.Set("status", helper.FlattenEntityStatus(props.Status))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the above comment, we should aim for simplicity here.

Suggested change
d.Set("status", helper.FlattenEntityStatus(props.Status))
d.Set("status", string(props.Status))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this to a one-liner in 87b1115


if count := props.MaxDeliveryCount; count != nil {
d.Set("max_delivery_count", int(*count))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,43 @@ func TestAccAzureRMServiceBusSubscription_updateForwardDeadLetteredMessagesTo(t
})
}

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMServiceBusSubscriptionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMServiceBusSubscription_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMServiceBusSubscriptionExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "status", "Active"),
),
},
{
Config: testAccAzureRMServiceBusSubscription_status(data, "Disabled"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(data.ResourceName, "status", "Disabled"),
),
},
{
Config: testAccAzureRMServiceBusSubscription_status(data, "ReceiveDisabled"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(data.ResourceName, "status", "ReceiveDisabled"),
),
},
{
Config: testAccAzureRMServiceBusSubscription_status(data, "Active"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(data.ResourceName, "status", "Active"),
),
},
},
})
}

func testCheckAzureRMServiceBusSubscriptionDestroy(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).ServiceBus.SubscriptionsClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
Expand Down Expand Up @@ -325,3 +362,8 @@ resource "azurerm_servicebus_topic" "forward_dl_messages_to" {
return fmt.Sprintf(forwardToTf, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger,
"forward_dead_lettered_messages_to = \"${azurerm_servicebus_topic.forward_dl_messages_to.name}\"\n", data.RandomInteger)
}

func testAccAzureRMServiceBusSubscription_status(data acceptance.TestData, status string) string {
return fmt.Sprintf(testAccAzureRMServiceBusSubscription_tfTemplate, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger,
fmt.Sprintf("status = \"%s\"", status))
}
2 changes: 2 additions & 0 deletions website/docs/r/servicebus_subscription.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ The following arguments are supported:

* `forward_dead_lettered_messages_to` - (Optional) The name of a Queue or Topic to automatically forward Dead Letter messages to.

* `status` - (Optional) The status of the Subscription. Possible values are `Active`,`ReceiveDisabled`, or `Disabled`. Defaults to `Active`.

## Attributes Reference

The following attributes are exported:
Expand Down