Skip to content

Instantly share code, notes, and snippets.

@dcaponi
Last active May 2, 2020 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcaponi/c5ad0ee16a0b569e617d654bd80e587a to your computer and use it in GitHub Desktop.
Save dcaponi/c5ad0ee16a0b569e617d654bd80e587a to your computer and use it in GitHub Desktop.
tf boilerplate
package main
import (
"github.com/hashicorp/terraform-plugin-sdk/plugin"
"github.com/onelogin/onelogin-terraform-provider/onelogin"
)
func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: onelogin.Provider,
})
}
package onelogin
import (
"errors"
"github.com/onelogin/onelogin-go-sdk/pkg/client"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)
var (
errClientCredentials = errors.New("client_id or client_sercret or region missing")
)
// Provider creates a new provider with all the neccessary configurations.
// It returns a pointer to the created provider.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"client_id": &schema.Schema{
Type: schema.TypeString,
DefaultFunc: schema.EnvDefaultFunc("ONELOGIN_CLIENT_ID", nil),
Required: true,
},
"client_secret": &schema.Schema{
Type: schema.TypeString,
DefaultFunc: schema.EnvDefaultFunc("ONELOGIN_CLIENT_SECRET", nil),
Required: true,
},
"url": &schema.Schema{
Type: schema.TypeString,
DefaultFunc: schema.EnvDefaultFunc("ONELOGIN_OAPI_URL", nil),
Optional: true,
},
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: client.USRegion,
},
},
ResourcesMap: map[string]*schema.Resource{
"onelogin_apps": Apps(),
"onelogin_oidc_apps": OIDCApps(),
"onelogin_saml_apps": SAMLApps(),
},
ConfigureFunc: configProvider,
}
}
// configProvider configures the provider, and if successful, it returns
// an interface containing the api client.
func configProvider(d *schema.ResourceData) (interface{}, error) {
clientID := d.Get("client_id").(string)
clientSecret := d.Get("client_secret").(string)
region := d.Get("region").(string)
url := d.Get("url").(string)
timeout := client.DefaultTimeout
oneloginClient, err := client.NewClient(&client.APIClientConfig{
Timeout: timeout,
ClientID: clientID,
ClientSecret: clientSecret,
Region: region,
Url: url,
})
if err != nil {
return nil, err
}
return oneloginClient, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment