akshaykarle / terraform-provider-mongodbatlas

Terraform provider for MongoDB Atlas
Mozilla Public License 2.0
122 stars 54 forks source link

Make resources importable #14

Closed kristofferahl closed 6 years ago

kristofferahl commented 6 years ago

As we have existing clusters we would like to "terraform" it would be great if the resources are importable. I've only tried importing containers and projects but they are not importable at the moment. Any plans on adding support for that or should I start hacking on a new pull-request?

kristofferahl commented 6 years ago

For the project resource, it seems simply adding the following will make it importable:

Importer: &schema.ResourceImporter{
    State: schema.ImportStatePassthrough,
},
akshaykarle commented 6 years ago

I haven't started any work on making the mongo resources importable. If you'd like to give it a try I'd be happy to accept a PR :)

kristofferahl commented 6 years ago

I'll give it a go if can find the time.. I'll let you know if I start doing any work on it.

kristofferahl commented 6 years ago

I've hit a bit of a wall trying to get import to work for the following resource (container, database_user and ip_whitelist). When running import I get the follwing error (or similar for the other resources):

Error reading MongoDB Container 5ac70ec896e82178a5fc2568: invalid character '<' looking for beginning of value

This seems to be happening in the underlying mongodbatlas client. I'm new to go so I don't really know how to debug this. I'm guessing it's a body parsing error perhaps caused by the client not receiving JSON..?

Importing a project resource still works so it's probably not a matter of invalid credentials or permissions. It could perhaps be that the read functions don't have access to the group id during import and that this causes the underlying client to be called with an empty group id?

Any guidance on how to debug this would be much appreciated?

kristofferahl commented 6 years ago

OK, so I just confirmed that the group/project id is unavailable during import which makes sense. It only has the ID provided by the user.

The issue for us is the way that the MongoDB Atlas API is structured, requiring the group/project id as well as the resource id (whitelist id, container id etc). With the AWS provider you usually provide the ARN when importing a resource which is enough to find and import the resource. In this case however, we need the group/project id as well.

The only way I can think of doing imports of resources belonging to a group/project is that we provide the group id as part of the import statement.

terraform import mongodbatlas_container.test /project/<project-id>/resource/<resource-id>

In the end it would look something like this:

terraform import mongodbatlas_container.test /project/5abb8914c0c6e375991353cf/resource/179.154.224.127/32

What do you think, would this be a reasonable solution for imports?

kristofferahl commented 6 years ago

@akshaykarle I'm guessing you haven't had time to look at this yet?

akshaykarle commented 6 years ago

Hey @kristofferahl, I was on leave for the last 2 weeks and didn't check my emails much. Sorry about that. So for importing other resources I think we would have to provide the project id in addition to the resource id since there is no global identifiers for the mongo resources. So the approach you described above is okay:

terraform import mongodbatlas_container.test /project/5abb8914c0c6e375991353cf/resource/179.154.224.127/32
kristofferahl commented 6 years ago

@akshaykarle Thanks for the feedback... I'll continue down that path for to see if we can get it working.

akshaykarle commented 6 years ago

Hey @kristofferahl how is the import of resources implementation going? If you've finished the project import I'm happy to just accept that while you implement the others :)

Also if you need any help let me know. Thank you for your contribution!

akshaykarle commented 6 years ago

Resolved by https://github.com/akshaykarle/terraform-provider-mongodbatlas/pull/24