Esempio n. 1
0
func createOrModify(args intf.Command) (result string, err error) {
	subs := args.Subs()
	typeName := subs[0]
	ciName := subs[1]

	ciType, err := metadata.Type(typeName)
	if err != nil {
		return
	}

	// put this as the root in a map containing a map
	// do this AFTER the for loop

	// create new map and put the below in it

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

	props := args.Arguments()
	for _, prop := range props {
		key := prop.Name()

		mapProps[key], err = translateProp(ciType, prop)
		if err != nil {
			return
		}
	}

	id := ciName
	id = AntiAbbreviate(id)
	if ciType.Root != "" && !strings.HasPrefix(id, ciType.Root) {
		id = ciType.Root + "/" + id
	}
	mapProps["-id"] = id

	final := map[string]interface{}{ciType.Type: mapProps}

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

	switch args.Main() {
	case "create":
		body, err := http.Create("/repository/ci/"+id, bytes.NewBuffer(xml))
		if err != nil {
			return "error", err
		}
		return Xml2CleanJson(body)
	case "modify-sub":
		body, err := http.Update("/repository/ci/"+id, bytes.NewBuffer(xml))
		if err != nil {
			return "error", err
		}
		return Xml2CleanJson(body)
	}
	return "error", errors.New("Unknown command " + args.Main())

}
Esempio n. 2
0
// Given a json as response body, convert it to xml.
func ConvertJsonToXml(r *http.Request, w *http.Response, body *bytes.Buffer) {
	b, err := ioutil.ReadAll(body)
	if err != nil {
		log.Print("err: ", err.Error())
		body.Write([]byte(err.Error()))
		return
	}
	m, err := j2x.JsonToXml(b)
	if err != nil {
		log.Print("err:", err.Error())
		body.Write([]byte(err.Error()))
		return
	}
	w.Header.Set("Content-Type", "application/xml")
	body.Reset()
	body.Write(m)
}
Esempio n. 3
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
}
Esempio n. 4
0
func prepare(args intf.Command, depType string) (result string, err error) {
	subs := args.Subs()
	appVersion := repo.AntiAbbreviate(subs[0])
	targetEnv := repo.AntiAbbreviate(subs[1])

	parts := strings.Split(appVersion, "/")

	app := parts[len(parts)-2]

	targetDeployed := targetEnv + "/" + app

	if depType == "*" {
		statusCode, _, err := http.Get("/repository/ci/" + targetDeployed)
		if err != nil {
			return "error", err
		} else if statusCode == 200 {
			depType = "UPDATE"
		} else if statusCode == 404 {
			depType = "INITIAL"
		} else {
			return "error", errors.New("Unexpected http status code " + strconv.Itoa(statusCode) + " while checking the existance of " + targetDeployed)
		}
	}

	deployedApplication := map[string]interface{}{
		"-id": targetDeployed,
		"version": map[string]interface{}{
			"-ref": appVersion,
		},
		"environment": map[string]interface{}{
			"-ref": targetEnv,
		},
		"optimizePlan": "true",
	}

	deployment := map[string]interface{}{
		"deployment": map[string]interface{}{
			"-type": depType,
			"application": map[string]interface{}{
				"udm.DeployedApplication": deployedApplication,
			},
		},
	}

	for _, arg := range args.Arguments() {
		if arg.Name() == "orchestrator" {
			deployedApplication["orchestrator"] =
				repo.MapSetOfStrings(arg.Values())
		}
	}

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

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

	body, err = http.Create("/deployment", bytes.NewBuffer(body))

	return string(body), err
}