Skip to content

Commit

Permalink
fix config data source
Browse files Browse the repository at this point in the history
  • Loading branch information
maciaszczykm committed Jul 25, 2024
1 parent 584fce6 commit a951e26
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 37 deletions.
4 changes: 3 additions & 1 deletion example/user/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ provider "plural" {
use_cli = true
}

data "plural_config" "config" {}

data "plural_user" "user" {
email = "[email protected]"
}

data "plural_group" "group" {
name = "team"
}
}
42 changes: 6 additions & 36 deletions internal/datasource/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand All @@ -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,
},
Expand All @@ -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)...)
}
48 changes: 48 additions & 0 deletions internal/model/config.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit a951e26

Please sign in to comment.