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

New Resource: SignalR service azurerm_signalr_service #2410

Merged
merged 21 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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/backup"
Expand Down Expand Up @@ -294,6 +295,9 @@ type ArmClient struct {
// Service Fabric
serviceFabricClustersClient servicefabric.ClustersClient

// SignalR
signalRClient signalr.Client

// Storage
storageServiceClient storage.AccountsClient
storageUsageClient storage.UsageClient
Expand Down Expand Up @@ -447,6 +451,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 @@ -1082,6 +1087,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 @@ -273,6 +273,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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

if input's nil we're going to set an empty sku block in the state (so there'll be an object there but it'll be empty) - can we update this to:

if input == nil {
  return []interface{}{}
}

...

this way it'll show that the Sku block isn't being returned from the API from the fact that Terraform will show the block being removed, rather than the fields being blank

if input.Name != nil {
result["name"] = *input.Name
}
if input.Capacity != nil {
result["capacity"] = *input.Capacity
}
}
return []interface{}{result}
}
Loading