2017-04-13 20:25:29 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2018-02-16 03:26:36 +00:00
|
|
|
"google.golang.org/api/compute/v1"
|
2017-04-13 20:25:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func dataSourceGoogleComputeSubnetwork() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Read: dataSourceGoogleComputeSubnetworkRead,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2018-12-14 20:51:11 +00:00
|
|
|
"name": {
|
2017-04-13 20:25:29 +00:00
|
|
|
Type: schema.TypeString,
|
2019-02-15 18:26:44 +00:00
|
|
|
Optional: true,
|
2017-04-13 20:25:29 +00:00
|
|
|
},
|
2019-02-15 18:26:44 +00:00
|
|
|
"self_link": {
|
2017-04-13 20:25:29 +00:00
|
|
|
Type: schema.TypeString,
|
2019-02-15 18:26:44 +00:00
|
|
|
Optional: true,
|
2017-04-13 20:25:29 +00:00
|
|
|
},
|
2019-02-15 18:26:44 +00:00
|
|
|
"description": {
|
2017-04-13 20:25:29 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2018-12-14 20:51:11 +00:00
|
|
|
"ip_cidr_range": {
|
2017-04-13 20:25:29 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2018-12-14 20:51:11 +00:00
|
|
|
"private_ip_google_access": {
|
2017-05-18 20:35:02 +00:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2018-12-14 20:51:11 +00:00
|
|
|
"secondary_ip_range": {
|
2017-11-06 19:19:46 +00:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Computed: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2018-12-14 20:51:11 +00:00
|
|
|
"range_name": {
|
2017-11-06 19:19:46 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2018-12-14 20:51:11 +00:00
|
|
|
"ip_cidr_range": {
|
2017-11-06 19:19:46 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-12-14 20:51:11 +00:00
|
|
|
"network": {
|
2017-04-13 20:25:29 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2018-12-14 20:51:11 +00:00
|
|
|
"gateway_address": {
|
2017-04-13 20:25:29 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2018-12-14 20:51:11 +00:00
|
|
|
"region": {
|
2017-04-13 20:25:29 +00:00
|
|
|
Type: schema.TypeString,
|
2017-11-06 19:19:46 +00:00
|
|
|
Computed: true,
|
2017-04-13 20:25:29 +00:00
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2018-12-14 20:51:11 +00:00
|
|
|
"project": {
|
2017-04-13 20:25:29 +00:00
|
|
|
Type: schema.TypeString,
|
2017-11-06 19:19:46 +00:00
|
|
|
Computed: true,
|
2017-04-13 20:25:29 +00:00
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func dataSourceGoogleComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2019-02-15 18:26:44 +00:00
|
|
|
project, region, name, err := GetRegionalResourcePropertiesFromSelfLinkOrSchema(d, config)
|
2017-04-13 20:25:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-13 19:19:59 +00:00
|
|
|
subnetwork, err := config.clientCompute.Subnetworks.Get(project, region, name).Do()
|
2017-04-13 20:25:29 +00:00
|
|
|
if err != nil {
|
2018-02-13 19:19:59 +00:00
|
|
|
return handleNotFoundError(err, d, fmt.Sprintf("Subnetwork Not Found : %s", name))
|
2017-04-13 20:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("ip_cidr_range", subnetwork.IpCidrRange)
|
2017-05-18 20:35:02 +00:00
|
|
|
d.Set("private_ip_google_access", subnetwork.PrivateIpGoogleAccess)
|
2017-04-13 20:25:29 +00:00
|
|
|
d.Set("self_link", subnetwork.SelfLink)
|
|
|
|
d.Set("description", subnetwork.Description)
|
|
|
|
d.Set("gateway_address", subnetwork.GatewayAddress)
|
|
|
|
d.Set("network", subnetwork.Network)
|
2017-11-06 19:19:46 +00:00
|
|
|
d.Set("project", project)
|
|
|
|
d.Set("region", region)
|
|
|
|
d.Set("secondary_ip_range", flattenSecondaryRanges(subnetwork.SecondaryIpRanges))
|
2017-04-13 20:25:29 +00:00
|
|
|
|
2019-02-15 18:26:44 +00:00
|
|
|
d.SetId(fmt.Sprintf("%s/%s", region, name))
|
2017-04-13 20:25:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-02-15 23:40:40 +00:00
|
|
|
|
|
|
|
func flattenSecondaryRanges(secondaryRanges []*compute.SubnetworkSecondaryRange) []map[string]interface{} {
|
|
|
|
secondaryRangesSchema := make([]map[string]interface{}, 0, len(secondaryRanges))
|
|
|
|
for _, secondaryRange := range secondaryRanges {
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"range_name": secondaryRange.RangeName,
|
|
|
|
"ip_cidr_range": secondaryRange.IpCidrRange,
|
|
|
|
}
|
|
|
|
|
|
|
|
secondaryRangesSchema = append(secondaryRangesSchema, data)
|
|
|
|
}
|
|
|
|
return secondaryRangesSchema
|
|
|
|
}
|