Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1 | package impl |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "path" |
| 6 | |
| 7 | "veyron/services/mgmt/profile" |
| 8 | |
| 9 | "veyron2/ipc" |
| 10 | "veyron2/storage" |
Tilak Sharma | a5e479f | 2014-05-11 22:27:43 -0700 | [diff] [blame] | 11 | "veyron2/storage/vstore/primitives" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 12 | "veyron2/vlog" |
| 13 | ) |
| 14 | |
| 15 | // invoker holds the profile manager invocation. |
| 16 | type invoker struct { |
| 17 | // store is the storage server used for storing profile data. |
| 18 | store storage.Store |
| 19 | // suffix is the suffix of the current invocation that is assumed to |
| 20 | // be used as a relative veyron name to identify a profile |
| 21 | // specification. |
| 22 | suffix string |
| 23 | } |
| 24 | |
| 25 | var ( |
| 26 | errNotFound = errors.New("not found") |
| 27 | errOperationFailed = errors.New("operation failed") |
| 28 | ) |
| 29 | |
| 30 | // NewInvoker is the invoker factory. |
| 31 | func NewInvoker(store storage.Store, suffix string) *invoker { |
| 32 | return &invoker{store: store, suffix: suffix} |
| 33 | } |
| 34 | |
| 35 | // STORE MANAGEMENT INTERFACE IMPLEMENTATION |
| 36 | |
| 37 | // dir is used to organize directory contents in the store. |
| 38 | type dir struct{} |
| 39 | |
| 40 | // makeParentNodes creates the parent nodes if they do not already exist. |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 41 | func makeParentNodes(context ipc.ServerContext, store storage.Store, transaction storage.Transaction, path string) error { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 42 | pathComponents := storage.ParsePath(path) |
| 43 | for i := 0; i < len(pathComponents); i++ { |
| 44 | name := pathComponents[:i].String() |
| 45 | object := store.Bind(name) |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 46 | if _, err := object.Get(context, transaction); err != nil { |
| 47 | if _, err := object.Put(context, transaction, &dir{}); err != nil { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 48 | return errOperationFailed |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | return nil |
| 53 | } |
| 54 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 55 | func (i *invoker) Put(context ipc.ServerContext, profile profile.Specification) error { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 56 | vlog.VI(0).Infof("%v.Put(%v)", i.suffix, profile) |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 57 | transaction := primitives.NewTransaction(context) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 58 | path := path.Join("/profiles", i.suffix) |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 59 | if err := makeParentNodes(context, i.store, transaction, path); err != nil { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 60 | return err |
| 61 | } |
| 62 | object := i.store.Bind(path) |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 63 | if _, err := object.Put(context, transaction, profile); err != nil { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 64 | return errOperationFailed |
| 65 | } |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 66 | if err := transaction.Commit(context); err != nil { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 67 | return errOperationFailed |
| 68 | } |
| 69 | return nil |
| 70 | } |
| 71 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 72 | func (i *invoker) Remove(context ipc.ServerContext) error { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 73 | vlog.VI(0).Infof("%v.Remove(%v)", i.suffix) |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 74 | transaction := primitives.NewTransaction(context) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 75 | path := path.Join("/profiles", i.suffix) |
| 76 | object := i.store.Bind(path) |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 77 | found, err := object.Exists(context, transaction) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 78 | if err != nil { |
| 79 | return errOperationFailed |
| 80 | } |
| 81 | if !found { |
| 82 | return errNotFound |
| 83 | } |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 84 | if err := object.Remove(context, transaction); err != nil { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 85 | return errOperationFailed |
| 86 | } |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 87 | if err := transaction.Commit(context); err != nil { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 88 | return errOperationFailed |
| 89 | } |
| 90 | return nil |
| 91 | } |
| 92 | |
| 93 | // PROFILE INTERACE IMPLEMENTATION |
| 94 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 95 | func (i *invoker) lookup(context ipc.ServerContext) (profile.Specification, error) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 96 | empty := profile.Specification{} |
| 97 | path := path.Join("/profiles", i.suffix) |
| 98 | object := i.store.Bind(path) |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 99 | entry, err := object.Get(context, nil) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 100 | if err != nil { |
| 101 | return empty, errNotFound |
| 102 | } |
| 103 | s, ok := entry.Value.(profile.Specification) |
| 104 | if !ok { |
| 105 | return empty, errOperationFailed |
| 106 | } |
| 107 | return s, nil |
| 108 | } |
| 109 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 110 | func (i *invoker) Label(context ipc.ServerContext) (string, error) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 111 | vlog.VI(0).Infof("%v.Label()", i.suffix) |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 112 | s, err := i.lookup(context) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 113 | if err != nil { |
| 114 | return "", err |
| 115 | } |
| 116 | return s.Label, nil |
| 117 | } |
| 118 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 119 | func (i *invoker) Description(context ipc.ServerContext) (string, error) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 120 | vlog.VI(0).Infof("%v.Description()", i.suffix) |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 121 | s, err := i.lookup(context) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 122 | if err != nil { |
| 123 | return "", err |
| 124 | } |
| 125 | return s.Description, nil |
| 126 | } |
| 127 | |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 128 | func (i *invoker) Specification(context ipc.ServerContext) (profile.Specification, error) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 129 | vlog.VI(0).Infof("%v.Specification()", i.suffix) |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 130 | return i.lookup(context) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 131 | } |