Skip to content

Commit e6cd8eb

Browse files
authored
Merge pull request #15 from coder/stevenmasley/set_owner
add owner information to the template context
2 parents 1142522 + d85d84a commit e6cd8eb

File tree

6 files changed

+51
-5
lines changed

6 files changed

+51
-5
lines changed

preview/apitypes/apitypes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ const (
3333
OptionTypeListString OptionType = "list(string)"
3434
)
3535

36+
type WorkspaceOwner = types.WorkspaceOwner
37+
3638
type PreviewOutput struct {
3739
Output *Output `json:"output"`
3840
Diags types.Diagnostics `json:"diags"`

preview/main.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818

1919
"github.com/coder/preview"
2020
"github.com/coder/preview/types"
21-
2221
"github.com/coder/parameters-playground/preview/apitypes"
2322
)
2423

@@ -61,12 +60,17 @@ func tfpreview(this js.Value, p []js.Value) (output any) {
6160
return err
6261
}
6362

63+
owner, err := workspaceOwner(p[1])
64+
if err != nil {
65+
return err
66+
}
67+
6468
handler := slog.NewJSONHandler(l, nil)
6569
logger := slog.New(handler)
6670

6771
var parameters map[string]string
68-
if len(p) >= 2 {
69-
params, err := jsValueToStringMap(p[1])
72+
if len(p) >= 3 {
73+
params, err := jsValueToStringMap(p[2])
7074
if err != nil {
7175
logger.Error("Unable to convert second prameter into map[string]string", "err", err)
7276
}
@@ -81,7 +85,7 @@ func tfpreview(this js.Value, p []js.Value) (output any) {
8185
PlanJSONPath: "",
8286
PlanJSON: nil,
8387
ParameterValues: parameters,
84-
Owner: types.WorkspaceOwner{},
88+
Owner: owner,
8589
Logger: logger,
8690
}, tf)
8791

@@ -110,6 +114,16 @@ func fileTreeFS(value js.Value) (fs.FS, error) {
110114
return afero.NewIOFS(mem), nil
111115
}
112116

117+
func workspaceOwner(value js.Value) (apitypes.WorkspaceOwner, error) {
118+
data := js.Global().Get("JSON").Call("stringify", value).String()
119+
var owner apitypes.WorkspaceOwner
120+
if err := json.Unmarshal([]byte(data), &owner); err != nil {
121+
return apitypes.WorkspaceOwner(types.WorkspaceOwner{}), err
122+
}
123+
124+
return owner, nil
125+
}
126+
113127
func loadTree(mem afero.Fs, fileTree map[string]any, path ...string) {
114128
dir := filepath.Join(path...)
115129
err := mem.MkdirAll(dir, 0755)

public/build/preview.wasm

2.01 KB
Binary file not shown.

src/client/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ import {
3131
} from "@/client/components/Tooltip";
3232
import { rpc } from "@/utils/rpc";
3333
import { useLoaderData, type LoaderFunctionArgs } from "react-router";
34+
import type {WorkspaceOwner} from "@/gen/types.ts";
3435

3536
type GoPreviewDef = (
3637
v: Record<string, string>,
38+
owner: WorkspaceOwner,
3739
params: Record<string, string>,
3840
) => Promise<string>;
3941

src/client/Preview.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from "@/client/diagnostics";
1515
import { useDebouncedValue } from "@/client/hooks/debounce";
1616
import { useStore } from "@/client/store";
17-
import type { Parameter, ParserLog, PreviewOutput } from "@/gen/types";
17+
import type {Parameter, ParserLog, PreviewOutput} from "@/gen/types";
1818
import { cn } from "@/utils/cn";
1919
import ReactJsonView from "@microlink/react-json-view";
2020
import * as Dialog from "@radix-ui/react-dialog";
@@ -83,6 +83,16 @@ export const Preview: FC = () => {
8383
{
8484
"main.tf": debouncedCode,
8585
},
86+
{
87+
id: "8d36e355-e775-4c49-9b8d-ac042ed50440",
88+
name: "coder",
89+
full_name: "Coder",
90+
email: "coder@coder.com",
91+
ssh_public_key: "",
92+
groups: ["Everyone"],
93+
login_type: "password",
94+
rbac_roles: [{name:"member", org_id:""}, {name:"organization-member",org_id:"09942665-ba1b-4661-be9f-36bf9f738c83"}]
95+
},
8696
$form,
8797
);
8898

src/gen/types.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,22 @@ export interface Range {
134134
End: Pos;
135135
}
136136

137+
// From apitypes/apitypes.go
138+
export interface WorkspaceOwner {
139+
id: string;
140+
name: string;
141+
full_name: string;
142+
email: string;
143+
ssh_public_key: string;
144+
groups: string[];
145+
login_type: string;
146+
rbac_roles: WorkspaceOwnerRBACRole[];
147+
}
148+
149+
// From types/owner.go
150+
export interface WorkspaceOwnerRBACRole {
151+
name: string;
152+
org_id: string;
153+
}
154+
137155

0 commit comments

Comments
 (0)