From 902e2483abb1f551f4b294b778e8d47824ba663e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Thu, 8 Jul 2021 20:40:06 +0200 Subject: [PATCH] provider: Add options --- provider/provider.go | 51 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/provider/provider.go b/provider/provider.go index 8f23107..e2e6566 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -1,10 +1,55 @@ package provider -import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +import ( + "context" + + "git.xenrox.net/~xenrox/terraform-provider-mailman/mailman" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) func MailmanProvider() *schema.Provider { return &schema.Provider{ - ResourcesMap: map[string]*schema.Resource{}, - DataSourcesMap: map[string]*schema.Resource{}, + Schema: map[string]*schema.Schema{ + "username": { + Type: schema.TypeString, + Required: true, + Sensitive: true, + DefaultFunc: schema.EnvDefaultFunc("MAILMAN_USER", nil), + }, + "password": { + Type: schema.TypeString, + Required: true, + Sensitive: true, + DefaultFunc: schema.EnvDefaultFunc("MAILMAN_PASSWORD", nil), + }, + "url": { + Type: schema.TypeString, + Required: true, + DefaultFunc: schema.EnvDefaultFunc("MAILMAN_URL", nil), + }, + }, + ResourcesMap: map[string]*schema.Resource{}, + DataSourcesMap: map[string]*schema.Resource{}, + ConfigureContextFunc: providerConfigure, } } + +func providerConfigure(ctx context.Context, data *schema.ResourceData) (interface{}, diag.Diagnostics) { + username := data.Get("username").(string) + password := data.Get("password").(string) + url := data.Get("url").(string) + + var diags diag.Diagnostics + + mailmanClient, err := mailman.NewMailmanCLient(url, username, password) + if err != nil { + diags = append(diags, diag.Diagnostic{ + Severity: diag.Error, + Summary: "error initializing mailman provider", + Detail: err.Error(), + }) + } + + return mailmanClient, diags +} -- 2.44.0