Skip to content

Commit

Permalink
Merge pull request #2410 from terraform-providers/resource_signalr
Browse files Browse the repository at this point in the history
New Resource: SignalR service `azurerm_signalr_service`
  • Loading branch information
katbyte authored Dec 12, 2018
2 parents 6f0486b + 3dddcc3 commit 642ff3a
Show file tree
Hide file tree
Showing 13 changed files with 2,657 additions and 0 deletions.
11 changes: 11 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/preview/operationsmanagement/mgmt/2015-11-01-preview/operationsmanagement"
"github.com/Azure/azure-sdk-for-go/services/preview/resources/mgmt/2018-03-01-preview/management"
"github.com/Azure/azure-sdk-for-go/services/preview/security/mgmt/2017-08-01-preview/security"
"github.com/Azure/azure-sdk-for-go/services/preview/signalr/mgmt/2018-03-01-preview/signalr"
"github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2015-05-01-preview/sql"
MsSql "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2017-10-01-preview/sql"
"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2016-06-01/recoveryservices"
Expand Down Expand Up @@ -298,6 +299,9 @@ type ArmClient struct {
// Service Fabric
serviceFabricClustersClient servicefabric.ClustersClient

// SignalR
signalRClient signalr.Client

// Storage
storageServiceClient storage.AccountsClient
storageUsageClient storage.UsageClient
Expand Down Expand Up @@ -451,6 +455,7 @@ func getArmClient(c *authentication.Config, skipProviderRegistration bool) (*Arm
client.registerServiceBusClients(endpoint, c.SubscriptionID, auth)
client.registerServiceFabricClients(endpoint, c.SubscriptionID, auth)
client.registerSchedulerClients(endpoint, c.SubscriptionID, auth)
client.registerSignalRClients(endpoint, c.SubscriptionID, auth)
client.registerStorageClients(endpoint, c.SubscriptionID, auth)
client.registerTrafficManagerClients(endpoint, c.SubscriptionID, auth)
client.registerWebClients(endpoint, c.SubscriptionID, auth)
Expand Down Expand Up @@ -1099,6 +1104,12 @@ func (c *ArmClient) registerServiceFabricClients(endpoint, subscriptionId string
c.serviceFabricClustersClient = clustersClient
}

func (c *ArmClient) registerSignalRClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
sc := signalr.NewClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&sc.Client, auth)
c.signalRClient = sc
}

func (c *ArmClient) registerStorageClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
accountsClient := storage.NewAccountsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&accountsClient.Client, auth)
Expand Down
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_shared_image": resourceArmSharedImage(),
"azurerm_shared_image_gallery": resourceArmSharedImageGallery(),
"azurerm_shared_image_version": resourceArmSharedImageVersion(),
"azurerm_signalr_service": resourceArmSignalRService(),
"azurerm_snapshot": resourceArmSnapshot(),
"azurerm_scheduler_job": resourceArmSchedulerJob(),
"azurerm_scheduler_job_collection": resourceArmSchedulerJobCollection(),
Expand Down
212 changes: 212 additions & 0 deletions azurerm/resource_arm_signalr_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package azurerm

import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/services/preview/signalr/mgmt/2018-03-01-preview/signalr"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmSignalRService() *schema.Resource {
return &schema.Resource{
Create: resourceArmSignalRServiceCreateOrUpdate,
Read: resourceArmSignalRServiceRead,
Update: resourceArmSignalRServiceCreateOrUpdate,
Delete: resourceArmSignalRServiceDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},

"location": locationSchema(),

"resource_group_name": resourceGroupNameSchema(),

"sku": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"Free_F1",
"Standard_S1",
}, false),
},

"capacity": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validate.IntInSlice([]int{1, 2, 5, 10, 20, 50, 100}),
},
},
},
},

"hostname": {
Type: schema.TypeString,
Computed: true,
},

"ip_address": {
Type: schema.TypeString,
Computed: true,
},

"public_port": {
Type: schema.TypeInt,
Computed: true,
},

"server_port": {
Type: schema.TypeInt,
Computed: true,
},

"tags": tagsSchema(),
},
}
}

func resourceArmSignalRServiceCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).signalRClient
ctx := meta.(*ArmClient).StopContext

name := d.Get("name").(string)
location := azureRMNormalizeLocation(d.Get("location").(string))
resourceGroup := d.Get("resource_group_name").(string)

sku := d.Get("sku").([]interface{})

tags := d.Get("tags").(map[string]interface{})
expandedTags := expandTags(tags)

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

future, err := client.CreateOrUpdate(ctx, resourceGroup, name, parameters)
if err != nil {
return fmt.Errorf("Error creating or updating SignalR %q (Resource Group %q): %+v", name, resourceGroup, err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for the result of creating or updating SignalR %q (Resource Group %q): %+v", name, resourceGroup, err)
}

read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("SignalR %q (Resource Group %q) ID is empty", name, resourceGroup)
}
d.SetId(*read.ID)

return resourceArmSignalRServiceRead(d, meta)
}

func resourceArmSignalRServiceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).signalRClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
name := id.Path["SignalR"]

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] SignalR %q was not found in Resource Group %q - removing from state!", name, resourceGroup)
d.SetId("")
return nil
}
return fmt.Errorf("Error getting SignalR %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}
if err = d.Set("sku", flattenSignalRServiceSku(resp.Sku)); err != nil {
return fmt.Errorf("Error setting `sku`: %+v", err)
}
if properties := resp.Properties; properties != nil {
d.Set("hostname", properties.HostName)
d.Set("ip_address", properties.ExternalIP)
d.Set("public_port", properties.PublicPort)
d.Set("server_port", properties.ServerPort)
}
flattenAndSetTags(d, resp.Tags)

return nil
}

func resourceArmSignalRServiceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).signalRClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
name := id.Path["SignalR"]

future, err := client.Delete(ctx, resourceGroup, name)
if err != nil {
if !response.WasNotFound(future.Response()) {
return fmt.Errorf("Error deleting SignalR %q (Resource Group %q): %+v", name, resourceGroup, err)
}
return nil
}
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
if !response.WasNotFound(future.Response()) {
return fmt.Errorf("Error waiting for the deletion of SignalR %q (Resource Group %q): %+v", name, resourceGroup, err)
}
}

return nil
}

func expandSignalRServiceSku(input []interface{}) *signalr.ResourceSku {
v := input[0].(map[string]interface{})
return &signalr.ResourceSku{
Name: utils.String(v["name"].(string)),
Capacity: utils.Int32(int32(v["capacity"].(int))),
}
}

func flattenSignalRServiceSku(input *signalr.ResourceSku) []interface{} {
result := make(map[string]interface{})
if input != nil {
if input.Name != nil {
result["name"] = *input.Name
}
if input.Capacity != nil {
result["capacity"] = *input.Capacity
}
}
return []interface{}{result}
}
Loading

0 comments on commit 642ff3a

Please sign in to comment.