diff --git a/internal/services/communication/communication_service_data_source.go b/internal/services/communication/communication_service_data_source.go new file mode 100644 index 000000000000..09852dca97c0 --- /dev/null +++ b/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) + }, + } +} diff --git a/internal/services/communication/communication_service_data_source_test.go b/internal/services/communication/communication_service_data_source_test.go new file mode 100644 index 000000000000..841a31ac626c --- /dev/null +++ b/internal/services/communication/communication_service_data_source_test.go @@ -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)) +} diff --git a/internal/services/communication/registration.go b/internal/services/communication/registration.go index 8c66767d34a3..a6fcf26f36ec 100644 --- a/internal/services/communication/registration.go +++ b/internal/services/communication/registration.go @@ -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 { diff --git a/website/docs/d/communication_service.html.markdown b/website/docs/d/communication_service.html.markdown new file mode 100644 index 000000000000..b2fcfd52ca4a --- /dev/null +++ b/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.