Example #1
0
// Save the node.
func (n *Node) Save() error {
	if config.UsingDB() {
		if err := n.saveSQL(); err != nil {
			return err
		}
	} else {
		ds := datastore.New()
		ds.Set("node", n.Name, n)
	}
	/* TODO Later: excellent candidate for a goroutine */
	indexer.IndexObj(n)
	return nil
}
Example #2
0
// Save the role.
func (r *Role) Save() error {
	if config.Config.UseMySQL {
		if err := r.saveMySQL(); err != nil {
			return nil
		}
	} else if config.Config.UsePostgreSQL {
		if err := r.savePostgreSQL(); err != nil {
			return nil
		}
	} else {
		ds := datastore.New()
		ds.Set("role", r.Name, r)
	}
	indexer.IndexObj(r)
	return nil
}
Example #3
0
// MakeDefaultEnvironment creates the default environment on startup.
func MakeDefaultEnvironment() {
	var de *ChefEnvironment
	if config.UsingDB() {
		// The default environment is pre-created in the db schema when
		// it's loaded. Re-indexing the default environment doesn't
		// hurt anything though, so just get the usual default env and
		// index it, not bothering with these other steps that are
		// easier to do with the in-memory mode.
		de = defaultEnvironment()
	} else {
		ds := datastore.New()
		// only create the new default environment if we don't already have one
		// saved
		if _, found := ds.Get("env", "_default"); found {
			return
		}
		de = defaultEnvironment()
		ds.Set("env", de.Name, de)
	}
	indexer.IndexObj(de)
}
Example #4
0
// Save the client. If a user with the same name as the client exists, returns
// an error. Additionally, if running with MySQL it will return any DB error.
func (c *Client) Save() error {
	if config.UsingDB() {
		var err error
		if config.Config.UseMySQL {
			err = c.saveMySQL()
		} else if config.Config.UsePostgreSQL {
			err = c.savePostgreSQL()
		}
		if err != nil {
			return err
		}
	} else {
		if err := chkInMemUser(c.Name); err != nil {
			return err
		}
		ds := datastore.New()
		ds.Set("client", c.Name, c)
	}
	indexer.IndexObj(c)
	return nil
}
Example #5
0
// Save the environment. Returns an error if you try to save the "_default"
// environment.
func (e *ChefEnvironment) Save() util.Gerror {
	if e.Name == "_default" {
		err := util.Errorf("The '_default' environment cannot be modified.")
		err.SetStatus(http.StatusMethodNotAllowed)
		return err
	}
	if config.Config.UseMySQL {
		err := e.saveEnvironmentMySQL()
		if err != nil {
			return err
		}
	} else if config.Config.UsePostgreSQL {
		err := e.saveEnvironmentPostgreSQL()
		if err != nil {
			return err
		}
	} else {
		ds := datastore.New()
		ds.Set("env", e.Name, e)
	}
	indexer.IndexObj(e)
	return nil
}
Example #6
0
// UpdateDBItem updates a data bag item in this data bag.
func (db *DataBag) UpdateDBItem(dbiID string, rawDbagItem map[string]interface{}) (*DataBagItem, error) {
	dbItem, err := db.GetDBItem(dbiID)
	if err != nil {
		if err == sql.ErrNoRows {
			err = fmt.Errorf("Cannot load data bag item %s for data bag %s", dbiID, db.Name)
		}
		return nil, err
	}
	dbItem.RawData = rawDbagItem
	if config.UsingDB() {
		err = dbItem.updateDBItemSQL()
		if err != nil {
			return nil, err
		}
	} else {
		db.DataBagItems[dbiID] = dbItem
	}
	err = db.Save()
	if err != nil {
		return nil, err
	}
	indexer.IndexObj(dbItem)
	return dbItem, nil
}
Example #7
0
// NewDBItem creates a new data bag item in the associated data bag.
func (db *DataBag) NewDBItem(rawDbagItem map[string]interface{}) (*DataBagItem, util.Gerror) {
	var dbiID string
	var dbagItem *DataBagItem
	switch t := rawDbagItem["id"].(type) {
	case string:
		if t == "" {
			err := util.Errorf("Field 'id' missing")
			return nil, err
		}
		dbiID = t
	default:
		err := util.Errorf("Field 'id' missing")
		return nil, err
	}
	if err := validateDataBagName(dbiID, true); err != nil {
		return nil, err
	}
	dbiFullName := fmt.Sprintf("data_bag_item_%s_%s", db.Name, dbiID)

	if config.UsingDB() {
		d, err := db.getDBItemSQL(dbiID)
		if d != nil || (err != nil && err != sql.ErrNoRows) {
			if err != nil {
				logger.Debugf("Log real SQL error in NewDBItem: %s", err.Error())
			}
			gerr := util.Errorf("Data Bag Item '%s' already exists in Data Bag '%s'.", dbiID, db.Name)
			gerr.SetStatus(http.StatusConflict)
			return nil, gerr
		}
		if config.Config.UseMySQL {
			dbagItem, err = db.newDBItemMySQL(dbiID, rawDbagItem)
		} else if config.Config.UsePostgreSQL {
			dbagItem, err = db.newDBItemPostgreSQL(dbiID, rawDbagItem)
		}
		if err != nil {
			gerr := util.Errorf(err.Error())
			gerr.SetStatus(http.StatusInternalServerError)
			return nil, gerr
		}
	} else {
		/* Look for an existing dbag item with this name */
		if d, _ := db.GetDBItem(dbiID); d != nil {
			gerr := util.Errorf("Data Bag Item '%s' already exists in Data Bag '%s'.", dbiID, db.Name)
			gerr.SetStatus(http.StatusConflict)
			return nil, gerr
		}
		/* But should we store the raw data as a JSON string? */
		dbagItem = &DataBagItem{
			Name:        dbiFullName,
			ChefType:    "data_bag_item",
			JSONClass:   "Chef::DataBagItem",
			DataBagName: db.Name,
			RawData:     rawDbagItem,
		}
		db.DataBagItems[dbiID] = dbagItem
	}
	err := db.Save()
	if err != nil {
		gerr := util.Errorf(err.Error())
		gerr.SetStatus(http.StatusInternalServerError)
		return nil, gerr
	}
	indexer.IndexObj(dbagItem)
	return dbagItem, nil
}