Skip to content

Add component template resource #39

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

Merged
merged 4 commits into from
Feb 10, 2022
Merged

Add component template resource #39

merged 4 commits into from
Feb 10, 2022

Conversation

xunleii
Copy link
Contributor

@xunleii xunleii commented Jan 20, 2022

This PR adds new component_template resource to manage components template through TF resources.
This is a "naive" implementation of this resource, mainly based on the index_template one (many part are copy/paste).

Of course, if needed, I can spend more time to see how can I simplify by sharing code between index_template and component_template, but could decrease the readability.

NOTE: this PR is "in conflict" with the https://github.com./elastic/terraform-provider-elasticstack/pull/38/files on how we flatten struct; I use a method directly on models and @olksdr uses a function that take the object as parameter. Because its PRs has been already reviewed, I can adapt my code for its PR to avoid conflict (keeping its work or using method on models, whichever you and @olksdr prefer).
NOTE²: https://github.com./elastic/terraform-provider-elasticstack/blob/main/internal/elasticsearch/index/template.go#L220 will panic if no aliases are defined on index_template. In order to avoid any conflict with https://github.com./elastic/terraform-provider-elasticstack/pull/38/files, I didn't made any changes to fix that. But if my PR is be based on the later one, I will fix it.
NOTE³: My changes need more tests, so I'll add them tomorrow

@elasticmachine
Copy link

Since this is a community submitted pull request, a Jenkins build has not been kicked off automatically. Can an Elastic organization member please verify the contents of this patch and then kick off a build manually?

@cla-checker-service
Copy link

cla-checker-service bot commented Jan 20, 2022

💚 CLA has been signed

@olksdr
Copy link
Contributor

olksdr commented Jan 21, 2022

Hey @xunleii! Thanks for your contribution!

NOTE²: https://github.com./elastic/terraform-provider-elasticstack/blob/main/internal/elasticsearch/index/template.go#L220 will panic if no aliases are defined on index_template.

Could you, please, also provide the error you get? It would be great if you could open an issue with error and possibly traces, your TF version, the resource definition you're using to test this and system you're running this on.

@olksdr olksdr added the enhancement New feature or request label Jan 21, 2022
@xunleii
Copy link
Contributor Author

xunleii commented Jan 21, 2022

Hi @olksdr, thanks for your response (and thanks to the team for this provider).
For the panic, I probably missed something when I ran the acceptance tests yesterday; I can no longer reproduce the panic on index_template on a clean environment (nor on component_template if I delete the last commit)... so should probably be a PEBCAK 😅
Also, is there anything to do to validate the CLA verification? I signed the agreement yesterday but this step remains failed (probably missed something).

@olksdr
Copy link
Contributor

olksdr commented Feb 2, 2022

Hey @xunleii ,
could you, please, merge the main in and resolve the conflicts in your branch? So we could review your changes.

NOTE: this PR is "in conflict" with the https://github.com./elastic/terraform-provider-elasticstack/pull/38/files on how we flatten struct; I use a method directly on models and @olksdr uses a function that take the object as parameter. Because its PRs has been already reviewed, I can adapt my code for its PR to avoid conflict (keeping its work or using method on models, whichever you and @olksdr prefer).

I would prefer to use existing function, which is already used in the index_template and index resources. But proposed changes also make sense.

It would be better to keep this PR simpler and just introduce the new resource, with tests and docs and move any refactoring into separate PR. With refactoring it would be great to move many of free standing functions into the models methods.

@olksdr olksdr added the Elasticsearch Elasticsearch related APIs label Feb 3, 2022
@xunleii xunleii force-pushed the main branch 2 times, most recently from bfc4ef2 to 0beb7d4 Compare February 3, 2022 18:35
@xunleii
Copy link
Contributor Author

xunleii commented Feb 3, 2022

Hi @olksdr,

I rebased my branch on main and removed all changes not related to the main subject (adding the component_template).
If I have some free time, I will try to make the PR about refactoring (but I a bit busy this time).

Thanks for your review !

@xunleii xunleii marked this pull request as ready for review February 3, 2022 18:42
@xunleii xunleii requested a review from olksdr as a code owner February 3, 2022 18:42
@olksdr olksdr requested a review from Crazybus February 4, 2022 05:14
@olksdr olksdr self-assigned this Feb 4, 2022
Copy link
Contributor

@olksdr olksdr left a comment

Choose a reason for hiding this comment

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

Looks nice!

I've added few comments about documentation generation, and the placement of the new resource.

@@ -0,0 +1,101 @@
---
Copy link
Contributor

Choose a reason for hiding this comment

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

All the documentation in the docs/ folder is generated and should not be changed manually. See how it's done for template resource:

---
subcategory: "Index"
layout: ""
page_title: "Elasticstack: elasticstack_elasticsearch_index_template Resource"
description: |-
Creates or updates an index template.
---
# Resource: elasticstack_elasticsearch_index_template
Creates or updates an index template. Index templates define settings, mappings, and aliases that can be applied automatically to new indices. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-template.html

you could copy this template and change the specific parts in names and the paths to the examples and run make gen to generate the required files, which you then can add into this PR as well.

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 used make docs-generate to generate this file, but without adding the template file. I'll add it and regenete it the new documentation just after

@@ -0,0 +1,290 @@
package cluster
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you, please, move this file into internal/elasticsearch/index/ folder (index module), where the template resource is located, just to keep them all together?


func ResourceComponentTemplate() *schema.Resource {
// NOTE: component_template and index_template uses the same schema
componentTemplateSchema := map[string]*schema.Schema{
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, also add the ID as Computed value:

Suggested change
componentTemplateSchema := map[string]*schema.Schema{
componentTemplateSchema := map[string]*schema.Schema{
"id": {
Description: "Internal identifier of the resource",
Type: schema.TypeString,
Computed: true,
},

@olksdr
Copy link
Contributor

olksdr commented Feb 4, 2022

ok to test

@olksdr
Copy link
Contributor

olksdr commented Feb 4, 2022

And one more thing:
please add the record in CHANGELOG.md, like here

- New resource `elasticstack_elasticsearch_data_stream` to manage Elasticsearch [data streams](https://www.elastic.co/guide/en/elasticsearch/reference/current/data-streams.html) ([#45](https://github.com./elastic/terraform-provider-elasticstack/pull/45))

@xunleii
Copy link
Contributor Author

xunleii commented Feb 5, 2022

Hi @olksdr, thanks for your review. I made all changes that you request me and put them inside two fixup commits if you want to check these changes only.
When all will be good, I will squash them to clean the git history.

@xunleii xunleii requested a review from olksdr February 5, 2022 17:03
Copy link
Contributor

@olksdr olksdr 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 addressing my review comments.

Only one small thing left to do, see my 2 proposed suggestion, to handle the situation when the resource was deleted outside of the TF. Those 2 pieces of. code also need to be added in, and we are good to merge this new resource.

And thanks again for working on this!

@xunleii
Copy link
Contributor Author

xunleii commented Feb 10, 2022

Hi, I applied your suggestion and resolved the conflict on CHANGELOG. Thanks

Copy link
Contributor

@olksdr olksdr left a comment

Choose a reason for hiding this comment

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

lgtm

@olksdr
Copy link
Contributor

olksdr commented Feb 10, 2022

jenkins test this

@olksdr olksdr merged commit 025037a into elastic:main Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Elasticsearch Elasticsearch related APIs enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants