Skip to content

Commit

Permalink
feat: support to get Tsuru target and token from ~/.tsuru files
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Aug 14, 2023
1 parent a996d6f commit d7ed62c
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}

Expand Down

0 comments on commit d7ed62c

Please sign in to comment.