// Create implements storage.Interface.Create. func (s *store) Create(ctx context.Context, key string, obj, out runtime.Object, ttl uint64) error { if version, err := s.versioner.ObjectResourceVersion(obj); err == nil && version != 0 { return errors.New("resourceVersion should not be set on objects to be created") } data, err := runtime.Encode(s.codec, obj) if err != nil { return err } key = keyWithPrefix(s.pathPrefix, key) opts, err := s.ttlOpts(ctx, int64(ttl)) if err != nil { return err } txnResp, err := s.client.KV.Txn(ctx).If( notFound(key), ).Then( clientv3.OpPut(key, string(data), opts...), ).Commit() if err != nil { return err } if !txnResp.Succeeded { return storage.NewKeyExistsError(key, 0) } if out != nil { putResp := txnResp.Responses[0].GetResponsePut() return decode(s.codec, s.versioner, data, out, putResp.Header.Revision) } return nil }
func toStorageErr(err error, key string, rv int64) error { if err == nil { return nil } switch { case etcdutil.IsEtcdNotFound(err): return storage.NewKeyNotFoundError(key, rv) case etcdutil.IsEtcdNodeExist(err): return storage.NewKeyExistsError(key, rv) case etcdutil.IsEtcdTestFailed(err): return storage.NewResourceVersionConflictsError(key, rv) case etcdutil.IsEtcdUnreachable(err): return storage.NewUnreachableError(key, rv) default: return err } }