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 ( import (
"context" "context"
"crypto/tls"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
@ -16,14 +15,12 @@ import (
"git.xeserv.us/xena/route/internal/database" "git.xeserv.us/xena/route/internal/database"
"git.xeserv.us/xena/route/internal/routecrypto" "git.xeserv.us/xena/route/internal/routecrypto"
proto "git.xeserv.us/xena/route/proto" proto "git.xeserv.us/xena/route/proto"
"git.xeserv.us/xena/route/proto/route"
"github.com/Xe/ln" "github.com/Xe/ln"
"github.com/Xe/uuid" "github.com/Xe/uuid"
jwtcreds "github.com/Xe/x/tools/svc/credentials/jwt"
"github.com/dickeyxxx/netrc" "github.com/dickeyxxx/netrc"
"github.com/kr/pretty" "github.com/kr/pretty"
"github.com/olekukonko/tablewriter" "github.com/olekukonko/tablewriter"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"gopkg.in/alecthomas/kingpin.v2" "gopkg.in/alecthomas/kingpin.v2"
) )
@ -192,28 +189,11 @@ retry_netrc:
m := n.Machine(*grpcServer) m := n.Machine(*grpcServer)
connCreds := credentials.NewTLS(&tls.Config{ cl := route.New("https://"+*grpcServer, m.Get("password"), &http.Client{})
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
switch cmdline { switch cmdline {
case "route create": 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 { if err != nil {
ln.FatalErr(ctx, err, ln.Action("create new route")) ln.FatalErr(ctx, err, ln.Action("create new route"))
} }
@ -223,7 +203,7 @@ retry_netrc:
return return
case "route inspect": case "route inspect":
r, err := rc.Get(context.Background(), &proto.GetRouteRequest{ r, err := cl.Routes.Get(context.Background(), &proto.GetRouteRequest{
Id: *routesInspectID, Id: *routesInspectID,
}) })
if err != nil { if err != nil {
@ -236,7 +216,7 @@ retry_netrc:
return return
case "route list": case "route list":
rts, err := rc.GetAll(context.Background(), &proto.Nil{}) rts, err := cl.Routes.GetAll(context.Background(), &proto.Nil{})
if err != nil { if err != nil {
ln.FatalErr(ctx, err, ln.Action("get all routes")) ln.FatalErr(ctx, err, ln.Action("get all routes"))
} }
@ -253,7 +233,7 @@ retry_netrc:
return return
case "route rm": 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 { if err != nil {
ln.FatalErr(ctx, err, ln.Action("remove single route"), ln.F{"id": *routesRmID}) ln.FatalErr(ctx, err, ln.Action("remove single route"), ln.F{"id": *routesRmID})
} }
@ -261,7 +241,7 @@ retry_netrc:
return return
case "backend list": case "backend list":
bkds, err := bc.List(context.Background(), &proto.BackendSelector{ bkds, err := cl.Backends.List(context.Background(), &proto.BackendSelector{
Domain: *backendListDomain, Domain: *backendListDomain,
User: *backendListUser, User: *backendListUser,
}) })
@ -281,7 +261,7 @@ retry_netrc:
return return
case "backend kill": 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 { if err != nil {
ln.FatalErr(ctx, err, ln.Action("attempt to kill backend"), ln.F{"backend_id": *backendKillID}) ln.FatalErr(ctx, err, ln.Action("attempt to kill backend"), ln.F{"backend_id": *backendKillID})
} }
@ -291,7 +271,7 @@ retry_netrc:
return return
case "token list": case "token list":
lis, err := tc.GetAll(ctx, &proto.Nil{}) lis, err := cl.Tokens.GetAll(ctx, &proto.Nil{})
if err != nil { if err != nil {
ln.FatalErr(ctx, err, ln.Action("get all tokens")) ln.FatalErr(ctx, err, ln.Action("get all tokens"))
} }
@ -313,7 +293,7 @@ retry_netrc:
Scopes: scps, Scopes: scps,
} }
ftkn, err := tc.Put(ctx, tkn) ftkn, err := cl.Tokens.Put(ctx, tkn)
if err != nil { if err != nil {
ln.FatalErr(ctx, err, ln.Action("put token to server")) ln.FatalErr(ctx, err, ln.Action("put token to server"))
} }
@ -324,7 +304,7 @@ retry_netrc:
return return
case "token inspect": 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 { if err != nil {
ln.FatalErr(ctx, err, ln.Action("fetch token from server"), ln.F{"token_id": *tokenInspectID}) ln.FatalErr(ctx, err, ln.Action("fetch token from server"), ln.F{"token_id": *tokenInspectID})
} }
@ -334,17 +314,17 @@ retry_netrc:
return return
case "token rm": 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 { if err != nil {
ln.FatalErr(ctx, err, ln.Action("fetch token from server"), ln.F{"token_id": *tokenRmID}) ln.FatalErr(ctx, err, ln.Action("fetch token from server"), ln.F{"token_id": *tokenRmID})
} }
var action ln.Fer var action ln.Fer
if *tokenRmHard { if *tokenRmHard {
_, err = tc.Delete(ctx, tkn) _, err = cl.Tokens.Delete(ctx, tkn)
action = ln.Action("actually delete token") action = ln.Action("actually delete token")
} else { } else {
_, err = tc.Deactivate(ctx, tkn) _, err = cl.Tokens.Deactivate(ctx, tkn)
action = ln.Action("deactivate token") action = ln.Action("deactivate token")
} }

View File

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

View File

@ -5,8 +5,8 @@ import (
"log" "log"
proto "git.xeserv.us/xena/route/proto" proto "git.xeserv.us/xena/route/proto"
"git.xeserv.us/xena/route/proto/route"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"google.golang.org/grpc"
) )
func routeResource() *schema.Resource { func routeResource() *schema.Resource {
@ -31,9 +31,9 @@ func routeResource() *schema.Resource {
} }
func resourceRouteCreate(d *schema.ResourceData, meta interface{}) error { 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), 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 { 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 { if err != nil {
return err return err
} }
_, err = cli.Delete(context.Background(), rt) _, err = cli.Routes.Delete(context.Background(), rt)
if err != nil { if err != nil {
return err return err
} }
@ -65,9 +65,9 @@ func resourceRouteDelete(d *schema.ResourceData, meta interface{}) error {
} }
func resourceRouteExists(d *schema.ResourceData, meta interface{}) (bool, 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 { if err != nil {
return false, err return false, err
} }
@ -76,9 +76,9 @@ func resourceRouteExists(d *schema.ResourceData, meta interface{}) (bool, error)
} }
func resourceRouteRead(d *schema.ResourceData, meta interface{}) 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 { if err != nil {
return err return err
} }

View File

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