Skip to content
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

azurerm_network_packet_capture - support for the target_type and scope properties #19385

Merged
merged 15 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions internal/services/network/migration/network_packet_capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/tombuildsstuff/kermit/sdk/network/2022-05-01/network"
)

var _ pluginsdk.StateUpgrade = NetworkPacketCaptureV0ToV1{}
Expand Down Expand Up @@ -38,6 +39,13 @@ func (NetworkPacketCaptureV0ToV1) Schema() map[string]*pluginsdk.Schema {
ForceNew: true,
},

"target_type": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Default: string(network.PacketCaptureTargetTypeAzureVM),
},

neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
"maximum_bytes_per_packet": {
Type: pluginsdk.TypeInt,
Optional: true,
Expand Down Expand Up @@ -123,6 +131,34 @@ func (NetworkPacketCaptureV0ToV1) Schema() map[string]*pluginsdk.Schema {
},
},
},

"scope": {
Type: pluginsdk.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"exclude": {
Type: pluginsdk.TypeList,
Optional: true,
ForceNew: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"include": {
Type: pluginsdk.TypeList,
Optional: true,
ForceNew: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
},
}
}

Expand Down
120 changes: 120 additions & 0 deletions internal/services/network/network_packet_capture_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ func resourceNetworkPacketCapture() *pluginsdk.Resource {
ForceNew: true,
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
},

"target_type": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Default: string(network.PacketCaptureTargetTypeAzureVM),
ValidateFunc: validation.StringInSlice([]string{
string(network.PacketCaptureTargetTypeAzureVM),
string(network.PacketCaptureTargetTypeAzureVMSS),
}, false),
},
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved

"maximum_bytes_per_packet": {
Type: pluginsdk.TypeInt,
Optional: true,
Expand Down Expand Up @@ -146,6 +157,36 @@ func resourceNetworkPacketCapture() *pluginsdk.Resource {
},
},
},

"scope": {
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
Type: pluginsdk.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"exclude": {
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
Type: pluginsdk.TypeList,
Optional: true,
ForceNew: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
},

"include": {
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
Type: pluginsdk.TypeList,
Optional: true,
ForceNew: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
},
},
}
}
Expand Down Expand Up @@ -190,6 +231,14 @@ func resourceNetworkPacketCaptureCreate(d *pluginsdk.ResourceData, meta interfac
},
}

if v, ok := d.GetOk("target_type"); ok {
properties.PacketCaptureParameters.TargetType = network.PacketCaptureTargetType(v.(string))
}

if v, ok := d.GetOk("scope"); ok {
properties.PacketCaptureParameters.Scope = expandNetworkPacketCaptureScope(v.([]interface{}))
}

future, err := client.Create(ctx, id.ResourceGroup, id.NetworkWatcherName, id.Name, properties)
if err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
Expand Down Expand Up @@ -231,6 +280,7 @@ func resourceNetworkPacketCaptureRead(d *pluginsdk.ResourceData, meta interface{

if props := resp.PacketCaptureResultProperties; props != nil {
d.Set("target_resource_id", props.Target)
d.Set("target_type", props.TargetType)
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
d.Set("maximum_bytes_per_packet", int(*props.BytesToCapturePerPacket))
d.Set("maximum_bytes_per_session", int(*props.TotalBytesPerSession))
d.Set("maximum_capture_duration", int(*props.TimeLimitInSeconds))
Expand All @@ -244,6 +294,14 @@ func resourceNetworkPacketCaptureRead(d *pluginsdk.ResourceData, meta interface{
if err := d.Set("filter", filters); err != nil {
return fmt.Errorf("setting `filter`: %+v", err)
}

scope, err := flattenNetworkPacketCaptureScope(props.Scope)
if err != nil {
return err
}
if err := d.Set("scope", scope); err != nil {
return fmt.Errorf(`setting "scope": %+v`, err)
}
}

return nil
Expand Down Expand Up @@ -374,3 +432,65 @@ func flattenNetworkPacketCaptureFilters(input *[]network.PacketCaptureFilter) []

return filters
}

func expandNetworkPacketCaptureScope(input []interface{}) *network.PacketCaptureMachineScope {
if len(input) == 0 || input[0] == nil {
return nil
}

raw := input[0].(map[string]interface{})
output := &network.PacketCaptureMachineScope{}

if exclude := raw["exclude"].([]interface{}); len(exclude) > 0 {
output.Exclude = utils.ExpandStringSlice(exclude)
}

if include := raw["include"].([]interface{}); len(include) > 0 {
output.Include = utils.ExpandStringSlice(include)
}

return output
}

func flattenNetworkPacketCaptureScope(input *network.PacketCaptureMachineScope) ([]interface{}, error) {
outputs := make([]interface{}, 0)
if input == nil || (input.Exclude == nil && input.Include == nil) || (len(*input.Exclude) == 0 && len(*input.Include) == 0) {
return outputs, nil
}

output := make(map[string]interface{}, 0)

excludedInstanceIds, err := flattenNetworkPacketCaptureScopeInstanceIds(input.Exclude)
if err != nil {
return nil, err
}
output["exclude"] = excludedInstanceIds

includedInstanceIds, err := flattenNetworkPacketCaptureScopeInstanceIds(input.Include)
if err != nil {
return nil, err
}
output["include"] = includedInstanceIds

outputs = append(outputs, output)

return outputs, nil
}

func flattenNetworkPacketCaptureScopeInstanceIds(input *[]string) ([]string, error) {
instances := make([]string, 0)
if input == nil {
return instances, nil
}

for _, instance := range *input {
instance, err := parse.VMSSInstanceID(instance)
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

instances = append(instances, instance.VirtualMachineName)
}

return instances, nil
}
123 changes: 123 additions & 0 deletions internal/services/network/network_packet_capture_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ func testAccNetworkPacketCapture_withFilters(t *testing.T) {
})
}

func testAccNetworkPacketCapture_scope(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_network_packet_capture", "test")
r := NetworkPacketCaptureResource{}

data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.scope(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (t NetworkPacketCaptureResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.PacketCaptureID(state.ID)
if err != nil {
Expand Down Expand Up @@ -182,6 +197,10 @@ resource "azurerm_virtual_machine" "test" {
os_profile_linux_config {
disable_password_authentication = false
}

lifecycle {
ignore_changes = [tags]
}
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
}

resource "azurerm_virtual_machine_extension" "test" {
Expand Down Expand Up @@ -318,3 +337,107 @@ resource "azurerm_network_packet_capture" "test" {
}
`, r.base(data), data.RandomString, data.RandomInteger)
}

func (NetworkPacketCaptureResource) scope(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-watcher-%d"
location = "%s"
}

resource "azurerm_network_watcher" "test" {
name = "acctestnw-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
}

resource "azurerm_virtual_network" "test" {
name = "acctvn-%d"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
}

resource "azurerm_subnet" "test" {
name = "internal"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.2.0/24"]
}

resource "azurerm_linux_virtual_machine_scale_set" "test" {
name = "acctestvmss-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku = "Standard_F2"
instances = 4
admin_username = "adminuser"
admin_password = "P@ssword1234!"
computer_name_prefix = "my-linux-computer-name-prefix"
upgrade_mode = "Automatic"

disable_password_authentication = false

source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}

os_disk {
storage_account_type = "Standard_LRS"
caching = "ReadWrite"
}

network_interface {
name = "example"
primary = true

ip_configuration {
name = "internal"
primary = true
subnet_id = azurerm_subnet.test.id
}
}

lifecycle {
ignore_changes = [identity, tags]
}
}

resource "azurerm_virtual_machine_scale_set_extension" "test" {
name = "network-watcher"
virtual_machine_scale_set_id = azurerm_linux_virtual_machine_scale_set.test.id
publisher = "Microsoft.Azure.NetworkWatcher"
type = "NetworkWatcherAgentLinux"
type_handler_version = "1.4"
auto_upgrade_minor_version = true
automatic_upgrade_enabled = true
}
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved

resource "azurerm_network_packet_capture" "test" {
name = "acctestpc-%d"
network_watcher_name = azurerm_network_watcher.test.name
resource_group_name = azurerm_resource_group.test.name
target_resource_id = azurerm_linux_virtual_machine_scale_set.test.id

storage_location {
file_path = "/var/captures/packet.cap"
}

target_type = "AzureVMSS"

scope {
include = ["0", "1"]
exclude = ["2", "3"]
}

depends_on = [azurerm_virtual_machine_scale_set_extension.test]
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestAccNetworkWatcher(t *testing.T) {
"storageAccountAndLocalDisk": testAccNetworkPacketCapture_storageAccountAndLocalDisk,
"withFilters": testAccNetworkPacketCapture_withFilters,
"requiresImport": testAccNetworkPacketCapture_requiresImport,
"scope": testAccNetworkPacketCapture_scope,
},
"FlowLog": {
"basic": testAccNetworkWatcherFlowLog_basic,
Expand Down
Loading