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

Bugfix SLO API: Update type for group_by to accept either string or array-of-strings #701

Merged
merged 4 commits into from
Aug 12, 2024

Conversation

gigerdo
Copy link
Member

@gigerdo gigerdo commented Aug 7, 2024

Starting with 8.14.0, the API can now return a list of strings instead of just a string. This causes a crash in the provider when reading from the cluster:

elasticstack % tf plan
elasticstack_kibana_slo.custom_kql: Refreshing state... [id=default/4712ef08-cb91-41ee-be7c-c31b2f02f0e1]

Planning failed. Terraform encountered an error while generating this plan.

╷
│ Error: json: cannot unmarshal array into Go struct field SloResponse.groupBy of type string
│
│   with elasticstack_kibana_slo.custom_kql,
│   on main.tf line 27, in resource "elasticstack_kibana_slo" "custom_kql":
│   27: resource "elasticstack_kibana_slo" "custom_kql" {
│

To be able to handle both old and new APIs, the spec has been updated to accept either a string or a list of strings.
This update was done manually, as the official Kibana API has not yet been updated to reflect this change.

This change only ensures that the provider doesn't crash with the new API, there is no support yet to configure multiple group-by arguments in the schema.

This is also the reason why the change didn't really cause many code changes: We are not actually using the group-by when reading. It is only passed through when creating/updating.

gigerdo added 2 commits August 7, 2024 17:43
- Starting with 8.14.0, the API can now return a list of strings instead of just a string.
- To be able to handle both old and new APIs, the spec has been updated to accept either a string or a list of strings.
- This change only ensures that the provider doesn't crash with the new API, there is no support yet for multiple group-by arguments.
@gigerdo gigerdo marked this pull request as ready for review August 7, 2024 15:48
@gigerdo gigerdo requested review from a team and tobio August 7, 2024 15:48
@gigerdo gigerdo changed the title Bugfix: Update SLO api type for groupBy to match latest stack versions. Bugfix SLO API: Update type for group_by to accept either string or array-of-strings Aug 7, 2024
Copy link
Member

@tobio tobio left a comment

Choose a reason for hiding this comment

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

Have we tested importing this resource from an SLO defined in the 8.14 UI?

The acceptance tests are passing against 8.14 currently so I expect that if groupBy is specified as a string during creation then it's returned as a string. I'm not sure we're properly exercising this code with the current tests.

We are not actually using the group-by when reading. It is only passed through when creating/updating.

This sounds like a bug, and looking at the code isn't one that's that hard to fix. I wonder if it's worth updating the schema for group_by to be a []string and fully fixing this attribute whilst we're here?

@gigerdo
Copy link
Member Author

gigerdo commented Aug 9, 2024

The acceptance tests are passing against 8.14 currently so I expect that if groupBy is specified as a string during creation then it's returned as a string. I'm not sure we're properly exercising this code with the current tests.

At least from the manual tests I did, the API always returns a list.

This sounds like a bug, and looking at the code isn't one that's that hard to fix. I wonder if it's worth updating the schema for group_by to be a []string and fully fixing this attribute whilst we're here?

I agree that it is another bug. The main effect is that TF doesn't see a diff in the groupBy if it is changed on the server side.

But wouldn't this break the config for all current users of the provider? Meaning they have to change the config after updating. Though at some point we have to switch over to a list anyway.

@tobio
Copy link
Member

tobio commented Aug 9, 2024

At least from the manual tests I did, the API always returns a list.

Can we double check that, at least one of the acceptance tests sets group by and I'd expect it to fail if this were the case right?

But wouldn't this break the config for all current users of the provider? Meaning they have to change the config after updating. Though at some point we have to switch over to a list anyway.

There's been a couple of breaking changes in the past, and this would be relatively simple to adjust to (just wrapping the current value in []). I think in theory we shouldn't shy away from making this change.

Another part of the problem is that the schema suggests group by could be a list, but for 8.13 or lower the provider will have to take only the first item and explicitly set it as a plain string.

@gigerdo
Copy link
Member Author

gigerdo commented Aug 9, 2024

Another part of the problem is that the schema suggests group by could be a list, but for 8.13 or lower the provider will have to take only the first item and explicitly set it as a plain string.

Yeah, I think I will make the change. I have to check what the error is if a list is sent when it isn't supported. Otherwise I guess we need to have a validation for <8.14 to only allow one item in the list.

When serializing I could probably make it work for every case by just always using the string if the list has exactly one element (as the new API supports both string and list-of-string)

Can we double check that, at least one of the acceptance tests sets group by and I'd expect it to fail if this were the case right?

Yeah it should be failing on main 🤔 I have to check why that isn't the case.

@gigerdo
Copy link
Member Author

gigerdo commented Aug 9, 2024

There's been a couple of breaking changes in the past, and this would be relatively simple to adjust to (just wrapping the current value in []). I think in theory we shouldn't shy away from making this change.

Seems like it isn't enough to just update the config. There also needs to be a state migration. It looks like this is possible in the sdk as well, although it is necessary to copy the whole schema definition.

@tobio
Copy link
Member

tobio commented Aug 11, 2024

There also needs to be a state migration

Yea, of course. I'm happy if you want to get this one merged/released and we can fix the rest fixed up in a follow up.

@gigerdo
Copy link
Member Author

gigerdo commented Aug 12, 2024

Yeah makes sense to split it into two parts. Then we can release a patch fix and maybe put the breaking change into a new minor version.

@gigerdo gigerdo merged commit 6c74e64 into elastic:main Aug 12, 2024
19 checks passed
@gigerdo gigerdo deleted the fix-slo-groupby-api-type branch August 12, 2024 09:20
tobio added a commit that referenced this pull request Aug 26, 2024
* origin/main:
  Add support for the `alert_delay` param in the Create Rule API (#715)
  chore: prepare release v0.11.6 (#716)
  Validate that mappings are a JSON object, not just valid json (#719)
  fix: move all resources in one namespace for tcp monitor acc tests (#717)
  Bump github.com/golangci/golangci-lint from 1.59.1 to 1.60.1 in /tools (#714)
  Bump github.com/docker/docker in /tools (#718)
  Bump github.com/goreleaser/goreleaser from 1.26.1 to 1.26.2 in /tools (#642)
  Bump github.com/hashicorp/terraform-plugin-framework (#705)
  Add kibana synthetics http and tcp monitor resources (#699)
  Kibana spaces data source (#682)
  Use ephemeral github token for build. (#712)
  chore: 8.15.0 is here - lets try it out (#708)
  Update changelog for 0.11.5
  Bump version for 0.11.5 (#706)
  Bugfix SLO API: Update type for `group_by` to accept either string or array-of-strings (#701)
  Support `restriction` in `elasticstack_elasticsearch_security_api_key` (#577)
  chore: follow-up CR changes for synthetics private location resource (#697)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants