-
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.
Merge pull request #3158 from romitgirdhar/master
New Data Source: `azurerm_express_route_circuit`
- Loading branch information
Showing
8 changed files
with
365 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func dataSourceArmExpressRouteCircuit() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceArmExpressRouteCircuitRead, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validate.NoEmptyStrings, | ||
}, | ||
|
||
"resource_group_name": resourceGroupNameForDataSourceSchema(), | ||
|
||
"location": locationForDataSourceSchema(), | ||
|
||
"peerings": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"peering_type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"primary_peer_address_prefix": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"secondary_peer_address_prefix": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"azure_asn": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"peer_asn": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"vlan_id": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"shared_key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"service_key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"service_provider_properties": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"service_provider_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"peering_location": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"bandwidth_in_mbps": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"service_provider_provisioning_state": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"sku": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"tier": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"family": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceArmExpressRouteCircuitRead(d *schema.ResourceData, meta interface{}) error { | ||
ctx := meta.(*ArmClient).StopContext | ||
client := meta.(*ArmClient).expressRouteCircuitClient | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
|
||
resp, err := client.Get(ctx, resourceGroup, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Error: Express Route Circuit %q (Resource Group %q) was not found", name, resourceGroup) | ||
} | ||
return fmt.Errorf("Error making Read request on the Express Route Circuit %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
d.SetId(*resp.ID) | ||
|
||
if location := resp.Location; location != nil { | ||
d.Set("location", azureRMNormalizeLocation(*location)) | ||
} | ||
|
||
if properties := resp.ExpressRouteCircuitPropertiesFormat; properties != nil { | ||
peerings := flattenExpressRouteCircuitPeerings(properties.Peerings) | ||
if err := d.Set("peerings", peerings); err != nil { | ||
return err | ||
} | ||
|
||
d.Set("service_key", properties.ServiceKey) | ||
d.Set("service_provider_provisioning_state", properties.ServiceProviderProvisioningState) | ||
|
||
if serviceProviderProperties := flattenExpressRouteCircuitServiceProviderProperties(properties.ServiceProviderProperties); serviceProviderProperties != nil { | ||
if err := d.Set("service_provider_properties", serviceProviderProperties); err != nil { | ||
return fmt.Errorf("Error setting `service_provider_properties`: %+v", err) | ||
} | ||
} | ||
|
||
} | ||
|
||
if resp.Sku != nil { | ||
sku := flattenExpressRouteCircuitSku(resp.Sku) | ||
if err := d.Set("sku", sku); err != nil { | ||
return fmt.Errorf("Error setting `sku`: %+v", err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func flattenExpressRouteCircuitPeerings(input *[]network.ExpressRouteCircuitPeering) []interface{} { | ||
peerings := make([]interface{}, 0) | ||
|
||
if input != nil { | ||
for _, peering := range *input { | ||
props := peering.ExpressRouteCircuitPeeringPropertiesFormat | ||
p := make(map[string]interface{}) | ||
|
||
p["peering_type"] = string(props.PeeringType) | ||
|
||
if primaryPeerAddressPrefix := props.PrimaryPeerAddressPrefix; primaryPeerAddressPrefix != nil { | ||
p["primary_peer_address_prefix"] = *primaryPeerAddressPrefix | ||
} | ||
|
||
if secondaryPeerAddressPrefix := props.SecondaryPeerAddressPrefix; secondaryPeerAddressPrefix != nil { | ||
p["secondary_peer_address_prefix"] = *secondaryPeerAddressPrefix | ||
} | ||
|
||
if azureAsn := props.AzureASN; azureAsn != nil { | ||
p["azure_asn"] = *azureAsn | ||
} | ||
|
||
if peerAsn := props.PeerASN; peerAsn != nil { | ||
p["peer_asn"] = *peerAsn | ||
} | ||
|
||
if vlanID := props.VlanID; vlanID != nil { | ||
p["vlan_id"] = *vlanID | ||
} | ||
|
||
if sharedKey := props.SharedKey; sharedKey != nil { | ||
p["shared_key"] = *sharedKey | ||
} | ||
|
||
peerings = append(peerings, p) | ||
} | ||
} | ||
|
||
return peerings | ||
} | ||
|
||
func flattenExpressRouteCircuitServiceProviderProperties(input *network.ExpressRouteCircuitServiceProviderProperties) []interface{} { | ||
serviceProviderProperties := make([]interface{}, 0) | ||
|
||
if input != nil { | ||
p := make(map[string]interface{}) | ||
|
||
if serviceProviderName := input.ServiceProviderName; serviceProviderName != nil { | ||
p["service_provider_name"] = *serviceProviderName | ||
} | ||
|
||
if peeringLocation := input.PeeringLocation; peeringLocation != nil { | ||
p["peering_location"] = *peeringLocation | ||
} | ||
|
||
if bandwidthInMbps := input.BandwidthInMbps; bandwidthInMbps != nil { | ||
p["bandwidth_in_mbps"] = *bandwidthInMbps | ||
} | ||
|
||
serviceProviderProperties = append(serviceProviderProperties, p) | ||
} | ||
|
||
return serviceProviderProperties | ||
} |
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,47 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" | ||
) | ||
|
||
func testAccDataSourceAzureRMExpressRoute_basicMetered(t *testing.T) { | ||
dataSourceName := "data.azurerm_express_route_circuit.test" | ||
ri := tf.AccRandTimeInt() | ||
location := testLocation() | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMExpressRouteCircuitDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceAzureRMExpressRoute_basic(ri, location), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(dataSourceName, "service_provider_properties.0.service_provider_name", "Equinix"), | ||
resource.TestCheckResourceAttr(dataSourceName, "service_provider_properties.0.peering_location", "Silicon Valley"), | ||
resource.TestCheckResourceAttr(dataSourceName, "service_provider_properties.0.bandwidth_in_mbps", "50"), | ||
resource.TestCheckResourceAttr(dataSourceName, "sku.0.tier", "Standard"), | ||
resource.TestCheckResourceAttr(dataSourceName, "sku.0.family", "MeteredData"), | ||
resource.TestCheckResourceAttr(dataSourceName, "service_provider_provisioning_state", "NotProvisioned"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAzureRMExpressRoute_basic(rInt int, location string) string { | ||
config := testAccAzureRMExpressRouteCircuit_basicMeteredConfig(rInt, location) | ||
|
||
return fmt.Sprintf(` | ||
%s | ||
data "azurerm_express_route_circuit" test { | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
name = "${azurerm_express_route_circuit.test.name}" | ||
} | ||
`, config) | ||
} |
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
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
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
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
Oops, something went wrong.