|
| 1 | +package credentials |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "maps" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + |
| 9 | + "github.com/BurntSushi/toml" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/function" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 13 | +) |
| 14 | + |
| 15 | +var _ function.Function = &Function{} |
| 16 | + |
| 17 | +const DefaultPath = ".config/oxide/credentials.toml" |
| 18 | + |
| 19 | +type credential struct { |
| 20 | + Host string `toml:"host" tfsdk:"host"` |
| 21 | + Token string `toml:"token" tfsdk:"token"` |
| 22 | + TokenID string `toml:"token_id" tfsdk:"token_id"` |
| 23 | + User string `toml:"user" tfsdk:"user"` |
| 24 | +} |
| 25 | + |
| 26 | +type credentials struct { |
| 27 | + Profiles map[string]credential `toml:"profile"` |
| 28 | +} |
| 29 | + |
| 30 | +func NewFunction() function.Function { |
| 31 | + return &Function{} |
| 32 | +} |
| 33 | + |
| 34 | +type Function struct{} |
| 35 | + |
| 36 | +func (f *Function) Metadata( |
| 37 | + ctx context.Context, |
| 38 | + req function.MetadataRequest, |
| 39 | + resp *function.MetadataResponse, |
| 40 | +) { |
| 41 | + resp.Name = "credentials" |
| 42 | +} |
| 43 | + |
| 44 | +func (f *Function) Definition( |
| 45 | + ctx context.Context, |
| 46 | + req function.DefinitionRequest, |
| 47 | + resp *function.DefinitionResponse, |
| 48 | +) { |
| 49 | + resp.Definition = function.Definition{ |
| 50 | + Summary: "Reads an Oxide credentials file.", |
| 51 | + MarkdownDescription: ` |
| 52 | +Reads Oxide API credentials from a local credentials file and returns a map of |
| 53 | +credentials grouped by profile name. Refer to the [Oxide CLI |
| 54 | +documentation](https://docs.oxide.computer/cli/guides/introduction) for more |
| 55 | +information. |
| 56 | +`, |
| 57 | + Parameters: []function.Parameter{ |
| 58 | + function.StringParameter{ |
| 59 | + Name: "path", |
| 60 | + Description: "Credentials file path. Defaults to `$HOME/.config/oxide/credentials.toml` if empty or `null`.", |
| 61 | + AllowNullValue: true, |
| 62 | + }, |
| 63 | + }, |
| 64 | + Return: function.MapReturn{ |
| 65 | + ElementType: types.ObjectType{ |
| 66 | + AttrTypes: map[string]attr.Type{ |
| 67 | + "host": types.StringType, |
| 68 | + "token": types.StringType, |
| 69 | + "token_id": types.StringType, |
| 70 | + "user": types.StringType, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func (f *Function) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { |
| 78 | + var pathInput types.String |
| 79 | + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &pathInput)) |
| 80 | + |
| 81 | + pathStr := pathInput.ValueString() |
| 82 | + if pathStr == "" { |
| 83 | + homeDir, err := os.UserHomeDir() |
| 84 | + if err != nil { |
| 85 | + resp.Error = function.ConcatFuncErrors(resp.Error, function.NewFuncError(err.Error())) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + pathStr = path.Join(homeDir, DefaultPath) |
| 90 | + } |
| 91 | + |
| 92 | + var creds credentials |
| 93 | + if _, err := toml.DecodeFile(pathStr, &creds); err != nil { |
| 94 | + resp.Error = function.ConcatFuncErrors(resp.Error, function.NewFuncError(err.Error())) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + output := make(map[string]credential) |
| 99 | + maps.Copy(output, creds.Profiles) |
| 100 | + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, output)) |
| 101 | +} |
0 commit comments