-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdata_source_subnet.go
100 lines (92 loc) · 2.32 KB
/
data_source_subnet.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
// File : resource_fake_object.go
package main
import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
log "github.com/sirupsen/logrus"
"github.com/stobias123/gosolar"
)
func dataSourceSubnet() *schema.Resource {
return &schema.Resource{
Read: dataSourceSubnetRead,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Subnet FriendlyName you'd like to search",
},
"address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"cidr": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"vlan": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"address_mask": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"display_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"reserved": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"total_count": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"used_count": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"available_count": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"reserved_count": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"transient_count": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceSubnetRead(d *schema.ResourceData, meta interface{}) error {
// return findIPAddress()
client := meta.(*gosolar.Client)
var s gosolar.Subnet
vlan := d.Get("vlan").(string)
address := d.Get("address").(string)
if len(vlan) > 1 {
s = client.GetSubnetByVLAN(vlan)
} else if len(address) > 1 {
s = client.GetSubnetByAddress(address)
} else {
log.Errorf("Provide vlan")
}
log.Infof("Subnet found: %s", s)
log.Infof("CIDR found: %s", s.CIDR)
d.SetId(s.VLAN)
d.Set("address", s.Address)
d.Set("cidr", s.CIDR)
d.Set("vlan", s.VLAN)
d.Set("address_mask", s.AddressMask)
d.Set("display_name", s.DisplayName)
d.Set("reserved", s.FriendlyName)
d.Set("total_count", s.TotalCount)
d.Set("used_count", s.UsedCount)
d.Set("available_count", s.AvailableCount)
d.Set("reserved_count", s.ReservedCount)
log.Printf("Subnet found: %s", d)
return nil
}