-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Fix BackendService group hash when instance groups use beta features #522
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func resourceComputeBackendServiceMigrateState( | ||
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) { | ||
if is.Empty() { | ||
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.") | ||
return is, nil | ||
} | ||
|
||
switch v { | ||
case 0: | ||
log.Println("[INFO] Found Compute Backend Service State v0; migrating to v1") | ||
is, err := migrateBackendServiceStateV0toV1(is) | ||
if err != nil { | ||
return is, err | ||
} | ||
return is, nil | ||
default: | ||
return is, fmt.Errorf("Unexpected schema version: %d", v) | ||
} | ||
} | ||
|
||
func migrateBackendServiceStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) { | ||
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes) | ||
|
||
oldHashToValue := map[string]map[string]interface{}{} | ||
for k, v := range is.Attributes { | ||
if !strings.HasPrefix(k, "backend.") || k == "backend.#" { | ||
continue | ||
} | ||
|
||
// Key is now of the form backend.%d.%s | ||
kParts := strings.Split(k, ".") | ||
|
||
// Sanity check: two parts should be there and <N> should be a number | ||
badFormat := false | ||
if len(kParts) != 3 { | ||
badFormat = true | ||
} else if _, err := strconv.Atoi(kParts[1]); err != nil { | ||
badFormat = true | ||
} | ||
|
||
if badFormat { | ||
return is, fmt.Errorf("migration error: found backend key in unexpected format: %s", k) | ||
} | ||
|
||
if oldHashToValue[kParts[1]] == nil { | ||
oldHashToValue[kParts[1]] = map[string]interface{}{} | ||
} | ||
oldHashToValue[kParts[1]][kParts[2]] = v | ||
} | ||
|
||
oldHashToNewHash := map[string]int{} | ||
for k, v := range oldHashToValue { | ||
oldHashToNewHash[k] = resourceGoogleComputeBackendServiceBackendHash(v) | ||
} | ||
|
||
values := map[string]string{} | ||
for k, v := range is.Attributes { | ||
if !strings.HasPrefix(k, "backend.") { | ||
continue | ||
} | ||
|
||
if k == "backend.#" { | ||
continue | ||
} | ||
|
||
// Key is now of the form backend.%d.%s | ||
kParts := strings.Split(k, ".") | ||
newKey := fmt.Sprintf("%s.%d.%s", kParts[0], oldHashToNewHash[kParts[1]], kParts[2]) | ||
values[newKey] = v | ||
delete(is.Attributes, k) | ||
} | ||
|
||
for k, v := range values { | ||
is.Attributes[k] = v | ||
} | ||
|
||
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes) | ||
return is, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package google | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestComputeBackendServiceMigrateState(t *testing.T) { | ||
cases := map[string]struct { | ||
StateVersion int | ||
Attributes map[string]string | ||
ExpectedAttributes map[string]string | ||
Meta interface{} | ||
}{ | ||
"v0 to v1": { | ||
StateVersion: 0, | ||
Attributes: map[string]string{ | ||
"backend.#": "1", | ||
"backend.242332812.group": "https://www.googleapis.com/compute/v1/projects/project_name/zones/zone_name/instances/instanceGroups/igName", | ||
"backend.242332812.balancing_mode": "UTILIZATION", | ||
"backend.242332812.max_utilization": "0.8", | ||
}, | ||
ExpectedAttributes: map[string]string{ | ||
"backend.#": "1", | ||
"backend.2573491210.group": "https://www.googleapis.com/compute/v1/projects/project_name/zones/zone_name/instances/instanceGroups/igName", | ||
"backend.2573491210.balancing_mode": "UTILIZATION", | ||
"backend.2573491210.max_utilization": "0.8", | ||
}, | ||
Meta: &Config{}, | ||
}, | ||
} | ||
|
||
for tn, tc := range cases { | ||
is := &terraform.InstanceState{ | ||
ID: "i-abc123", | ||
Attributes: tc.Attributes, | ||
} | ||
is, err := resourceComputeBackendServiceMigrateState( | ||
tc.StateVersion, is, tc.Meta) | ||
|
||
if err != nil { | ||
t.Fatalf("bad: %s, err: %#v", tn, err) | ||
} | ||
|
||
for k, v := range tc.ExpectedAttributes { | ||
if is.Attributes[k] != v { | ||
t.Fatalf( | ||
"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v", | ||
tn, k, v, k, is.Attributes[k], is.Attributes) | ||
} | ||
} | ||
|
||
for k, v := range is.Attributes { | ||
if tc.ExpectedAttributes[k] != v { | ||
t.Fatalf( | ||
"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v", | ||
tn, k, tc.ExpectedAttributes[k], k, v, is.Attributes) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestComputeBackendServiceMigrateState_empty(t *testing.T) { | ||
cases := map[string]struct { | ||
StateVersion int | ||
}{ | ||
"v0": { | ||
StateVersion: 0, | ||
}, | ||
} | ||
|
||
for tn, tc := range cases { | ||
var is *terraform.InstanceState | ||
var meta *Config | ||
|
||
// should handle nil | ||
is, err := resourceComputeBackendServiceMigrateState(tc.StateVersion, is, meta) | ||
|
||
if err != nil { | ||
t.Fatalf("bad %s, err: %#v", tn, err) | ||
} | ||
if is != nil { | ||
t.Fatalf("bad %s, expected nil instancestate, got: %#v", tn, is) | ||
} | ||
|
||
// should handle non-nil but empty | ||
is = &terraform.InstanceState{} | ||
is, err = resourceComputeBackendServiceMigrateState(tc.StateVersion, is, meta) | ||
|
||
if err != nil { | ||
t.Fatalf("bad %s, err: %#v", tn, err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could add also a test with a beta group. Even though it wouldn't add much value I guess...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eh, not going to bother on this one since this test doesn't make any API calls or really look at the version