fix build, switch to twirp completely

This commit is contained in:
Cadey Ratio 2018-01-21 08:35:41 -08:00
parent 0566205ec2
commit dd53921e77
5 changed files with 39 additions and 76 deletions

View File

@ -2,7 +2,6 @@ package main
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"log"
@ -16,14 +15,12 @@ import (
"git.xeserv.us/xena/route/internal/database"
"git.xeserv.us/xena/route/internal/routecrypto"
proto "git.xeserv.us/xena/route/proto"
"git.xeserv.us/xena/route/proto/route"
"github.com/Xe/ln"
"github.com/Xe/uuid"
jwtcreds "github.com/Xe/x/tools/svc/credentials/jwt"
"github.com/dickeyxxx/netrc"
"github.com/kr/pretty"
"github.com/olekukonko/tablewriter"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"gopkg.in/alecthomas/kingpin.v2"
)
@ -69,7 +66,7 @@ var (
routesCreate = routes.Command("create", "create a new route")
routesCreateDomain = routesCreate.Flag("domain", "domain for the route (if not given one will be generated for you)").String()
routesInspect = routes.Command("inspect", "inspect one route")
routesInspect = routes.Command("inspect", "inspect one route")
routesInspectID = routesInspect.Flag("id", "ID of the route to inspect").Required().String()
routesList = routes.Command("list", "list all routes owned by you")
@ -192,28 +189,11 @@ retry_netrc:
m := n.Machine(*grpcServer)
connCreds := credentials.NewTLS(&tls.Config{
InsecureSkipVerify: true,
})
creds := jwtcreds.NewFromToken(m.Get("password"))
conn, err := grpc.Dial(*grpcServer,
grpc.WithTransportCredentials(connCreds),
grpc.WithPerRPCCredentials(creds))
if err != nil {
ln.FatalErr(ctx, err, ln.Action("dialing grpc server"), ln.F{"hostname": *grpcServer})
}
rc := proto.NewRoutesClient(conn)
tc := proto.NewTokensClient(conn)
bc := proto.NewBackendsClient(conn)
_ = rc
_ = tc
_ = bc
cl := route.New("https://"+*grpcServer, m.Get("password"), &http.Client{})
switch cmdline {
case "route create":
rt, err := rc.Put(ctx, &proto.Route{Host: *routesCreateDomain})
rt, err := cl.Routes.Put(ctx, &proto.Route{Host: *routesCreateDomain})
if err != nil {
ln.FatalErr(ctx, err, ln.Action("create new route"))
}
@ -223,7 +203,7 @@ retry_netrc:
return
case "route inspect":
r, err := rc.Get(context.Background(), &proto.GetRouteRequest{
r, err := cl.Routes.Get(context.Background(), &proto.GetRouteRequest{
Id: *routesInspectID,
})
if err != nil {
@ -236,7 +216,7 @@ retry_netrc:
return
case "route list":
rts, err := rc.GetAll(context.Background(), &proto.Nil{})
rts, err := cl.Routes.GetAll(context.Background(), &proto.Nil{})
if err != nil {
ln.FatalErr(ctx, err, ln.Action("get all routes"))
}
@ -253,7 +233,7 @@ retry_netrc:
return
case "route rm":
_, err := rc.Delete(context.Background(), &proto.Route{Id: *routesRmID})
_, err := cl.Routes.Delete(context.Background(), &proto.Route{Id: *routesRmID})
if err != nil {
ln.FatalErr(ctx, err, ln.Action("remove single route"), ln.F{"id": *routesRmID})
}
@ -261,7 +241,7 @@ retry_netrc:
return
case "backend list":
bkds, err := bc.List(context.Background(), &proto.BackendSelector{
bkds, err := cl.Backends.List(context.Background(), &proto.BackendSelector{
Domain: *backendListDomain,
User: *backendListUser,
})
@ -281,7 +261,7 @@ retry_netrc:
return
case "backend kill":
_, err := bc.Kill(context.Background(), &proto.BackendID{Id: *backendKillID})
_, err := cl.Backends.Kill(context.Background(), &proto.BackendID{Id: *backendKillID})
if err != nil {
ln.FatalErr(ctx, err, ln.Action("attempt to kill backend"), ln.F{"backend_id": *backendKillID})
}
@ -291,7 +271,7 @@ retry_netrc:
return
case "token list":
lis, err := tc.GetAll(ctx, &proto.Nil{})
lis, err := cl.Tokens.GetAll(ctx, &proto.Nil{})
if err != nil {
ln.FatalErr(ctx, err, ln.Action("get all tokens"))
}
@ -313,7 +293,7 @@ retry_netrc:
Scopes: scps,
}
ftkn, err := tc.Put(ctx, tkn)
ftkn, err := cl.Tokens.Put(ctx, tkn)
if err != nil {
ln.FatalErr(ctx, err, ln.Action("put token to server"))
}
@ -324,7 +304,7 @@ retry_netrc:
return
case "token inspect":
tkn, err := tc.Get(ctx, &proto.GetTokenRequest{Id: *tokenInspectID})
tkn, err := cl.Tokens.Get(ctx, &proto.GetTokenRequest{Id: *tokenInspectID})
if err != nil {
ln.FatalErr(ctx, err, ln.Action("fetch token from server"), ln.F{"token_id": *tokenInspectID})
}
@ -334,17 +314,17 @@ retry_netrc:
return
case "token rm":
tkn, err := tc.Get(ctx, &proto.GetTokenRequest{Id: *tokenRmID})
tkn, err := cl.Tokens.Get(ctx, &proto.GetTokenRequest{Id: *tokenRmID})
if err != nil {
ln.FatalErr(ctx, err, ln.Action("fetch token from server"), ln.F{"token_id": *tokenRmID})
}
var action ln.Fer
if *tokenRmHard {
_, err = tc.Delete(ctx, tkn)
_, err = cl.Tokens.Delete(ctx, tkn)
action = ln.Action("actually delete token")
} else {
_, err = tc.Deactivate(ctx, tkn)
_, err = cl.Tokens.Deactivate(ctx, tkn)
action = ln.Action("deactivate token")
}

View File

@ -1,15 +1,13 @@
package main
import (
"crypto/tls"
"log"
"net/http"
jwtcreds "github.com/Xe/x/tools/svc/credentials/jwt"
"git.xeserv.us/xena/route/proto/route"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/terraform"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
func main() {
@ -33,11 +31,6 @@ func provider() terraform.ResourceProvider {
Required: true,
DefaultFunc: schema.EnvDefaultFunc("ROUTE_HOST", nil),
},
"verify_tls": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("ROUTE_VERIFY_TLS", nil),
},
},
ResourcesMap: map[string]*schema.Resource{
@ -52,20 +45,10 @@ func provider() terraform.ResourceProvider {
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
token := d.Get("token").(string)
host := d.Get("host").(string)
verifyTLS := d.Get("verify_tls").(bool)
log.Printf("[INFO] Initializing route client connecting to %s", host)
connCreds := credentials.NewTLS(&tls.Config{
InsecureSkipVerify: verifyTLS,
})
creds := jwtcreds.NewFromToken(token)
conn, err := grpc.Dial(host,
grpc.WithTransportCredentials(connCreds),
grpc.WithPerRPCCredentials(creds))
if err != nil {
return nil, err
}
cl := route.New(host, token, &http.Client{})
return conn, nil
return cl, nil
}

View File

@ -5,8 +5,8 @@ import (
"log"
proto "git.xeserv.us/xena/route/proto"
"git.xeserv.us/xena/route/proto/route"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/grpc"
)
func routeResource() *schema.Resource {
@ -31,9 +31,9 @@ func routeResource() *schema.Resource {
}
func resourceRouteCreate(d *schema.ResourceData, meta interface{}) error {
cli := proto.NewRoutesClient(meta.(*grpc.ClientConn))
cli := meta.(*route.Client)
rt, err := cli.Put(context.Background(), &proto.Route{
rt, err := cli.Routes.Put(context.Background(), &proto.Route{
Host: d.Get("host").(string),
})
@ -47,14 +47,14 @@ func resourceRouteCreate(d *schema.ResourceData, meta interface{}) error {
}
func resourceRouteDelete(d *schema.ResourceData, meta interface{}) error {
cli := proto.NewRoutesClient(meta.(*grpc.ClientConn))
cli := meta.(*route.Client)
rt, err := cli.Get(context.Background(), &proto.GetRouteRequest{Id: d.Id()})
rt, err := cli.Routes.Get(context.Background(), &proto.GetRouteRequest{Id: d.Id()})
if err != nil {
return err
}
_, err = cli.Delete(context.Background(), rt)
_, err = cli.Routes.Delete(context.Background(), rt)
if err != nil {
return err
}
@ -65,9 +65,9 @@ func resourceRouteDelete(d *schema.ResourceData, meta interface{}) error {
}
func resourceRouteExists(d *schema.ResourceData, meta interface{}) (bool, error) {
cli := proto.NewRoutesClient(meta.(*grpc.ClientConn))
cli := meta.(*route.Client)
_, err := cli.Get(context.Background(), &proto.GetRouteRequest{Id: d.Id()})
_, err := cli.Routes.Get(context.Background(), &proto.GetRouteRequest{Id: d.Id()})
if err != nil {
return false, err
}
@ -76,9 +76,9 @@ func resourceRouteExists(d *schema.ResourceData, meta interface{}) (bool, error)
}
func resourceRouteRead(d *schema.ResourceData, meta interface{}) error {
cli := proto.NewRoutesClient(meta.(*grpc.ClientConn))
cli := meta.(*route.Client)
rt, err := cli.Get(context.Background(), &proto.GetRouteRequest{Id: d.Id()})
rt, err := cli.Routes.Get(context.Background(), &proto.GetRouteRequest{Id: d.Id()})
if err != nil {
return err
}

View File

@ -5,8 +5,8 @@ import (
"log"
proto "git.xeserv.us/xena/route/proto"
"git.xeserv.us/xena/route/proto/route"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/grpc"
)
func tokenResource() *schema.Resource {
@ -31,7 +31,7 @@ func tokenResource() *schema.Resource {
}
func resourceTokenCreate(d *schema.ResourceData, meta interface{}) error {
cli := proto.NewTokensClient(meta.(*grpc.ClientConn))
cli := meta.(*route.Client)
var scps []string
@ -45,7 +45,7 @@ func resourceTokenCreate(d *schema.ResourceData, meta interface{}) error {
scps = append(scps, sc)
}
tok, err := cli.Put(context.Background(), &proto.Token{Scopes: scps})
tok, err := cli.Tokens.Put(context.Background(), &proto.Token{Scopes: scps})
if err != nil {
return err
}
@ -56,14 +56,14 @@ func resourceTokenCreate(d *schema.ResourceData, meta interface{}) error {
}
func resourceTokenDelete(d *schema.ResourceData, meta interface{}) error {
cli := proto.NewTokensClient(meta.(*grpc.ClientConn))
cli := meta.(*route.Client)
tok, err := cli.Get(context.Background(), &proto.GetTokenRequest{Id: d.Id()})
tok, err := cli.Tokens.Get(context.Background(), &proto.GetTokenRequest{Id: d.Id()})
if err != nil {
return err
}
_, err = cli.Deactivate(context.Background(), tok)
_, err = cli.Tokens.Deactivate(context.Background(), tok)
if err != nil {
return err
}
@ -72,9 +72,9 @@ func resourceTokenDelete(d *schema.ResourceData, meta interface{}) error {
}
func resourceTokenExists(d *schema.ResourceData, meta interface{}) (bool, error) {
cli := proto.NewTokensClient(meta.(*grpc.ClientConn))
cli := meta.(*route.Client)
_, err := cli.Get(context.Background(), &proto.GetTokenRequest{Id: d.Id()})
_, err := cli.Tokens.Get(context.Background(), &proto.GetTokenRequest{Id: d.Id()})
if err != nil {
return false, err
}
@ -83,9 +83,9 @@ func resourceTokenExists(d *schema.ResourceData, meta interface{}) (bool, error)
}
func resourceTokenRead(d *schema.ResourceData, meta interface{}) error {
cli := proto.NewTokensClient(meta.(*grpc.ClientConn))
cli := meta.(*route.Client)
tok, err := cli.Get(context.Background(), &proto.GetTokenRequest{Id: d.Id()})
tok, err := cli.Tokens.Get(context.Background(), &proto.GetTokenRequest{Id: d.Id()})
if err != nil {
return err
}