route/cmd/terraform-provider-route/route.go

92 lines
1.8 KiB
Go
Raw Permalink Normal View History

2017-12-02 23:34:35 +00:00
package main
import (
"context"
"log"
proto "git.xeserv.us/xena/route/proto"
2018-01-21 16:35:41 +00:00
"git.xeserv.us/xena/route/proto/route"
2017-12-02 23:34:35 +00:00
"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 {
2018-01-21 16:35:41 +00:00
cli := meta.(*route.Client)
2017-12-02 23:34:35 +00:00
2018-01-21 16:35:41 +00:00
rt, err := cli.Routes.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 {
2018-01-21 16:35:41 +00:00
cli := meta.(*route.Client)
2017-12-02 23:34:35 +00:00
2018-01-21 16:35:41 +00:00
rt, err := cli.Routes.Get(context.Background(), &proto.GetRouteRequest{Id: d.Id()})
2017-12-02 23:34:35 +00:00
if err != nil {
return err
}
2018-01-21 16:35:41 +00:00
_, err = cli.Routes.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) {
2018-01-21 16:35:41 +00:00
cli := meta.(*route.Client)
2017-12-03 02:00:32 +00:00
2018-01-21 16:35:41 +00:00
_, err := cli.Routes.Get(context.Background(), &proto.GetRouteRequest{Id: d.Id()})
2017-12-03 02:00:32 +00:00
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 {
2018-01-21 16:35:41 +00:00
cli := meta.(*route.Client)
2017-12-02 23:34:35 +00:00
2018-01-21 16:35:41 +00:00
rt, err := cli.Routes.Get(context.Background(), &proto.GetRouteRequest{Id: d.Id()})
2017-12-02 23:34:35 +00:00
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
}