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

Upgrade network connection monitor from v1 to v2 #8640

Merged
merged 31 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from 21 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
978 changes: 857 additions & 121 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,40 @@
package parse

import (
"strings"

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

type NetworkConnectionMonitorId struct {
ResourceGroup string
WatcherId string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we have the WatcherName, we do we need to export this?

Copy link
Contributor Author

@neil-yechenwei neil-yechenwei Nov 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we have to use it to set watcher id to state file.

d.Set("network_watcher_id", id.WatcherId)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, can we opt in ID formatter for the network watcher, and use the NewXXX() function in place where you need to set the network watcher id?

Example: https://github.com/magodo/terraform-provider-azurerm/blob/azurerm_vpn_gateway_connection/azurerm/internal/services/network/vpn_gateway_connection_resource.go#L373-L373

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. updated

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,
WatcherId: strings.Split(input, "/connectionMonitors/")[0],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we insist on using the id.Popsegment to do the id parsing?

Copy link
Contributor Author

@neil-yechenwei neil-yechenwei Nov 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the id of network watcher. So I assume we have to do this since id.Popsegment cannot expose it.

}

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,91 @@
package parse

import (
"testing"
)

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",
WatcherId: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Network/networkWatchers/watcher1",
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)
}
}
}
29 changes: 29 additions & 0 deletions azurerm/internal/services/network/parse/network_watcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package parse

import "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
}
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