-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update segment of resource id for network packet capture (#8167)
* Update segment of network packet capture * Update code * Update code * Update code * Update code * Update code
- Loading branch information
Neil Ye
authored
Sep 11, 2020
1 parent
928462a
commit bbaa762
Showing
5 changed files
with
281 additions
and
20 deletions.
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
azurerm/internal/services/network/migration/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,134 @@ | ||
package migration | ||
|
||
import ( | ||
"log" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func NetworkPacketCaptureV0Schema() *schema.Resource { | ||
return &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"resource_group_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"network_watcher_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"target_resource_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"maximum_bytes_per_packet": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
ForceNew: true, | ||
Default: 0, | ||
}, | ||
|
||
"maximum_bytes_per_session": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
ForceNew: true, | ||
Default: 1073741824, | ||
}, | ||
|
||
"maximum_capture_duration": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
ForceNew: true, | ||
Default: 18000, | ||
}, | ||
|
||
"storage_location": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
ForceNew: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"file_path": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
|
||
"storage_account_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
|
||
"storage_path": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"filter": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
ForceNew: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"local_ip_address": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"local_port": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"protocol": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"remote_ip_address": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"remote_port": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func NetworkPacketCaptureV0ToV1(rawState map[string]interface{}, _ interface{}) (map[string]interface{}, error) { | ||
oldId := rawState["id"].(string) | ||
newId := strings.Replace(rawState["id"].(string), "/NetworkPacketCaptures/", "/packetCaptures/", 1) | ||
|
||
log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newId) | ||
|
||
rawState["id"] = newId | ||
|
||
return rawState, 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