Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 193: Implement Tasks to let the user verify his setup #180

Merged
merged 16 commits into from
May 13, 2024
Merged
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"context"
"flag"
"os"

"github.com/hobbyfarm/gargantua/v3/pkg/accesscode"
"github.com/hobbyfarm/gargantua/v3/pkg/authserver"
hfClientset "github.com/hobbyfarm/gargantua/v3/pkg/client/clientset/versioned"
Expand Down Expand Up @@ -34,7 +36,6 @@ import (
"github.com/hobbyfarm/gargantua/v3/protos/authn"
"github.com/hobbyfarm/gargantua/v3/protos/authr"
"github.com/hobbyfarm/gargantua/v3/protos/setting"
"os"

"github.com/ebauman/crder"
"golang.org/x/sync/errgroup"
Expand Down
14 changes: 14 additions & 0 deletions v3/pkg/apis/hobbyfarm.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,27 @@ type ScenarioSpec struct {
KeepAliveDuration string `json:"keepalive_duration"`
PauseDuration string `json:"pause_duration"`
Pauseable bool `json:"pauseable"`
Tasks []VirtualMachineTasks `json:"vm_tasks"`
}

type ScenarioStep struct {
Title string `json:"title"`
Content string `json:"content"`
}

type VirtualMachineTasks struct {
VMName string `json:"vm_name"`
Tasks []Task `json:"tasks"`
}
type Task struct {
Name string `json:"name"`
Description string `json:"description"`
Command string `json:"command"`
ExpectedOutputValue string `json:"expected_output_value"`
ExpectedReturnCode int `json:"expected_return_code"`
ReturnType string `json:"return_type"`
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

Expand Down
44 changes: 44 additions & 0 deletions v3/pkg/apis/hobbyfarm.io/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion v3/pkg/crd/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package crd

import (
"github.com/ebauman/crder"
"github.com/hobbyfarm/gargantua/v3/pkg/apis/hobbyfarm.io/v1"
v1 "github.com/hobbyfarm/gargantua/v3/pkg/apis/hobbyfarm.io/v1"
terraformv1 "github.com/hobbyfarm/gargantua/v3/pkg/apis/terraformcontroller.cattle.io/v1"
)

Expand Down
85 changes: 73 additions & 12 deletions v3/pkg/scenarioserver/scenarioserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"slices"
"strconv"
"strings"

"github.com/hobbyfarm/gargantua/v3/pkg/accesscode"
hfv1 "github.com/hobbyfarm/gargantua/v3/pkg/apis/hobbyfarm.io/v1"
hfClientset "github.com/hobbyfarm/gargantua/v3/pkg/client/clientset/versioned"
hfInformers "github.com/hobbyfarm/gargantua/v3/pkg/client/informers/externalversions"
"github.com/hobbyfarm/gargantua/v3/pkg/courseclient"
rbac2 "github.com/hobbyfarm/gargantua/v3/pkg/rbac"
"github.com/hobbyfarm/gargantua/v3/pkg/util"
"net/http"
"slices"
"strconv"
"strings"

"github.com/hobbyfarm/gargantua/v3/protos/authn"
"github.com/hobbyfarm/gargantua/v3/protos/authr"
Expand Down Expand Up @@ -50,13 +51,14 @@ type PreparedScenarioStep struct {
}

type PreparedScenario struct {
Id string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
StepCount int `json:"stepcount"`
VirtualMachines []map[string]string `json:"virtualmachines"`
Pauseable bool `json:"pauseable"`
Printable bool `json:"printable"`
Id string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
StepCount int `json:"stepcount"`
VirtualMachines []map[string]string `json:"virtualmachines"`
Pauseable bool `json:"pauseable"`
Printable bool `json:"printable"`
Tasks []hfv1.VirtualMachineTasks `json:"vm_tasks"`
}

type AdminPreparedScenario struct {
Expand Down Expand Up @@ -111,7 +113,7 @@ func (s ScenarioServer) prepareScenario(scenario hfv1.Scenario, printable bool)
ps.Pauseable = scenario.Spec.Pauseable
ps.Printable = printable
ps.StepCount = len(scenario.Spec.Steps)

ps.Tasks = scenario.Spec.Tasks
return ps, nil
}

Expand Down Expand Up @@ -723,6 +725,31 @@ func (s ScenarioServer) CopyFunc(w http.ResponseWriter, r *http.Request) {
return
}

func VerifyTaskContent(vm_tasks []hfv1.VirtualMachineTasks) error {
//Verify that name, description, command must not empty
for _, vm_task := range vm_tasks {
if vm_task.VMName == "" {
glog.Errorf("error while vm_name empty")
return fmt.Errorf("bad")
}
for _, task := range vm_task.Tasks {
if task.Name == "" {
glog.Errorf("error while Name of task empty")
return fmt.Errorf("bad")
}
if task.Description == "" {
glog.Errorf("error while Description of task empty")
return fmt.Errorf("bad")
}
if task.Command == "" || task.Command == "[]" {
glog.Errorf("error while Command of task empty")
return fmt.Errorf("bad")
}
}
}
return nil
}

func (s ScenarioServer) CreateFunc(w http.ResponseWriter, r *http.Request) {
user, err := rbac2.AuthenticateRequest(r, s.authnClient)
if err != nil {
Expand Down Expand Up @@ -813,6 +840,22 @@ func (s ScenarioServer) CreateFunc(w http.ResponseWriter, r *http.Request) {
scenario.Spec.Categories = categories
scenario.Spec.Tags = tags
scenario.Spec.KeepAliveDuration = keepaliveDuration
rawVMTasks := r.PostFormValue("vm_tasks")
if rawVMTasks != "" {
vm_tasks := []hfv1.VirtualMachineTasks{}

err = json.Unmarshal([]byte(rawVMTasks), &vm_tasks)
if err != nil {
glog.Errorf("error while unmarshaling tasks %v", err)
return
}
err = VerifyTaskContent(vm_tasks)
if err != nil {
glog.Errorf("error tasks content %v", err)
return
}
scenario.Spec.Tasks = vm_tasks
}

scenario.Spec.Pauseable = false
if pauseable != "" {
Expand Down Expand Up @@ -875,6 +918,7 @@ func (s ScenarioServer) UpdateFunc(w http.ResponseWriter, r *http.Request) {
rawVirtualMachines := r.PostFormValue("virtualmachines")
rawCategories := r.PostFormValue("categories")
rawTags := r.PostFormValue("tags")
rawVMTasks := r.PostFormValue("vm_tasks")

if name != "" {
scenario.Spec.Name = name
Expand Down Expand Up @@ -956,6 +1000,23 @@ func (s ScenarioServer) UpdateFunc(w http.ResponseWriter, r *http.Request) {
scenario.Spec.Tags = tagsSlice
}

if rawVMTasks != "" {
vm_tasks := []hfv1.VirtualMachineTasks{}

err = json.Unmarshal([]byte(rawVMTasks), &vm_tasks)
if err != nil {
glog.Errorf("error while unmarshaling tasks %v", err)
return fmt.Errorf("bad")
}

err = VerifyTaskContent(vm_tasks)
if err != nil {
glog.Errorf("error tasks content %v", err)
return err
}
scenario.Spec.Tasks = vm_tasks
}

_, updateErr := s.hfClientSet.HobbyfarmV1().Scenarios(util.GetReleaseNamespace()).Update(s.ctx, scenario, metav1.UpdateOptions{})
return updateErr
})
Expand Down
Loading
Loading