-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade network connection monitor from v1 to v2 (#8640)
- Loading branch information
Neil Ye
authored
Nov 10, 2020
1 parent
d37a3f6
commit 57a17ff
Showing
12 changed files
with
2,092 additions
and
307 deletions.
There are no files selected for viewing
964 changes: 859 additions & 105 deletions
964
azurerm/internal/services/network/network_connection_monitor_resource.go
Large diffs are not rendered by default.
Oops, something went wrong.
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
36 changes: 36 additions & 0 deletions
36
azurerm/internal/services/network/parse/network_connection_monitor.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,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 | ||
} |
104 changes: 104 additions & 0 deletions
104
azurerm/internal/services/network/parse/network_connection_monitor_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,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
45
azurerm/internal/services/network/parse/network_watcher.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,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
69
azurerm/internal/services/network/parse/network_watcher_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,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) | ||
} | ||
} | ||
} |
Oops, something went wrong.