-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Update segment of resource id for network packet capture #8167
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 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
36 changes: 36 additions & 0 deletions
36
azurerm/internal/services/network/network_packet_capture_migration_resource.go
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,36 @@ | ||
package network | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
func ResourceNetworkPacketCaptureMigrateState(v int, is *terraform.InstanceState, _ interface{}) (*terraform.InstanceState, error) { | ||
switch v { | ||
case 0: | ||
log.Println("[INFO] Found AzureRM Network Packet Capture State v0; migrating to v1") | ||
return resourceNetworkPacketCaptureStateV0toV1(is) | ||
default: | ||
return is, fmt.Errorf("Unexpected schema version: %d", v) | ||
} | ||
} | ||
|
||
func resourceNetworkPacketCaptureStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) { | ||
if is.Empty() { | ||
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.") | ||
return is, nil | ||
} | ||
|
||
log.Printf("[DEBUG] ARM Network Packet Capture before Migration: %#v", is.Attributes) | ||
|
||
newID := strings.Replace(is.Attributes["id"], "/NetworkPacketCaptures/", "/packetCaptures/", 1) | ||
is.Attributes["id"] = newID | ||
is.ID = newID | ||
|
||
log.Printf("[DEBUG] ARM Network Packet Capture Attributes after State 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
34 changes: 34 additions & 0 deletions
34
azurerm/internal/services/network/parse/network_packet_capture.go
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,34 @@ | ||
package parse | ||
|
||
import "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
|
||
type NetworkPacketCaptureId struct { | ||
ResourceGroup string | ||
WatcherName string | ||
Name string | ||
} | ||
|
||
func NetworkPacketCaptureID(input string) (*NetworkPacketCaptureId, error) { | ||
id, err := azure.ParseAzureResourceID(input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
packetCapture := NetworkPacketCaptureId{ | ||
ResourceGroup: id.ResourceGroup, | ||
} | ||
|
||
if packetCapture.WatcherName, err = id.PopSegment("networkWatchers"); err != nil { | ||
return nil, err | ||
} | ||
|
||
if packetCapture.Name, err = id.PopSegment("packetCaptures"); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := id.ValidateNoEmptySegments(input); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &packetCapture, nil | ||
} |
90 changes: 90 additions & 0 deletions
90
azurerm/internal/services/network/parse/network_packet_capture_test.go
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 parse | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestNetworkPacketCaptureID(t *testing.T) { | ||
testData := []struct { | ||
Name string | ||
Input string | ||
Error bool | ||
Expect *NetworkPacketCaptureId | ||
}{ | ||
{ | ||
Name: "Empty", | ||
Input: "", | ||
Error: true, | ||
}, | ||
{ | ||
Name: "No Resource Groups Segment", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000", | ||
Error: true, | ||
}, | ||
{ | ||
Name: "No Resource Groups Value", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/", | ||
Error: true, | ||
}, | ||
{ | ||
Name: "Resource Group ID", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/", | ||
Error: true, | ||
}, | ||
{ | ||
Name: "Missing Network Watcher Key", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Network/networkWatchers/", | ||
Error: true, | ||
}, | ||
{ | ||
Name: "Missing Network Watcher Value", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Network/networkWatchers/watcher1", | ||
Error: true, | ||
}, | ||
{ | ||
Name: "Missing Network Packet Capture Key", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Network/networkWatchers/watcher1/packetCaptures", | ||
Error: true, | ||
}, | ||
{ | ||
Name: "Namespace Network Packet Capture Value", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Network/networkWatchers/watcher1/packetCaptures/packetCapture1", | ||
Error: false, | ||
Expect: &NetworkPacketCaptureId{ | ||
ResourceGroup: "group1", | ||
WatcherName: "watcher1", | ||
Name: "packetCapture1", | ||
}, | ||
}, | ||
{ | ||
Name: "Wrong Segment", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Network/networkWatchers/watcher1/NetworkPacketCaptures/packetCapture1", | ||
Error: true, | ||
}, | ||
} | ||
|
||
for _, v := range testData { | ||
t.Logf("[DEBUG] Testing %q", v.Name) | ||
|
||
actual, err := NetworkPacketCaptureID(v.Input) | ||
if err != nil { | ||
if v.Error { | ||
continue | ||
} | ||
|
||
t.Fatalf("Expected a value but got an error: %s", err) | ||
} | ||
|
||
if actual.Name != v.Expect.Name { | ||
t.Fatalf("Expected %q but got %q for Name", v.Expect.Name, actual.Name) | ||
} | ||
|
||
if actual.WatcherName != v.Expect.WatcherName { | ||
t.Fatalf("Expected %q but got %q for Name", v.Expect.WatcherName, actual.WatcherName) | ||
} | ||
|
||
if actual.ResourceGroup != v.Expect.ResourceGroup { | ||
t.Fatalf("Expected %q but got %q for Resource Group", v.Expect.ResourceGroup, actual.ResourceGroup) | ||
} | ||
} | ||
} |
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.
can you confirm this migration works correctly when provisioning a resource using 2.0.0 -> that then shows no diff using tis branch?
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.
@tombuildsstuff , yes. I tested the scenario you mentioned and it works fine.
The scenario:
tf plan
with this branchThere 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.
Hi @neil-yechenwei - Can you post the manual test results / output here? (Ideally with an
apply
rather than aplan
as this will actually update the state and exercise the code.).Thanks
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.
@jackofallops , sure.
tf apply
to create Network Packet Capture with provider.azurerm v2.0.0 (I have to manually update the name segment to "NetworkPacketCaptures" since the name segment in all api versions has been updated to "packetCaptures" such as api version 2018-08-01. So we cannot get old value anymore.)2. Switch provider.azurerm from v2.0.0 to this branch
3. Run
tf plan
4. Run
tf apply
5. Update the name of network packet capture in tfconfig. (I cannot test update scenario since this resource doesn't support update operation.)
6. Run
tf apply
7. Run
tf plan
8. Run
tf destroy