Exemple #1
0
func (c *EditController) Post() {
	guimessage := guimessagedisplay.GetGUIMessage(c)

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	name := c.GetString("name")

	namespace := Namespace{name, false, false, "", "", "", ""}

	url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort + "/api/v1/namespaces"

	tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

	_, err := restclient.RequestPostWithStructure(url, namespace, nil, tokenHeaderMap)

	if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
		return
	}

	if err != nil {
		// Error
		guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
	} else {
		guimessage.AddSuccess("Namespace " + name + " is edited")
	}

	c.Ctx.Redirect(302, "/gui/system/namespace/list")

	guimessage.RedirectMessage(c)
}
Exemple #2
0
func (c *EditController) Post() {
	guimessage := guimessagedisplay.GetGUIMessage(c)

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	namespace, _ := c.GetSession("namespace").(string)

	selectorName := c.GetString("name")
	replicaAmount, _ := c.GetInt("replicaAmount")
	image := c.GetString("image")
	containerPort, err := c.GetInt("containerPort")

	version := ""
	name := selectorName + version

	portName := "generated"

	replicationControllerContainerPortSlice := make([]ReplicationControllerContainerPort, 0)
	replicationControllerContainerPortSlice = append(replicationControllerContainerPortSlice, ReplicationControllerContainerPort{portName, containerPort})
	replicationControllerContainerSlice := make([]ReplicationControllerContainer, 0)
	replicationControllerContainerSlice = append(replicationControllerContainerSlice, ReplicationControllerContainer{name, image, replicationControllerContainerPortSlice})
	replicationController := ReplicationController{
		name,
		replicaAmount,
		ReplicationControllerSelector{
			selectorName,
			version,
		},
		ReplicationControllerLabel{
			name,
		},
		replicationControllerContainerSlice}

	url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
		"/api/v1/replicationcontrollers/" + namespace

	tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

	_, err = restclient.RequestPostWithStructure(url, replicationController, nil, tokenHeaderMap)

	if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
		return
	}

	if err != nil {
		// Error
		guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
	} else {
		guimessage.AddSuccess("Replication Controller " + name + " is edited")
	}

	c.Ctx.Redirect(302, "/gui/inventory/replicationcontroller/list")

	guimessage.RedirectMessage(c)
}
Exemple #3
0
func (c *CreateController) Post() {
	guimessage := guimessagedisplay.GetGUIMessage(c)

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	name := c.GetString("name")
	account := c.GetString("account")
	password := c.GetString("password")
	host := c.GetString("host")
	port, _ := c.GetInt("port")

	url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
		"/api/v1/notifiers/emailserversmtp/"

	emailServerSMTP := EmailServerSMTP{
		name,
		account,
		password,
		host,
		port,
		"",
	}

	tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

	_, err := restclient.RequestPostWithStructure(url, emailServerSMTP, nil, tokenHeaderMap)

	if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
		return
	}

	if err != nil {
		// Error
		guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
	} else {
		guimessage.AddSuccess("Email server configuration " + name + " is created")
	}

	c.Ctx.Redirect(302, "/gui/system/notification/emailserver/list")

	guimessage.RedirectMessage(c)
}
Exemple #4
0
func (c *CreateController) Post() {
	guimessage := guimessagedisplay.GetGUIMessage(c)

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	name := c.GetString("name")
	urlPath := c.GetString("url")
	apiKey := c.GetString("apiKey")
	apiSecret := c.GetString("apiSecret")

	url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
		"/api/v1/notifiers/smsnexmo/"

	smsNexmo := SMSNexmo{
		name,
		urlPath,
		apiKey,
		apiSecret,
		"",
	}

	tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

	_, err := restclient.RequestPostWithStructure(url, smsNexmo, nil, tokenHeaderMap)

	if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
		return
	}

	if err != nil {
		// Error
		guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
	} else {
		guimessage.AddSuccess("SMS configuration " + name + " is created")
	}

	c.Ctx.Redirect(302, "/gui/system/notification/sms/list")

	guimessage.RedirectMessage(c)
}
Exemple #5
0
// @Title create
// @Description create service
// @Param body body guirestapi.inventory.service.Service true "body for service"
// @Success 200 {string} {}
// @Failure 404 error reason
// @router / [post]
func (c *EditController) Post() {
	inputBody := c.Ctx.Input.CopyBody(limit.InputPostBodyMaximum)
	service := Service{}
	err := json.Unmarshal(inputBody, &service)
	if err != nil {
		// Error
		c.Data["json"] = make(map[string]interface{})
		c.Data["json"].(map[string]interface{})["error"] = err.Error()
		c.Ctx.Output.Status = 404
		c.ServeJSON()
		return
	}

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	namespace, _ := c.GetSession("namespace").(string)

	url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
		"/api/v1/services/" + namespace

	tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

	_, err = restclient.RequestPostWithStructure(url, service, nil, tokenHeaderMap)

	if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
		return
	}

	if err != nil {
		// Error
		c.Data["json"] = make(map[string]interface{})
		c.Data["json"].(map[string]interface{})["error"] = err.Error()
		c.Ctx.Output.Status = 404
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = make(map[string]interface{})
		c.ServeJSON()
	}
}
Exemple #6
0
func (c *LoginController) Post() {
	inputBody := c.Ctx.Input.CopyBody(limit.InputPostBodyMaximum)
	userData := UserData{}
	err := json.Unmarshal(inputBody, &userData)
	if err != nil {
		// Error
		errorJsonMap := make(map[string]interface{})
		errorJsonMap["ErrorMessage"] = err.Error()
		c.Data["json"] = errorJsonMap
		c.Ctx.Output.Status = 401
		c.ServeJSON()
		return
	}

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	// User
	url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
		"/api/v1/authorizations/tokens/"

	tokenData := TokenData{}
	errorInterface, err := restclient.RequestPostWithStructure(url, userData, &tokenData, nil)
	errorJsonMap, _ := errorInterface.(map[string]interface{})

	if err != nil {
		// Error
		c.Data["json"] = errorJsonMap
		c.Ctx.Output.Status = 401
		c.ServeJSON()
		return
	}

	jsonMap := make(map[string]interface{})
	jsonMap["Token"] = tokenData.Token
	c.Data["json"] = jsonMap

	c.ServeJSON()
}
Exemple #7
0
func (c *PushController) Post() {
	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	user := c.GetString("user")
	imageInformation := c.GetString("imageInformation")
	signature := c.Ctx.Input.Header("X-Hub-Signature")
	payload := string(c.Ctx.Input.CopyBody(limit.InputPostBodyMaximum))

	url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
		"/api/v1/webhooks/github"

	githubPost := GithubPost{
		user,
		imageInformation,
		signature,
		payload,
	}

	errorJsonMap := make(map[string]interface{})

	tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

	_, err := restclient.RequestPostWithStructure(url, githubPost, &errorJsonMap, tokenHeaderMap)

	if err != nil {
		// Error
		errorJsonMap := make(map[string]interface{})
		errorJsonMap["error"] = err.Error()
		c.Data["json"] = errorJsonMap
		c.Ctx.Output.Status = 400
		c.ServeJSON()
		return
	}

	jsonMap := make(map[string]interface{})
	c.Data["json"] = jsonMap
	c.ServeJSON()
}
Exemple #8
0
func (c *EditController) Post() {
	guimessage := guimessagedisplay.GetGUIMessage(c)

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	createOrUpdate := c.GetString("createOrUpdate")
	ip := c.GetString("ip")
	sshPort, _ := c.GetInt("sshPort")
	disabledText := c.GetString("disabled")
	sshUser := c.GetString("sshUser")
	sshPassword := c.GetString("sshPassword")

	disabled := false
	if disabledText == "on" {
		disabled = true
	}

	credential := Credential{
		ip,
		SSH{
			sshPort,
			sshUser,
			sshPassword,
		},
		disabled,
		"",
		"",
	}

	if createOrUpdate == "create" {
		url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
			"/api/v1/hosts/credentials/"

		tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

		_, err := restclient.RequestPostWithStructure(url, credential, nil, tokenHeaderMap)

		if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
			return
		}

		if err != nil {
			// Error
			guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
		} else {
			guimessage.AddSuccess("Host credential " + ip + " is created")
		}
	} else {
		url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
			"/api/v1/hosts/credentials/" + ip

		tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

		_, err := restclient.RequestPutWithStructure(url, credential, nil, tokenHeaderMap)

		if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
			return
		}

		if err != nil {
			// Error
			guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
		} else {
			guimessage.AddSuccess("Host credential " + ip + " is updated")
		}
	}

	c.Ctx.Redirect(302, "/gui/system/host/credential/list")

	guimessage.RedirectMessage(c)
}
Exemple #9
0
func (c *CloneController) Post() {
	guimessage := guimessagedisplay.GetGUIMessage(c)

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	// Get topology
	name := c.GetString("name")
	url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
		"/api/v1/topology/" + name

	topology := Topology{}

	tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

	_, err := restclient.RequestGetWithStructure(url, &topology, tokenHeaderMap)

	if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
		return
	}

	if err != nil {
		// Error
		guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
		guimessage.RedirectMessage(c)
		c.Ctx.Redirect(302, "/gui/repository/topologytemplate/list")
		return
	}

	// Generate topology order
	inputMap := c.Input()
	launchNameSlice := make([]string, 0)
	environmentMap := make(map[string]string)
	if inputMap != nil {
		for key, _ := range inputMap {
			// Collect the clone name
			if strings.HasPrefix(key, "launchUse") {
				launchName := key[len("launchUse"):]
				if c.GetString(key) == "on" && len(launchName) > 0 {
					launchNameSlice = append(launchNameSlice, launchName)
				}
				// Collect environment
			} else if strings.HasPrefix(key, "clusterEnvironment") {
				environmentMap[key] = c.GetString(key)
			} else if strings.HasPrefix(key, "applicationEnvironment") {
				environmentMap[key] = c.GetString(key)
			}
		}
	}

	launchSlice := make([]Launch, 0)
	for _, launchName := range launchNameSlice {
		launchOrder, _ := c.GetInt("launchOrder" + launchName)

		clusterName := c.GetString("clusterName" + launchName)
		clusterSize, _ := c.GetInt("clusterSize" + launchName)

		applicationImageInformationName := c.GetString("applicationImageInformationName" + launchName)
		applicationVersion := c.GetString("applicationVersion" + launchName)
		applicationDescription := c.GetString("applicationDescription" + launchName)
		applicationReplicaAmount, _ := c.GetInt("applicationReplicaAmount" + launchName)

		clusterEnvironmentMap := make(map[string]string)
		applicationEnvironmentMap := make(map[string]string)
		for key, value := range environmentMap {
			if strings.HasPrefix(key, "clusterEnvironment"+launchName) {
				environemntKey := key[len("clusterEnvironment"+launchName):]
				clusterEnvironmentMap[environemntKey] = value
			} else if strings.HasPrefix(key, "applicationEnvironment"+launchName) {
				environemntKey := key[len("applicationEnvironment"+launchName):]
				applicationEnvironmentMap[environemntKey] = value
			}
		}

		// Location Affinity
		region := c.GetString("launchRegion" + launchName)
		zone := c.GetString("launchZone" + launchName)

		if region == "Any" {
			region = ""
		}
		if zone == "Any" {
			zone = ""
		}

		extraJsonMap := make(map[string]interface{})
		if len(region) > 0 {
			extraJsonMap["spec"] = make(map[string]interface{})
			extraJsonMap["spec"].(map[string]interface{})["template"] = make(map[string]interface{})
			extraJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"] = make(map[string]interface{})
			extraJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["nodeSelector"] = make(map[string]interface{})

			extraJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["nodeSelector"].(map[string]interface{})["region"] = region
			if len(zone) > 0 {
				extraJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["nodeSelector"].(map[string]interface{})["zone"] = zone
			}
		} else {
			extraJsonMap = nil
		}

		// Cluster or application
		if len(clusterName) > 0 {
			environmentSlice := make([]interface{}, 0)
			for key, value := range clusterEnvironmentMap {
				environmentJsonMap := make(map[string]interface{})
				environmentJsonMap["name"] = key
				environmentJsonMap["value"] = value
				environmentSlice = append(environmentSlice, environmentJsonMap)
			}

			clusterLaunch := &LaunchClusterApplication{
				clusterName,
				clusterSize,
				environmentSlice,
				extraJsonMap,
			}

			launch := Launch{
				launchOrder,
				nil,
				clusterLaunch,
				"",
				"",
				nil,
				"",
				"",
			}

			launchSlice = append(launchSlice, launch)
		} else if len(applicationImageInformationName) > 0 {
			environmentSlice := make([]ReplicationControllerContainerEnvironment, 0)
			for key, value := range applicationEnvironmentMap {
				environmentSlice = append(environmentSlice, ReplicationControllerContainerEnvironment{key, value})
			}

			oldLaunch := Launch{}
			for i := 0; i < len(topology.LaunchSlice); i++ {
				if topology.LaunchSlice[i].LaunchApplication != nil && launchName == topology.LaunchSlice[i].LaunchApplication.ImageInformationName {
					oldLaunch = topology.LaunchSlice[i]
				}
			}

			// Change the assigned Node Port to auto generated
			for i, port := range oldLaunch.LaunchApplication.PortSlice {
				if port.NodePort > 0 {
					oldLaunch.LaunchApplication.PortSlice[i].NodePort = 0
				}
			}

			launchApplication := &LaunchApplication{
				applicationImageInformationName,
				applicationVersion,
				applicationDescription,
				applicationReplicaAmount,
				oldLaunch.LaunchApplication.PortSlice,
				environmentSlice,
				oldLaunch.LaunchApplication.ResourceMap,
				extraJsonMap,
			}

			launch := Launch{
				launchOrder,
				launchApplication,
				nil,
				"",
				"",
				nil,
				"",
				"",
			}

			launchSlice = append(launchSlice, launch)
		}
	}

	sort.Sort(ByLaunch(launchSlice))

	// Clone
	namespace, _ := c.GetSession("namespace").(string)

	for _, launch := range launchSlice {
		if launch.LaunchApplication != nil {
			url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
				"/api/v1/deploys/create/" + namespace

			_, err = restclient.RequestPostWithStructure(url, launch.LaunchApplication, nil, tokenHeaderMap)

			if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
				return
			}

			if err != nil {
				// Error
				guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
				guimessage.RedirectMessage(c)
				c.Ctx.Redirect(302, "/gui/repository/topologytemplate/list")
				return
			}
		}
		if launch.LaunchClusterApplication != nil {
			url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
				"/api/v1/clusterapplications/launch/" + namespace + "/" + launch.LaunchClusterApplication.Name

			jsonMap := make(map[string]interface{})

			_, err = restclient.RequestPostWithStructure(url, launch.LaunchClusterApplication, &jsonMap, tokenHeaderMap)

			if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
				return
			}

			if err != nil {
				guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
				guimessage.RedirectMessage(c)
				c.Ctx.Redirect(302, "/gui/repository/topologytemplate/list")
				return
			}
		}
	}

	guimessage.AddSuccess("Clone from the template " + name + " to the namespace " + namespace)

	c.Ctx.Redirect(302, "/gui/repository/topologytemplate/list")

	guimessage.RedirectMessage(c)
}
Exemple #10
0
func (c *EditController) Post() {
	guimessage := guimessagedisplay.GetGUIMessage(c)

	name := c.GetString("name")
	password := c.GetString("password")
	disabledText := c.GetString("disabled")
	expiredTimeText := c.GetString("expiredTime")
	description := c.GetString("description")
	githubWebhookSecret := c.GetString("githubWebhookSecret")
	action := c.GetString("action")

	loginNamespace := c.GetString("loginNamespace")

	disabled := false
	if disabledText == "on" {
		disabled = true
	}

	var expiredTime *time.Time = nil
	expiredTimeData, err := time.Parse(guiWidgetTimePickerFormat, expiredTimeText)
	if err == nil {
		expiredTime = &expiredTimeData
	}

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	roleSlice := make([]*rbac.Role, 0)
	resourceSlice := make([]*rbac.Resource, 0)

	namespaceNameSlice := make([]string, 0)
	hasNamespaceNameAll := false

	inputMap := c.Input()
	if inputMap != nil {
		for key, value := range inputMap {
			if strings.HasPrefix(key, "role_") {
				roleName := key[len("role_"):]
				if value[0] == "on" {
					roleSlice = append(roleSlice, &rbac.Role{roleName, nil, ""})
				}
			}
			if strings.HasPrefix(key, "namespace_") {
				namespaceName := key[len("namespace_"):]
				if value[0] == "on" {
					namespaceNameSlice = append(namespaceNameSlice, namespaceName)
					if namespaceName == "*" {
						hasNamespaceNameAll = true
					}
				}
			}
		}
	}

	if hasNamespaceNameAll {
		resourceSlice = append(resourceSlice, &rbac.Resource{"namespace_*", "*", "/namespaces/"})
	} else {
		for _, namespaceName := range namespaceNameSlice {
			resourceSlice = append(resourceSlice, &rbac.Resource{"namespace_" + namespaceName, "*", "/namespaces/" + namespaceName})
		}
	}

	metaDataMap := make(map[string]string)
	if len(loginNamespace) > 0 {
		metaDataMap["loginNamespace"] = loginNamespace
	}

	if len(githubWebhookSecret) > 0 {
		metaDataMap["githubWebhookSecret"] = githubWebhookSecret
	}

	user := rbac.User{
		name,
		password,
		roleSlice,
		resourceSlice,
		description,
		metaDataMap,
		expiredTime,
		disabled,
	}

	tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

	if action == "create" {
		url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
			"/api/v1/authorizations/users"

		_, err = restclient.RequestPostWithStructure(url, user, nil, tokenHeaderMap)

	} else {
		url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
			"/api/v1/authorizations/users/" + name

		_, err = restclient.RequestPutWithStructure(url, user, nil, tokenHeaderMap)
	}

	if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
		return
	}

	if err != nil {
		// Error
		guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
	} else {
		guimessage.AddSuccess("User " + name + " is edited")
	}

	c.Ctx.Redirect(302, "/gui/system/rbac/user/list")

	guimessage.RedirectMessage(c)
}
Exemple #11
0
func (c *EditController) Post() {
	guimessage := guimessagedisplay.GetGUIMessage(c)

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	createOrUpdate := c.GetString("createOrUpdate")
	name := c.GetString("name")
	description := c.GetString("description")

	nodeHostSlice := make([]string, 0)
	inputMap := c.Input()
	if inputMap != nil {
		for key, _ := range inputMap {
			// Only collect host
			if strings.HasPrefix(key, "nodeHost_") {
				nodeHostSlice = append(nodeHostSlice, key[len("nodeHost_"):])
			}
		}
	}

	endPointList := c.GetString("endPointList")
	endPointSlice := make([]string, 0)
	splitSlice := strings.Split(endPointList, ",")
	for _, split := range splitSlice {
		endPoint := strings.TrimSpace(split)
		if len(endPoint) > 0 {
			endPointSlice = append(endPointSlice, endPoint)
		}
	}

	slbDaemon := SLBDaemon{
		name,
		endPointSlice,
		nodeHostSlice,
		description,
		"",
		"",
		"",
	}

	if createOrUpdate == "create" {
		url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
			"/api/v1/slbs/daemons/"

		tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

		_, err := restclient.RequestPostWithStructure(url, slbDaemon, nil, tokenHeaderMap)

		if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
			return
		}

		if err != nil {
			// Error
			guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
		} else {
			guimessage.AddSuccess("SLB daemon " + name + " is created")
		}
	} else {
		url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
			"/api/v1/slbs/daemons/" + name

		tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

		_, err := restclient.RequestPutWithStructure(url, slbDaemon, nil, tokenHeaderMap)

		if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
			return
		}

		if err != nil {
			// Error
			guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
		} else {
			guimessage.AddSuccess("SLB daemon " + name + " is updated")
		}
	}

	c.Ctx.Redirect(302, "/gui/system/slb/daemon/list")

	guimessage.RedirectMessage(c)
}
Exemple #12
0
func (c *LaunchController) Post() {
	guimessage := guimessagedisplay.GetGUIMessage(c)

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	namespace, _ := c.GetSession("namespace").(string)
	name := c.GetString("name")
	size, _ := c.GetInt("size")

	region := c.GetString("region")
	zone := c.GetString("zone")

	if region == "Any" {
		region = ""
	}
	if zone == "Any" {
		zone = ""
	}

	keySlice := make([]string, 0)
	inputMap := c.Input()
	if inputMap != nil {
		for key, _ := range inputMap {
			// Ignore the non environment field
			if strings.HasPrefix(key, "environment_") {
				keySlice = append(keySlice, key)
			}
		}
	}

	environmentSlice := make([]interface{}, 0)
	for _, key := range keySlice {
		value := c.GetString(key)
		if len(value) > 0 {
			environmentMap := make(map[string]string)
			environmentMap["name"] = key[len("environment_"):]
			environmentMap["value"] = value
			environmentSlice = append(environmentSlice, environmentMap)
		}
	}

	extraJsonMap := make(map[string]interface{})
	if len(region) > 0 {
		extraJsonMap["spec"] = make(map[string]interface{})
		extraJsonMap["spec"].(map[string]interface{})["template"] = make(map[string]interface{})
		extraJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"] = make(map[string]interface{})
		extraJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["nodeSelector"] = make(map[string]interface{})

		extraJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["nodeSelector"].(map[string]interface{})["region"] = region
		if len(zone) > 0 {
			extraJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["nodeSelector"].(map[string]interface{})["zone"] = zone
		}
	} else {
		extraJsonMap = nil
	}

	clusterLaunch := ClusterLaunch{
		size,
		environmentSlice,
		extraJsonMap,
	}

	url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
		"/api/v1/clusterapplications/launch/" + namespace + "/" + name

	tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

	_, err := restclient.RequestPostWithStructure(url, clusterLaunch, nil, tokenHeaderMap)

	if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
		return
	}

	if err != nil {
		// Error
		guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
	} else {
		guimessage.AddSuccess("Cluster application " + name + " is launched")
	}

	// Redirect to list
	c.Ctx.Redirect(302, "/gui/deploy/deployclusterapplication/list")

	guimessage.RedirectMessage(c)
}
Exemple #13
0
func (c *CreateController) Post() {
	guimessage := guimessagedisplay.GetGUIMessage(c)

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	name := c.GetString("name")
	kind := c.GetString("kind")
	description := c.GetString("description")

	// Name need to be a DNS 952 label
	match, _ := regexp.MatchString("^[a-z]{1}[a-z0-9-]{1,23}$", name)
	if match == false {
		guimessage.AddDanger("The name need to be a DNS 952 label ^[a-z]{1}[a-z0-9-]{1,23}$")
		c.Ctx.Redirect(302, "/gui/repository/imageinformation/list")
		guimessage.RedirectMessage(c)
		return
	}

	// Generate random work space
	workingDirectory := "/tmp/tmp_" + random.UUID()

	buildParameter := make(map[string]string)
	buildParameter["workingDirectory"] = workingDirectory
	buildParameter["repositoryPath"] = c.GetString("repositoryPath")
	buildParameter["sourceCodeProject"] = c.GetString("sourceCodeProject")
	buildParameter["sourceCodeDirectory"] = c.GetString("sourceCodeDirectory")
	buildParameter["sourceCodeMakeScript"] = c.GetString("sourceCodeMakeScript")
	buildParameter["environmentFile"] = c.GetString("environmentFile")

	switch kind {
	case "git":
		buildParameter["sourceCodeURL"] = c.GetString("sourceCodeURL")
		buildParameter["versionFile"] = c.GetString("versionFile")
	case "scp":
		buildParameter["hostAndPort"] = c.GetString("hostAndPort")
		buildParameter["username"] = c.GetString("username")
		buildParameter["password"] = c.GetString("password")
		buildParameter["sourcePath"] = c.GetString("sourcePath")
		buildParameter["compressFileName"] = c.GetString("compressFileName")
		buildParameter["unpackageCommand"] = c.GetString("unpackageCommand")
		buildParameter["versionFile"] = c.GetString("versionFile")
	case "sftp":
		buildParameter["hostAndPort"] = c.GetString("hostAndPort")
		buildParameter["username"] = c.GetString("username")
		buildParameter["password"] = c.GetString("password")
		buildParameter["sourcePath"] = c.GetString("sourcePath")
		buildParameter["versionFile"] = c.GetString("versionFile")
	}

	imageInformation := ImageInformation{
		name,
		kind,
		description,
		"",
		buildParameter,
		"",
		"",
		"",
		"",
		"",
		"",
	}

	url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
		"/api/v1/imageinformations/create/"

	resultJsonMap := make(map[string]interface{})

	tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

	_, err := restclient.RequestPostWithStructure(url, imageInformation, &resultJsonMap, tokenHeaderMap)

	if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
		return
	}

	if err != nil {
		guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
	} else {
		guimessage.AddSuccess("The build " + name + " is launched asynchronizedly")
	}

	c.Ctx.Redirect(302, "/gui/repository/imageinformation/log?imageInformation="+imageInformation.Name)

	guimessage.RedirectMessage(c)
}
Exemple #14
0
func (c *CreateController) Post() {
	guimessage := guimessagedisplay.GetGUIMessage(c)

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	clusterName := c.GetString("clusterName")
	hostList := c.GetString("hostList")

	name := c.GetString("name")
	stripe, _ := c.GetInt("stripe")
	replica, _ := c.GetInt("replica")
	arbiter, _ := c.GetInt("arbiter")
	disperse, _ := c.GetInt("disperse")
	disperseData, _ := c.GetInt("disperseData")
	redundancy, _ := c.GetInt("redundancy")
	transport := c.GetString("transport")

	allHostSlice := strings.Split(hostList, ",")

	hostSlice := make([]string, 0)
	for _, host := range allHostSlice {
		hostSelected := c.GetString(host)
		if hostSelected == "on" {
			hostSlice = append(hostSlice, host)
		}
	}

	url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
		"/api/v1/glusterfs/clusters/" + clusterName + "/volumes/"

	glusterfsVolumeCreateParameter := GlusterfsVolumeCreateParameter{
		clusterName,
		name,
		stripe,
		replica,
		arbiter,
		disperse,
		disperseData,
		redundancy,
		transport,
		hostSlice,
	}

	tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

	_, err := restclient.RequestPostWithStructure(url, glusterfsVolumeCreateParameter, nil, tokenHeaderMap)

	if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
		return
	}

	if err != nil {
		// Error
		guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
	} else {
		guimessage.AddSuccess("Glusterfs volume " + name + " is created and started")
	}

	c.Ctx.Redirect(302, "/gui/filesystem/glusterfs/volume/list?clusterName="+clusterName)

	guimessage.RedirectMessage(c)
}
Exemple #15
0
// @Title launch
// @Description launch the cluster application template
// @Param body body guirestapi.repository.thirdparty.Cluster true "body for cluster application template"
// @Param name query string true "The name to use"
// @Param size query string true "The size to use"
// @Success 200 {string} {}
// @Failure 404 error reason
// @router /launch/ [post]
func (c *LaunchController) Post() {
	name := c.GetString("name")
	size, _ := c.GetInt("size")

	inputBody := c.Ctx.Input.CopyBody(limit.InputPostBodyMaximum)
	environmentSlice := make([]interface{}, 0)
	err := json.Unmarshal(inputBody, &environmentSlice)
	if err != nil {
		// Error
		c.Data["json"] = make(map[string]interface{})
		c.Data["json"].(map[string]interface{})["error"] = err.Error()
		c.Ctx.Output.Status = 404
		c.ServeJSON()
		return
	}

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	namespace, _ := c.GetSession("namespace").(string)

	clusterLaunch := ClusterLaunch{
		size,
		environmentSlice,
		nil,
	}

	url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
		"/api/v1/clusterapplications/launch/" + namespace + "/" + name
	jsonMap := make(map[string]interface{})

	tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

	_, err = restclient.RequestPostWithStructure(url, clusterLaunch, &jsonMap, tokenHeaderMap)

	if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
		return
	}

	if err != nil {
		// Error
		errorMessage, _ := jsonMap["Error"].(string)
		if strings.HasPrefix(errorMessage, "Replication controller already exists") {
			// Error
			c.Data["json"] = make(map[string]interface{})
			c.Data["json"].(map[string]interface{})["error"] = "Replication controller " + name + " already exists"
			c.Ctx.Output.Status = 404
			c.ServeJSON()
			return
		} else {
			// Error
			c.Data["json"] = make(map[string]interface{})
			c.Data["json"].(map[string]interface{})["error"] = err.Error()
			c.Ctx.Output.Status = 404
			c.ServeJSON()
			return
		}
	} else {
		c.Data["json"] = make(map[string]interface{})
		c.ServeJSON()
	}
}
Exemple #16
0
func (c *EditController) Post() {
	guimessage := guimessagedisplay.GetGUIMessage(c)

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	namespace, _ := c.GetSession("namespace").(string)

	name := c.GetString("name")
	selectorName := c.GetString("selectorName")
	//labelName := c.GetString("labelName")
	//portName := c.GetString("portName")
	protocol := c.GetString("protocol")
	port, _ := c.GetInt("port")
	targetPort := c.GetString("targetPort")
	useNodePort := c.GetString("useNodePort")
	autoGeneratedNodePort := c.GetString("autoGeneratedNodePort")
	nodePort, _ := c.GetInt("nodePort")
	sessionAffinity := c.GetString("sessionAffinity")

	// Name need to be a DNS 952 label
	match, _ := regexp.MatchString("^[a-z]{1}[a-z0-9-]{1,23}$", name)
	if match == false {
		guimessage.AddDanger("The name need to be a DNS 952 label ^[a-z]{1}[a-z0-9-]{1,23}$")
		c.Ctx.Redirect(302, "/gui/inventory/service/list")
		guimessage.RedirectMessage(c)
		return
	}

	labelName := selectorName
	portName := selectorName

	if useNodePort == "on" {
		if autoGeneratedNodePort == "on" {
			nodePort = 0
		}
	} else {
		nodePort = -1
	}

	portSlice := make([]ServicePort, 0)
	portSlice = append(portSlice, ServicePort{portName, protocol, port, targetPort, nodePort, "", "", ""})
	selectorMap := make(map[string]interface{})
	selectorMap["name"] = selectorName
	labelMap := make(map[string]interface{})
	labelMap["name"] = labelName

	service := Service{name, namespace, portSlice, selectorMap, "", labelMap, sessionAffinity, "", ""}

	url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
		"/api/v1/services/" + namespace

	tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

	_, err := restclient.RequestPostWithStructure(url, service, nil, tokenHeaderMap)

	if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
		return
	}

	if err != nil {
		// Error
		guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
	} else {
		guimessage.AddSuccess("Service " + name + " is edited")
	}

	c.Ctx.Redirect(302, "/gui/inventory/service/list")

	guimessage.RedirectMessage(c)
}
Exemple #17
0
func (c *CreateController) Post() {
	guimessage := guimessagedisplay.GetGUIMessage(c)

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	namespaces, _ := c.GetSession("namespace").(string)

	imageInformationName := c.GetString("imageInformationName")
	version := c.GetString("version")
	description := c.GetString("description")
	replicaAmount, _ := c.GetInt("replicaAmount")

	autoUpdateForNewBuildText := c.GetString("autoUpdateForNewBuild")

	autoUpdateForNewBuild := false
	if autoUpdateForNewBuildText == "on" {
		autoUpdateForNewBuild = true
	}

	region := c.GetString("region")
	zone := c.GetString("zone")

	if region == "Any" {
		region = ""
	}
	if zone == "Any" {
		zone = ""
	}

	resourceCPURequest, resourceCPURequestError := c.GetFloat("resourceCPURequest")
	resourceCPULimit, resourceCPULimitError := c.GetFloat("resourceCPULimit")
	resourceMemoryRequest, resourceMemoryRequestError := c.GetInt("resourceMemoryRequest")
	resourceMemoryLimit, resourceMemoryLimitError := c.GetInt("resourceMemoryLimit")

	// Limit must be bigger than request
	if resourceCPURequestError == nil && resourceCPULimitError == nil {
		if resourceCPURequest > resourceCPULimit {
			// Error
			guimessage.AddWarning("CPU request must be smaller or equal to CPU limit")
			guimessage.RedirectMessage(c)
			c.Ctx.Redirect(302, "/gui/deploy/deploy/list")
			return
		}
	}

	if resourceMemoryRequestError == nil && resourceMemoryLimitError == nil {
		if resourceMemoryRequest > resourceMemoryLimit {
			// Error
			guimessage.AddWarning("Memory request must be smaller or equal to Memory limit")
			guimessage.RedirectMessage(c)
			c.Ctx.Redirect(302, "/gui/deploy/deploy/list")
			return
		}
	}

	portName := "generated"

	indexContainerPortMap := make(map[string]int)

	keySlice := make([]string, 0)
	inputMap := c.Input()
	if inputMap != nil {
		for key, _ := range inputMap {
			// Only collect environment belonging to this version
			if strings.HasPrefix(key, version) {
				keySlice = append(keySlice, key)
			}

			// Collect dynamically generated containerPort
			if strings.HasPrefix(key, "containerPort") {
				containerPort, _ := c.GetInt(key)
				index := key[len("containerPort"):]
				indexContainerPortMap[index] = containerPort
			}
		}
	}

	environmentSlice := make([]ReplicationControllerContainerEnvironment, 0)
	length := len(version) + 1 // + 1 for _
	for _, key := range keySlice {
		value := c.GetString(key)
		if len(value) > 0 {
			environmentSlice = append(environmentSlice,
				ReplicationControllerContainerEnvironment{key[length:], value})
		}
	}

	deployContainerPortSlice := make([]DeployContainerPort, 0)
	i := 0
	for index, containerPort := range indexContainerPortMap {
		nodePort := -1
		if c.GetString("useNodePort"+index) == "on" {
			if c.GetString("autoGeneratedNodePort"+index) == "on" {
				nodePort = 0
			} else {
				// If fail to parse, nodePort will be set to 0 that means auto-generated
				nodePort, _ = c.GetInt("nodePort" + index)
			}
		}
		protocol := c.GetString("protocol" + index)
		deployContainerPortSlice = append(deployContainerPortSlice, DeployContainerPort{portName + strconv.Itoa(i), containerPort, nodePort, protocol})
		i++
	}

	// Resource reservation
	resourceMap := make(map[string]interface{})
	if resourceCPURequestError == nil || resourceMemoryRequestError == nil {
		resourceMap["requests"] = make(map[string]interface{})
		if resourceCPURequestError == nil {
			resourceMap["requests"].(map[string]interface{})["cpu"] = resourceCPURequest
		}
		if resourceMemoryRequestError == nil {
			resourceMap["requests"].(map[string]interface{})["memory"] = strconv.Itoa(resourceMemoryRequest) + "Mi"
		}
	}

	if resourceCPULimitError == nil || resourceMemoryLimitError == nil {
		resourceMap["limits"] = make(map[string]interface{})
		if resourceCPULimitError == nil {
			resourceMap["limits"].(map[string]interface{})["cpu"] = resourceCPULimit
		}
		if resourceMemoryLimitError == nil {
			resourceMap["limits"].(map[string]interface{})["memory"] = strconv.Itoa(resourceMemoryLimit) + "Mi"
		}
	}

	extraJsonMap := make(map[string]interface{})
	if len(region) > 0 {
		extraJsonMap["spec"] = make(map[string]interface{})
		extraJsonMap["spec"].(map[string]interface{})["template"] = make(map[string]interface{})
		extraJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"] = make(map[string]interface{})
		extraJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["nodeSelector"] = make(map[string]interface{})

		extraJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["nodeSelector"].(map[string]interface{})["region"] = region
		if len(zone) > 0 {
			extraJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["nodeSelector"].(map[string]interface{})["zone"] = zone
		}
	} else {
		extraJsonMap = nil
	}

	deployCreateInput := DeployCreateInput{
		imageInformationName,
		version,
		description,
		replicaAmount,
		deployContainerPortSlice,
		environmentSlice,
		resourceMap,
		extraJsonMap,
		autoUpdateForNewBuild,
	}

	url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort + "/api/v1/deploys/create/" + namespaces

	tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

	_, err := restclient.RequestPostWithStructure(url, deployCreateInput, nil, tokenHeaderMap)

	if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
		return
	}

	if err != nil {
		// Error
		guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
	} else {
		guimessage.AddSuccess("Create deploy " + imageInformationName + " version " + version + " success")
	}

	c.Ctx.Redirect(302, "/gui/deploy/deploy/list")

	guimessage.RedirectMessage(c)
}
Exemple #18
0
func (c *EditController) Post() {
	guimessage := guimessagedisplay.GetGUIMessage(c)

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	name := c.GetString("name")
	hostList := c.GetString("hostList")
	path := c.GetString("path")
	sshDialTimeoutInMilliSecond, _ := c.GetInt("sshDialTimeoutInMilliSecond")
	sshSessionTimeoutInMilliSecond, _ := c.GetInt("sshSessionTimeoutInMilliSecond")
	sshPort, _ := c.GetInt("sshPort")
	sshUser := c.GetString("sshUser")
	sshPassword := c.GetString("sshPassword")
	createOrUpdate := c.GetString("createOrUpdate")

	hostSlice := make([]string, 0)
	splitSlice := strings.Split(hostList, ",")
	for _, split := range splitSlice {
		host := strings.TrimSpace(split)
		if len(host) > 0 {
			hostSlice = append(hostSlice, host)
		}
	}

	glusterfsClusterInput := GlusterfsClusterInput{
		name,
		hostSlice,
		path,
		sshDialTimeoutInMilliSecond,
		sshSessionTimeoutInMilliSecond,
		sshPort,
		sshUser,
		sshPassword}

	if createOrUpdate == "create" {
		url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
			"/api/v1/glusterfs/clusters/"

		tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

		_, err := restclient.RequestPostWithStructure(url, glusterfsClusterInput, nil, tokenHeaderMap)

		if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
			return
		}

		if err != nil {
			// Error
			guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
		} else {
			guimessage.AddSuccess("Glusterfs cluster " + name + " is created")
		}
	} else {
		url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
			"/api/v1/glusterfs/clusters/" + name

		tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

		_, err := restclient.RequestPutWithStructure(url, glusterfsClusterInput, nil, tokenHeaderMap)

		if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
			return
		}

		if err != nil {
			// Error
			guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
		} else {
			guimessage.AddSuccess("Glusterfs cluster " + name + " is updated")
		}
	}

	c.Ctx.Redirect(302, "/gui/filesystem/glusterfs/cluster/list")

	guimessage.RedirectMessage(c)
}
Exemple #19
0
func (c *TopologyController) Post() {
	guimessage := guimessagedisplay.GetGUIMessage(c)

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	action := c.GetString("action")

	// Generate topology order
	inputMap := c.Input()
	cloneNameSlice := make([]string, 0)
	environmentMap := make(map[string]string)
	if inputMap != nil {
		for key, _ := range inputMap {
			// Collect the clone name
			if strings.HasPrefix(key, "cloneUse") {
				cloneName := key[len("cloneUse"):]
				if c.GetString(key) == "on" && len(cloneName) > 0 {
					cloneNameSlice = append(cloneNameSlice, cloneName)
				}
				// Collect environment
			} else if strings.HasPrefix(key, "clusterEnvironment") {
				environmentMap[key] = c.GetString(key)
			} else if strings.HasPrefix(key, "applicationEnvironment") {
				environmentMap[key] = c.GetString(key)
			}
		}
	}

	sourceNamespace := c.GetString("sourceNamespace")

	// Application
	url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
		"/api/v1/deploys/" + sourceNamespace

	deployInformationSlice := make([]DeployInformation, 0)

	tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

	_, err := restclient.RequestGetWithStructure(url, &deployInformationSlice, tokenHeaderMap)

	if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
		return
	}

	if err != nil {
		// Error
		guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
		guimessage.RedirectMessage(c)
		c.Ctx.Redirect(302, "/gui/deploy/clone/select")
		return
	}

	launchSlice := make([]Launch, 0)
	for _, cloneName := range cloneNameSlice {
		cloneOrder, _ := c.GetInt("cloneOrder" + cloneName)

		clusterName := c.GetString("clusterName" + cloneName)
		clusterSize, _ := c.GetInt("clusterSize" + cloneName)

		applicationImageInformationName := c.GetString("applicationImageInformationName" + cloneName)
		applicationVersion := c.GetString("applicationVersion" + cloneName)
		applicationDescription := c.GetString("applicationDescription" + cloneName)
		applicationReplicaAmount, _ := c.GetInt("applicationReplicaAmount" + cloneName)

		clusterEnvironmentMap := make(map[string]string)
		applicationEnvironmentMap := make(map[string]string)
		for key, value := range environmentMap {
			if strings.HasPrefix(key, "clusterEnvironment"+cloneName) {
				environemntKey := key[len("clusterEnvironment"+cloneName):]
				clusterEnvironmentMap[environemntKey] = value
			} else if strings.HasPrefix(key, "applicationEnvironment"+cloneName) {
				environemntKey := key[len("applicationEnvironment"+cloneName):]
				applicationEnvironmentMap[environemntKey] = value
			}
		}

		// Location Affinity
		region := c.GetString("clusterRegion" + cloneName)
		zone := c.GetString("clusterZone" + cloneName)

		if region == "Any" {
			region = ""
		}
		if zone == "Any" {
			zone = ""
		}

		extraJsonMap := make(map[string]interface{})
		if len(region) > 0 {
			extraJsonMap["spec"] = make(map[string]interface{})
			extraJsonMap["spec"].(map[string]interface{})["template"] = make(map[string]interface{})
			extraJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"] = make(map[string]interface{})
			extraJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["nodeSelector"] = make(map[string]interface{})

			extraJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["nodeSelector"].(map[string]interface{})["region"] = region
			if len(zone) > 0 {
				extraJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["nodeSelector"].(map[string]interface{})["zone"] = zone
			}
		} else {
			extraJsonMap = nil
		}

		// Cluster or application
		if len(clusterName) > 0 {
			environmentSlice := make([]interface{}, 0)
			for key, value := range clusterEnvironmentMap {
				environmentJsonMap := make(map[string]interface{})
				environmentJsonMap["name"] = key
				environmentJsonMap["value"] = value
				environmentSlice = append(environmentSlice, environmentJsonMap)
			}

			clusterLaunch := &LaunchClusterApplication{
				clusterName,
				clusterSize,
				environmentSlice,
				extraJsonMap,
			}

			launch := Launch{
				cloneOrder,
				nil,
				clusterLaunch,
			}

			launchSlice = append(launchSlice, launch)
		} else if len(applicationImageInformationName) > 0 {
			for _, deployInformation := range deployInformationSlice {
				if deployInformation.ImageInformationName == applicationImageInformationName {
					environmentSlice := make([]ReplicationControllerContainerEnvironment, 0)
					for key, value := range applicationEnvironmentMap {
						environmentSlice = append(environmentSlice, ReplicationControllerContainerEnvironment{key, value})
					}

					// Change the assigned Node Port to auto generated
					for i, containerPort := range deployInformation.ContainerPortSlice {
						if containerPort.NodePort > 0 {
							deployInformation.ContainerPortSlice[i].NodePort = 0
						}
					}

					launchApplication := &LaunchApplication{
						applicationImageInformationName,
						applicationVersion,
						applicationDescription,
						applicationReplicaAmount,
						deployInformation.ContainerPortSlice,
						environmentSlice,
						deployInformation.ResourceMap,
						extraJsonMap,
					}

					launch := Launch{
						cloneOrder,
						launchApplication,
						nil,
					}

					launchSlice = append(launchSlice, launch)

					break
				}
			}
		}
	}

	sort.Sort(ByLaunch(launchSlice))

	// Action: clone or create tempalte
	if action == "clone" {
		namespace, _ := c.GetSession("namespace").(string)

		for _, launch := range launchSlice {
			if launch.LaunchApplication != nil {
				url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort + "/api/v1/deploys/create/" + namespace

				_, err = restclient.RequestPostWithStructure(url, launch.LaunchApplication, nil, tokenHeaderMap)

				if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
					return
				}

				if err != nil {
					// Error
					guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
					guimessage.RedirectMessage(c)
					c.Ctx.Redirect(302, "/gui/deploy/clone/select")
					return
				}
			}
			if launch.LaunchClusterApplication != nil {
				url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
					"/api/v1/clusterapplications/launch/" + namespace + "/" + launch.LaunchClusterApplication.Name

				jsonMap := make(map[string]interface{})

				_, err = restclient.RequestPostWithStructure(url, launch.LaunchClusterApplication, &jsonMap, tokenHeaderMap)

				if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
					return
				}

				if err != nil {
					// Error
					guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
					guimessage.RedirectMessage(c)
					c.Ctx.Redirect(302, "/gui/deploy/clone/select")
					return
				}
			}
		}

		guimessage.AddSuccess("Clone the namespace " + sourceNamespace + " to the namespace " + namespace)
	} else if action == "template" {
		templateName := c.GetString("templateName")
		templateDescription := c.GetString("templateDescription")

		createdUserName := ""
		user, ok := c.GetSession("user").(*rbac.User)
		if ok {
			createdUserName = user.Name
		}

		topology := Topology{
			templateName,
			sourceNamespace,
			createdUserName,
			time.Now(),
			templateDescription,
			launchSlice,
		}

		url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort + "/api/v1/topology/"

		_, err = restclient.RequestPostWithStructure(url, topology, nil, tokenHeaderMap)

		if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
			return
		}

		if err != nil {
			// Error
			guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
			guimessage.RedirectMessage(c)
			c.Ctx.Redirect(302, "/gui/deploy/clone/select")
			return
		}

		guimessage.AddSuccess("Create topology template from the namespace " + sourceNamespace + " as the template " + templateName)
	} else {
		guimessage.AddDanger("No such action: " + action)
	}

	c.Ctx.Redirect(302, "/gui/deploy/clone/select")

	guimessage.RedirectMessage(c)
}
Exemple #20
0
func (c *LoginController) Post() {
	guimessage := guimessagedisplay.GetGUIMessage(c)

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	username := c.GetString("username")
	password := c.GetString("password")
	timeZoneOffset, err := c.GetInt("timeZoneOffset")
	if err != nil {
		guimessage.AddDanger("Fail to get browser time zone offset. Use UTC instead")
	} else {
		hourOffset := float64(timeZoneOffset) / 60.0
		// Since it is time offset, it needs to multiple -1 to get the UTC format
		sign := "-"
		if hourOffset < 0 {
			sign = "+"
		}
		guimessage.AddSuccess("Browser time zone is UTC " + sign + strconv.FormatFloat(math.Abs(hourOffset), 'f', -1, 64) + "")
		c.SetSession("timeZoneOffset", timeZoneOffset)
	}

	// User
	url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
		"/api/v1/authorizations/tokens/"
	userData := UserData{username, password}
	tokenData := TokenData{}
	_, err = restclient.RequestPostWithStructure(url, userData, &tokenData, nil)

	if err != nil {
		guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
		guimessage.RedirectMessage(c)
		c.Ctx.Redirect(302, "/gui/login/")
		return
	}

	url = cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
		"/api/v1/authorizations/tokens/" + tokenData.Token + "/components/" + componentName
	user := &rbac.User{}
	_, err = restclient.RequestGetWithStructure(url, &user, nil)
	if err != nil {
		guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
		guimessage.RedirectMessage(c)
		c.Ctx.Redirect(302, "/gui/login/")
		return
	}

	// Set session
	// User is used by this component to authorize
	c.SetSession("user", user)
	c.SetSession("username", user.Name)
	// Token is used to submit to other componentes to authorize
	headerMap := make(map[string]string)
	headerMap["token"] = tokenData.Token
	c.SetSession("tokenHeaderMap", headerMap)
	// Layout menu is used to display common layout menu
	layoutMenu := GetLayoutMenu(user)
	c.SetSession("layoutMenu", layoutMenu)

	// Namespace
	namespace := beego.AppConfig.String("namespace")
	// Get first namespace
	for _, resource := range user.ResourceSlice {
		if resource.Component == componentName || resource.Component == "*" {
			if strings.HasPrefix(resource.Path, "/namespaces/") {
				splitSlice := strings.Split(resource.Path, "/")
				name := splitSlice[2]
				if name != "" && name != "*" {
					namespace = name
					break
				}
			}
		}
	}
	metaDataMap := user.MetaDataMap
	if metaDataMap == nil {
		metaDataMap = make(map[string]string)
	}
	// If loginNamespace is set, use it
	loginNamespace := metaDataMap["loginNamespace"]
	if len(loginNamespace) > 0 {
		namespace = loginNamespace
	}

	// Check if the namespace existing
	url = cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort + "/api/v1/namespaces/"

	nameSlice := make([]string, 0)

	_, err = restclient.RequestGetWithStructure(url, &nameSlice, headerMap)

	if IsTokenInvalidAndRedirect(c, c.Ctx, err) {
		return
	}

	if err != nil {
		guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
		guimessage.RedirectMessage(c)
		c.Ctx.Redirect(302, "/gui/login/")
		return
	}

	namespaceExist := false
	for _, name := range nameSlice {
		if name == namespace {
			namespaceExist = true
		}
	}

	if namespaceExist == false {
		guimessage.AddDanger("Namespace " + namespace + " set for user doesn't exist")
		guimessage.RedirectMessage(c)
		c.Ctx.Redirect(302, "/gui/login/")
		return
	}

	// Set namespace
	c.SetSession("namespace", namespace)

	// Send audit log since this page will pass filter
	go func() {
		sendAuditLog(c.Ctx, user.Name, false)
	}()

	guimessage.AddSuccess("User " + username + " login")

	c.Ctx.Redirect(302, "/gui/dashboard/topology/")

	guimessage.RedirectMessage(c)
}
Exemple #21
0
func (c *EditController) Post() {
	guimessage := guimessagedisplay.GetGUIMessage(c)

	cloudoneProtocol := beego.AppConfig.String("cloudoneProtocol")
	cloudoneHost := beego.AppConfig.String("cloudoneHost")
	cloudonePort := beego.AppConfig.String("cloudonePort")

	name := c.GetString("name")
	host := c.GetString("host")
	port, _ := c.GetInt("port")
	createOrUpdate := c.GetString("createOrUpdate")

	privateRegistry := PrivateRegistry{
		name,
		host,
		port,
		"",
		"",
		"",
	}

	if createOrUpdate == "create" {
		url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
			"/api/v1/privateregistries/servers/"

		tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

		_, err := restclient.RequestPostWithStructure(url, privateRegistry, nil, tokenHeaderMap)

		if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
			return
		}

		if err != nil {
			// Error
			guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
		} else {
			guimessage.AddSuccess("Private register server configuration " + name + " is created")
		}
	} else {
		url := cloudoneProtocol + "://" + cloudoneHost + ":" + cloudonePort +
			"/api/v1/privateregistries/servers/" + name

		tokenHeaderMap, _ := c.GetSession("tokenHeaderMap").(map[string]string)

		_, err := restclient.RequestPutWithStructure(url, privateRegistry, nil, tokenHeaderMap)

		if identity.IsTokenInvalidAndRedirect(c, c.Ctx, err) {
			return
		}

		if err != nil {
			// Error
			guimessage.AddDanger(guimessagedisplay.GetErrorMessage(err))
		} else {
			guimessage.AddSuccess("Private register server configuration " + name + " is updated")
		}
	}

	c.Ctx.Redirect(302, "/gui/system/privateregistry/server/list")

	guimessage.RedirectMessage(c)
}