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_kusto_cosmosdb_data_connection #22295

Merged
merged 23 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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 @@ -154,6 +154,7 @@ func SupportedTypedServices() []sdk.TypedServiceRegistration {
iothub.Registration{},
iotcentral.Registration{},
keyvault.Registration{},
kusto.Registration{},
labservice.Registration{},
loadbalancer.Registration{},
loganalytics.Registration{},
Expand Down
248 changes: 248 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,248 @@
package kusto

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-helpers/resourcemanager/location"
"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"`
ResourceGroupName string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
CosmosDbAccountId string `tfschema:"cosmosdb_account_id"`
CosmosDbContainer string `tfschema:"cosmosdb_container"`
CosmosDbDatabase string `tfschema:"cosmosdb_database"`
ClusterName string `tfschema:"cluster_name"`
DatabaseName string `tfschema:"database_name"`
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,
},
"resource_group_name": commonschema.ResourceGroupName(),
liuwuliuyun marked this conversation as resolved.
Show resolved Hide resolved
"location": commonschema.Location(),
"cosmosdb_account_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},
"cosmosdb_container": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},
"cosmosdb_database": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},
liuwuliuyun marked this conversation as resolved.
Show resolved Hide resolved
"cluster_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ClusterName,
},
"database_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.DatabaseName,
},
liuwuliuyun marked this conversation as resolved.
Show resolved Hide resolved
"managed_identity_id": {
Copy link
Member

Choose a reason for hiding this comment

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

If this is the managed identity from the cosmosdb resource can we specify that in the name?

Suggested change
"managed_identity_id": {
"cosmosdb_managed_identity_id": {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not exactly, this identity id could be user assigned identity id or a system assigned identity id (resource id) in some cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You could see the similar managed_identity_id in other kusto data connection resources

Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
liuwuliuyun marked this conversation as resolved.
Show resolved Hide resolved
},
"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
id := dataconnections.NewDataConnectionID(subscriptionId, model.ResourceGroupName, model.ClusterName, model.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: model.CosmosDbAccountId,
CosmosDbContainer: model.CosmosDbContainer,
CosmosDbDatabase: model.CosmosDbDatabase,
TableName: model.TableName,
ManagedIdentityResourceId: model.ManagedIdentityId,
}

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

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

l := location.Normalize(model.Location)

dataConnection := dataconnections.CosmosDbDataConnection{
Location: &l,
liuwuliuyun marked this conversation as resolved.
Show resolved Hide resolved
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 fmt.Errorf("parsing %s: %+v", metadata.ResourceData.Id(), err)
liuwuliuyun marked this conversation as resolved.
Show resolved Hide resolved
}

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)
}

state := CosmosDBDataConnectionModel{
Name: id.DataConnectionName,
ResourceGroupName: id.ResourceGroupName,
ClusterName: id.ClusterName,
DatabaseName: id.DatabaseName,
}

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

if properties := cosmosDbModel.Properties; properties != nil {
state.CosmosDbAccountId = properties.CosmosDbAccountResourceId
state.CosmosDbContainer = properties.CosmosDbContainer
state.CosmosDbDatabase = properties.CosmosDbDatabase
state.TableName = properties.TableName
state.ManagedIdentityId = properties.ManagedIdentityResourceId

if properties.MappingRuleName != nil {
state.MappingRuleName = *properties.MappingRuleName
}

if properties.RetrievalStartDate != nil {
state.RetrievalStartDate = *properties.RetrievalStartDate
}
liuwuliuyun marked this conversation as resolved.
Show resolved Hide resolved
}
}

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