-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Resource: azurerm_communication_service (#11066)
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
Showing
25 changed files
with
3,224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
168 changes: 168 additions & 0 deletions
168
azurerm/internal/services/communication/communication_service_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ¶meter) | ||
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 | ||
} |
Oops, something went wrong.