2017-12-02 23:34:35 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
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,
|
2017-12-03 01:13:52 +00:00
|
|
|
Optional: true,
|
2017-12-02 23:34:35 +00:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("ROUTE_TOKEN", nil),
|
|
|
|
},
|
|
|
|
"host": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: false,
|
2017-12-03 02:00:32 +00:00
|
|
|
Required: true,
|
2017-12-02 23:34:35 +00:00
|
|
|
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(),
|
2017-12-03 16:53:07 +00:00
|
|
|
"route_token": tokenResource(),
|
2017-12-02 23:34:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-12-03 16:53:07 +00:00
|
|
|
return conn, nil
|
2017-12-02 23:34:35 +00:00
|
|
|
}
|