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

New resource azurerm_express_route_circuit_connection #11303

Merged
merged 31 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions azurerm/internal/services/network/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Client struct {
DDOSProtectionPlansClient *network.DdosProtectionPlansClient
ExpressRouteAuthsClient *network.ExpressRouteCircuitAuthorizationsClient
ExpressRouteCircuitsClient *network.ExpressRouteCircuitsClient
ExpressRouteCircuitConnectionClient *network.ExpressRouteCircuitConnectionsClient
ExpressRouteGatewaysClient *network.ExpressRouteGatewaysClient
ExpressRoutePeeringsClient *network.ExpressRouteCircuitPeeringsClient
HubRouteTableClient *network.HubRouteTablesClient
Expand Down Expand Up @@ -79,6 +80,9 @@ func NewClient(o *common.ClientOptions) *Client {
ExpressRouteCircuitsClient := network.NewExpressRouteCircuitsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ExpressRouteCircuitsClient.Client, o.ResourceManagerAuthorizer)

ExpressRouteCircuitConnectionClient := network.NewExpressRouteCircuitConnectionsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ExpressRouteCircuitConnectionClient.Client, o.ResourceManagerAuthorizer)

ExpressRouteGatewaysClient := network.NewExpressRouteGatewaysClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ExpressRouteGatewaysClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -213,6 +217,7 @@ func NewClient(o *common.ClientOptions) *Client {
DDOSProtectionPlansClient: &DDOSProtectionPlansClient,
ExpressRouteAuthsClient: &ExpressRouteAuthsClient,
ExpressRouteCircuitsClient: &ExpressRouteCircuitsClient,
ExpressRouteCircuitConnectionClient: &ExpressRouteCircuitConnectionClient,
ExpressRouteGatewaysClient: &ExpressRouteGatewaysClient,
ExpressRoutePeeringsClient: &ExpressRoutePeeringsClient,
HubRouteTableClient: &HubRouteTableClient,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package network

import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceExpressRouteCircuitConnection() *schema.Resource {
return &schema.Resource{
Read: dataSourceExpressRouteCircuitConnectionRead,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"circuit_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
}
}

func dataSourceExpressRouteCircuitConnectionRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.ExpressRouteCircuitConnectionClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
circuitName := d.Get("circuit_name").(string)

resp, err := client.Get(ctx, resourceGroup, circuitName, "AzurePrivatePeering", name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf(" ExpressRouteCircuitConnection %q (Resource Group %q / circuitName %q) does not exist", name, resourceGroup, circuitName)
}
return fmt.Errorf("retrieving ExpressRouteCircuitConnection %q (Resource Group %q / circuitName %q): %+v", name, resourceGroup, circuitName, err)
}
if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("empty or nil ID returned for ExpressRouteCircuitConnection %q (Resource Group %q / circuitName %q) ID", name, resourceGroup, circuitName)
}

d.SetId(*resp.ID)
jackofallops marked this conversation as resolved.
Show resolved Hide resolved
d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
d.Set("circuit_name", circuitName)
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package network_test

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
)

type ExpressRouteCircuitConnectionDataSource struct{}

func TestAccExpressRouteCircuitConnectionDataSource_basic(t *testing.T) {
if os.Getenv("ARM_TEST_DATA_RESOURCE_GROUP") == "" || os.Getenv("ARM_TEST_CIRCUIT_NAME_FIRST") == "" ||
os.Getenv("ARM_TEST_CIRCUIT_NAME_SECOND") == "" {
t.Skip("Skipping as ARM_TEST_DATA_RESOURCE_GROUP and/or ARM_TEST_CIRCUIT_NAME_FIRST and/or ARM_TEST_CIRCUIT_NAME_SECOND are not specified")
return
}

data := acceptance.BuildTestData(t, "data.azurerm_express_route_circuit_connection", "test")
r := ExpressRouteCircuitConnectionDataSource{}
data.DataSourceTest(t, []resource.TestStep{
{
Config: r.basic(data),
Check: resource.ComposeTestCheckFunc(),
},
})
}
func (ExpressRouteCircuitConnectionDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

data "azurerm_express_route_circuit_connection" "test" {
name = azurerm_express_route_circuit_connection.test.name
resource_group_name = azurerm_express_route_circuit_connection.test.resource_group_name
circuit_name = azurerm_express_route_circuit_connection.test.circuit_name
}
`, ExpressRouteCircuitConnectionResource{}.basic(data))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
package network

import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2020-05-01/network"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"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"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceExpressRouteCircuitConnection() *schema.Resource {
return &schema.Resource{
Create: resourceExpressRouteCircuitConnectionCreateUpdate,
Read: resourceExpressRouteCircuitConnectionRead,
Update: resourceExpressRouteCircuitConnectionCreateUpdate,
Copy link
Contributor

Choose a reason for hiding this comment

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

newer resources shouldn't be reusing the Create/Update functions as it means that it's not possible to ignore_changes on fields - can we split the Create and Update methods to support Delta Updates (e.g. d.HasChanges) here

Copy link
Contributor

Choose a reason for hiding this comment

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

There is no Update operation for this RP. So seems we cannot split the Create and Update methods.

Delete: resourceExpressRouteCircuitConnectionDelete,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parse.ExpressRouteCircuitConnectionID(id)
return err
}),

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"resource_group_name": azure.SchemaResourceGroupName(),

"circuit_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
jackofallops marked this conversation as resolved.
Show resolved Hide resolved

"peering_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
jackofallops marked this conversation as resolved.
Show resolved Hide resolved
},

"peer_peering_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
jackofallops marked this conversation as resolved.
Show resolved Hide resolved
},

"address_prefix": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.IsCIDR,
},

"authorization_key": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},

"ipv6circuit_connection_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address_prefix": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsCIDR,
},
jackofallops marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
},
}
}
func resourceExpressRouteCircuitConnectionCreateUpdate(d *schema.ResourceData, meta interface{}) error {
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
client := meta.(*clients.Client).Network.ExpressRouteCircuitConnectionClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
circuitName := d.Get("circuit_name").(string)

id := parse.NewExpressRouteCircuitConnectionID(subscriptionId, resourceGroup, circuitName, "AzurePrivatePeering", name).ID()
jackofallops marked this conversation as resolved.
Show resolved Hide resolved

if d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, circuitName, "AzurePrivatePeering", name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for existing ExpressRouteCircuitConnection %q (Resource Group %q / circuitName %q): %+v", name, resourceGroup, circuitName, err)
}
}
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_express_route_circuit_connection", id)
}
jackofallops marked this conversation as resolved.
Show resolved Hide resolved
}

expressRouteCircuitConnectionParameters := network.ExpressRouteCircuitConnection{
Name: utils.String(d.Get("name").(string)),
jackofallops marked this conversation as resolved.
Show resolved Hide resolved
ExpressRouteCircuitConnectionPropertiesFormat: &network.ExpressRouteCircuitConnectionPropertiesFormat{
AddressPrefix: utils.String(d.Get("address_prefix").(string)),
ExpressRouteCircuitPeering: &network.SubResource{
ID: utils.String(d.Get("peering_id").(string)),
},
PeerExpressRouteCircuitPeering: &network.SubResource{
ID: utils.String(d.Get("peer_peering_id").(string)),
},
Ipv6CircuitConnectionConfig: expandExpressRouteCircuitConnectionIpv6CircuitConnectionConfig(d.Get("ipv6circuit_connection_config").([]interface{})),
},
}

if v, ok := d.GetOk("authorization_key"); ok {
expressRouteCircuitConnectionParameters.ExpressRouteCircuitConnectionPropertiesFormat.AuthorizationKey = utils.String(v.(string))
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, circuitName, "AzurePrivatePeering", name, expressRouteCircuitConnectionParameters)
if err != nil {
return fmt.Errorf("creating/updating ExpressRouteCircuitConnection %q (Resource Group %q / circuitName %q): %+v", name, resourceGroup, circuitName, err)
}

if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation/update of the ExpressRouteCircuitConnection %q (Resource Group %q / circuitName %q): %+v", name, resourceGroup, circuitName, err)
}
jackofallops marked this conversation as resolved.
Show resolved Hide resolved

d.SetId(id)
return resourceExpressRouteCircuitConnectionRead(d, meta)
}

func resourceExpressRouteCircuitConnectionRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.ExpressRouteCircuitConnectionClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ExpressRouteCircuitConnectionID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.CircuitName, id.PeeringName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] express route circuit connection %q does not exist - removing from state", d.Id())
jackofallops marked this conversation as resolved.
Show resolved Hide resolved
d.SetId("")
return nil
}
return fmt.Errorf("retrieving ExpressRouteCircuitConnection %q (Resource Group %q / circuitName %q / peeringName %q): %+v", id.Name, id.ResourceGroup, id.CircuitName, id.PeeringName, err)
jackofallops marked this conversation as resolved.
Show resolved Hide resolved
}
d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("circuit_name", id.CircuitName)
d.Set("peering_id", resp.ExpressRouteCircuitPeering.ID)
d.Set("peer_peering_id", resp.PeerExpressRouteCircuitPeering.ID)
Copy link
Contributor

Choose a reason for hiding this comment

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

both of these are nested within the properties block, (that these are at the top level is a legacy decision in the Track1 SDK to work around API design bugs) - so can we move these into the properties block, and also nil-check these

Copy link
Contributor

Choose a reason for hiding this comment

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

Updated

if props := resp.ExpressRouteCircuitConnectionPropertiesFormat; props != nil {
d.Set("address_prefix", props.AddressPrefix)
d.Set("authorization_key", props.AuthorizationKey)
if err := d.Set("ipv6circuit_connection_config", flattenExpressRouteCircuitConnectionIpv6CircuitConnectionConfig(props.Ipv6CircuitConnectionConfig)); err != nil {
return fmt.Errorf("setting `ipv6circuit_connection_config`: %+v", err)
}
}
return nil
}

func resourceExpressRouteCircuitConnectionDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.ExpressRouteCircuitConnectionClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ExpressRouteCircuitConnectionID(d.Id())
if err != nil {
return err
}

future, err := client.Delete(ctx, id.ResourceGroup, id.CircuitName, id.PeeringName, id.Name)
if err != nil {
return fmt.Errorf("deleting ExpressRouteCircuitConnection %q (Resource Group %q / circuitName %q / peeringName %q): %+v", id.Name, id.ResourceGroup, id.CircuitName, id.PeeringName, err)
}

if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of the ExpressRouteCircuitConnection %q (Resource Group %q / circuitName %q / peeringName %q): %+v", id.Name, id.ResourceGroup, id.CircuitName, id.PeeringName, err)
}
jackofallops marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

func expandExpressRouteCircuitConnectionIpv6CircuitConnectionConfig(input []interface{}) *network.Ipv6CircuitConnectionConfig {
if len(input) == 0 {
return nil
}
v := input[0].(map[string]interface{})
return &network.Ipv6CircuitConnectionConfig{
AddressPrefix: utils.String(v["address_prefix"].(string)),
}
}

func flattenExpressRouteCircuitConnectionIpv6CircuitConnectionConfig(input *network.Ipv6CircuitConnectionConfig) []interface{} {
if input == nil {
return make([]interface{}, 0)
}

var addressPrefix string
if input.AddressPrefix != nil {
addressPrefix = *input.AddressPrefix
}
return []interface{}{
map[string]interface{}{
"address_prefix": addressPrefix,
},
}
}
Loading