Skip to content

Commit

Permalink
New Resource: azurerm_communication_service (#11066)
Browse files Browse the repository at this point in the history
Currently, there is no way to create Azure Communication Service. So I have to submit this PR to implement it.

The overview of Communication Service:
docs.microsoft.com/en-us/azure/communication-services/overview

API Doc:
https://github.com/Azure/azure-rest-api-specs/blob/master/specification/communication/resource-manager/Microsoft.Communication/stable/2020-08-20/CommunicationService.json

--- PASS: TestAccCommunicationService_basic (283.97s)
--- PASS: TestAccCommunicationService_complete (291.39s)
--- PASS: TestAccCommunicationService_requiresImport (306.33s)
--- PASS: TestAccCommunicationService_update (390.25s)
  • Loading branch information
Neil Ye authored Apr 5, 2021
1 parent e2c5160 commit 1be7b09
Show file tree
Hide file tree
Showing 25 changed files with 3,224 additions and 0 deletions.
1 change: 1 addition & 0 deletions .teamcity/components/generated/services.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var services = mapOf(
"bot" to "Bot",
"cdn" to "CDN",
"cognitive" to "Cognitive Services",
"communication" to "Communication",
"compute" to "Compute",
"containers" to "Container Services",
"cosmos" to "CosmosDB",
Expand Down
3 changes: 3 additions & 0 deletions azurerm/internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
bot "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/bot/client"
cdn "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn/client"
cognitiveServices "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cognitive/client"
communication "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/communication/client"
compute "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/compute/client"
containerServices "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/containers/client"
cosmosdb "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cosmos/client"
Expand Down Expand Up @@ -120,6 +121,7 @@ type Client struct {
Bot *bot.Client
Cdn *cdn.Client
Cognitive *cognitiveServices.Client
Communication *communication.Client
Compute *compute.Client
Containers *containerServices.Client
Cosmos *cosmosdb.Client
Expand Down Expand Up @@ -221,6 +223,7 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
client.Bot = bot.NewClient(o)
client.Cdn = cdn.NewClient(o)
client.Cognitive = cognitiveServices.NewClient(o)
client.Communication = communication.NewClient(o)
client.Compute = compute.NewClient(o)
client.Containers = containerServices.NewClient(o)
client.Cosmos = cosmosdb.NewClient(o)
Expand Down
2 changes: 2 additions & 0 deletions azurerm/internal/provider/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/bot"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cdn"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cognitive"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/communication"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/compute"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/containers"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cosmos"
Expand Down Expand Up @@ -122,6 +123,7 @@ func SupportedUntypedServices() []sdk.UntypedServiceRegistration {
bot.Registration{},
cdn.Registration{},
cognitive.Registration{},
communication.Registration{},
compute.Registration{},
containers.Registration{},
cosmos.Registration{},
Expand Down
19 changes: 19 additions & 0 deletions azurerm/internal/services/communication/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/communication/mgmt/2020-08-20/communication"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

type Client struct {
ServiceClient *communication.ServiceClient
}

func NewClient(o *common.ClientOptions) *Client {
serviceClient := communication.NewServiceClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&serviceClient.Client, o.ResourceManagerAuthorizer)

return &Client{
ServiceClient: &serviceClient,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package communication

import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/communication/mgmt/2020-08-20/communication"
"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/location"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/communication/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/communication/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmCommunicationService() *schema.Resource {
return &schema.Resource{
Create: resourceArmCommunicationServiceCreateUpdate,
Read: resourceArmCommunicationServiceRead,
Update: resourceArmCommunicationServiceCreateUpdate,
Delete: resourceArmCommunicationServiceDelete,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parse.CommunicationServiceID(id)
return err
}),

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

"resource_group_name": azure.SchemaResourceGroupName(),

"data_location": {
Type: schema.TypeString,
Optional: true,
Default: "United States",
ValidateFunc: validation.StringInSlice([]string{
"Asia Pacific",
"Australia",
"Europe",
"UK",
"United States",
}, false),
},

"tags": tags.Schema(),
},
}
}

func resourceArmCommunicationServiceCreateUpdate(d *schema.ResourceData, meta interface{}) error {
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
client := meta.(*clients.Client).Communication.ServiceClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

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

id := parse.NewCommunicationServiceID(subscriptionId, resourceGroup, name)

if d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}

if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_communication_service", id.ID())
}
}

parameter := communication.ServiceResource{
// The location is always `global` from the Azure Portal
Location: utils.String(location.Normalize("global")),
ServiceProperties: &communication.ServiceProperties{
DataLocation: utils.String(d.Get("data_location").(string)),
},
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, name, &parameter)
if err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for create/update of %s: %+v", id, err)
}

d.SetId(id.ID())

return resourceArmCommunicationServiceRead(d, meta)
}

func resourceArmCommunicationServiceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Communication.ServiceClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.CommunicationServiceID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] %s was not found - removing from state", *id)
d.SetId("")
return nil
}

return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)

if props := resp.ServiceProperties; props != nil {
d.Set("data_location", props.DataLocation)
}

return tags.FlattenAndSet(d, resp.Tags)
}

func resourceArmCommunicationServiceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Communication.ServiceClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.CommunicationServiceID(d.Id())
if err != nil {
return err
}

future, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}

if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of %s: %+v", *id, err)
}

return nil
}
Loading

0 comments on commit 1be7b09

Please sign in to comment.