func nodeHandling(w http.ResponseWriter, r *http.Request) map[string]string { /* We're dealing with nodes, then. */ nodeResponse := make(map[string]string) opUser, oerr := actor.GetReqUser(r.Header.Get("X-OPS-USERID")) if oerr != nil { jsonErrorReport(w, r, oerr.Error(), oerr.Status()) return nil } switch r.Method { case "GET": if opUser.IsValidator() { jsonErrorReport(w, r, "You are not allowed to take this action.", http.StatusForbidden) return nil } nodeList := node.GetList() for _, k := range nodeList { itemURL := fmt.Sprintf("/nodes/%s", k) nodeResponse[k] = util.CustomURL(itemURL) } case "POST": if opUser.IsValidator() { jsonErrorReport(w, r, "You are not allowed to take this action.", http.StatusForbidden) return nil } nodeData, jerr := parseObjJSON(r.Body) if jerr != nil { jsonErrorReport(w, r, jerr.Error(), http.StatusBadRequest) return nil } nodeName, sterr := util.ValidateAsString(nodeData["name"]) if sterr != nil { jsonErrorReport(w, r, sterr.Error(), http.StatusBadRequest) return nil } chefNode, _ := node.Get(nodeName) if chefNode != nil { httperr := fmt.Errorf("Node already exists") jsonErrorReport(w, r, httperr.Error(), http.StatusConflict) return nil } var nerr util.Gerror chefNode, nerr = node.NewFromJSON(nodeData) if nerr != nil { jsonErrorReport(w, r, nerr.Error(), nerr.Status()) return nil } err := chefNode.Save() if err != nil { jsonErrorReport(w, r, err.Error(), http.StatusInternalServerError) return nil } err = chefNode.UpdateStatus("new") if err != nil { jsonErrorReport(w, r, err.Error(), http.StatusInternalServerError) return nil } if lerr := loginfo.LogEvent(opUser, chefNode, "create"); lerr != nil { jsonErrorReport(w, r, lerr.Error(), http.StatusInternalServerError) return nil } nodeResponse["uri"] = util.ObjURL(chefNode) w.WriteHeader(http.StatusCreated) default: jsonErrorReport(w, r, "Method not allowed for nodes", http.StatusMethodNotAllowed) return nil } return nodeResponse }
func importAll(fileName string) error { fp, err := os.Open(fileName) if err != nil { return err } exportedData := &ExportData{} dec := json.NewDecoder(fp) if err := dec.Decode(&exportedData); err != nil { return err } // What versions of the exported data are supported? // At the moment it's only 1.0. if exportedData.MajorVersion == 1 && (exportedData.MinorVersion == 0 || exportedData.MinorVersion == 1) { logger.Infof("Importing data, version %d.%d created on %s", exportedData.MajorVersion, exportedData.MinorVersion, exportedData.CreatedTime) // load clients logger.Infof("Loading clients") for _, v := range exportedData.Data["client"] { c, err := client.NewFromJSON(v.(map[string]interface{})) if err != nil { return err } pkerr := c.SetPublicKey(v.(map[string]interface{})["public_key"]) if pkerr != nil { return pkerr } gerr := c.Save() if gerr != nil { return gerr } } // load users logger.Infof("Loading users") for _, v := range exportedData.Data["user"] { pwhash, _ := v.(map[string]interface{})["password"].(string) v.(map[string]interface{})["password"] = "" u, err := user.NewFromJSON(v.(map[string]interface{})) if err != nil { return err } u.SetPasswdHash(pwhash) pkerr := u.SetPublicKey(v.(map[string]interface{})["public_key"]) if pkerr != nil { return pkerr } gerr := u.Save() if gerr != nil { return gerr } } // load filestore logger.Infof("Loading filestore") for _, v := range exportedData.Data["filestore"] { fileData, err := base64.StdEncoding.DecodeString(v.(map[string]interface{})["Data"].(string)) if err != nil { return err } fdBuf := bytes.NewBuffer(fileData) fdRc := ioutil.NopCloser(fdBuf) fs, err := filestore.New(v.(map[string]interface{})["Chksum"].(string), fdRc, int64(fdBuf.Len())) if err != nil { return err } if err = fs.Save(); err != nil { return err } } // load cookbooks logger.Infof("Loading cookbooks") for _, v := range exportedData.Data["cookbook"] { cb, err := cookbook.New(v.(map[string]interface{})["Name"].(string)) if err != nil { return err } gerr := cb.Save() if gerr != nil { return gerr } for ver, cbvData := range v.(map[string]interface{})["Versions"].(map[string]interface{}) { cbvData, cerr := checkAttrs(cbvData.(map[string]interface{})) if cerr != nil { return cerr } _, cbverr := cb.NewVersion(ver, cbvData) if cbverr != nil { return cbverr } } } // load data bags logger.Infof("Loading data bags") for _, v := range exportedData.Data["data_bag"] { dbag, err := databag.New(v.(map[string]interface{})["Name"].(string)) if err != nil { return err } gerr := dbag.Save() if gerr != nil { return gerr } for _, dbagData := range v.(map[string]interface{})["DataBagItems"].(map[string]interface{}) { _, dbierr := dbag.NewDBItem(dbagData.(map[string]interface{})["raw_data"].(map[string]interface{})) if dbierr != nil { return dbierr } } gerr = dbag.Save() if gerr != nil { return gerr } } // load environments logger.Infof("Loading environments") for _, v := range exportedData.Data["environment"] { envData, cerr := checkAttrs(v.(map[string]interface{})) if cerr != nil { return nil } if envData["name"].(string) != "_default" { e, err := environment.NewFromJSON(envData) if err != nil { return err } gerr := e.Save() if gerr != nil { return gerr } } } // load nodes logger.Infof("Loading nodes") for _, v := range exportedData.Data["node"] { nodeData, cerr := checkAttrs(v.(map[string]interface{})) if cerr != nil { return nil } n, err := node.NewFromJSON(nodeData) if err != nil { return err } gerr := n.Save() if gerr != nil { return gerr } } // load roles logger.Infof("Loading roles") for _, v := range exportedData.Data["role"] { roleData, cerr := checkAttrs(v.(map[string]interface{})) if cerr != nil { return nil } r, err := role.NewFromJSON(roleData) if err != nil { return err } gerr := r.Save() if gerr != nil { return gerr } } // load sandboxes logger.Infof("Loading sandboxes") for _, v := range exportedData.Data["sandbox"] { sbid, _ := v.(map[string]interface{})["Id"].(string) sbts, _ := v.(map[string]interface{})["CreationTime"].(string) sbcomplete, _ := v.(map[string]interface{})["Completed"].(bool) sbck, _ := v.(map[string]interface{})["Checksums"].([]interface{}) sbTime, err := time.Parse(time.RFC3339, sbts) if err != nil { return err } sbChecksums := make([]string, len(sbck)) for i, c := range sbck { sbChecksums[i] = c.(string) } sbox := &sandbox.Sandbox{ID: sbid, CreationTime: sbTime, Completed: sbcomplete, Checksums: sbChecksums} if err = sbox.Save(); err != nil { return err } } // load loginfos logger.Infof("Loading loginfo") for _, v := range exportedData.Data["loginfo"] { if err := loginfo.Import(v.(map[string]interface{})); err != nil { return err } } // load reports logger.Infof("Loading reports") for _, o := range exportedData.Data["report"] { // handle data exported from a bugged report export var nodeName string v := o.(map[string]interface{}) if n, ok := v["node_name"]; ok { nodeName = n.(string) } else if n, ok := v["nodeName"]; ok { nodeName = n.(string) } v["action"] = "start" if st, ok := v["start_time"].(string); ok { t, err := time.Parse(time.RFC3339, st) if err != nil { return err } v["start_time"] = t.Format(report.ReportTimeFormat) } if et, ok := v["end_time"].(string); ok { t, err := time.Parse(time.RFC3339, et) if err != nil { return err } v["end_time"] = t.Format(report.ReportTimeFormat) } r, err := report.NewFromJSON(nodeName, v) if err != nil { return err } gerr := r.Save() if gerr != nil { return gerr } v["action"] = "end" if err := r.UpdateFromJSON(v); err != nil { return err } gerr = r.Save() if gerr != nil { return gerr } } if exportedData.MinorVersion == 1 { // import shovey jobs, run, and streams, and node // statuses logger.Infof("Loading node statuses...") for _, v := range exportedData.Data["node_status"] { ns := v.(map[string]interface{}) err := node.ImportStatus(ns) if err != nil { return err } } logger.Infof("Loading shoveys...") for _, v := range exportedData.Data["shovey"] { s := v.(map[string]interface{}) err := shovey.ImportShovey(s) if err != nil { return err } } logger.Infof("Loading shovey runs...") for _, v := range exportedData.Data["shovey_run"] { s := v.(map[string]interface{}) err := shovey.ImportShoveyRun(s) if err != nil { return err } } logger.Infof("Loading shovey run streams...") for _, v := range exportedData.Data["shovey_run_stream"] { s := v.(map[string]interface{}) err := shovey.ImportShoveyRunStream(s) if err != nil { return err } } } } else { err := fmt.Errorf("goiardi export data version %d.%d is not supported by this version of goiardi", exportedData.MajorVersion, exportedData.MinorVersion) return err } return nil }