-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
9a34aee
add new resource `azurerm_kusto_cosmosdb_data_connection`
liuwuliuyun df98a48
update testcases
liuwuliuyun e7fc946
update testcases
liuwuliuyun 8317ab8
update test cases
liuwuliuyun f8d058a
update test cases
liuwuliuyun c9a03c0
document update and testcase fix
liuwuliuyun e47776b
update test cases
liuwuliuyun 72bdd1e
update test cases
liuwuliuyun 2ca14d8
update test cases
liuwuliuyun 0297804
update
liuwuliuyun cc31cc8
update document
liuwuliuyun faaf4fd
update document
liuwuliuyun e7fd0d9
update document
liuwuliuyun fce953f
remove update function
liuwuliuyun 4daec9b
update format
liuwuliuyun 5892a69
update documentation
liuwuliuyun effe9f8
Update internal/services/kusto/kusto_cosmosdb_data_connection_resourc…
liuwuliuyun 07e2d1b
Update internal/services/kusto/kusto_cosmosdb_data_connection_resourc…
liuwuliuyun f26383e
update resource `azurerm_kusto_cosmosdb_data_connection`
liuwuliuyun 80b8feb
update document for `azurerm_kusto_cosmosdb_data_connection`
liuwuliuyun ae294b8
update create and read function for `azurerm_kusto_cosmosdb_data_conn…
liuwuliuyun dfe5ab3
remove resource group property
liuwuliuyun e4ab88b
update document and fix error msg
liuwuliuyun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
238 changes: 238 additions & 0 deletions
238
internal/services/kusto/kusto_cosmosdb_data_connection_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
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 | ||
|
||
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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