diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 9f6ce27..f5922ab 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -19,6 +19,7 @@ import ( tsuruclient "github.com/tsuru/go-tsuruclient/pkg/client" rpaasclient "github.com/tsuru/rpaas-operator/pkg/rpaas/client" "github.com/tsuru/rpaas-operator/pkg/rpaas/client/autogenerated" + "github.com/tsuru/tsuru/cmd" ) var Version string = "undefined" @@ -98,7 +99,10 @@ func (rp *rpaasProvider) Client(service, instance string) *autogenerated.APIClie } func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) { - providerOpts := getProviderConfigOpts(d) + providerOpts, err := getProviderConfigOpts(d) + if err != nil { + return nil, diag.FromErr(err) + } legacyClient, err := getLegacyClient(providerOpts) if err != nil { @@ -123,7 +127,7 @@ type ProviderConfigOptions struct { InsecureSkipVerify bool } -func getProviderConfigOpts(d *schema.ResourceData) *ProviderConfigOptions { +func getProviderConfigOpts(d *schema.ResourceData) (*ProviderConfigOptions, error) { var opts ProviderConfigOptions if v, ok := d.GetOk("rpaas_url"); ok { @@ -154,7 +158,25 @@ func getProviderConfigOpts(d *schema.ResourceData) *ProviderConfigOptions { opts.InsecureSkipVerify = v.(bool) } - return &opts + if opts.TsuruTarget != "" { + target, err := cmd.ReadTarget() + if err != nil { + return nil, err + } + + opts.TsuruTarget = target + } + + if opts.TsuruTarget != "" && opts.TsuruToken == "" { + token, err := cmd.ReadToken() + if err != nil { + return nil, err + } + + opts.TsuruToken = token + } + + return &opts, nil } func getLegacyClient(opts *ProviderConfigOptions) (rpaasclient.Client, error) { @@ -172,6 +194,15 @@ func getLegacyClient(opts *ProviderConfigOptions) (rpaasclient.Client, error) { service = opts.TsuruService } + if opts.TsuruToken == "" { + token, err := cmd.ReadToken() + if err != nil { + return nil, err + } + + opts.TsuruToken = token + } + return rpaasclient.NewClientThroughTsuruWithOptions(opts.TsuruTarget, opts.TsuruToken, service, legacyClientOpts) }