-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New resource:
azurerm_virtual_hub_bgp_connection
(#8959)
- Loading branch information
Neil Ye
committed
Nov 9, 2020
1 parent
b58ca77
commit d660a4a
Showing
8 changed files
with
592 additions
and
0 deletions.
There are no files selected for viewing
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
38 changes: 38 additions & 0 deletions
38
azurerm/internal/services/network/parse/virtual_hub_bgp_connection.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,38 @@ | ||
package parse | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
) | ||
|
||
type VirtualHubBgpConnectionId struct { | ||
ResourceGroup string | ||
VirtualHubName string | ||
Name string | ||
} | ||
|
||
func VirtualHubBgpConnectionID(input string) (*VirtualHubBgpConnectionId, error) { | ||
id, err := azure.ParseAzureResourceID(input) | ||
if err != nil { | ||
return nil, fmt.Errorf("parsing virtualHubBgpConnection ID %q: %+v", input, err) | ||
} | ||
|
||
virtualHubBgpConnection := VirtualHubBgpConnectionId{ | ||
ResourceGroup: id.ResourceGroup, | ||
} | ||
|
||
if virtualHubBgpConnection.VirtualHubName, err = id.PopSegment("virtualHubs"); err != nil { | ||
return nil, err | ||
} | ||
|
||
if virtualHubBgpConnection.Name, err = id.PopSegment("bgpConnections"); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := id.ValidateNoEmptySegments(input); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &virtualHubBgpConnection, nil | ||
} |
77 changes: 77 additions & 0 deletions
77
azurerm/internal/services/network/parse/virtual_hub_bgp_connection_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,77 @@ | ||
package parse | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestVirtualHubBgpConnectionID(t *testing.T) { | ||
testData := []struct { | ||
Name string | ||
Input string | ||
Expected *VirtualHubBgpConnectionId | ||
}{ | ||
{ | ||
Name: "Empty", | ||
Input: "", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "No Resource Groups Segment", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "No Resource Groups Value", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "Resource Group ID", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "Missing VirtualHubBgpConnection Value", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resourceGroup1/providers/Microsoft.Network/virtualHubs/virtualHub1/bgpConnections", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "VirtualHubBgpConnection ID", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resourceGroup1/providers/Microsoft.Network/virtualHubs/virtualHub1/bgpConnections/connection1", | ||
Expected: &VirtualHubBgpConnectionId{ | ||
ResourceGroup: "resourceGroup1", | ||
VirtualHubName: "virtualHub1", | ||
Name: "connection1", | ||
}, | ||
}, | ||
{ | ||
Name: "Wrong Casing", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resourceGroup1/providers/Microsoft.Network/virtualHubs/virtualHub1/BgpConnections/connection1", | ||
Expected: nil, | ||
}, | ||
} | ||
|
||
for _, v := range testData { | ||
t.Logf("[DEBUG] Testing %q..", v.Name) | ||
|
||
actual, err := VirtualHubBgpConnectionID(v.Input) | ||
if err != nil { | ||
if v.Expected == nil { | ||
continue | ||
} | ||
t.Fatalf("Expected a value but got an error: %s", err) | ||
} | ||
|
||
if actual.ResourceGroup != v.Expected.ResourceGroup { | ||
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup) | ||
} | ||
|
||
if actual.VirtualHubName != v.Expected.VirtualHubName { | ||
t.Fatalf("Expected %q but got %q for VirtualHubName", v.Expected.VirtualHubName, actual.VirtualHubName) | ||
} | ||
|
||
if actual.Name != v.Expected.Name { | ||
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) | ||
} | ||
} | ||
} |
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
182 changes: 182 additions & 0 deletions
182
azurerm/internal/services/network/tests/virtual_hub_bgp_connection_resource_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,182 @@ | ||
package tests | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" | ||
"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/utils" | ||
) | ||
|
||
func TestAccAzureRMVirtualHubBgpConnection_basic(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "azurerm_virtual_hub_bgp_connection", "test") | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.PreCheck(t) }, | ||
Providers: acceptance.SupportedProviders, | ||
CheckDestroy: testCheckAzureRMVirtualHubBgpConnectionDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAzureRMVirtualHubBgpConnection_basic(data), | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMVirtualHubBgpConnectionExists(data.ResourceName), | ||
), | ||
}, | ||
data.ImportStep(), | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAzureRMVirtualHubBgpConnection_requiresImport(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "azurerm_virtual_hub_bgp_connection", "test") | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.PreCheck(t) }, | ||
Providers: acceptance.SupportedProviders, | ||
CheckDestroy: testCheckAzureRMVirtualHubBgpConnectionDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAzureRMVirtualHubBgpConnection_basic(data), | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMVirtualHubBgpConnectionExists(data.ResourceName), | ||
), | ||
}, | ||
data.RequiresImportErrorStep(testAccAzureRMVirtualHubBgpConnection_requiresImport), | ||
}, | ||
}) | ||
} | ||
|
||
func testCheckAzureRMVirtualHubBgpConnectionExists(resourceName string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
client := acceptance.AzureProvider.Meta().(*clients.Client).Network.VirtualHubBgpConnectionClient | ||
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext | ||
|
||
rs, ok := s.RootModule().Resources[resourceName] | ||
if !ok { | ||
return fmt.Errorf("virtualHubBgpConnection not found: %s", resourceName) | ||
} | ||
|
||
id, err := parse.VirtualHubBgpConnectionID(rs.Primary.ID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp, err := client.Get(ctx, id.ResourceGroup, id.VirtualHubName, id.Name); err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("bad: Network VirtualHubBgpConnection %q does not exist", id.Name) | ||
} | ||
|
||
return fmt.Errorf("bad: Get on Network.VirtualHubBgpConnectionClient: %+v", err) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testCheckAzureRMVirtualHubBgpConnectionDestroy(s *terraform.State) error { | ||
client := acceptance.AzureProvider.Meta().(*clients.Client).Network.VirtualHubBgpConnectionClient | ||
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "azurerm_virtual_hub_bgp_connection" { | ||
continue | ||
} | ||
|
||
id, err := parse.VirtualHubBgpConnectionID(rs.Primary.ID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp, err := client.Get(ctx, id.ResourceGroup, id.VirtualHubName, id.Name); err != nil { | ||
if !utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("bad: Get on Network.VirtualHubBgpConnectionClient: %+v", err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccAzureRMVirtualHubBgpConnection_template(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-VHub-%d" | ||
location = "%s" | ||
} | ||
resource "azurerm_virtual_hub" "test" { | ||
name = "acctest-VHub-%d" | ||
resource_group_name = azurerm_resource_group.test.name | ||
location = azurerm_resource_group.test.location | ||
sku = "Standard" | ||
} | ||
resource "azurerm_public_ip" "test" { | ||
name = "acctest-PIP-%d" | ||
location = azurerm_resource_group.test.location | ||
resource_group_name = azurerm_resource_group.test.name | ||
allocation_method = "Dynamic" | ||
} | ||
resource "azurerm_virtual_network" "test" { | ||
name = "acctest-VNet-%d" | ||
address_space = ["10.5.0.0/16"] | ||
location = azurerm_resource_group.test.location | ||
resource_group_name = azurerm_resource_group.test.name | ||
} | ||
resource "azurerm_subnet" "test" { | ||
name = "acctest-Subnet-%d" | ||
resource_group_name = azurerm_resource_group.test.name | ||
virtual_network_name = azurerm_virtual_network.test.name | ||
address_prefix = "10.5.1.0/24" | ||
} | ||
resource "azurerm_virtual_hub_ip" "test" { | ||
name = "acctest-VHub-IP-%d" | ||
virtual_hub_id = azurerm_virtual_hub.test.id | ||
private_ip_address = "10.5.1.18" | ||
private_ip_allocation_method = "Static" | ||
public_ip_address_id = azurerm_public_ip.test.id | ||
subnet_id = azurerm_subnet.test.id | ||
} | ||
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger) | ||
} | ||
|
||
func testAccAzureRMVirtualHubBgpConnection_basic(data acceptance.TestData) string { | ||
template := testAccAzureRMVirtualHubBgpConnection_template(data) | ||
return fmt.Sprintf(` | ||
%s | ||
resource "azurerm_virtual_hub_bgp_connection" "test" { | ||
name = "acctest-VHub-BgpConnection-%d" | ||
virtual_hub_id = azurerm_virtual_hub.test.id | ||
peer_asn = 65514 | ||
peer_ip = "169.254.21.5" | ||
depends_on = [azurerm_virtual_hub_ip.test] | ||
} | ||
`, template, data.RandomInteger) | ||
} | ||
|
||
func testAccAzureRMVirtualHubBgpConnection_requiresImport(data acceptance.TestData) string { | ||
config := testAccAzureRMVirtualHubBgpConnection_basic(data) | ||
return fmt.Sprintf(` | ||
%s | ||
resource "azurerm_virtual_hub_bgp_connection" "import" { | ||
name = azurerm_virtual_hub_bgp_connection.test.name | ||
virtual_hub_id = azurerm_virtual_hub_bgp_connection.test.virtual_hub_id | ||
peer_asn = azurerm_virtual_hub_bgp_connection.test.peer_asn | ||
peer_ip = azurerm_virtual_hub_bgp_connection.test.peer_ip | ||
} | ||
`, config) | ||
} |
Oops, something went wrong.