2017-12-02 23:34:35 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
proto "git.xeserv.us/xena/route/proto"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func routeResource() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceRouteCreate,
|
|
|
|
Read: resourceRouteRead,
|
|
|
|
Delete: resourceRouteDelete,
|
2017-12-03 02:00:32 +00:00
|
|
|
Exists: resourceRouteExists,
|
2017-12-02 23:34:35 +00:00
|
|
|
|
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-12-03 02:00:32 +00:00
|
|
|
"host": {
|
2017-12-02 23:34:35 +00:00
|
|
|
Type: schema.TypeString,
|
2017-12-03 02:00:32 +00:00
|
|
|
Required: true,
|
2017-12-03 01:13:52 +00:00
|
|
|
ForceNew: true,
|
2017-12-02 23:34:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceRouteCreate(d *schema.ResourceData, meta interface{}) error {
|
2017-12-02 23:45:44 +00:00
|
|
|
cli := meta.(proto.RoutesClient)
|
2017-12-02 23:34:35 +00:00
|
|
|
|
2017-12-02 23:45:44 +00:00
|
|
|
rt, err := cli.Put(context.Background(), &proto.Route{
|
2017-12-03 02:00:32 +00:00
|
|
|
Host: d.Get("host").(string),
|
2017-12-02 23:34:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-02 23:45:44 +00:00
|
|
|
log.Printf("[INFO] created route for host %s with ID %s", rt.Host, rt.Id)
|
2017-12-02 23:34:35 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceRouteDelete(d *schema.ResourceData, meta interface{}) error {
|
2017-12-02 23:45:44 +00:00
|
|
|
cli := meta.(proto.RoutesClient)
|
2017-12-02 23:34:35 +00:00
|
|
|
|
|
|
|
rt, err := cli.Get(context.Background(), &proto.GetRouteRequest{Id: d.Id()})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-02 23:45:44 +00:00
|
|
|
_, err = cli.Delete(context.Background(), rt)
|
2017-12-02 23:34:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-02 23:45:44 +00:00
|
|
|
log.Printf("[INFO] deleted route for host %s with ID %s", rt.Host, rt.Id)
|
2017-12-02 23:34:35 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-03 02:00:32 +00:00
|
|
|
func resourceRouteExists(d *schema.ResourceData, meta interface{}) (bool, error) {
|
|
|
|
cli := meta.(proto.RoutesClient)
|
|
|
|
|
|
|
|
_, err := cli.Get(context.Background(), &proto.GetRouteRequest{Id: d.Id()})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2017-12-02 23:34:35 +00:00
|
|
|
func resourceRouteRead(d *schema.ResourceData, meta interface{}) error {
|
2017-12-02 23:45:44 +00:00
|
|
|
cli := meta.(proto.RoutesClient)
|
2017-12-02 23:34:35 +00:00
|
|
|
|
|
|
|
rt, err := cli.Get(context.Background(), &proto.GetRouteRequest{Id: d.Id()})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-02 23:45:44 +00:00
|
|
|
d.SetId(rt.Id)
|
2017-12-02 23:34:35 +00:00
|
|
|
d.Set("host", rt.Host)
|
|
|
|
d.Set("creator", rt.Creator)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|