github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/openstack/data_source_openstack_networking_network_v2.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "log" 6 "strconv" 7 8 "github.com/hashicorp/terraform/helper/schema" 9 10 "github.com/gophercloud/gophercloud" 11 "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" 12 "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets" 13 ) 14 15 func dataSourceNetworkingNetworkV2() *schema.Resource { 16 return &schema.Resource{ 17 Read: dataSourceNetworkingNetworkV2Read, 18 19 Schema: map[string]*schema.Schema{ 20 "name": &schema.Schema{ 21 Type: schema.TypeString, 22 Optional: true, 23 }, 24 "matching_subnet_cidr": &schema.Schema{ 25 Type: schema.TypeString, 26 Optional: true, 27 }, 28 "region": &schema.Schema{ 29 Type: schema.TypeString, 30 Required: true, 31 DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""), 32 }, 33 "tenant_id": &schema.Schema{ 34 Type: schema.TypeString, 35 Optional: true, 36 DefaultFunc: schema.MultiEnvDefaultFunc([]string{ 37 "OS_TENANT_ID", 38 "OS_PROJECT_ID", 39 }, ""), 40 Description: descriptions["tenant_id"], 41 }, 42 "admin_state_up": &schema.Schema{ 43 Type: schema.TypeString, 44 Computed: true, 45 }, 46 "shared": &schema.Schema{ 47 Type: schema.TypeString, 48 Computed: true, 49 }, 50 }, 51 } 52 } 53 54 func dataSourceNetworkingNetworkV2Read(d *schema.ResourceData, meta interface{}) error { 55 config := meta.(*Config) 56 networkingClient, err := config.networkingV2Client(GetRegion(d)) 57 58 listOpts := networks.ListOpts{ 59 Name: d.Get("name").(string), 60 TenantID: d.Get("tenant_id").(string), 61 Status: "ACTIVE", 62 } 63 64 pages, err := networks.List(networkingClient, listOpts).AllPages() 65 allNetworks, err := networks.ExtractNetworks(pages) 66 if err != nil { 67 return fmt.Errorf("Unable to retrieve networks: %s", err) 68 } 69 70 var refinedNetworks []networks.Network 71 if cidr := d.Get("matching_subnet_cidr").(string); cidr != "" { 72 for _, n := range allNetworks { 73 for _, s := range n.Subnets { 74 subnet, err := subnets.Get(networkingClient, s).Extract() 75 if err != nil { 76 if _, ok := err.(gophercloud.ErrDefault404); ok { 77 continue 78 } 79 return fmt.Errorf("Unable to retrieve network subnet: %s", err) 80 } 81 if cidr == subnet.CIDR { 82 refinedNetworks = append(refinedNetworks, n) 83 } 84 } 85 } 86 } else { 87 refinedNetworks = allNetworks 88 } 89 90 if len(refinedNetworks) < 1 { 91 return fmt.Errorf("Your query returned no results. " + 92 "Please change your search criteria and try again.") 93 } 94 95 if len(refinedNetworks) > 1 { 96 return fmt.Errorf("Your query returned more than one result." + 97 " Please try a more specific search criteria") 98 } 99 100 network := refinedNetworks[0] 101 102 log.Printf("[DEBUG] Retrieved Network %s: %+v", network.ID, network) 103 d.SetId(network.ID) 104 105 d.Set("name", network.Name) 106 d.Set("admin_state_up", strconv.FormatBool(network.AdminStateUp)) 107 d.Set("shared", strconv.FormatBool(network.Shared)) 108 d.Set("tenant_id", network.TenantID) 109 d.Set("region", GetRegion(d)) 110 111 return nil 112 }