Skip to content

Commit

Permalink
New Resource: azurerm_kusto_cosmosdb_data_connection (#22295)
Browse files Browse the repository at this point in the history
* add new resource `azurerm_kusto_cosmosdb_data_connection`

* update testcases

* update testcases

* update test cases

* update test cases

* document update and testcase fix

* update test cases

* update test cases

* update test cases

* update

* update document

* update document

* update document

* remove update function

* update format

* update documentation

* Update internal/services/kusto/kusto_cosmosdb_data_connection_resource.go

Co-authored-by: stephybun <[email protected]>

* Update internal/services/kusto/kusto_cosmosdb_data_connection_resource.go

Co-authored-by: stephybun <[email protected]>

* update resource `azurerm_kusto_cosmosdb_data_connection`

* update document for `azurerm_kusto_cosmosdb_data_connection`

* update create and read function for `azurerm_kusto_cosmosdb_data_connection`

* remove resource group property

* update document and fix error msg

---------

Co-authored-by: stephybun <[email protected]>
  • Loading branch information
liuwuliuyun and stephybun authored Jul 5, 2023
1 parent c90b2fb commit 5bcb3f4
Show file tree
Hide file tree
Showing 5 changed files with 681 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/provider/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration {
iothub.Registration{},
iotcentral.Registration{},
keyvault.Registration{},
kusto.Registration{},
labservice.Registration{},
loadbalancer.Registration{},
loganalytics.Registration{},
Expand Down
238 changes: 238 additions & 0 deletions internal/services/kusto/kusto_cosmosdb_data_connection_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
package kusto

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/cosmosdb/2023-04-15/cosmosdb"
"github.com/hashicorp/go-azure-sdk/resource-manager/kusto/2022-12-29/databases"
"github.com/hashicorp/go-azure-sdk/resource-manager/kusto/2022-12-29/dataconnections"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"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"
)

type CosmosDBDataConnectionModel struct {
Name string `tfschema:"name"`
Location string `tfschema:"location"`
CosmosDbContainerId string `tfschema:"cosmosdb_container_id"`
DatabaseId string `tfschema:"kusto_database_id"`
ManagedIdentityId string `tfschema:"managed_identity_id"`
MappingRuleName string `tfschema:"mapping_rule_name"`
RetrievalStartDate string `tfschema:"retrieval_start_date"`
TableName string `tfschema:"table_name"`
}

var _ sdk.Resource = CosmosDBDataConnectionResource{}

type CosmosDBDataConnectionResource struct{}

func (r CosmosDBDataConnectionResource) Arguments() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.DataConnectionName,
},
"location": commonschema.Location(),
"cosmosdb_container_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: cosmosdb.ValidateContainerID,
},
"kusto_database_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.DatabaseID,
},
"managed_identity_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},
"table_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"mapping_rule_name": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"retrieval_start_date": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
}
}

func (r CosmosDBDataConnectionResource) Attributes() map[string]*schema.Schema {
return map[string]*schema.Schema{}
}

func (r CosmosDBDataConnectionResource) ModelObject() interface{} {
return &CosmosDBDataConnectionModel{}
}

func (r CosmosDBDataConnectionResource) ResourceType() string {
return "azurerm_kusto_cosmosdb_data_connection"
}

func (r CosmosDBDataConnectionResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,

// the Func returns a function which retrieves the current state of the Resource Group into the state
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var model CosmosDBDataConnectionModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding %s: %+v", r.ResourceType(), err)
}

client := metadata.Client.Kusto.DataConnectionsClient
subscriptionId := metadata.Client.Account.SubscriptionId

cosmosDbContainerId, err := cosmosdb.ParseContainerID(model.CosmosDbContainerId)
if err != nil {
return err
}

kustoDatabaseId, err := databases.ParseDatabaseID(model.DatabaseId)
if err != nil {
return err
}

cosmosDbAccountResourceId := cosmosdb.NewDatabaseAccountID(subscriptionId, kustoDatabaseId.ResourceGroupName, cosmosDbContainerId.DatabaseAccountName)

id := dataconnections.NewDataConnectionID(subscriptionId, kustoDatabaseId.ResourceGroupName, kustoDatabaseId.ClusterName, kustoDatabaseId.DatabaseName, model.Name)

existing, err := client.Get(ctx, id)
if err != nil && !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
if !response.WasNotFound(existing.HttpResponse) {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

properties := dataconnections.CosmosDbDataConnectionProperties{
CosmosDbAccountResourceId: cosmosDbAccountResourceId.ID(),
CosmosDbContainer: cosmosDbContainerId.ContainerName,
CosmosDbDatabase: cosmosDbContainerId.SqlDatabaseName,
TableName: model.TableName,
ManagedIdentityResourceId: model.ManagedIdentityId,
}

if model.MappingRuleName != "" {
properties.MappingRuleName = &model.MappingRuleName
}

if model.RetrievalStartDate != "" {
properties.RetrievalStartDate = &model.RetrievalStartDate
}

dataConnection := dataconnections.CosmosDbDataConnection{
Location: pointer.To(location.Normalize(model.Location)),
Name: &model.Name,
Properties: &properties,
}

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

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

func (r CosmosDBDataConnectionResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Kusto.DataConnectionsClient

id, err := dataconnections.ParseDataConnectionID(metadata.ResourceData.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return metadata.MarkAsGone(id)
}

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

kustoDatabaseId := databases.NewDatabaseID(id.SubscriptionId, id.ResourceGroupName, id.ClusterName, id.DatabaseName)

state := CosmosDBDataConnectionModel{
Name: id.DataConnectionName,
DatabaseId: kustoDatabaseId.ID(),
}

if model := resp.Model; model != nil {
cosmosDbModel := (*model).(dataconnections.CosmosDbDataConnection)
state.Location = location.Normalize(*cosmosDbModel.Location)

if properties := cosmosDbModel.Properties; properties != nil {
cosmosdbAccountId, err := cosmosdb.ParseDatabaseAccountID(properties.CosmosDbAccountResourceId)
if err != nil {
return err
}
cosmosDbContainerId := cosmosdb.NewContainerID(id.SubscriptionId, id.ResourceGroupName, cosmosdbAccountId.DatabaseAccountName, properties.CosmosDbDatabase, properties.CosmosDbContainer)
state.CosmosDbContainerId = cosmosDbContainerId.ID()
state.TableName = properties.TableName
state.ManagedIdentityId = properties.ManagedIdentityResourceId
state.MappingRuleName = pointer.From(properties.MappingRuleName)
state.RetrievalStartDate = pointer.From(properties.RetrievalStartDate)
}
}

return metadata.Encode(&state)
},
}
}

func (r CosmosDBDataConnectionResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Kusto.DataConnectionsClient

id, err := dataconnections.ParseDataConnectionID(metadata.ResourceData.Id())
if err != nil {
return err
}

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

return nil
},
}
}

func (r CosmosDBDataConnectionResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return dataconnections.ValidateDataConnectionID
}
Loading

0 comments on commit 5bcb3f4

Please sign in to comment.