func (c *ModelAdaptor) Apply(ctx context.Context, d *model.KVPair) (*model.KVPair, error) {
	var err error
	switch d.Key.(type) {
	case model.ProfileKey:
		t, l, r := ToTagsLabelsRules(d)
		if t, err = c.client.Apply(ctx, t); err != nil {
			return nil, err
		} else if _, err := c.client.Apply(ctx, l); err != nil {
			return nil, err
		} else if _, err := c.client.Apply(ctx, r); err != nil {
			return nil, errors.UpdateErrorIdentifier(err, d.Key)
		} else {
			d.Revision = t.Revision
			return d, nil
		}
	case model.NodeKey:
		p, o := toNodeComponents(d)
		if p, err = c.client.Apply(ctx, p); err != nil {
			return nil, err
		}
		if err = c.applyOrDeleteSubcomponents(context.Background(), o); err != nil {
			return nil, err
		}
		d.Revision = p.Revision
		return d, nil
	case model.BlockKey:
		if err = validateBlockValue(d); err != nil {
			return nil, err
		}
		b, err := c.client.Apply(ctx, d)
		if err != nil {
			return nil, err
		}
		d.Revision = b.Revision
		return d, nil
	case model.GlobalBGPConfigKey:
		nd := toDatastoreGlobalBGPConfig(*d)
		b, err := c.client.Apply(ctx, nd)
		if err != nil {
			return nil, errors.UpdateErrorIdentifier(err, d.Key)
		}
		d.Revision = b.Revision
		return d, nil
	default:
		return c.client.Apply(ctx, d)
	}
}
Set an existing entry in the datastore.  This ignores whether an entry already
exists.
package compat
import (
	"encoding/json"
	goerrors "errors"
	log "github.com/sirupsen/logrus"
	"context"
	"github.com/projectcalico/api/pkg/lib/numorstring"
	"github.com/projectcalico/libcalico-go/lib/backend/api"
	"github.com/projectcalico/libcalico-go/lib/backend/model"
	"github.com/projectcalico/libcalico-go/lib/errors"
	"github.com/projectcalico/libcalico-go/lib/net"
)
type ModelAdaptor struct {
	client api.Client
}
var _ api.Client = (*ModelAdaptor)(nil)
func NewAdaptor(c api.Client) *ModelAdaptor {
	return &ModelAdaptor{client: c}
}
func (c *ModelAdaptor) EnsureInitialized() error {
	return c.client.EnsureInitialized()
}
func (c *ModelAdaptor) Clean() error {
	return c.client.Clean()
}
func (c *ModelAdaptor) Create(ctx context.Context, d *model.KVPair) (*model.KVPair, error) {
	var err error
	switch k := d.Key.(type) {
	case model.ProfileKey:
		t, l, r := ToTagsLabelsRules(d)
		if t, err = c.client.Create(ctx, t); err != nil {
			return nil, err
		} else if _, err := c.client.Create(ctx, l); err != nil {
			return nil, err
		} else if _, err := c.client.Create(ctx, r); err != nil {
			return nil, err
		} else {
			d.Revision = t.Revision
			return d, nil
		}
	case model.NodeKey:
		p, o := toNodeComponents(d)
		if p, err = c.client.Create(ctx, p); err != nil {
			return nil, err
		}
		if err = c.applyOrDeleteSubcomponents(ctx, o); err != nil {
			return nil, err
		}
		d.Revision = p.Revision
		return d, nil
	case model.BlockKey:
		if err = validateBlockValue(d); err != nil {
			return nil, err
		}
		b, err := c.client.Create(ctx, d)
		if err != nil {
			return nil, err
		}
		d.Revision = b.Revision
		return d, nil
	case model.GlobalBGPConfigKey:
		nd := toDatastoreGlobalBGPConfig(*d)
		b, err := c.client.Create(ctx, nd)
		if err != nil {
			return nil, errors.UpdateErrorIdentifier(err, k)
		}
		d.Revision = b.Revision
		return d, nil
	default:
		return c.client.Create(ctx, d)
	}
}
func (c *ModelAdaptor) Update(ctx context.Context, d *model.KVPair) (*model.KVPair, error) {
	var err error
	switch d.Key.(type) {
	case model.ProfileKey:
		t, l, r := ToTagsLabelsRules(d)
		if t, err = c.client.Update(ctx, t); err != nil {
			return nil, err
		} else if _, err := c.client.Apply(ctx, l); err != nil {
			return nil, err
		} else if _, err := c.client.Apply(ctx, r); err != nil {
			return nil, err
		} else {
			d.Revision = t.Revision
			return d, nil
		}
	case model.NodeKey:
		p, o := toNodeComponents(d)
		if p, err = c.client.Update(ctx, p); err != nil {
			return nil, err
		}
		if err = c.applyOrDeleteSubcomponents(ctx, o); err != nil {
			return nil, err
		}
		d.Revision = p.Revision
		return d, nil
	case model.BlockKey:
		if err = validateBlockValue(d); err != nil {
			return nil, err
		}
		b, err := c.client.Update(ctx, d)
		if err != nil {
			return nil, err
		}
		d.Revision = b.Revision
		return d, nil
	case model.GlobalBGPConfigKey:
		nd := toDatastoreGlobalBGPConfig(*d)
		b, err := c.client.Update(ctx, nd)
		if err != nil {
			return nil, errors.UpdateErrorIdentifier(err, d.Key)
		}
		d.Revision = b.Revision
		return d, nil
	default:
		return c.client.Update(ctx, d)
	}
}