Skip to content

Commit 94eb484

Browse files
committed
Refactor parameters to enable schema matching
1 parent 65380db commit 94eb484

25 files changed

+390
-634
lines changed

cli/clitest/clitest.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ package clitest
22

33
import (
44
"bufio"
5+
"context"
56
"io"
67
"testing"
78

89
"github.com/spf13/cobra"
10+
"github.com/stretchr/testify/require"
911

1012
"github.com/coder/coder/cli"
1113
"github.com/coder/coder/cli/config"
14+
"github.com/coder/coder/coderd"
15+
"github.com/coder/coder/coderd/coderdtest"
16+
"github.com/coder/coder/codersdk"
1217
)
1318

1419
func New(t *testing.T, args ...string) (*cobra.Command, config.Root) {
@@ -19,6 +24,20 @@ func New(t *testing.T, args ...string) (*cobra.Command, config.Root) {
1924
return cmd, root
2025
}
2126

27+
func CreateInitialUser(t *testing.T, client *codersdk.Client, root config.Root) coderd.CreateInitialUserRequest {
28+
user := coderdtest.CreateInitialUser(t, client)
29+
resp, err := client.LoginWithPassword(context.Background(), coderd.LoginWithPasswordRequest{
30+
Email: user.Email,
31+
Password: user.Password,
32+
})
33+
require.NoError(t, err)
34+
err = root.Session().Write(resp.SessionToken)
35+
require.NoError(t, err)
36+
err = root.URL().Write(client.URL.String())
37+
require.NoError(t, err)
38+
return user
39+
}
40+
2241
func StdoutLogs(t *testing.T) io.Writer {
2342
reader, writer := io.Pipe()
2443
scanner := bufio.NewScanner(reader)

cli/projectcreate.go

Lines changed: 81 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import (
2121
)
2222

2323
func projectCreate() *cobra.Command {
24-
return &cobra.Command{
24+
var (
25+
directory string
26+
)
27+
cmd := &cobra.Command{
2528
Use: "create",
2629
Short: "Create a project from the current directory",
2730
RunE: func(cmd *cobra.Command, args []string) error {
@@ -33,23 +36,17 @@ func projectCreate() *cobra.Command {
3336
if err != nil {
3437
return err
3538
}
36-
37-
workingDir, err := os.Getwd()
38-
if err != nil {
39-
return err
40-
}
41-
4239
_, err = runPrompt(cmd, &promptui.Prompt{
4340
Default: "y",
4441
IsConfirm: true,
45-
Label: fmt.Sprintf("Set up %s in your organization?", color.New(color.FgHiCyan).Sprintf("%q", workingDir)),
42+
Label: fmt.Sprintf("Set up %s in your organization?", color.New(color.FgHiCyan).Sprintf("%q", directory)),
4643
})
4744
if err != nil {
4845
return err
4946
}
5047

5148
name, err := runPrompt(cmd, &promptui.Prompt{
52-
Default: filepath.Base(workingDir),
49+
Default: filepath.Base(directory),
5350
Label: "What's your project's name?",
5451
Validate: func(s string) error {
5552
_, err = client.Project(cmd.Context(), organization.Name, s)
@@ -63,12 +60,12 @@ func projectCreate() *cobra.Command {
6360
return err
6461
}
6562

66-
spin := spinner.New(spinner.CharSets[0], 50*time.Millisecond)
63+
spin := spinner.New(spinner.CharSets[0], 25*time.Millisecond)
6764
spin.Suffix = " Uploading current directory..."
6865
spin.Start()
6966
defer spin.Stop()
7067

71-
bytes, err := tarDirectory(workingDir)
68+
bytes, err := tarDirectory(directory)
7269
if err != nil {
7370
return err
7471
}
@@ -84,6 +81,12 @@ func projectCreate() *cobra.Command {
8481
Provisioner: database.ProvisionerTypeTerraform,
8582
// SkipResources on first import to detect variables defined by the project.
8683
SkipResources: true,
84+
// ParameterValues: []coderd.CreateParameterValueRequest{{
85+
// Name: "aws_access_key",
86+
// SourceValue: "tomato",
87+
// SourceScheme: database.ParameterSourceSchemeData,
88+
// DestinationScheme: database.ParameterDestinationSchemeProvisionerVariable,
89+
// }},
8790
})
8891
if err != nil {
8992
return err
@@ -102,19 +105,85 @@ func projectCreate() *cobra.Command {
102105
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s %s\n", color.HiGreenString("[parse]"), log.Output)
103106
}
104107

108+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Parsed project source... displaying parameters:")
109+
105110
schemas, err := client.ProvisionerJobParameterSchemas(cmd.Context(), organization.Name, job.ID)
106111
if err != nil {
107112
return err
108113
}
109114

115+
values, err := client.ProvisionerJobParameterValues(cmd.Context(), organization.Name, job.ID)
116+
if err != nil {
117+
return err
118+
}
119+
valueBySchemaID := map[string]coderd.ComputedParameterValue{}
120+
for _, value := range values {
121+
valueBySchemaID[value.SchemaID.String()] = value
122+
}
123+
110124
for _, schema := range schemas {
111-
fmt.Printf("Schema: %+v\n", schema)
125+
if value, ok := valueBySchemaID[schema.ID.String()]; ok {
126+
fmt.Printf("Value for: %s %s\n", value.Name, value.SourceValue)
127+
continue
128+
}
129+
fmt.Printf("No value for: %s\n", schema.Name)
112130
}
113131

132+
// schemas, err := client.ProvisionerJobParameterSchemas(cmd.Context(), organization.Name, job.ID)
133+
// if err != nil {
134+
// return err
135+
// }
136+
// _, _ = fmt.Fprintf(cmd.OutOrStdout(), "\n %s\n\n", color.HiBlackString("Parameters"))
137+
138+
// for _, param := range params {
139+
// if param.Value == nil {
140+
// _, _ = fmt.Fprintf(cmd.OutOrStdout(), " %s = must be set\n", color.HiRedString(param.Schema.Name))
141+
// continue
142+
// }
143+
// value := param.Value.DestinationValue
144+
// if !param.Schema.RedisplayValue {
145+
// value = "<redacted>"
146+
// }
147+
// output := fmt.Sprintf(" %s = %s", color.HiGreenString(param.Value.SourceValue), color.CyanString(value))
148+
// param.Value.DefaultSourceValue = false
149+
// param.Value.Scope = database.ParameterScopeOrganization
150+
// param.Value.ScopeID = organization.ID
151+
// if param.Value.DefaultSourceValue {
152+
// output += " (default value)"
153+
// } else {
154+
// output += fmt.Sprintf(" (inherited from %s)", param.Value.Scope)
155+
// }
156+
// root := treeprint.NewWithRoot(output)
157+
// root.AddNode(color.HiBlackString("Description") + "\n" + param.Schema.Description)
158+
// fmt.Fprintln(cmd.OutOrStdout(), strings.Join(strings.Split(root.String(), "\n"), "\n "))
159+
// }
160+
161+
// for _, param := range params {
162+
// if param.Value != nil {
163+
// continue
164+
// }
165+
166+
// value, err := runPrompt(cmd, &promptui.Prompt{
167+
// Label: "Specify value for " + color.HiCyanString(param.Schema.Name),
168+
// Validate: func(s string) error {
169+
// // param.Schema.Vali
170+
// return nil
171+
// },
172+
// })
173+
// if err != nil {
174+
// continue
175+
// }
176+
// fmt.Printf(": %s\n", value)
177+
// }
178+
114179
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Create project %q!\n", name)
115180
return nil
116181
},
117182
}
183+
currentDirectory, _ := os.Getwd()
184+
cmd.Flags().StringVarP(&directory, "directory", "d", currentDirectory, "Specify the directory to create from")
185+
186+
return cmd
118187
}
119188

120189
func tarDirectory(directory string) ([]byte, error) {

cli/projectcreate_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package cli_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/Netflix/go-expect"
7+
"github.com/coder/coder/cli/clitest"
8+
"github.com/coder/coder/coderd/coderdtest"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestProjectCreate(t *testing.T) {
13+
t.Parallel()
14+
t.Run("InitialUserTTY", func(t *testing.T) {
15+
t.Parallel()
16+
console, err := expect.NewConsole(expect.WithStdout(clitest.StdoutLogs(t)))
17+
require.NoError(t, err)
18+
client := coderdtest.New(t)
19+
directory := t.TempDir()
20+
cmd, root := clitest.New(t, "projects", "create", "--directory", directory)
21+
_ = clitest.CreateInitialUser(t, client, root)
22+
cmd.SetIn(console.Tty())
23+
cmd.SetOut(console.Tty())
24+
go func() {
25+
err := cmd.Execute()
26+
require.NoError(t, err)
27+
}()
28+
29+
matches := []string{
30+
"organization?", "y",
31+
"name?", "",
32+
}
33+
for i := 0; i < len(matches); i += 2 {
34+
match := matches[i]
35+
value := matches[i+1]
36+
_, err = console.ExpectString(match)
37+
require.NoError(t, err)
38+
_, err = console.SendLine(value)
39+
require.NoError(t, err)
40+
}
41+
})
42+
}

cli/projects.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77

88
func projects() *cobra.Command {
99
cmd := &cobra.Command{
10-
Use: "projects",
11-
Long: "Testing something",
10+
Use: "projects",
11+
Aliases: []string{"project"},
12+
Long: "Testing something",
1213
Example: `
1314
- Create a project for developers to create workspaces
1415

coderd/coderd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ func New(options *Options) http.Handler {
121121
r.Route("/{provisionerjob}", func(r chi.Router) {
122122
r.Use(httpmw.ExtractProvisionerJobParam(options.Database))
123123
r.Get("/", api.provisionerJobByOrganization)
124-
r.Get("/parameters", api.provisionerJobParametersByID)
124+
r.Get("/schemas", api.provisionerJobParameterSchemasByID)
125+
r.Get("/computed", api.provisionerJobComputedParametersByID)
125126
r.Get("/logs", api.provisionerJobLogsByID)
126127
})
127128
})

coderd/parameter/compute_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,22 @@ func TestCompute(t *testing.T) {
201201
require.Len(t, computed, 1)
202202
require.Equal(t, false, computed[0].DefaultSourceValue)
203203
})
204+
205+
t.Run("HideRedisplay", func(t *testing.T) {
206+
t.Parallel()
207+
db := databasefake.New()
208+
scope := generateScope()
209+
_ = generateParameter(t, db, parameterOptions{
210+
ProjectImportJobID: scope.ProjectImportJobID,
211+
DefaultDestinationScheme: database.ParameterDestinationSchemeProvisionerVariable,
212+
})
213+
computed, err := parameter.Compute(context.Background(), db, scope, &parameter.ComputeOptions{
214+
HideRedisplayValues: true,
215+
})
216+
require.NoError(t, err)
217+
require.Len(t, computed, 1)
218+
computedValue := computed[0]
219+
require.True(t, computedValue.DefaultSourceValue)
220+
require.Equal(t, computedValue.SourceValue, "")
221+
})
204222
}

0 commit comments

Comments
 (0)