Skip to content

Commit 34f8567

Browse files
authored
Merge pull request #65 from maysunfaisal/357-1
Add Delete Comp, Command, Proj, StarterProj
2 parents 813456e + 5e32c06 commit 34f8567

14 files changed

+597
-362
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/openshift/api v0.0.0-20200930075302-db52bc4ef99f
1616
github.com/pkg/errors v0.9.1
1717
github.com/spf13/afero v1.2.2
18-
github.com/stretchr/testify v1.6.1 // indirect
18+
github.com/stretchr/testify v1.6.1
1919
github.com/xeipuuv/gojsonschema v1.2.0
2020
k8s.io/api v0.19.0
2121
k8s.io/apimachinery v0.19.0

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
494494
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
495495
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
496496
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
497+
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
497498
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
498499
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
499500
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

pkg/devfile/parser/data/interface.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,30 @@ type DevfileData interface {
2626
GetComponents(common.DevfileOptions) ([]v1.Component, error)
2727
AddComponents(components []v1.Component) error
2828
UpdateComponent(component v1.Component)
29+
DeleteComponent(name string) error
2930

3031
// project related methods
3132
GetProjects(common.DevfileOptions) ([]v1.Project, error)
3233
AddProjects(projects []v1.Project) error
3334
UpdateProject(project v1.Project)
35+
DeleteProject(name string) error
3436

3537
// starter projects related commands
3638
GetStarterProjects(common.DevfileOptions) ([]v1.StarterProject, error)
3739
AddStarterProjects(projects []v1.StarterProject) error
3840
UpdateStarterProject(project v1.StarterProject)
41+
DeleteStarterProject(name string) error
3942

4043
// command related methods
4144
GetCommands(common.DevfileOptions) ([]v1.Command, error)
42-
AddCommands(commands ...v1.Command) error
45+
AddCommands(commands []v1.Command) error
4346
UpdateCommand(command v1.Command)
47+
DeleteCommand(id string) error
4448

45-
// volume related methods
46-
AddVolume(volume v1.Component, path string) error
47-
DeleteVolume(name string) error
48-
GetVolumeMountPath(name string) (string, error)
49+
// volume mount related methods
50+
AddVolumeMounts(componentName string, volumeMounts []v1.VolumeMount) error
51+
DeleteVolumeMount(name string) error
52+
GetVolumeMountPath(mountName, componentName string) (string, error)
4953

5054
// workspace related methods
5155
GetDevfileWorkspace() *v1.DevWorkspaceTemplateSpecContent

pkg/devfile/parser/data/v2/commands.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,10 @@ func (d *DevfileV2) GetCommands(options common.DevfileOptions) ([]v1.Command, er
3131

3232
// AddCommands adds the slice of Command objects to the Devfile's commands
3333
// if a command is already defined, error out
34-
func (d *DevfileV2) AddCommands(commands ...v1.Command) error {
35-
devfileCommands, err := d.GetCommands(common.DevfileOptions{})
36-
if err != nil {
37-
return err
38-
}
34+
func (d *DevfileV2) AddCommands(commands []v1.Command) error {
3935

4036
for _, command := range commands {
41-
for _, devfileCommand := range devfileCommands {
37+
for _, devfileCommand := range d.Commands {
4238
if command.Id == devfileCommand.Id {
4339
return &common.FieldAlreadyExistError{Name: command.Id, Field: "command"}
4440
}
@@ -57,3 +53,19 @@ func (d *DevfileV2) UpdateCommand(command v1.Command) {
5753
}
5854
}
5955
}
56+
57+
// DeleteCommand removes the specified command
58+
func (d *DevfileV2) DeleteCommand(id string) error {
59+
60+
for i := range d.Commands {
61+
if d.Commands[i].Id == id {
62+
d.Commands = append(d.Commands[:i], d.Commands[i+1:]...)
63+
return nil
64+
}
65+
}
66+
67+
return &common.FieldNotFoundError{
68+
Field: "command",
69+
Name: id,
70+
}
71+
}

pkg/devfile/parser/data/v2/commands_test.go

+82-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
88
"github.com/devfile/api/v2/pkg/attributes"
99
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
10+
"github.com/stretchr/testify/assert"
1011
)
1112

1213
func TestDevfile200_GetCommands(t *testing.T) {
@@ -216,7 +217,7 @@ func TestDevfile200_AddCommands(t *testing.T) {
216217
},
217218
}
218219

219-
got := d.AddCommands(tt.newCommands...)
220+
got := d.AddCommands(tt.newCommands)
220221
if !tt.wantErr && got != nil {
221222
t.Errorf("TestDevfile200_AddCommands() unexpected error - %v", got)
222223
} else if tt.wantErr && got == nil {
@@ -300,3 +301,83 @@ func TestDevfile200_UpdateCommands(t *testing.T) {
300301
})
301302
}
302303
}
304+
305+
func TestDeleteCommands(t *testing.T) {
306+
307+
d := &DevfileV2{
308+
v1.Devfile{
309+
DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{
310+
DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{
311+
Commands: []v1.Command{
312+
{
313+
Id: "command1",
314+
CommandUnion: v1.CommandUnion{
315+
Exec: &v1.ExecCommand{},
316+
},
317+
},
318+
{
319+
Id: "command2",
320+
CommandUnion: v1.CommandUnion{
321+
Exec: &v1.ExecCommand{},
322+
},
323+
},
324+
{
325+
Id: "command3",
326+
CommandUnion: v1.CommandUnion{
327+
Composite: &v1.CompositeCommand{
328+
Commands: []string{"command1", "command2", "command1"},
329+
},
330+
},
331+
},
332+
},
333+
},
334+
},
335+
},
336+
}
337+
338+
tests := []struct {
339+
name string
340+
commandToDelete string
341+
wantCommands []v1.Command
342+
wantErr bool
343+
}{
344+
{
345+
name: "Successfully delete command",
346+
commandToDelete: "command1",
347+
wantCommands: []v1.Command{
348+
{
349+
Id: "command2",
350+
CommandUnion: v1.CommandUnion{
351+
Exec: &v1.ExecCommand{},
352+
},
353+
},
354+
{
355+
Id: "command3",
356+
CommandUnion: v1.CommandUnion{
357+
Composite: &v1.CompositeCommand{
358+
Commands: []string{"command1", "command2", "command1"},
359+
},
360+
},
361+
},
362+
},
363+
wantErr: false,
364+
},
365+
{
366+
name: "Missing Command",
367+
commandToDelete: "command34",
368+
wantErr: true,
369+
},
370+
}
371+
for _, tt := range tests {
372+
t.Run(tt.name, func(t *testing.T) {
373+
err := d.DeleteCommand(tt.commandToDelete)
374+
if (err != nil) != tt.wantErr {
375+
t.Errorf("DeleteCommand() error = %v, wantErr %v", err, tt.wantErr)
376+
return
377+
} else if err == nil {
378+
assert.Equal(t, tt.wantCommands, d.Commands, "The two values should be the same.")
379+
}
380+
})
381+
}
382+
383+
}

pkg/devfile/parser/data/v2/components.go

+21-9
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,13 @@ func (d *DevfileV2) GetDevfileVolumeComponents(options common.DevfileOptions) ([
6060
// if a component is already defined, error out
6161
func (d *DevfileV2) AddComponents(components []v1.Component) error {
6262

63-
componentMap := make(map[string]bool)
64-
65-
for _, component := range d.Components {
66-
componentMap[component.Name] = true
67-
}
6863
for _, component := range components {
69-
if _, ok := componentMap[component.Name]; !ok {
70-
d.Components = append(d.Components, component)
71-
} else {
72-
return &common.FieldAlreadyExistError{Name: component.Name, Field: "component"}
64+
for _, devfileComponent := range d.Components {
65+
if component.Name == devfileComponent.Name {
66+
return &common.FieldAlreadyExistError{Name: component.Name, Field: "component"}
67+
}
7368
}
69+
d.Components = append(d.Components, component)
7470
}
7571
return nil
7672
}
@@ -88,3 +84,19 @@ func (d *DevfileV2) UpdateComponent(component v1.Component) {
8884
d.Components[index] = component
8985
}
9086
}
87+
88+
// DeleteComponent removes the specified component
89+
func (d *DevfileV2) DeleteComponent(name string) error {
90+
91+
for i := range d.Components {
92+
if d.Components[i].Name == name {
93+
d.Components = append(d.Components[:i], d.Components[i+1:]...)
94+
return nil
95+
}
96+
}
97+
98+
return &common.FieldNotFoundError{
99+
Field: "component",
100+
Name: name,
101+
}
102+
}

pkg/devfile/parser/data/v2/components_test.go

+92
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/devfile/api/v2/pkg/attributes"
99
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
1010
"github.com/devfile/library/pkg/testingutil"
11+
"github.com/stretchr/testify/assert"
1112
)
1213

1314
func TestDevfile200_AddComponent(t *testing.T) {
@@ -403,3 +404,94 @@ func TestGetDevfileVolumeComponents(t *testing.T) {
403404
}
404405

405406
}
407+
408+
func TestDeleteComponents(t *testing.T) {
409+
410+
d := &DevfileV2{
411+
v1.Devfile{
412+
DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{
413+
DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{
414+
Components: []v1.Component{
415+
{
416+
Name: "comp2",
417+
ComponentUnion: v1.ComponentUnion{
418+
Container: &v1.ContainerComponent{
419+
Container: v1.Container{
420+
VolumeMounts: []v1.VolumeMount{
421+
testingutil.GetFakeVolumeMount("comp2", "/path"),
422+
testingutil.GetFakeVolumeMount("comp2", "/path2"),
423+
testingutil.GetFakeVolumeMount("comp3", "/path"),
424+
},
425+
},
426+
},
427+
},
428+
},
429+
{
430+
Name: "comp2",
431+
ComponentUnion: v1.ComponentUnion{
432+
Volume: &v1.VolumeComponent{},
433+
},
434+
},
435+
{
436+
Name: "comp3",
437+
ComponentUnion: v1.ComponentUnion{
438+
Volume: &v1.VolumeComponent{},
439+
},
440+
},
441+
},
442+
},
443+
},
444+
},
445+
}
446+
447+
tests := []struct {
448+
name string
449+
componentToDelete string
450+
wantComponents []v1.Component
451+
wantErr bool
452+
}{
453+
{
454+
name: "Successfully delete a Component",
455+
componentToDelete: "comp3",
456+
wantComponents: []v1.Component{
457+
{
458+
Name: "comp2",
459+
ComponentUnion: v1.ComponentUnion{
460+
Container: &v1.ContainerComponent{
461+
Container: v1.Container{
462+
VolumeMounts: []v1.VolumeMount{
463+
testingutil.GetFakeVolumeMount("comp2", "/path"),
464+
testingutil.GetFakeVolumeMount("comp2", "/path2"),
465+
testingutil.GetFakeVolumeMount("comp3", "/path"),
466+
},
467+
},
468+
},
469+
},
470+
},
471+
{
472+
Name: "comp2",
473+
ComponentUnion: v1.ComponentUnion{
474+
Volume: &v1.VolumeComponent{},
475+
},
476+
},
477+
},
478+
wantErr: false,
479+
},
480+
{
481+
name: "Missing Component",
482+
componentToDelete: "comp12",
483+
wantErr: true,
484+
},
485+
}
486+
for _, tt := range tests {
487+
t.Run(tt.name, func(t *testing.T) {
488+
err := d.DeleteComponent(tt.componentToDelete)
489+
if (err != nil) != tt.wantErr {
490+
t.Errorf("DeleteComponent() error = %v, wantErr %v", err, tt.wantErr)
491+
} else if err == nil {
492+
assert.Equal(t, tt.wantComponents, d.Components, "The two values should be the same.")
493+
}
494+
})
495+
}
496+
497+
}

pkg/devfile/parser/data/v2/events.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ func (d *DevfileV2) GetEvents() v1.Events {
1616
// AddEvents adds the Events Object to the devfile's events
1717
// if the event is already defined in the devfile, error out
1818
func (d *DevfileV2) AddEvents(events v1.Events) error {
19+
20+
if d.Events == nil {
21+
d.Events = &v1.Events{}
22+
}
23+
1924
if len(events.PreStop) > 0 {
2025
if len(d.Events.PreStop) > 0 {
2126
return &common.FieldAlreadyExistError{Field: "pre stop"}
@@ -50,16 +55,21 @@ func (d *DevfileV2) AddEvents(events v1.Events) error {
5055
// UpdateEvents updates the devfile's events
5156
// it only updates the events passed to it
5257
func (d *DevfileV2) UpdateEvents(postStart, postStop, preStart, preStop []string) {
53-
if len(postStart) != 0 {
58+
59+
if d.Events == nil {
60+
d.Events = &v1.Events{}
61+
}
62+
63+
if postStart != nil {
5464
d.Events.PostStart = postStart
5565
}
56-
if len(postStop) != 0 {
66+
if postStop != nil {
5767
d.Events.PostStop = postStop
5868
}
59-
if len(preStart) != 0 {
69+
if preStart != nil {
6070
d.Events.PreStart = preStart
6171
}
62-
if len(preStop) != 0 {
72+
if preStop != nil {
6373
d.Events.PreStop = preStop
6474
}
6575
}

0 commit comments

Comments
 (0)