This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_source_opentelekomcloud_networking_network_v2.go
123 lines (107 loc) · 2.98 KB
/
data_source_opentelekomcloud_networking_network_v2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package opentelekomcloud
import (
"fmt"
"log"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/networks"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/subnets"
)
func dataSourceNetworkingNetworkV2() *schema.Resource {
return &schema.Resource{
Read: dataSourceNetworkingNetworkV2Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"network_id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"matching_subnet_cidr": {
Type: schema.TypeString,
Optional: true,
},
"tenant_id": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"OS_TENANT_ID",
"OS_PROJECT_ID",
}, ""),
Description: descriptions["tenant_id"],
},
"admin_state_up": {
Type: schema.TypeString,
Computed: true,
},
"shared": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceNetworkingNetworkV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
_, id := ExtractValSFromNid(d.Get("network_id").(string))
listOpts := networks.ListOpts{
ID: id,
Name: d.Get("name").(string),
TenantID: d.Get("tenant_id").(string),
Status: "ACTIVE",
}
pages, err := networks.List(networkingClient, listOpts).AllPages()
if err != nil {
return err
}
allNetworks, err := networks.ExtractNetworks(pages)
if err != nil {
return fmt.Errorf("Unable to retrieve networks: %s", err)
}
var refinedNetworks []networks.Network
if cidr := d.Get("matching_subnet_cidr").(string); cidr != "" {
for _, n := range allNetworks {
for _, s := range n.Subnets {
subnet, err := subnets.Get(networkingClient, s).Extract()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
continue
}
return fmt.Errorf("Unable to retrieve network subnet: %s", err)
}
if cidr == subnet.CIDR {
refinedNetworks = append(refinedNetworks, n)
}
}
}
} else {
refinedNetworks = allNetworks
}
if len(refinedNetworks) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
if len(refinedNetworks) > 1 {
return fmt.Errorf("Your query returned more than one result." +
" Please try a more specific search criteria")
}
network := refinedNetworks[0]
log.Printf("[DEBUG] Retrieved Network %s: %+v", network.ID, network)
d.SetId(network.ID)
d.Set("name", network.Name)
d.Set("admin_state_up", strconv.FormatBool(network.AdminStateUp))
d.Set("shared", strconv.FormatBool(network.Shared))
d.Set("tenant_id", network.TenantID)
d.Set("region", GetRegion(d, config))
return nil
}