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()) }
func modify(args intf.Command) (result string, err error) { resultMap, err := ReadAsMap(args) if err != nil { return "error", err } content := resultMap["content"] mapContent, ok := content.(map[string]interface{}) if !ok { return "error", errors.New("Unable to modify existing content") } props := args.Arguments() for _, prop := range props { key := prop.Name() split := strings.SplitN(key, "-", 2) prefix := split[0] switch prefix { case "add", "remove", "change": if len(split) > 1 { key = split[1] } err = handleAddRemoveChange(mapContent, prefix, key, prop) if err != nil { return "error", err } default: typeName, ok := resultMap["type"].(string) if !ok { return "error", errors.New("Cannot resolve type for existing item") } ciType, err := metadata.Type(typeName) if err != nil { return "error", err } err = handleDefault(mapContent, key, prop, ciType) if err != nil { return "error", err } } } json, err := json.MarshalIndent(resultMap, "", " ") createCmd, err := cmd.NewStdinCmd("modify-sub", string(json)) if err != nil { return "error", err } return createOrModify(createCmd) }
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 }
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 }