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

74 lines
1.7 KiB
Go

package main
import (
"crypto/tls"
"log"
proto "git.xeserv.us/xena/route/proto"
jwtcreds "github.com/Xe/x/tools/svc/credentials/jwt"
"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() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: provider,
})
}
// provider returns a terraform.ResourceProvider.
func provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"token": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("ROUTE_TOKEN", nil),
},
"host": &schema.Schema{
Type: schema.TypeString,
Optional: false,
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{
"route_route": routeResource(),
},
ConfigureFunc: providerConfigure,
}
}
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
}
rc := proto.NewRoutesClient(conn)
return rc, nil
}