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 resource azurerm_digital_twins_time_series_database_connection #19576

Merged
merged 1 commit into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions internal/provider/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration {
costmanagement.Registration{},
dashboard.Registration{},
databricks.Registration{},
digitaltwins.Registration{},
disks.Registration{},
domainservices.Registration{},
eventhub.Registration{},
Expand Down
14 changes: 10 additions & 4 deletions internal/services/digitaltwins/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package client

import (
"github.com/Azure/azure-sdk-for-go/services/digitaltwins/mgmt/2020-12-01/digitaltwins"
"github.com/hashicorp/go-azure-sdk/resource-manager/digitaltwins/2022-10-31/timeseriesdatabaseconnections"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
EndpointClient *digitaltwins.EndpointClient
InstanceClient *digitaltwins.Client
EndpointClient *digitaltwins.EndpointClient
InstanceClient *digitaltwins.Client
TimeSeriesDatabaseConnectionsClient *timeseriesdatabaseconnections.TimeSeriesDatabaseConnectionsClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand All @@ -17,8 +19,12 @@ func NewClient(o *common.ClientOptions) *Client {
InstanceClient := digitaltwins.NewClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&InstanceClient.Client, o.ResourceManagerAuthorizer)

TimeSeriesDatabaseConnectionsClient := timeseriesdatabaseconnections.NewTimeSeriesDatabaseConnectionsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&TimeSeriesDatabaseConnectionsClient.Client, o.ResourceManagerAuthorizer)

return &Client{
EndpointClient: &endpointClient,
InstanceClient: &InstanceClient,
EndpointClient: &endpointClient,
InstanceClient: &InstanceClient,
TimeSeriesDatabaseConnectionsClient: &TimeSeriesDatabaseConnectionsClient,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
package digitaltwins

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/digitaltwins/2022-10-31/timeseriesdatabaseconnections"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/eventhubs"
"github.com/hashicorp/go-azure-sdk/resource-manager/kusto/2022-07-07/clusters"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/digitaltwins/validate"
eventhubValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/eventhub/validate"
kustoValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/kusto/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type TimeSeriesDatabaseConnectionModel struct {
Name string `tfschema:"name"`
DigitalTwinsId string `tfschema:"digital_twins_id"`
EventhubConsumerGroupName string `tfschema:"eventhub_consumer_group_name"`
EventhubName string `tfschema:"eventhub_name"`
EventhubNamespaceEndpointUri string `tfschema:"eventhub_namespace_endpoint_uri"`
EventhubNamespaceId string `tfschema:"eventhub_namespace_id"`
KustoClusterId string `tfschema:"kusto_cluster_id"`
KustoClusterUri string `tfschema:"kusto_cluster_uri"`
KustoDatabaseName string `tfschema:"kusto_database_name"`
KustoTableName string `tfschema:"kusto_table_name"`
}

type TimeSeriesDatabaseConnectionResource struct{}

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

"digital_twins_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: timeseriesdatabaseconnections.ValidateDigitalTwinsInstanceID,
},

"eventhub_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: eventhubValidate.ValidateEventHubName(),
},

"eventhub_namespace_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: eventhubs.ValidateNamespaceID,
},

"eventhub_namespace_endpoint_uri": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"kusto_cluster_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: clusters.ValidateClusterID,
},

"kusto_cluster_uri": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"kusto_database_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: kustoValidate.DatabaseName,
},

"eventhub_consumer_group_name": {
Type: pluginsdk.TypeString,
Optional: true,
Default: "$Default",
ForceNew: true,
ValidateFunc: eventhubValidate.ValidateEventHubConsumerName(),
},

"kusto_table_name": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: kustoValidate.EntityName,
},
}
}

func (m TimeSeriesDatabaseConnectionResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{}
}

func (m TimeSeriesDatabaseConnectionResource) ModelObject() interface{} {
return &TimeSeriesDatabaseConnectionModel{}
}

func (m TimeSeriesDatabaseConnectionResource) ResourceType() string {
return "azurerm_digital_twins_time_series_database_connection"
}

func (m TimeSeriesDatabaseConnectionResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return timeseriesdatabaseconnections.ValidateTimeSeriesDatabaseConnectionID
}

func (m TimeSeriesDatabaseConnectionResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, meta sdk.ResourceMetaData) error {
client := meta.Client.DigitalTwins.TimeSeriesDatabaseConnectionsClient

var model TimeSeriesDatabaseConnectionModel
if err := meta.Decode(&model); err != nil {
return err
}

digitalTwinsId, err := timeseriesdatabaseconnections.ParseDigitalTwinsInstanceID(model.DigitalTwinsId)
if err != nil {
return err
}

id := timeseriesdatabaseconnections.NewTimeSeriesDatabaseConnectionID(digitalTwinsId.SubscriptionId, digitalTwinsId.ResourceGroupName, digitalTwinsId.ResourceName, model.Name)

existing, err := client.Get(ctx, id)
if !response.WasNotFound(existing.HttpResponse) {
if err != nil {
return fmt.Errorf("retrieving %s: %+v", id, err)
}
return meta.ResourceRequiresImport(m.ResourceType(), id)
}

properties := timeseriesdatabaseconnections.AzureDataExplorerConnectionProperties{
AdxDatabaseName: model.KustoDatabaseName,
AdxEndpointUri: model.KustoClusterUri,
AdxResourceId: model.KustoClusterId,
EventHubEndpointUri: model.EventhubNamespaceEndpointUri,
EventHubEntityPath: model.EventhubName,
EventHubNamespaceResourceId: model.EventhubNamespaceId,
}

if model.KustoTableName != "" {
properties.AdxTableName = utils.String(model.KustoTableName)
}

if model.EventhubConsumerGroupName != "" {
properties.EventHubConsumerGroup = utils.String(model.EventhubConsumerGroupName)
}

req := timeseriesdatabaseconnections.TimeSeriesDatabaseConnection{
Properties: properties,
}

if err := client.CreateOrUpdateThenPoll(ctx, id, req); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

meta.SetID(id)
return nil
},
}
}

func (m TimeSeriesDatabaseConnectionResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, meta sdk.ResourceMetaData) error {
id, err := timeseriesdatabaseconnections.ParseTimeSeriesDatabaseConnectionID(meta.ResourceData.Id())
if err != nil {
return err
}

client := meta.Client.DigitalTwins.TimeSeriesDatabaseConnectionsClient
result, err := client.Get(ctx, *id)
if err != nil {
return err
}

if result.Model == nil {
return fmt.Errorf("retrieving %s got nil model", id)
}

var output TimeSeriesDatabaseConnectionModel
output.Name = id.TimeSeriesDatabaseConnectionName
output.DigitalTwinsId = timeseriesdatabaseconnections.NewDigitalTwinsInstanceID(id.SubscriptionId, id.ResourceGroupName, id.ResourceName).ID()

if properties, ok := result.Model.Properties.(timeseriesdatabaseconnections.AzureDataExplorerConnectionProperties); ok {
output.EventhubName = properties.EventHubEntityPath
output.EventhubNamespaceEndpointUri = properties.EventHubEndpointUri
output.EventhubNamespaceId = properties.EventHubNamespaceResourceId
output.KustoClusterId = properties.AdxResourceId
output.KustoClusterUri = properties.AdxEndpointUri
output.KustoDatabaseName = properties.AdxDatabaseName

eventhubConsumerGroupName := "$Default"
if properties.EventHubConsumerGroup != nil {
eventhubConsumerGroupName = *properties.EventHubConsumerGroup
}
output.EventhubConsumerGroupName = eventhubConsumerGroupName

kustoTableName := ""
if properties.AdxTableName != nil {
kustoTableName = *properties.AdxTableName
}
output.KustoTableName = kustoTableName
}

return meta.Encode(&output)
},
}
}

func (m TimeSeriesDatabaseConnectionResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, meta sdk.ResourceMetaData) error {
id, err := timeseriesdatabaseconnections.ParseTimeSeriesDatabaseConnectionID(meta.ResourceData.Id())
if err != nil {
return err
}

client := meta.Client.DigitalTwins.TimeSeriesDatabaseConnectionsClient
if err = client.DeleteThenPoll(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}
return nil
},
}
}
Loading