Skip to content

Commit

Permalink
Merge pull request #39024 from architec/b-mq-simplified-version-auto-…
Browse files Browse the repository at this point in the history
…minor-version-upgrade

fix: RabbitMQ 3.13 and ActiveMQ 5.18 Version Mismatch
  • Loading branch information
jar-b authored Sep 13, 2024
2 parents 9390420 + cda3a9f commit 07e6480
Show file tree
Hide file tree
Showing 5 changed files with 317 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/39024.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_mq_broker: Fix `engine_version` mismatch with RabbitMQ 3.13 and ActiveMQ 5.18 and above
```
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ require (
github.com/pquerna/otp v1.4.0
github.com/shopspring/decimal v1.4.0
golang.org/x/crypto v0.27.0
golang.org/x/mod v0.20.0
golang.org/x/text v0.18.0
golang.org/x/tools v0.24.0
gopkg.in/dnaeon/go-vcr.v3 v3.2.1
Expand Down Expand Up @@ -357,7 +358,6 @@ require (
go.opentelemetry.io/otel v1.30.0 // indirect
go.opentelemetry.io/otel/metric v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
Expand Down
45 changes: 42 additions & 3 deletions internal/service/mq/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
"github.com/mitchellh/copystructure"
"golang.org/x/mod/semver"
)

// @SDKResource("aws_mq_broker", name="Broker")
Expand Down Expand Up @@ -486,7 +487,7 @@ func resourceBrokerRead(ctx context.Context, d *schema.ResourceData, meta interf
d.Set("data_replication_mode", output.DataReplicationMode)
d.Set("deployment_mode", output.DeploymentMode)
d.Set("engine_type", output.EngineType)
d.Set(names.AttrEngineVersion, output.EngineVersion)
d.Set(names.AttrEngineVersion, normalizeEngineVersion(string(output.EngineType), aws.ToString(output.EngineVersion), aws.ToBool(output.AutoMinorVersionUpgrade)))
d.Set("host_instance_type", output.HostInstanceType)
d.Set("instances", flattenBrokerInstances(output.BrokerInstances))
d.Set("pending_data_replication_mode", output.PendingDataReplicationMode)
Expand Down Expand Up @@ -556,11 +557,15 @@ func resourceBrokerUpdate(ctx context.Context, d *schema.ResourceData, meta inte
}

if d.HasChanges(names.AttrConfiguration, "logs", names.AttrEngineVersion) {
engineType := d.Get("engine_type").(string)
engineVersion := d.Get(names.AttrEngineVersion).(string)
autoMinorVersionUpgrade := d.Get(names.AttrAutoMinorVersionUpgrade).(bool)

input := &mq.UpdateBrokerInput{
BrokerId: aws.String(d.Id()),
Configuration: expandConfigurationId(d.Get(names.AttrConfiguration).([]interface{})),
EngineVersion: aws.String(d.Get(names.AttrEngineVersion).(string)),
Logs: expandLogs(d.Get("engine_type").(string), d.Get("logs").([]interface{})),
EngineVersion: aws.String(normalizeEngineVersion(engineType, engineVersion, autoMinorVersionUpgrade)),
Logs: expandLogs(engineType, d.Get("logs").([]interface{})),
}

_, err := conn.UpdateBroker(ctx, input)
Expand Down Expand Up @@ -912,6 +917,40 @@ func DiffBrokerUsers(bId string, oldUsers, newUsers []interface{}) (cr []*mq.Cre
return cr, di, ur, nil
}

// normalizeEngineVersion normalizes the engine version depending on whether auto
// minor version upgrades are enabled
//
// Beginning with RabbitMQ 3.13 and ActiveMQ 5.18, brokers with `auto_minor_version_upgrade`
// set to `true` will automatically receive patch updates during scheduled maintenance
// windows. To account for automated changes to patch versions, the returned engine
// value is normalized to only major/minor when these conditions are met.
//
// If `auto_minor_version_upgrade` is not enabled, or the engine version is less than
// the version in which AWS began automatically applying patch upgrades, the engine
// version is returned unmodified.
//
// Ref: https://docs.aws.amazon.com/amazon-mq/latest/developer-guide/upgrading-brokers.html
// func normalizeEngineVersion(output *mq.DescribeBrokerOutput) string {
func normalizeEngineVersion(engineType string, engineVersion string, autoMinorVersionUpgrade bool) string {
majorMinor := semver.MajorMinor("v" + engineVersion)

// initial versions where `auto_minor_version_upgrade` triggers automatic
// patch updates, and only the major/minor should be supplied to the update API
minRabbit := "v3.13"
minActive := "v5.18"

if !autoMinorVersionUpgrade {
return engineVersion
}

if (strings.EqualFold(engineType, string(types.EngineTypeRabbitmq)) && semver.Compare(majorMinor, minRabbit) >= 0) ||
(strings.EqualFold(engineType, string(types.EngineTypeActivemq)) && semver.Compare(majorMinor, minActive) >= 0) {
return majorMinor[1:]
}

return engineVersion
}

func expandEncryptionOptions(l []interface{}) *types.EncryptionOptions {
if len(l) == 0 || l[0] == nil {
return nil
Expand Down
Loading

0 comments on commit 07e6480

Please sign in to comment.