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