forked from digitalocean/terraform-provider-digitalocean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasource_digitalocean_floating_ip.go
65 lines (55 loc) · 1.64 KB
/
datasource_digitalocean_floating_ip.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
package digitalocean
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
func dataSourceDigitalOceanFloatingIp() *schema.Resource {
return &schema.Resource{
Read: dataSourceDigitalOceanFloatingIpRead,
Schema: map[string]*schema.Schema{
"ip_address": {
Type: schema.TypeString,
Required: true,
Description: "floating ip address",
ValidateFunc: validation.NoZeroValues,
},
// computed attributes
"urn": {
Type: schema.TypeString,
Computed: true,
Description: "the uniform resource name for the floating ip",
},
"region": {
Type: schema.TypeString,
Computed: true,
Description: "the region that the floating ip is reserved to",
},
"droplet_id": {
Type: schema.TypeInt,
Computed: true,
Description: "the droplet id that the floating ip has been assigned to.",
},
},
}
}
func dataSourceDigitalOceanFloatingIpRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
ipAddress := d.Get("ip_address").(string)
floatingIp, resp, err := client.FloatingIPs.Get(context.Background(), ipAddress)
if err != nil {
if resp != nil && resp.StatusCode == 404 {
return fmt.Errorf("floating ip not found: %s", err)
}
return fmt.Errorf("Error retrieving floating ip: %s", err)
}
d.SetId(floatingIp.IP)
d.Set("ip_address", floatingIp.IP)
d.Set("urn", floatingIp.URN())
d.Set("region", floatingIp.Region.Slug)
if floatingIp.Droplet != nil {
d.Set("droplet_id", floatingIp.Droplet.ID)
}
return nil
}