Skip to content

Commit

Permalink
Upgrade network connection monitor from v1 to v2 (#8640)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Ye authored Nov 10, 2020
1 parent d37a3f6 commit 57a17ff
Show file tree
Hide file tree
Showing 12 changed files with 2,092 additions and 307 deletions.
964 changes: 859 additions & 105 deletions azurerm/internal/services/network/network_connection_monitor_resource.go

Large diffs are not rendered by default.

21 changes: 9 additions & 12 deletions azurerm/internal/services/network/network_watcher_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -100,24 +101,22 @@ func resourceArmNetworkWatcherRead(d *schema.ResourceData, meta interface{}) err
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.NetworkWatcherID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
name := id.Path["networkWatchers"]

resp, err := client.Get(ctx, resourceGroup, name)
resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Network Watcher %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("Error making Read request on Network Watcher %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
Expand All @@ -130,22 +129,20 @@ func resourceArmNetworkWatcherDelete(d *schema.ResourceData, meta interface{}) e
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.NetworkWatcherID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
name := id.Path["networkWatchers"]

future, err := client.Delete(ctx, resourceGroup, name)
future, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !response.WasNotFound(future.Response()) {
return fmt.Errorf("Error deleting Network Watcher %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("Error deleting Network Watcher %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for the deletion of Network Watcher %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("Error waiting for the deletion of Network Watcher %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package parse

import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type NetworkConnectionMonitorId struct {
ResourceGroup string
WatcherName string
Name string
}

func NetworkConnectionMonitorID(input string) (*NetworkConnectionMonitorId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

connectionMonitor := NetworkConnectionMonitorId{
ResourceGroup: id.ResourceGroup,
}

if connectionMonitor.WatcherName, err = id.PopSegment("networkWatchers"); err != nil {
return nil, err
}

if connectionMonitor.Name, err = id.PopSegment("connectionMonitors"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &connectionMonitor, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package parse

import (
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid"
)

var _ resourceid.Formatter = NetworkWatcherId{}

func TestNetworkWatcherIDFormatter(t *testing.T) {
subscriptionId := "12345678-1234-5678-1234-123456789012"
actual := NewNetworkWatcherID("group1", "watcher1").ID(subscriptionId)
expected := "/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Network/networkWatchers/watcher1"

if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestNetworkConnectionMonitorID(t *testing.T) {
testData := []struct {
Name string
Input string
Error bool
Expect *NetworkConnectionMonitorId
}{
{
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 Connection Monitor Key",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Network/networkWatchers/watcher1/connectionMonitors",
Error: true,
},
{
Name: "Namespace Network Connection Monitor Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Network/networkWatchers/watcher1/connectionMonitors/connectionMonitor1",
Error: false,
Expect: &NetworkConnectionMonitorId{
ResourceGroup: "group1",
WatcherName: "watcher1",
Name: "connectionMonitor1",
},
},
{
Name: "Wrong Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Network/networkWatchers/watcher1/NetworkConnectionMonitors/connectionMonitor1",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := NetworkConnectionMonitorID(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)
}
}
}
45 changes: 45 additions & 0 deletions azurerm/internal/services/network/parse/network_watcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package parse

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type NetworkWatcherId struct {
ResourceGroup string
Name string
}

func NetworkWatcherID(input string) (*NetworkWatcherId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

watcher := NetworkWatcherId{
ResourceGroup: id.ResourceGroup,
}

if watcher.Name, err = id.PopSegment("networkWatchers"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &watcher, nil
}

func (id NetworkWatcherId) ID(subscriptionId string) string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/networkWatchers/%s",
subscriptionId, id.ResourceGroup, id.Name)
}

func NewNetworkWatcherID(resourceGroup, name string) NetworkWatcherId {
return NetworkWatcherId{
ResourceGroup: resourceGroup,
Name: name,
}
}
69 changes: 69 additions & 0 deletions azurerm/internal/services/network/parse/network_watcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package parse

import (
"testing"
)

func TestNetworkWatcherID(t *testing.T) {
testData := []struct {
Name string
Input string
Error bool
Expect *NetworkWatcherId
}{
{
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",
Expect: &NetworkWatcherId{
ResourceGroup: "group1",
Name: "watcher1",
},
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := NetworkWatcherID(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.ResourceGroup != v.Expect.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expect.ResourceGroup, actual.ResourceGroup)
}
}
}
Loading

0 comments on commit 57a17ff

Please sign in to comment.