Skip to content

Commit

Permalink
Update segment of resource id for network packet capture (#8167)
Browse files Browse the repository at this point in the history
* 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
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 20 deletions.
134 changes: 134 additions & 0 deletions azurerm/internal/services/network/migration/network_packet_capture.go
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/network/migration"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/network/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand All @@ -26,6 +28,15 @@ func resourceArmNetworkPacketCapture() *schema.Resource {
State: schema.ImportStatePassthrough,
},

SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: migration.NetworkPacketCaptureV0Schema().CoreConfigSchema().ImpliedType(),
Upgrade: migration.NetworkPacketCaptureV0ToV1,
Version: 0,
},
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Expand Down Expand Up @@ -207,29 +218,25 @@ func resourceArmNetworkPacketCaptureRead(d *schema.ResourceData, meta interface{
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.NetworkPacketCaptureID(d.Id())
if err != nil {
return err
}

resourceGroup := id.ResourceGroup
watcherName := id.Path["networkWatchers"]
name := id.Path["NetworkPacketCaptures"]

resp, err := client.Get(ctx, resourceGroup, watcherName, name)
resp, err := client.Get(ctx, id.ResourceGroup, id.WatcherName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[WARN] Packet Capture %q (Watcher %q / Resource Group %q) %qw not found - removing from state", name, watcherName, resourceGroup, id)
log.Printf("[WARN] Packet Capture %q (Watcher %q / Resource Group %q) %qw not found - removing from state", id.Name, id.WatcherName, id.ResourceGroup, id)
d.SetId("")
return nil
}

return fmt.Errorf("Error reading Packet Capture %q (Watcher %q / Resource Group %q) %+v", name, watcherName, resourceGroup, err)
return fmt.Errorf("Error reading Packet Capture %q (Watcher %q / Resource Group %q) %+v", id.Name, id.WatcherName, id.ResourceGroup, err)
}

d.Set("name", name)
d.Set("network_watcher_name", watcherName)
d.Set("resource_group_name", resourceGroup)
d.Set("name", id.Name)
d.Set("network_watcher_name", id.WatcherName)
d.Set("resource_group_name", id.ResourceGroup)

if props := resp.PacketCaptureResultProperties; props != nil {
d.Set("target_resource_id", props.Target)
Expand All @@ -256,30 +263,26 @@ func resourceArmNetworkPacketCaptureDelete(d *schema.ResourceData, meta interfac
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.NetworkPacketCaptureID(d.Id())
if err != nil {
return err
}

resourceGroup := id.ResourceGroup
watcherName := id.Path["networkWatchers"]
name := id.Path["NetworkPacketCaptures"]

future, err := client.Delete(ctx, resourceGroup, watcherName, name)
future, err := client.Delete(ctx, id.ResourceGroup, id.WatcherName, id.Name)
if err != nil {
if response.WasNotFound(future.Response()) {
return nil
}

return fmt.Errorf("Error deleting Packet Capture %q (Watcher %q / Resource Group %q): %+v", name, watcherName, resourceGroup, err)
return fmt.Errorf("Error deleting Packet Capture %q (Watcher %q / Resource Group %q): %+v", id.Name, id.WatcherName, id.ResourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
if response.WasNotFound(future.Response()) {
return nil
}

return fmt.Errorf("Error waiting for the deletion of Packet Capture %q (Watcher %q / Resource Group %q): %+v", name, watcherName, resourceGroup, err)
return fmt.Errorf("Error waiting for the deletion of Packet Capture %q (Watcher %q / Resource Group %q): %+v", id.Name, id.WatcherName, id.ResourceGroup, err)
}

return nil
Expand Down
34 changes: 34 additions & 0 deletions azurerm/internal/services/network/parse/network_packet_capture.go
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
}
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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ resource "azurerm_virtual_machine" "test" {
resource "azurerm_virtual_machine_extension" "test" {
name = "network-watcher"
virtual_machine_id = azurerm_virtual_machine.src.id
virtual_machine_id = azurerm_virtual_machine.test.id
publisher = "Microsoft.Azure.NetworkWatcher"
type = "NetworkWatcherAgentLinux"
type_handler_version = "1.4"
Expand Down

0 comments on commit bbaa762

Please sign in to comment.