Skip to content

Conversation

kpatel71716
Copy link
Collaborator

@kpatel71716 kpatel71716 commented Aug 21, 2025

Description

JIRA: CLOUDP-339895, following the plan discussed with APIx team in CLOUDP-336156

As part of an ongoing effort to rename stream instances to stream workspaces, we will support a workspace_name field for the stream_connection resource and datasource that will function identically to 1instance_name`. To avoid confusion, only one of instance_name or workspace name will be allowed to be set

In a future major version of terraform, we will remove the instance_name field

Link to any related issue(s):

Type of change:

  • Bug fix (non-breaking change which fixes an issue). Please, add the "bug" label to the PR.
  • New feature (non-breaking change which adds functionality). Please, add the "enhancement" label to the PR. A migration guide must be created or updated if the new feature will go in a major version.
  • Breaking change (fix or feature that would cause existing functionality to not work as expected). Please, add the "breaking change" label to the PR. A migration guide must be created or updated.
  • This change requires a documentation update
  • Documentation fix/enhancement

Required Checklist:

  • I have signed the MongoDB CLA
  • I have read the contributing guides
  • I have checked that this change does not generate any credentials and that they are NOT accidentally logged anywhere.
  • I have added tests that prove my fix is effective or that my feature works per HashiCorp requirements
  • I have added any necessary documentation (if appropriate)
  • I have run make fmt and formatted my code
  • If changes include deprecations or removals I have added appropriate changelog entries.
  • If changes include removal or addition of 3rd party GitHub actions, I updated our internal document. Reach out to the APIx Integration slack channel to get access to the internal document.

Further comments

@kpatel71716 kpatel71716 marked this pull request as ready for review September 25, 2025 12:42
@kpatel71716 kpatel71716 requested review from a team as code owners September 25, 2025 12:42
Copy link
Contributor

APIx bot: a message has been sent to Docs Slack channel

resp.Schema = conversion.DataSourceSchemaFromResource(ResourceSchema(ctx), &conversion.DataSourceSchemaRequest{
RequiredFields: []string{"project_id", "instance_name", "connection_name"},
RequiredFields: []string{"project_id", "connection_name"},
OverridenFields: map[string]dsschema.Attribute{
Copy link
Collaborator

Choose a reason for hiding this comment

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

what do we mean here with overridden?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was unnecessary. I removed it

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually looks like we still need this so that terraform can use the workspace_name field in place of the instance name field. Without this we get errors due to terraform thinking that the datasource is being modified due to the mapping of the instance_name and workspace_name fields

"workspace_name": schema.StringAttribute{
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
Copy link
Member

@lantoli lantoli Sep 26, 2025

Choose a reason for hiding this comment

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

this means that if user changes workspace_name, Delete and Create will be called instead of Update.

qq: is this the behavior we want? there are 3 main alternatives:

  • Destroy and create a new resource (current approach)
  • Handle workspace_name changes in Update
  • Raise an error that workspace_name can't be updated

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Double checked and discussed with our team. This is intentional behavior. The same happens with the equivalent instance_name attribute today

@lantoli
Copy link
Member

lantoli commented Sep 26, 2025

tf-validate PR check fail will probably get fixed if you bring latest from master to this branch, there was a fix here: #3713

Copy link
Contributor

@carriecwk carriecwk left a comment

Choose a reason for hiding this comment

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

lgtm

@github-actions github-actions bot added the stale label Oct 2, 2025
@mongodb mongodb deleted a comment from github-actions bot Oct 2, 2025
@mongodb mongodb deleted a comment from github-actions bot Oct 2, 2025
@github-actions github-actions bot removed the stale label Oct 3, 2025
@kpatel71716 kpatel71716 requested a review from marcosuma October 3, 2025 07:34
Copy link
Collaborator

@LijieZhang1998 LijieZhang1998 left a comment

Choose a reason for hiding this comment

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

LGTM 👍 Just have a few small things.

@kpatel71716
Copy link
Collaborator Author

Hi @lantoli @marcosuma . Apologies for the noise + delay on this PR, we found a bug after initial review that caused cascading issues trying to fix, but the PR is passing all checks now.

Would y'all be able to re-review this PR? Especially this change to preserver backwards compatibility with instance_name


// Generate ID with workspace prefix only for workspace_name to distinguish from instance_name
// Keep original format for instance_name to maintain backward compatibility
var rID string
Copy link
Member

Choose a reason for hiding this comment

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

why do we have ID computed attribute? It was required in SDKv2 but it's not needed in TPF, for instance you can see the package paths:

"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"

ID makes sense if it's calculated and returned by the server - Atlas, but not so useful if generated in client, Terraform, and we're in TPF as this resource.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for catching this. It seems I added unnecessary validation that led to errors that made me think I had to make changes here. I'll revert those to keep things much more simple

Copy link
Member

Choose a reason for hiding this comment

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

can you remove ID attribute entirely?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am a bit confused on how that would work. I am probably missing something but from my understanding, we need to be able to set the instance_name/workspace_name fields or else we will always fail reading here

Is there a way around this using the basetypes and id packages above if we need the workspace/instance for the api call in the linked code?

Copy link
Member

Choose a reason for hiding this comment

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

yes, my point is why we have id in the schema

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We need it avoid errors converting the schema to streamconnection.TFStreamConnectionModel. While the model needs the ID field so that the ImportStateRequest can have the information it needs in the ID field.

If we had access to the connection_name and instance_name in the ImportState() function then we wouldn't need to construct and parse an ID with that information. I am not sure if there is an example of this anywhere in the codebase though. fwiw the original implementation also contained ID so it's existed before these changes. But happy to create a follow-up ticket for any refactor once I understand if there is any way we can avoid using ID

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@lantoli let me know if you have any thoughts on the above. Happy to explore any ideas in a follow-up. Or we can circle back to it later

Copy link
Member

@lantoli lantoli Oct 16, 2025

Choose a reason for hiding this comment

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

I know this ID is not from this PR so feel free not to do anything in this PR.

I think there is some confusion, ID in the Import is not the schema attribute but the value passed to the import command or block, e.g:


terraform import mongodbatlas_stream_connection.test "DefaultInstance-12251446ae5f3f6ec7968b13-NewConnection"      # ID will be "DefaultInstance-12251446ae5f3f6ec7968b13-NewConnection" 

terraform import mongodbatlas_stream_connection.test "hello"      # ID will be "hello" 


import {
  to = mongodbatlas_stream_connection.test
  id = "my-id" # ID will be "my-id"
}

So this ID is passed to ImportState, current code is getting this value, splitting by dashes and populating the corresponding values, but there is no need for ID schema attribute in the resource:

func (r *streamConnectionRS) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
	workspaceName, projectID, connectionName, err := splitStreamConnectionImportID(req.ID)
	if err != nil {
		resp.Diagnostics.AddError("error splitting stream connection import ID", err.Error())
		return
	}
	resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_name"), workspaceName)...)
	resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("workspace_name"), workspaceName)...)
	resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), projectID)...)
	resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("connection_name"), connectionName)...)
}

this implementation is correct, we get the "id" from import and fill it the main attributes, no need to fill it evertything as Read is called just after Import, so you only need to fill in the fields required by Read, as done in the current code.

but there is no need for ID in the resource schema or datasource schemas

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah ok, thank for you bearing with me and explaining! That makes sense to me, I'll create a follow-up ticket for our team to tackle this since to clean up our logic and link

WorkspaceName types.String `tfsdk:"workspace_name"`
Results []TFStreamConnectionModel `tfsdk:"results"`
PageNum types.Int64 `tfsdk:"page_num"`
ItemsPerPage types.Int64 `tfsdk:"items_per_page"`
Copy link
Member

Choose a reason for hiding this comment

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

not probably for this PR, but we're preferring not to expose pagination parameter and instead just return the whole list of items

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

that sgtm, I just took the existing pattern but will make a JIRA ticket for us to track cleaning this up

@kpatel71716 kpatel71716 requested a review from carriecwk October 16, 2025 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants