Beispiel #1
0
// Get retrieves the item from etcd's key/value store and then populates obj with its data.
func (c *Client) Get(obj db.Entity) error {
	if obj.Hooks().PreGet != nil {
		if err := obj.Hooks().PreGet(c, obj); err != nil {
			return errors.EtcdToErrored(err)
		}
	}

	path, err := obj.Path()
	if err != nil {
		return err
	}

	resp, err := c.client.Get(context.Background(), c.qualified(path), nil)
	if err != nil {
		return errors.EtcdToErrored(err)
	}

	if err := jsonio.Read(obj, []byte(resp.Node.Value)); err != nil {
		return err
	}

	if err := obj.SetKey(c.trimPath(resp.Node.Key)); err != nil {
		return err
	}

	if obj.Hooks().PostGet != nil {
		if err := obj.Hooks().PostGet(c, obj); err != nil {
			return errors.EtcdToErrored(err)
		}
	}

	return obj.Validate()
}
Beispiel #2
0
// Set takes the object and commits it to the database.
func (c *Client) Set(obj db.Entity) error {
	if err := obj.Validate(); err != nil {
		return err
	}

	if obj.Hooks().PreSet != nil {
		if err := obj.Hooks().PreSet(c, obj); err != nil {
			return errors.EtcdToErrored(err)
		}
	}

	content, err := jsonio.Write(obj)
	if err != nil {
		return err
	}

	path, err := obj.Path()
	if err != nil {
		return err
	}

	if _, err := c.client.Set(context.Background(), c.qualified(path), string(content), nil); err != nil {
		return errors.EtcdToErrored(err)
	}

	if obj.Hooks().PostSet != nil {
		if err := obj.Hooks().PostSet(c, obj); err != nil {
			return errors.EtcdToErrored(err)
		}
	}

	return nil
}
Beispiel #3
0
// Delete removes the object from the store.
func (c *Client) Delete(obj db.Entity) error {
	if obj.Hooks().PreDelete != nil {
		if err := obj.Hooks().PreDelete(c, obj); err != nil {
			return errors.EtcdToErrored(err)
		}
	}

	path, err := obj.Path()
	if err != nil {
		return err
	}

	if _, err := c.client.Delete(context.Background(), c.qualified(path), nil); err != nil {
		return errors.EtcdToErrored(err)
	}

	if obj.Hooks().PostDelete != nil {
		if err := obj.Hooks().PostDelete(c, obj); err != nil {
			return errors.EtcdToErrored(err)
		}
	}

	return nil
}