package controllers import ( "encoding/json" "fmt" "oc-datacenter/infrastructure" "slices" beego "github.com/beego/beego/v2/server/web" ) type KubeInfo struct { Url *string KubeCA *string KubeCert *string KubeKey *string } type Kubeconfig struct { Data *string } // Operations about the admiralty objects of the datacenter type AdmiraltyController struct { beego.Controller } // @Title GetAllTargets // @Description find all Admiralty Target // @Success 200 // @router /targets [get] func (c *AdmiraltyController) GetAllTargets() { serv, err := infrastructure.NewService() if err != nil { // change code to 500 c.Ctx.Output.SetStatus(500) c.ServeJSON() c.Data["json"] = map[string]string{"error": err.Error()} return } res, err := serv.GetTargets(c.Ctx.Request.Context()) c.Data["json"] = res c.ServeJSON() } // @Title GetOneTarget // @Description find one Admiralty Target // @Param id path string true "the name of the target to get" // @Success 200 // @router /targets/:id [get] func (c *AdmiraltyController) GetOneTarget() { id := c.Ctx.Input.Param(":id") serv, err := infrastructure.NewService() if err != nil { // change code to 500 c.Ctx.Output.SetStatus(500) c.ServeJSON() c.Data["json"] = map[string]string{"error": err.Error()} return } res, err := serv.GetTargets(c.Ctx.Request.Context()) id = "target-"+id found := slices.Contains(res,id) if !found { c.Ctx.Output.SetStatus(404) c.ServeJSON() } c.Data["json"] = id c.ServeJSON() } // @Title CreateSource // @Description Create an Admiralty Source on remote cluster // @Param dc_id path string true "which dc to contact" // @Param execution path string true "execution id of the workflow" // @Param serviceAccount body controllers.KubeInfo true "url and serviceAccount to use with the source formatted as json object" // @Success 200 // @router /sources/:dc_id/:execution [post] func (c *AdmiraltyController) CreateSource() { var data KubeInfo json.Unmarshal(c.Ctx.Input.CopyBody(10000000),&data) if data.Url == nil || data.KubeCA == nil || data.KubeCert == nil|| data.KubeKey == nil { c.Ctx.Output.SetStatus(500) c.ServeJSON() missingData := fmt.Sprint(data) c.Data["json"] = map[string]string{"error" : "Missing something in " + missingData} c.ServeJSON() } fmt.Println("") fmt.Println("URL : %v", data.Url) fmt.Println("") fmt.Println("CA : %v", data.KubeCA) fmt.Println("") fmt.Println("Key : ", data.KubeKey) dc_id := c.Ctx.Input.Param(":dc_id") execution := c.Ctx.Input.Param(":execution") _ = dc_id serv, err := infrastructure.NewRemoteKubernetesService( *data.Url, *data.KubeCA, *data.KubeCert, *data.KubeKey, ) if err != nil { // change code to 500 c.Ctx.Output.SetStatus(500) c.ServeJSON() c.Data["json"] = map[string]string{"error": err.Error()} c.ServeJSON() return } res, err := serv.CreateAdmiraltySource(execution) if err != nil { // change code to 500 c.Ctx.Output.SetStatus(500) c.Data["json"] = map[string]string{"error": err.Error()} c.ServeJSON() return } // TODO : Return a description of the created resource var respData map[string]interface{} err = json.Unmarshal(res,&respData) c.Data["json"] = respData c.ServeJSON() } // @Title CreateAdmiraltyTarget // @Description Create an Admiralty Target in the namespace associated to the executionID // @Param dc_id path string true "which dc to contact" // @Param execution path string true "execution id of the workflow" // @Success 201 // @router /target/:dc_id/:execution [post] func (c *AdmiraltyController) CreateAdmiraltyTarget(){ var data map[string]interface{} dc_id := c.Ctx.Input.Param(":dc_id") execution := c.Ctx.Input.Param(":execution") _ = dc_id serv, err := infrastructure.NewService() if err != nil { // change code to 500 c.Ctx.Output.SetStatus(500) c.Data["json"] = map[string]string{"error": err.Error()} c.ServeJSON() return } resp, err := serv.CreateAdmiraltyTarget(execution) if err != nil { // change code to 500 c.Ctx.Output.SetStatus(500) c.Data["json"] = map[string]string{"error": err.Error()} c.ServeJSON() return } if resp == nil { fmt.Println("Error while trying to create Admiralty target") fmt.Println(resp) fmt.Println(err) c.Ctx.Output.SetStatus(401) c.Data["json"] = map[string]string{"error" : "Could not perform the action" } c.ServeJSON() return } err = json.Unmarshal(resp,&data) if err != nil { // change code to 500 c.Ctx.Output.SetStatus(500) c.ServeJSON() c.Data["json"] = map[string]string{"error": err.Error()} return } c.Data["json"] = data c.ServeJSON() } // @Title GetKubeSecret // @Description Retrieve the secret created from a Kubeconfig that will be associated to an Admiralty Target // @Param dc_id path string true "which dc to contact" // @Param execution path string true "execution id of the workflow" // @Success 200 // @router /secret/:dc_id/:execution [get] func(c *AdmiraltyController) GetKubeSecret() { var data map[string]interface{} dc_id := c.Ctx.Input.Param(":dc_id") execution := c.Ctx.Input.Param(":execution") _ = dc_id serv, err := infrastructure.NewService() if err != nil { // change code to 500 c.Ctx.Output.SetStatus(500) c.Data["json"] = map[string]string{"error": err.Error()} c.ServeJSON() return } resp, err := serv.GetKubeconfigSecret(execution) if err != nil { // change code to 500 c.Ctx.Output.SetStatus(500) c.Data["json"] = map[string]string{"error": err.Error()} c.ServeJSON() return } if resp == nil { c.Ctx.Output.SetStatus(404) c.ServeJSON() return } err = json.Unmarshal(resp,&data) if err != nil { // change code to 500 c.Ctx.Output.SetStatus(500) c.ServeJSON() c.Data["json"] = map[string]string{"error": err.Error()} return } c.Data["json"] = data c.ServeJSON() } // @Title CreateKubeSecret // @Description Creat a secret from a Kubeconfig that will be associated to an Admiralty Target // @Param dc_id path string true "which dc to contact" // @Param execution path string true "execution id of the workflow" // @Param kubeconfig body controllers.Kubeconfig true "Kubeconfig to use when creating secret" // @Success 200 // @router /secret/:dc_id/:execution [post] func (c *AdmiraltyController) CreateKubeSecret() { var kubeconfig Kubeconfig var respData map[string]interface{} data := c.Ctx.Input.CopyBody(100000) err := json.Unmarshal(data, &kubeconfig) if err != nil { fmt.Println("Error when retrieving the data for kubeconfig from request") fmt.Println(err) c.Ctx.Output.SetStatus(500) c.Data["json"] = map[string]string{"error": err.Error()} c.ServeJSON() return } dc_id := c.Ctx.Input.Param(":dc_id") execution := c.Ctx.Input.Param(":execution") _ = dc_id serv, err := infrastructure.NewService() if err != nil { // change code to 500 c.Ctx.Output.SetStatus(500) c.Data["json"] = map[string]string{"error": err.Error()} c.ServeJSON() return } resp, err := serv.CreateKubeconfigSecret(*kubeconfig.Data,execution) if err != nil { // change code to 500 c.Ctx.Output.SetStatus(500) c.Data["json"] = map[string]string{"error": err.Error()} c.ServeJSON() return } err = json.Unmarshal(resp,&respData) c.Data["json"] = respData c.ServeJSON() }