-
Notifications
You must be signed in to change notification settings - Fork 13
/
datasource_vlan.go
79 lines (69 loc) · 2.26 KB
/
datasource_vlan.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
package ddcloud
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceVLAN() *schema.Resource {
return &schema.Resource{
Read: dataSourceVLANRead,
Schema: map[string]*schema.Schema{
resourceKeyVLANName: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The name of the target VLAN",
},
resourceKeyVLANNetworkDomainID: &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The Id of the network domain that contains the target VLAN",
},
resourceKeyVLANDescription: &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A description of the VLAN",
},
resourceKeyVLANIPv4BaseAddress: &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The VLAN's private IPv4 base address.",
},
resourceKeyVLANIPv4PrefixSize: &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The VLAN's private IPv4 prefix length.",
},
resourceKeyVLANIPv6BaseAddress: &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The VLAN's IPv6 base address.",
},
resourceKeyVLANIPv6PrefixSize: &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The VLAN's IPv6 prefix length.",
},
},
}
}
// Read a network domain data source.
func dataSourceVLANRead(data *schema.ResourceData, provider interface{}) error {
name := data.Get(resourceKeyVLANName).(string)
networkDomainID := data.Get(resourceKeyVLANNetworkDomainID).(string)
log.Printf("Read VLAN '%s' in network domain '%s'.", name, networkDomainID)
apiClient := provider.(*providerState).Client()
vlan, err := apiClient.GetVLANByName(name, networkDomainID)
if err != nil {
return err
}
if vlan != nil {
data.SetId(vlan.ID)
data.Set(resourceKeyVLANIPv4BaseAddress, vlan.IPv4Range.BaseAddress)
data.Set(resourceKeyVLANIPv4PrefixSize, vlan.IPv4Range.PrefixSize)
data.Set(resourceKeyVLANIPv6BaseAddress, vlan.IPv6Range.BaseAddress)
data.Set(resourceKeyVLANIPv6PrefixSize, vlan.IPv6Range.PrefixSize)
} else {
return fmt.Errorf("failed to find VLAN '%s' in network domain '%s'", name, networkDomainID)
}
return nil
}