forked from TritonDataCenter/terraform-provider-triton
-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_source_network.go
57 lines (49 loc) · 1.12 KB
/
data_source_network.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
package triton
import (
"context"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/joyent/triton-go/network"
)
func dataSourceNetwork() *schema.Resource {
return &schema.Resource{
Read: dataSourceNetworkRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
func dataSourceNetworkRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client)
net, err := client.Network()
if err != nil {
return err
}
networks, err := net.List(context.Background(), &network.ListInput{})
if err != nil {
return err
}
if len(networks) == 0 {
return fmt.Errorf("Your query returned no results. Please change " +
"your search criteria and try again.")
}
var networkName string
if name, hasName := d.GetOk("name"); hasName {
networkName = name.(string)
}
var network *network.Network
for _, found := range networks {
if found.Name == networkName {
network = found
}
}
if network == nil {
return fmt.Errorf("No Networks found by name %q", networkName)
}
d.SetId(network.Id)
return nil
}