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

Add signalr args: 'service_mode' and 'allowed_origins' #4716

Merged
merged 5 commits into from
Oct 29, 2019
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
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
145 changes: 141 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,
jackbatzner marked this conversation as resolved.
Show resolved Hide resolved
MaxItems: 1,
jackbatzner marked this conversation as resolved.
Show resolved Hide resolved
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"flag": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"ServiceMode",
}, false),
jackbatzner marked this conversation as resolved.
Show resolved Hide resolved
},

"value": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"Default",
"Serverless",
"Classic",
}, false),
},
jackbatzner marked this conversation as resolved.
Show resolved Hide resolved
},
},
},

"cors": {
Type: schema.TypeList,
Optional: true,
jackbatzner marked this conversation as resolved.
Show resolved Hide resolved
MaxItems: 1,
jackbatzner marked this conversation as resolved.
Show resolved Hide resolved
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"allowed_origins": {
Type: schema.TypeSet,
Required: 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 @@ -202,6 +252,7 @@ func resourceArmSignalRServiceRead(d *schema.ResourceData, meta interface{}) err

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)

if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
Expand All @@ -215,6 +266,14 @@ 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)

if err := d.Set("features", flattenSignalRFeatures(properties.Features)); err != nil {
return fmt.Errorf("Error setting `features`: %+v", err)
}

if err := d.Set("cors", flattenSignalRCors(properties.Cors)); err != nil {
return fmt.Errorf("Error setting `cors`: %+v", err)
}
}

d.Set("primary_access_key", keys.PrimaryKey)
Expand Down Expand Up @@ -253,6 +312,84 @@ 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 {
flag := ""
if feature.Flag != nil {
flag = *feature.Flag
}

value := ""
if feature.Value != nil {
value = *feature.Value
}

result = append(result, map[string]interface{}{
"flag": flag,
"value": 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"),
jackbatzner marked this conversation as resolved.
Show resolved Hide resolved
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}"

jackbatzner marked this conversation as resolved.
Show resolved Hide resolved
cors {
allowed_origins = [
"http://www.contoso.com",
jackbatzner marked this conversation as resolved.
Show resolved Hide resolved
"www.contoso.com",
"contoso.com",
]
jackbatzner marked this conversation as resolved.
Show resolved Hide resolved
}
`, 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}"

jackbatzner marked this conversation as resolved.
Show resolved Hide resolved
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