Пример #1
0
func ReadAsMap(args intf.Command) (resultMap map[string]interface{}, err error) {
	subs := args.Subs()

	ci := AntiAbbreviate(subs[0])

	body, err := http.Read("/repository/ci/" + ci)
	if err != nil {
		return
	}

	return xml2CleanMap(body)

}
Пример #2
0
func permissions(args intf.Command) (result string, err error) {
	body, err := http.Read("/metadata/permissions")
	if err != nil {
		return
	}

	list := PermissionsList{}
	err = xml.Unmarshal(body, &list)

	for _, el := range list.Permissions {
		fmt.Println(eq(el.Name, 20), eq(el.Level, 8), el.Root)
	}

	return "", nil
}
Пример #3
0
func displayStatus(taskId string) {
	timer := time.Tick(100 * time.Millisecond)

	previousStep := -1

	for _ = range timer {

		// TODO support parallel deployments
		body, err := http.Read("/task/" + taskId + "/step")
		if err != nil {
			return
		}

		task := Task{}
		err = xml.Unmarshal(body, &task)
		if err != nil {
			return
		}

		currentStep := task.CurrentStep

		for i, step := range task.Steps {

			// Fix current step - sometimes it is one too high
			if step.State == "EXECUTING" {
				currentStep = i
			}

			if i > previousStep && i <= currentStep {
				fmt.Printf("%d/%d - "+step.Description+"\n", i+1, task.TotalSteps)
			}
		}

		previousStep = currentStep

		if task.State == "EXECUTED" {
			break
		} else if task.State == "FAILED" {
			// TODO throw error on deployment FAILED
			// TODO show logs on deployment error
			// can we simulate an error?
			break
		}

	}
}
Пример #4
0
func list(args intf.Command) (result string, err error) {
	subs := args.Subs()

	arguments := make([]string, 0)

	if len(subs) > 0 {
		if strings.HasSuffix(subs[0], "*") {
			arguments = append(arguments, "ancestor="+url.QueryEscape(AntiAbbreviate(strings.Replace(subs[0], "*", "", -1))))
		} else {
			arguments = append(arguments, "parent="+url.QueryEscape(AntiAbbreviate(subs[0])))
		}
	}

	extra := args.Arguments()
	for _, el := range extra {
		name := el.Name()

		switch name {
		case "type":
			arguments = append(arguments, "type="+url.QueryEscape(el.Value()))
		case "like":
			arguments = append(arguments, "namePattern="+url.QueryEscape(el.Value()))
		case "before":
			//TODO lastModifiedBefore
		case "after":
			//TODO lastModifiedAfter
		case "page":
			arguments = append(arguments, "page="+url.QueryEscape(el.Value()))
		case "pagesize":
			arguments = append(arguments, "resultsPerPage="+url.QueryEscape(el.Value()))
		}
	}

	body, err := http.Read("/repository/query?" + strings.Join(arguments, "&"))
	list, err := readCiList(body)
	if err != nil {
		return "error", err
	}
	for _, el := range list.CIs {
		fmt.Println(el.CiRef)
	}

	return "", err
}
Пример #5
0
func types(args intf.Command) (result string, err error) {
	body, err := http.Read("/metadata/type")
	if err != nil {
		return
	}

	list := List{}
	err = xml.Unmarshal(body, &list)
	if err != nil {
		return
	}

	for _, ciType := range list.CITypes {
		fmt.Println(ciType.Type)
	}

	return "", nil

}
Пример #6
0
// TODO Make this a util?
func AntiAbbreviate(ciName string) string {
	prefix := strings.SplitN(ciName, "/", 2)
	longer := shorthand[prefix[0]]

	remainder := ""

	if len(prefix) > 1 {
		remainder = prefix[1]

	}

	if longer != "" {
		ciName = longer + "/" + remainder
	}

	if strings.HasSuffix(ciName, "latest") {
		searchFor := ciName[:len(ciName)-len("latest")]
		body, err := http.Read("/repository/query?parent=" + searchFor)
		list, err := readCiList(body)
		if err != nil || len(list.CIs) == 0 {
			// TODO ERROR
			return "error"
		}

		splitter := regexp.MustCompile("[\\.?\\-?_?]+")

		versions := make([][]string, 0)
		ciMap := make(map[string]Ci)
		for _, el := range list.CIs {
			version := el.CiRef[len(searchFor):]

			split := splitter.Split(version, -1)
			ciMap[strings.Join(split, ".")] = el
			versions = append(versions, split)
		}
		sort.Sort(ByVersion(versions))
		ciName = ciMap[strings.Join(versions[len(versions)-1], ".")].CiRef

	}

	return ciName
}
Пример #7
0
func Type(typeName string) (retType *CIType, err error) {
	long := shorthand[typeName]
	if long != "" {
		typeName = long
	}

	body, err := http.Read("/metadata/type/" + typeName)
	if err != nil {
		return
	}

	ciType := CIType{}
	err = xml.Unmarshal(body, &ciType)

	if err == io.EOF {
		return nil, errors.New("Type " + typeName + " not found")
	} else if err != nil {
		return
	}

	return &ciType, nil
}
Пример #8
0
func plan(args intf.Command, depType string) (result string, err error) {
	result, err = prepare(args, depType)

	body, err := http.Read("/task/" + result + "/step")
	if err != nil {
		return
	}

	task := Task{}
	err = xml.Unmarshal(body, &task)
	if err != nil {
		return "error", err
	}

	fmt.Println("Plan", task.Description)

	for i, step := range task.Steps {
		fmt.Printf("%d/%d - "+step.Description+"\n", i+1, task.TotalSteps)
	}

	return "", err
}
Пример #9
0
func undeploy(args intf.Command) (result string, err error) {
	subs := args.Subs()
	appToUndeploy := repo.AntiAbbreviate(subs[0])
	body, err := http.Read("/deployment/prepare/undeploy?deployedApplication=" + appToUndeploy)
	if err != nil {
		return
	}

	body, err = http.Create("/deployment", bytes.NewBuffer(body))
	if err != nil {
		return
	}

	taskId := string(body)

	body, err = http.Create("/task/"+string(body)+"/start", nil)
	if err != nil {
		return
	}

	displayStatus(taskId)

	return string(body), err
}
Пример #10
0
func grant(args intf.Command) (result string, err error) {
	subs := args.Subs()

	ci := subs[0]
	roleToChange := subs[1]
	perms := subs[2:]

	// Get current permissions
	// (already works for global keyword :) )
	body, err := http.Read("/internal/security/roles/permissions/" + repo.AntiAbbreviate(ci))
	if err != nil {
		return
	}

	values, err := x2j.XmlToMap(body)
	if err != nil {
		return
	}

	arr := arrayFromMap(values["collection"], "rolePermissions")
	changed := false

	for _, elMap := range arr {
		role := elMap["role"].(map[string]interface{})
		if role["-name"] == roleToChange {

			oldPerms := elMap["permissions"].([]interface{})
			for _, el := range perms {
				oldPerms = append(oldPerms, el)
			}
			elMap["permissions"], changed = oldPerms, true
		}
	}

	if !changed {
		roleMap := map[string]string{"-id": strconv.Itoa(len(arr)), "-role": roleToChange}
		fmt.Println("Need to add it.... :( ", roleMap)
		fullMap := map[string]interface{}{"permissions": perms, "role": roleMap}
		arr = append(arr, fullMap)
		values["collection"].(map[string]interface{})["rolePermissions"] = arr
	}

	// TODO Make this a util?
	json, _ := j2x.MapToJson(values)
	xml, _ := j2x.JsonToXml(json)

	fmt.Println("modified(?) response = ", string(xml))

	/*
		body, err = http.Update("/internal/security/roles/permissions/" +
			repo.AntiAbbreviate(ci),
			bytes.NewBuffer(xml))
		if err != nil {
			return
		}

		fmt.Println("modified(?) response = ", body)
		//*/

	//fmt.Println("permset = ", permSet)

	// decode XML to ... JSON or marshall to objects?

	// is role already in there?
	// if not: add role

	// is permission already in there? -> ignore

	// add permission

	// encode as XML

	// post to http
	return
}