diff --git a/example/user/main.tf b/example/user/main.tf index 4a1aaa1..5d09f80 100644 --- a/example/user/main.tf +++ b/example/user/main.tf @@ -11,10 +11,12 @@ provider "plural" { use_cli = true } +data "plural_config" "config" {} + data "plural_user" "user" { email = "marcin@plural.sh" } data "plural_group" "group" { name = "team" -} \ No newline at end of file +} diff --git a/internal/datasource/config.go b/internal/datasource/config.go index e6eafe3..6eaacf7 100644 --- a/internal/datasource/config.go +++ b/internal/datasource/config.go @@ -3,25 +3,15 @@ package datasource import ( "context" "fmt" - "os" "terraform-provider-plural/internal/client" "terraform-provider-plural/internal/common" - - "github.com/mitchellh/go-homedir" + "terraform-provider-plural/internal/model" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" - "github.com/hashicorp/terraform-plugin-framework/types" - - "gopkg.in/yaml.v2" ) -type config struct { - Email types.String `tfsdk:"email" yaml:"email"` - Token types.String `tfsdk:"token" yaml:"email"` -} - func NewConfigDataSource() datasource.DataSource { return &configDataSource{} } @@ -39,11 +29,12 @@ func (d *configDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, MarkdownDescription: "A representation of a config to authenticate to app.plural.sh", Attributes: map[string]schema.Attribute{ "email": schema.StringAttribute{ - Optional: true, - Computed: true, + Description: "The email used to authenticate to plural.", MarkdownDescription: "The email used to authenticate to plural.", + Computed: true, }, "token": schema.StringAttribute{ + Description: "Access token used to authenticate to plural.", MarkdownDescription: "Access token used to authenticate to plural.", Computed: true, }, @@ -69,33 +60,12 @@ func (d *configDataSource) Configure(_ context.Context, req datasource.Configure } func (d *configDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { - var data config + var data model.Config resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } - p, err := homedir.Expand("~/.plural/config.yml") - if err != nil { - resp.Diagnostics.AddWarning("Client Error", fmt.Sprintf("Could not read local plural config: %s", err)) - return - } - - res, err := os.ReadFile(p) - if err != nil { - resp.Diagnostics.AddWarning("Client Error", fmt.Sprintf("Could not read local plural config: %s", err)) - return - } - - var conf struct { - Spec config - } - - if err := yaml.Unmarshal(res, &conf); err != nil { - resp.Diagnostics.AddWarning("Client Error", fmt.Sprintf("Could not parse local plural config: %s", err)) - return - } - - data = conf.Spec + data.From(resp.Diagnostics) resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } diff --git a/internal/model/config.go b/internal/model/config.go new file mode 100644 index 0000000..3c2a483 --- /dev/null +++ b/internal/model/config.go @@ -0,0 +1,48 @@ +package model + +import ( + "fmt" + "os" + + "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/mitchellh/go-homedir" + "gopkg.in/yaml.v2" +) + +type Config struct { + Email types.String `tfsdk:"email" yaml:"email"` + Token types.String `tfsdk:"token" yaml:"token"` +} + +type LocalConfig struct { + Spec LocalConfigSpec `yaml:"spec"` +} + +type LocalConfigSpec struct { + Email string `yaml:"email"` + Token string `yaml:"token"` +} + +func (c *Config) From(d diag.Diagnostics) { + p, err := homedir.Expand("~/.plural/config.yml") + if err != nil { + d.AddError("Client Error", fmt.Sprintf("Could not find local plural config: %s", err)) + return + } + + res, err := os.ReadFile(p) + if err != nil { + d.AddError("Client Error", fmt.Sprintf("Could not read local plural config: %s", err)) + return + } + + var config LocalConfig + if err := yaml.Unmarshal(res, &config); err != nil { + d.AddError("Client Error", fmt.Sprintf("Could not parse local plural config: %s", err)) + return + } + + c.Email = types.StringValue(config.Spec.Email) + c.Token = types.StringValue(config.Spec.Token) +}