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

Stream analytics output to CosmosDB #16441

Merged
merged 7 commits into from
Apr 26, 2022
Merged

Conversation

jiaweitao001
Copy link
Contributor

Added azurerm_stream_analytics_output_cosmosdb resource and corresponding tests.

Copy link
Member

@stephybun stephybun left a comment

Choose a reason for hiding this comment

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

Hi @jiaweitao001, thanks for your effort on this PR. Overall this is off to a good start, I left some questions and comments in-line, once those are fixed up we can take another look through this.
Thanks!


* `database` - (Required) The name or id of the CosmosDB database.

* `collection_name_pattern` - (Required) The collection name pattern for the collection to be used. It can be the name of the container, or a collection name format which contains an optional {partition} token.
Copy link
Member

Choose a reason for hiding this comment

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

As mentioned above I think it makes more sense for this to be named container_id - I also haven't seen an a test that provides this property with an optional partition token. Could you find an example of what that looks like? This might need to be added as an additional/separate field to the resource.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add another test for the scenario with an optional partition token.


* `document_id` - (Optional) The name of the field in output events used to specify the primary key which insert or update operations are based on.

* `partition_key` - (Optional) The name of the field in output events used to specify the key for partitioning output across collections. If 'collection_name_pattern' contains the {partition} token, this property is required to be specified.
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 required in specific circumstances should we be checking this in the create function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the partition_key does not exist under the described circumstance, an error message will show up to complain about the missing field, test proven.

Copy link
Member

@stephybun stephybun left a comment

Choose a reason for hiding this comment

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

Thanks for making those changes @jiaweitao001! There are still a few things to fix up before we can merge this though.

In addition to the comments left in-line I would like to come back to the comments left in my initial review on the collection_name_pattern. As mentioned this property is referred to as Container Name in the portal. In addition the field partition_key isn't exposed at all and as far as I can tell doesn't actually do anything.

Despite the fact that the API allows the creation of a name pattern with a partition key e.g. container-name{partition}, this results in an unusable connection with the following error.
image
After some digging I found this comment which says that this is deprecated and no longer supported - the portal reflects this behaviour.

As a result I think we should rename the property collection_name_pattern to container_name and remove the partition_key property. WDYT?

Comment on lines 43 to 47
func (id OutputId) StreamingJobID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.StreamAnalytics/streamingjobs/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.StreamingjobName)
}

Copy link
Member

Choose a reason for hiding this comment

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

These files are generated so this will be overwritten when the provider is built

Suggested change
func (id OutputId) StreamingJobID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.StreamAnalytics/streamingjobs/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.StreamingjobName)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will remove.

Comment on lines 181 to 184

state := OutputCosmosDBResourceModel{
Name: id.Name,
StreamAnalyticsJob: id.StreamingJobID(),
Copy link
Member

Choose a reason for hiding this comment

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

We should generate the streaming job ID here

Suggested change
state := OutputCosmosDBResourceModel{
Name: id.Name,
StreamAnalyticsJob: id.StreamingJobID(),
streamingJobId := parse.NewStreamingJobID(id.SubscriptionId, id.ResourceGroup, id.StreamingjobName)
state := OutputCosmosDBResourceModel{
Name: id.Name,
StreamAnalyticsJob: streamingJobId.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.

Sure. Will fix.

client := metadata.Client.StreamAnalytics.OutputsClient
subscriptionId := metadata.Client.Account.SubscriptionId

streamingJobStruct, err := parse.StreamingJobID(model.StreamAnalyticsJob)
Copy link
Member

Choose a reason for hiding this comment

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

The variable type Struct in the name is redundant information, we usually append these with Id to help with readability

Suggested change
streamingJobStruct, err := parse.StreamingJobID(model.StreamAnalyticsJob)
streamingJobId err := parse.StreamingJobID(model.StreamAnalyticsJob)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will rename.

return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

databaseIdStruct, err := cosmosParse.SqlDatabaseID(model.Database)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
databaseIdStruct, err := cosmosParse.SqlDatabaseID(model.Database)
databaseId, err := cosmosParse.SqlDatabaseID(model.Database)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will rename.

documentDbOutputProps := &streamanalytics.DocumentDbOutputDataSourceProperties{
AccountID: utils.String(databaseIdStruct.DatabaseAccountName),
AccountKey: utils.String(model.AccountKey),
Database: utils.String(model.Database),
Copy link
Member

Choose a reason for hiding this comment

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

This should actually be the database name and not the ID. Even though you can create the SA output with the ID, the connectivity test will fail.

Suggested change
Database: utils.String(model.Database),
Database: utils.String(databaseId.Name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will fix.

Comment on lines 252 to 260

if metadata.ResourceData.HasChangesExcept("name", "stream_analytics_job_id") {
props := streamanalytics.Output{
OutputProperties: &streamanalytics.OutputProperties{
Datasource: streamanalytics.DocumentDbOutputDataSource{
Type: streamanalytics.TypeBasicOutputDataSourceTypeMicrosoftStorageDocumentDB,
DocumentDbOutputDataSourceProperties: &streamanalytics.DocumentDbOutputDataSourceProperties{
AccountKey: &state.AccountKey,
Database: &state.Database,
Copy link
Member

Choose a reason for hiding this comment

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

Same here, we need to parse this value as an ID and send the name of the database

Suggested change
if metadata.ResourceData.HasChangesExcept("name", "stream_analytics_job_id") {
props := streamanalytics.Output{
OutputProperties: &streamanalytics.OutputProperties{
Datasource: streamanalytics.DocumentDbOutputDataSource{
Type: streamanalytics.TypeBasicOutputDataSourceTypeMicrosoftStorageDocumentDB,
DocumentDbOutputDataSourceProperties: &streamanalytics.DocumentDbOutputDataSourceProperties{
AccountKey: &state.AccountKey,
Database: &state.Database,
databaseId, err := cosmosParse.SqlDatabaseID(state.Database)
if err != nil {
return err
}
if metadata.ResourceData.HasChangesExcept("name", "stream_analytics_job_id") {
props := streamanalytics.Output{
OutputProperties: &streamanalytics.OutputProperties{
Datasource: streamanalytics.DocumentDbOutputDataSource{
Type: streamanalytics.TypeBasicOutputDataSourceTypeMicrosoftStorageDocumentDB,
DocumentDbOutputDataSourceProperties: &streamanalytics.DocumentDbOutputDataSourceProperties{
AccountKey: &state.AccountKey,
Database: &databaseId.Name,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will fix.

resource_group_name = azurerm_cosmosdb_account.test.resource_group_name
account_name = azurerm_cosmosdb_account.test.name
database_name = azurerm_cosmosdb_sql_database.updated.name
partition_key_path = "/definition/id"
Copy link
Member

Choose a reason for hiding this comment

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

This is a nested partition key which isn't supported by Stream Analytics, an error will be thrown when testing the connectivity

Suggested change
partition_key_path = "/definition/id"
partition_key_path = "/definition"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will fix.

resource_group_name = azurerm_cosmosdb_account.test.resource_group_name
account_name = azurerm_cosmosdb_account.test.name
database_name = azurerm_cosmosdb_sql_database.test.name
partition_key_path = "/definition/id"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
partition_key_path = "/definition/id"
partition_key_path = "/definition"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will fix.

@jiaweitao001
Copy link
Contributor Author

After testing on this param, I agree that this CollectionNamePattern can only be used as container name now. I'll rename it and remove the partition key param. Thanks for the effort on looking into this.

Copy link
Member

@stephybun stephybun left a comment

Choose a reason for hiding this comment

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

Thanks for @jiaweitao001! LGTM 🍰

@stephybun stephybun merged commit 88fc6bc into hashicorp:main Apr 26, 2022
stephybun added a commit that referenced this pull request Apr 26, 2022
@github-actions github-actions bot added this to the v3.4.0 milestone Apr 26, 2022
@github-actions
Copy link

This functionality has been released in v3.4.0 of the Terraform Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants