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 data source azurerm_communication_service #22426

Merged
merged 2 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
146 changes: 146 additions & 0 deletions internal/services/communication/communication_service_data_source.go
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

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

data sources should gracefully handle having limited permissions, so we shouldn't raise this error when there's a permissions error (presumably a 401?)

}

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
}
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW we can reduce this to:

Suggested change
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
}
state.PrimaryConnectionString = pointer.From(model.PrimaryConnectionString)
state.PrimaryKey = pointer.From(model.PrimaryKey)
state.SecondaryConnectionString = pointer.From(model.SecondaryConnectionString)
state.SecondaryKey = pointer.From(model.SecondaryKey)

}

metadata.SetID(id)

return metadata.Encode(&state)
},
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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.