Skip to content

Commit

Permalink
Add service-mode and allowed-origins to azurerm_signalr_service
Browse files Browse the repository at this point in the history
Fixes #4422 by adding service_mode and allowed_origins to the azurerm_signalr_service.

Fix formatting

Update signalr vendors version

Remove old vendor version

Move signalr outside of preview folder

Fix linting error
  • Loading branch information
Jack Batzner committed Oct 24, 2019
1 parent 885a36c commit ead11c3
Show file tree
Hide file tree
Showing 11 changed files with 453 additions and 33 deletions.
2 changes: 1 addition & 1 deletion azurerm/internal/services/signalr/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package signalr

import (
"github.com/Azure/azure-sdk-for-go/services/preview/signalr/mgmt/2018-03-01-preview/signalr"
"github.com/Azure/azure-sdk-for-go/services/signalr/mgmt/2018-10-01/signalr"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

Expand Down
134 changes: 130 additions & 4 deletions azurerm/resource_arm_signalr_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/signalr/mgmt/2018-03-01-preview/signalr"
"github.com/Azure/azure-sdk-for-go/services/signalr/mgmt/2018-10-01/signalr"
"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"
Expand Down Expand Up @@ -72,6 +72,48 @@ func resourceArmSignalRService() *schema.Resource {
},
},

"features": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"flag": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"ServiceMode",
}, false),
},

"value": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"Default",
"Serverless",
"Classic",
}, false),
},
},
},
},

"cors": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"allowed_origins": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},

"hostname": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -132,6 +174,8 @@ func resourceArmSignalRServiceCreateUpdate(d *schema.ResourceData, meta interfac

sku := d.Get("sku").([]interface{})
t := d.Get("tags").(map[string]interface{})
featureFlags := d.Get("features").([]interface{})
cors := d.Get("cors").([]interface{})
expandedTags := tags.Expand(t)

if features.ShouldResourcesBeImported() && d.IsNewResource() {
Expand All @@ -147,10 +191,16 @@ func resourceArmSignalRServiceCreateUpdate(d *schema.ResourceData, meta interfac
}
}

properties := &signalr.CreateOrUpdateProperties{
Cors: expandSignalRCors(cors),
Features: expandSignalRFeatures(featureFlags),
}

parameters := &signalr.CreateParameters{
Location: utils.String(location),
Sku: expandSignalRServiceSku(sku),
Tags: expandedTags,
Location: utils.String(location),
Sku: expandSignalRServiceSku(sku),
Tags: expandedTags,
Properties: properties,
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, name, parameters)
Expand Down Expand Up @@ -215,6 +265,8 @@ func resourceArmSignalRServiceRead(d *schema.ResourceData, meta interface{}) err
d.Set("ip_address", properties.ExternalIP)
d.Set("public_port", properties.PublicPort)
d.Set("server_port", properties.ServerPort)
d.Set("features", flattenSignalRFeatures(properties.Features))
d.Set("cors", flattenSignalRCors(properties.Cors))
}

d.Set("primary_access_key", keys.PrimaryKey)
Expand Down Expand Up @@ -253,6 +305,80 @@ func resourceArmSignalRServiceDelete(d *schema.ResourceData, meta interface{}) e
return nil
}

func expandSignalRFeatures(input []interface{}) *[]signalr.Feature {
features := make([]signalr.Feature, 0)
for _, featureValue := range input {
value := featureValue.(map[string]interface{})

feature := signalr.Feature{
Flag: utils.String(value["flag"].(string)),
Value: utils.String(value["value"].(string)),
}

features = append(features, feature)
}

return &features
}

func flattenSignalRFeatures(features *[]signalr.Feature) []interface{} {
result := make([]interface{}, 0)
if features != nil {
for _, feature := range *features {
value := make(map[string]interface{})

if feature.Flag != nil {
value["flag"] = *feature.Flag
}
if feature.Value != nil {
value["value"] = *feature.Value
}

result = append(result, value)
}
}
return result
}

func expandSignalRCors(input []interface{}) *signalr.CorsSettings {
corsSettings := signalr.CorsSettings{}

if len(input) == 0 {
return &corsSettings
}

setting := input[0].(map[string]interface{})
origins := setting["allowed_origins"].(*schema.Set).List()

allowedOrigins := make([]string, 0)
for _, param := range origins {
allowedOrigins = append(allowedOrigins, param.(string))
}

corsSettings.AllowedOrigins = &allowedOrigins

return &corsSettings
}

func flattenSignalRCors(input *signalr.CorsSettings) []interface{} {
results := make([]interface{}, 0)
if input == nil {
return results
}

result := make(map[string]interface{})

allowedOrigins := make([]interface{}, 0)
if s := input.AllowedOrigins; s != nil {
for _, v := range *s {
allowedOrigins = append(allowedOrigins, v)
}
}
result["allowed_origins"] = schema.NewSet(schema.HashString, allowedOrigins)

return append(results, result)
}

func expandSignalRServiceSku(input []interface{}) *signalr.ResourceSku {
v := input[0].(map[string]interface{})
return &signalr.ResourceSku{
Expand Down
112 changes: 112 additions & 0 deletions azurerm/resource_arm_signalr_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestAccAzureRMSignalRService_basic(t *testing.T) {
},
})
}

func TestAccAzureRMSignalRService_requiresImport(t *testing.T) {
if !features.ShouldResourcesBeImported() {
t.Skip("Skipping since resources aren't required to be imported")
Expand Down Expand Up @@ -341,6 +342,75 @@ func TestAccAzureRMSignalRService_skuAndCapacityUpdate(t *testing.T) {
})
}

func TestAccAzureRMSignalRService_serviceMode(t *testing.T) {
resourceName := "azurerm_signalr_service.test"
ri := tf.AccRandTimeInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMSignalRServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMSignalRService_withServiceMode(ri, testLocation(), "Serverless"),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMSignalRServiceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "features.#", "1"),
resource.TestCheckResourceAttr(resourceName, "features.0.flag", "ServiceMode"),
resource.TestCheckResourceAttr(resourceName, "features.0.value", "Serverless"),
resource.TestCheckResourceAttrSet(resourceName, "hostname"),
resource.TestCheckResourceAttrSet(resourceName, "ip_address"),
resource.TestCheckResourceAttrSet(resourceName, "public_port"),
resource.TestCheckResourceAttrSet(resourceName, "server_port"),
resource.TestCheckResourceAttrSet(resourceName, "primary_access_key"),
resource.TestCheckResourceAttrSet(resourceName, "primary_connection_string"),
resource.TestCheckResourceAttrSet(resourceName, "secondary_access_key"),
resource.TestCheckResourceAttrSet(resourceName, "secondary_connection_string"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMSignalRService_cors(t *testing.T) {
resourceName := "azurerm_signalr_service.test"
ri := tf.AccRandTimeInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMSignalRServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMSignalRService_withCors(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMSignalRServiceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "site_config.0.cors.#", "1"),
resource.TestCheckResourceAttr(resourceName, "site_config.0.cors.0.allowed_origins.#", "3"),
resource.TestCheckResourceAttrSet(resourceName, "hostname"),
resource.TestCheckResourceAttrSet(resourceName, "ip_address"),
resource.TestCheckResourceAttrSet(resourceName, "public_port"),
resource.TestCheckResourceAttrSet(resourceName, "server_port"),
resource.TestCheckResourceAttrSet(resourceName, "primary_access_key"),
resource.TestCheckResourceAttrSet(resourceName, "primary_connection_string"),
resource.TestCheckResourceAttrSet(resourceName, "secondary_access_key"),
resource.TestCheckResourceAttrSet(resourceName, "secondary_connection_string"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccAzureRMSignalRService_basic(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down Expand Up @@ -398,6 +468,48 @@ resource "azurerm_signalr_service" "test" {
`, rInt, location, rInt, capacity)
}

func testAccAzureRMSignalRService_withCors(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_signalr_service" "test" {
name = "acctestSignalR-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
cors {
allowed_origins = [
"http://www.contoso.com",
"www.contoso.com",
"contoso.com",
]
}
`, rInt, location, rInt)
}

func testAccAzureRMSignalRService_withServiceMode(rInt int, location string, serviceMode string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_signalr_service" "test" {
name = "acctestSignalR-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
features {
flag = "ServiceMode"
value = "%s"
}
}
`, rInt, location, rInt, serviceMode)
}

func testCheckAzureRMSignalRServiceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).SignalR.Client
ctx := testAccProvider.Meta().(*ArmClient).StopContext
Expand Down

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

Loading

0 comments on commit ead11c3

Please sign in to comment.