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

79 lines
1.5 KiB
Go

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,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Required: false,
ForceNew: true,
},
},
}
}
func resourceRouteCreate(d *schema.ResourceData, meta interface{}) error {
cli := meta.(proto.RoutesClient)
rt, err := cli.Put(context.Background(), &proto.Route{
Host: d.Get("domain").(string),
})
if err != nil {
return err
}
log.Printf("[INFO] created route for host %s with ID %s", rt.Host, rt.Id)
return nil
}
func resourceRouteDelete(d *schema.ResourceData, meta interface{}) error {
cli := meta.(proto.RoutesClient)
rt, err := cli.Get(context.Background(), &proto.GetRouteRequest{Id: d.Id()})
if err != nil {
return err
}
_, err = cli.Delete(context.Background(), rt)
if err != nil {
return err
}
log.Printf("[INFO] deleted route for host %s with ID %s", rt.Host, rt.Id)
return nil
}
func resourceRouteRead(d *schema.ResourceData, meta interface{}) error {
cli := meta.(proto.RoutesClient)
rt, err := cli.Get(context.Background(), &proto.GetRouteRequest{Id: d.Id()})
if err != nil {
return err
}
d.SetId(rt.Id)
d.Set("host", rt.Host)
d.Set("creator", rt.Creator)
return nil
}