diff options
Diffstat (limited to 'src/orchestrator/pkg/rtcontext')
-rw-r--r-- | src/orchestrator/pkg/rtcontext/rtcontext.go | 45 | ||||
-rw-r--r-- | src/orchestrator/pkg/rtcontext/rtcontext_test.go | 22 |
2 files changed, 52 insertions, 15 deletions
diff --git a/src/orchestrator/pkg/rtcontext/rtcontext.go b/src/orchestrator/pkg/rtcontext/rtcontext.go index 1835251a..5610ea58 100644 --- a/src/orchestrator/pkg/rtcontext/rtcontext.go +++ b/src/orchestrator/pkg/rtcontext/rtcontext.go @@ -29,13 +29,15 @@ const maxrand = 0x7fffffffffffffff const prefix string = "/context/" type RunTimeContext struct { - cid interface{} + cid interface{} + meta interface{} } type Rtcontext interface { RtcInit() (interface{}, error) RtcLoad(interface{}) (interface{}, error) RtcCreate() (interface{}, error) + RtcAddMeta(meta interface{}) error RtcGet() (interface{}, error) RtcAddLevel(handle interface{}, level string, value string) (interface{}, error) RtcAddResource(handle interface{}, resname string, value interface{}) (interface{}, error) @@ -45,6 +47,7 @@ type Rtcontext interface { RtcGetHandles(handle interface{}) ([]interface{}, error) RtcGetValue(handle interface{}, value interface{}) error RtcUpdateValue(handle interface{}, value interface{}) error + RtcGetMeta() (interface{}, error) } //Intialize context by assiging a new id @@ -76,7 +79,6 @@ func (rtc *RunTimeContext) RtcLoad(id interface{}) (interface{}, error) { return handle, nil } -//Create context using the id and prefix func (rtc *RunTimeContext) RtcCreate() (interface{}, error) { cid := fmt.Sprintf("%v", rtc.cid) if cid == "" { @@ -94,6 +96,26 @@ func (rtc *RunTimeContext) RtcCreate() (interface{}, error) { return rtc.cid, nil } +//RtcAddMeta is used for saving meta data of appContext into ETCD. +func (rtc *RunTimeContext) RtcAddMeta(meta interface{}) error { + cid := fmt.Sprintf("%v", rtc.cid) + if cid == "" { + return pkgerrors.Errorf("Error, context not intialized") + } + if !strings.HasPrefix(cid, prefix) { + return pkgerrors.Errorf("Not a valid run time context prefix") + } + + rtc.meta = meta + k := cid + "meta" + "/" + err := contextdb.Db.Put(k, rtc.meta) + if err != nil { + return pkgerrors.Errorf("Error saving metadata in run time context: %s", err.Error()) + } + + return nil +} + //Get the root handle func (rtc *RunTimeContext) RtcGet() (interface{}, error) { str := fmt.Sprintf("%v", rtc.cid) @@ -113,6 +135,23 @@ func (rtc *RunTimeContext) RtcGet() (interface{}, error) { return rtc.cid, nil } +// RtcGetMeta method fetches the meta data of the rtc object and returns it. +func (rtc *RunTimeContext) RtcGetMeta() (interface{}, error) { + str := fmt.Sprintf("%v", rtc.cid) + if !strings.HasPrefix(str, prefix) { + return nil, pkgerrors.Errorf("Not a valid run time context") + } + + var value interface{} + k := str + "meta" + "/" + err := contextdb.Db.Get(k, &value) + if err != nil { + return nil, pkgerrors.Errorf("Error getting run time context metadata: %s", err.Error()) + } + return value, nil + +} + //Add a new level at a given handle and return the new handle func (rtc *RunTimeContext) RtcAddLevel(handle interface{}, level string, value string) (interface{}, error) { str := fmt.Sprintf("%v", handle) @@ -177,7 +216,6 @@ func (rtc *RunTimeContext) RtcAddInstruction(handle interface{}, level string, i if value == nil { return nil, pkgerrors.Errorf("Not a valid run time context instruction value") } - k := str + level + "/" + "instruction" + "/" + insttype + "/" err := contextdb.Db.Put(k, fmt.Sprintf("%v", value)) if err != nil { @@ -194,7 +232,6 @@ func (rtc *RunTimeContext) RtcDeletePair(handle interface{}) error { if !strings.HasPrefix(str, sid) { return pkgerrors.Errorf("Not a valid run time context handle") } - err := contextdb.Db.Delete(str) if err != nil { return pkgerrors.Errorf("Error deleting run time context pair: %s", err.Error()) diff --git a/src/orchestrator/pkg/rtcontext/rtcontext_test.go b/src/orchestrator/pkg/rtcontext/rtcontext_test.go index eedbeb82..49e83748 100644 --- a/src/orchestrator/pkg/rtcontext/rtcontext_test.go +++ b/src/orchestrator/pkg/rtcontext/rtcontext_test.go @@ -112,7 +112,7 @@ func TestRtcInit(t *testing.T) { } func TestRtcLoad(t *testing.T) { - var rtc = RunTimeContext{""} + var rtc = RunTimeContext{"", ""} testCases := []struct { label string mockContextDb *MockContextDb @@ -147,7 +147,7 @@ func TestRtcLoad(t *testing.T) { } func TestRtcCreate(t *testing.T) { - var rtc = RunTimeContext{"/context/5345674458787728/"} + var rtc = RunTimeContext{"/context/5345674458787728/", ""} testCases := []struct { label string mockContextDb *MockContextDb @@ -179,7 +179,7 @@ func TestRtcCreate(t *testing.T) { } func TestRtcGet(t *testing.T) { - var rtc = RunTimeContext{"/context/5345674458787728/"} + var rtc = RunTimeContext{"/context/5345674458787728/", ""} testCases := []struct { label string mockContextDb *MockContextDb @@ -240,7 +240,7 @@ func TestRtcGet(t *testing.T) { } func TestRtcAddLevel(t *testing.T) { - var rtc = RunTimeContext{"/context/3528435435454354/"} + var rtc = RunTimeContext{"/context/3528435435454354/", ""} testCases := []struct { label string mockContextDb *MockContextDb @@ -304,7 +304,7 @@ func TestRtcAddLevel(t *testing.T) { } func TestRtcAddResource(t *testing.T) { - var rtc = RunTimeContext{"/context/3528435435454354/"} + var rtc = RunTimeContext{"/context/3528435435454354/", ""} testCases := []struct { label string mockContextDb *MockContextDb @@ -368,7 +368,7 @@ func TestRtcAddResource(t *testing.T) { } func TestRtcAddInstruction(t *testing.T) { - var rtc = RunTimeContext{"/context/3528435435454354/"} + var rtc = RunTimeContext{"/context/3528435435454354/", ""} testCases := []struct { label string mockContextDb *MockContextDb @@ -447,7 +447,7 @@ func TestRtcAddInstruction(t *testing.T) { } func TestRtcGetHandles(t *testing.T) { - var rtc = RunTimeContext{"/context/5345674458787728/"} + var rtc = RunTimeContext{"/context/5345674458787728/", ""} testCases := []struct { label string mockContextDb *MockContextDb @@ -490,7 +490,7 @@ func TestRtcGetHandles(t *testing.T) { } func TestRtcGetValue(t *testing.T) { - var rtc = RunTimeContext{"/context/5345674458787728/"} + var rtc = RunTimeContext{"/context/5345674458787728/", ""} testCases := []struct { label string mockContextDb *MockContextDb @@ -534,7 +534,7 @@ func TestRtcGetValue(t *testing.T) { } func TestRtcUpdateValue(t *testing.T) { - var rtc = RunTimeContext{"/context/5345674458787728/"} + var rtc = RunTimeContext{"/context/5345674458787728/", ""} testCases := []struct { label string mockContextDb *MockContextDb @@ -581,7 +581,7 @@ func TestRtcUpdateValue(t *testing.T) { } func TestRtcDeletePair(t *testing.T) { - var rtc = RunTimeContext{"/context/5345674458787728/"} + var rtc = RunTimeContext{"/context/5345674458787728/", ""} testCases := []struct { label string mockContextDb *MockContextDb @@ -621,7 +621,7 @@ func TestRtcDeletePair(t *testing.T) { } func TestRtcDeletePrefix(t *testing.T) { - var rtc = RunTimeContext{"/context/5345674458787728/"} + var rtc = RunTimeContext{"/context/5345674458787728/", ""} testCases := []struct { label string mockContextDb *MockContextDb |