|
5 | 5 | package mq |
6 | 6 |
|
7 | 7 | import ( |
| 8 | + "encoding/base64" |
8 | 9 | "fmt" |
9 | 10 |
|
| 11 | + "github.com/crossplane/crossplane-runtime/pkg/errors" |
| 12 | + "github.com/crossplane/crossplane-runtime/pkg/fieldpath" |
| 13 | + xpresource "github.com/crossplane/crossplane-runtime/pkg/resource" |
10 | 14 | "github.com/crossplane/upjet/pkg/config" |
11 | 15 | "github.com/crossplane/upjet/pkg/registry" |
| 16 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
12 | 17 | ) |
13 | 18 |
|
14 | 19 | // Configure adds configurations for the mq group. |
@@ -74,4 +79,66 @@ func Configure(p *config.Provider) { //nolint:gocyclo |
74 | 79 | } |
75 | 80 | r.MetaResource.ArgumentDocs["console_access"] = `- (Optional) Setting consoleAccess will result in an update loop till the MQ Broker to which this user belongs is restarted.` |
76 | 81 | }) |
| 82 | + |
| 83 | + p.AddResourceConfigurator("aws_mq_configuration", func(r *config.Resource) { |
| 84 | + c := &repeatedDiffCheckerForData{} |
| 85 | + r.UpdateLoopPrevention = c |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +// repeatedDiffCheckerForData implements the UpdateLoopPrevention interface. |
| 90 | +// It is responsible for checking if the same diff related to the "data" |
| 91 | +// argument (in this case, XML content) appears repeatedly, which may indicate |
| 92 | +// a schema violation or invalid data scenario. If a repeated diff is detected, |
| 93 | +// it blocks the update process. |
| 94 | +type repeatedDiffCheckerForData struct { |
| 95 | + // previousDiff stores the base64-encoded string of the last diff. |
| 96 | + // This is used to compare against the current diff to detect repeated |
| 97 | + // updates. |
| 98 | + previousDiff *string |
| 99 | +} |
| 100 | + |
| 101 | +// UpdateLoopPreventionFunc checks for repeated diffs in the resource's "data" |
| 102 | +// attribute. If the diff has not changed since the previous reconciliation |
| 103 | +// loop, it blocks the update by returning an appropriate result. |
| 104 | +func (c *repeatedDiffCheckerForData) UpdateLoopPreventionFunc(diff *terraform.InstanceDiff, mg xpresource.Managed) (*config.UpdateLoopPreventResult, error) { //nolint:gocyclo // easier to follow as a unit |
| 105 | + // Skip processing if there is no diff, the diff is empty, or it is a destroy operation. |
| 106 | + if diff == nil || diff.Empty() || diff.Destroy || diff.Attributes == nil { |
| 107 | + return nil, nil |
| 108 | + } |
| 109 | + paved, err := fieldpath.PaveObject(mg) |
| 110 | + if err != nil { |
| 111 | + return nil, errors.Wrap(err, "cannot pave object") |
| 112 | + } |
| 113 | + // Retrieve the value of the "spec.forProvider.engineType" field from the paved object. |
| 114 | + // This field is used to determine the engine type of the MQ configuration. |
| 115 | + engineType, err := paved.GetString("spec.forProvider.engineType") |
| 116 | + if err != nil { |
| 117 | + return nil, errors.Wrap(err, "cannot get value of spec.forProvider.engineType") |
| 118 | + } |
| 119 | + // Check if the engine type is "ActiveMQ". If it is not, skip further checks and return nil. |
| 120 | + // This block ensures that the diff check logic only applies to resources with ActiveMQ engine type, |
| 121 | + // avoiding unnecessary diff processing for other engine types. |
| 122 | + if engineType != "ActiveMQ" { |
| 123 | + return nil, nil |
| 124 | + } |
| 125 | + // Encode the "data" attribute of the diff into a base64 string for comparison. |
| 126 | + var encodedDiff string |
| 127 | + if dataDiff, ok := diff.Attributes["data"]; ok { |
| 128 | + // Use GoString to get a string representation of the attribute. |
| 129 | + encodedDiff = base64.StdEncoding.EncodeToString([]byte(dataDiff.GoString())) |
| 130 | + } |
| 131 | + // If there is no previous diff recorded, store the current diff and allow the update to proceed. |
| 132 | + if c.previousDiff == nil { |
| 133 | + c.previousDiff = &encodedDiff |
| 134 | + return nil, nil |
| 135 | + } |
| 136 | + // If the current diff matches the previous diff, block the update and return a reason. |
| 137 | + if encodedDiff == *c.previousDiff { |
| 138 | + return &config.UpdateLoopPreventResult{Reason: "Repeated diff for the provided XML data, please check the XML content you have provided. " + |
| 139 | + "It may contain invalid or schema violating content."}, nil |
| 140 | + } |
| 141 | + // Update the previous diff with the current diff for the next reconciliation loop. |
| 142 | + c.previousDiff = &encodedDiff |
| 143 | + return nil, nil |
77 | 144 | } |
0 commit comments