2018-09-04 11:37:12 +00:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2020-10-16 19:50:20 +00:00
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
|
2018-09-04 11:37:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func dataSourceDigitalOceanFloatingIp() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Read: dataSourceDigitalOceanFloatingIpRead,
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
|
2019-01-07 22:05:13 +00:00
|
|
|
"ip_address": {
|
2018-09-04 11:37:12 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
Description: "floating ip address",
|
|
|
|
ValidateFunc: validation.NoZeroValues,
|
|
|
|
},
|
|
|
|
// computed attributes
|
2019-04-15 15:30:42 +00:00
|
|
|
"urn": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
Description: "the uniform resource name for the floating ip",
|
|
|
|
},
|
2019-01-07 22:05:13 +00:00
|
|
|
"region": {
|
2018-09-04 11:37:12 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
Description: "the region that the floating ip is reserved to",
|
|
|
|
},
|
2019-01-07 22:05:13 +00:00
|
|
|
"droplet_id": {
|
2018-09-04 11:37:12 +00:00
|
|
|
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 {
|
2019-01-07 23:48:17 +00:00
|
|
|
client := meta.(*CombinedConfig).godoClient()
|
2018-09-04 11:37:12 +00:00
|
|
|
|
|
|
|
ipAddress := d.Get("ip_address").(string)
|
|
|
|
|
|
|
|
floatingIp, resp, err := client.FloatingIPs.Get(context.Background(), ipAddress)
|
|
|
|
if err != nil {
|
2019-07-23 16:11:05 +00:00
|
|
|
if resp != nil && resp.StatusCode == 404 {
|
2018-09-04 11:37:12 +00:00
|
|
|
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)
|
2019-04-15 15:30:42 +00:00
|
|
|
d.Set("urn", floatingIp.URN())
|
2018-09-04 11:37:12 +00:00
|
|
|
d.Set("region", floatingIp.Region.Slug)
|
|
|
|
|
|
|
|
if floatingIp.Droplet != nil {
|
|
|
|
d.Set("droplet_id", floatingIp.Droplet.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|