Skip to content

Commit

Permalink
New data source azurerm_communication_service
Browse files Browse the repository at this point in the history
  • Loading branch information
myc2h6o committed Jul 8, 2023
1 parent e1f3669 commit 58f3374
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 1 deletion.
146 changes: 146 additions & 0 deletions internal/services/communication/communication_service_data_source.go
@@ -0,0 +1,146 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package communication

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-sdk/resource-manager/communication/2023-03-31/communicationservices"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/communication/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

var _ sdk.DataSource = CommunicationServiceDataSource{}

type CommunicationServiceDataSource struct{}

type CommunicationServiceDataSourceModel struct {
Name string `tfschema:"name"`
ResourceGroupName string `tfschema:"resource_group_name"`
DataLocation string `tfschema:"data_location"`
PrimaryConnectionString string `tfschema:"primary_connection_string"`
PrimaryKey string `tfschema:"primary_key"`
SecondaryConnectionString string `tfschema:"secondary_connection_string"`
SecondaryKey string `tfschema:"secondary_key"`
Tags map[string]string `tfschema:"tags"`
}

func (CommunicationServiceDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.CommunicationServiceName,
},

"resource_group_name": commonschema.ResourceGroupNameForDataSource(),
}
}

func (CommunicationServiceDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"data_location": {
Type: pluginsdk.TypeString,
Computed: true,
},

"primary_connection_string": {
Type: pluginsdk.TypeString,
Computed: true,
},

"primary_key": {
Type: pluginsdk.TypeString,
Computed: true,
},

"secondary_connection_string": {
Type: pluginsdk.TypeString,
Computed: true,
},

"secondary_key": {
Type: pluginsdk.TypeString,
Computed: true,
},

"tags": commonschema.TagsDataSource(),
}
}

func (CommunicationServiceDataSource) ModelObject() interface{} {
return &CommunicationServiceDataSourceModel{}
}

func (CommunicationServiceDataSource) ResourceType() string {
return "azurerm_communication_service"
}

func (CommunicationServiceDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Communication.ServiceClient
subscriptionId := metadata.Client.Account.SubscriptionId

var state CommunicationServiceDataSourceModel
if err := metadata.Decode(&state); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

id := communicationservices.NewCommunicationServiceID(subscriptionId, state.ResourceGroupName, state.Name)

resp, err := client.Get(ctx, id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}

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

keysResp, err := client.ListKeys(ctx, id)
if err != nil {
return fmt.Errorf("listing keys for %s: %+v", id, err)
}

if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
state.DataLocation = props.DataLocation
}

if model.Tags != nil {
state.Tags = *model.Tags
}
}

if model := keysResp.Model; model != nil {
if model.PrimaryConnectionString != nil {
state.PrimaryConnectionString = *model.PrimaryConnectionString
}

if model.PrimaryKey != nil {
state.PrimaryKey = *model.PrimaryKey
}

if model.SecondaryConnectionString != nil {
state.SecondaryConnectionString = *model.SecondaryConnectionString
}

if model.SecondaryKey != nil {
state.SecondaryKey = *model.SecondaryKey
}
}

metadata.SetID(id)

return metadata.Encode(&state)
},
}
}
@@ -0,0 +1,69 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package communication_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type CommunicationServiceDataSource struct{}

func TestAccCommunicationServiceDataSource_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_communication_service", "test")
d := CommunicationServiceDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: d.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("data_location").Exists(),
check.That(data.ResourceName).Key("primary_connection_string").Exists(),
check.That(data.ResourceName).Key("secondary_connection_string").Exists(),
check.That(data.ResourceName).Key("primary_key").Exists(),
check.That(data.ResourceName).Key("secondary_key").Exists(),
),
},
})
}

func TestAccCommunicationServiceDataSource_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_communication_service", "test")
d := CommunicationServiceDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: d.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
check.That(data.ResourceName).Key("tags.env").HasValue("Test"),
),
},
})
}

func (d CommunicationServiceDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_communication_service" "test" {
name = azurerm_communication_service.test.name
resource_group_name = azurerm_communication_service.test.resource_group_name
}
`, CommunicationServiceTestResource{}.basic(data))
}

func (d CommunicationServiceDataSource) complete(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_communication_service" "test" {
name = azurerm_communication_service.test.name
resource_group_name = azurerm_communication_service.test.resource_group_name
}
`, CommunicationServiceTestResource{}.complete(data))
}
4 changes: 3 additions & 1 deletion internal/services/communication/registration.go
Expand Up @@ -28,7 +28,9 @@ func (r Registration) WebsiteCategories() []string {
}

func (r Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{}
return []sdk.DataSource{
CommunicationServiceDataSource{},
}
}

func (r Registration) Resources() []sdk.Resource {
Expand Down
56 changes: 56 additions & 0 deletions website/docs/d/communication_service.html.markdown
@@ -0,0 +1,56 @@
---
subcategory: "Communication"
layout: "azurerm"
page_title: "Azure Resource Manager: Data Source: azurerm_communication_service"
description: |-
Gets information about an existing Communication Service.
---

# Data Source: azurerm_communication_service

Use this data source to access information about an existing Communication Service.

## Example Usage

```hcl
data "azurerm_communication_service" "example" {
name = "existing"
resource_group_name = "existing"
}
output "id" {
value = data.azurerm_communication_service.example.id
}
```

## Arguments Reference

The following arguments are supported:

* `name` - (Required) The name of this Communication Service.
*
* `resource_group_name` - (Required) The name of the Resource Group where the Communication Service exists.
*
## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:

* `id` - The ID of the Communication Service.

* `data_location` - The location where the Communication service stores its data at rest.

* `primary_connection_string` - The primary connection string of the Communication Service.

* `primary_key` - The primary key of the Communication Service.

* `secondary_connection_string` - The secondary connection string of the Communication Service.

* `secondary_key` - The secondary key of the Communication Service.

* `tags` - A mapping of tags assigned to the Communication Service.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions:

* `read` - (Defaults to 5 minutes) Used when retrieving the Communication Service.

0 comments on commit 58f3374

Please sign in to comment.