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

72 lines
1.7 KiB
Go

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,
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(),
"route_token": tokenResource(),
},
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
}
return conn, nil
}