From 5207bd099a84832a5d7c3333bf540fa8481ce78a Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Thu, 23 May 2019 13:50:50 -0700 Subject: Update broker responses to match spec and SO The broker responses need to match the spec and what SO expects as responses. CREATE_COMPLETE instead of CREATED in both POST and GET DELETE now returns a response body GET by name is now supported Name is an alias for vf_module_id which is expected to be provided as an attribute in sdnc_directives in the original POST request Issue-ID: MULTICLOUD-645 Change-Id: Ifeca755a07298d0a858cbe9e80f9ce654d6d21b8 Signed-off-by: Kiran Kamineni --- src/k8splugin/internal/app/config_backend.go | 2 +- src/k8splugin/internal/app/instance.go | 66 +++++++++++++++++++--------- src/k8splugin/internal/app/instance_test.go | 22 +++++++--- 3 files changed, 63 insertions(+), 27 deletions(-) (limited to 'src/k8splugin/internal/app') diff --git a/src/k8splugin/internal/app/config_backend.go b/src/k8splugin/internal/app/config_backend.go index 81696b3a..9894b6ac 100644 --- a/src/k8splugin/internal/app/config_backend.go +++ b/src/k8splugin/internal/app/config_backend.go @@ -343,7 +343,7 @@ func scheduleResources(c chan configResourceList) { data := <-c //TODO: ADD Check to see if Application running ic := NewInstanceClient() - resp, err := ic.Find(data.profile.RBName, data.profile.RBVersion, data.profile.ProfileName) + resp, err := ic.Find(data.profile.RBName, data.profile.RBVersion, data.profile.ProfileName, nil) if err != nil || len(resp) == 0 { log.Println("Error finding a running instance. Retrying later...") time.Sleep(time.Second * 10) diff --git a/src/k8splugin/internal/app/instance.go b/src/k8splugin/internal/app/instance.go index fe9f3857..19841c01 100644 --- a/src/k8splugin/internal/app/instance.go +++ b/src/k8splugin/internal/app/instance.go @@ -50,7 +50,7 @@ type InstanceResponse struct { type InstanceManager interface { Create(i InstanceRequest) (InstanceResponse, error) Get(id string) (InstanceResponse, error) - Find(rbName string, ver string, profile string) ([]InstanceResponse, error) + Find(rbName string, ver string, profile string, labelKeys map[string]string) ([]InstanceResponse, error) Delete(id string) error } @@ -131,13 +131,8 @@ func (v *InstanceClient) Create(i InstanceRequest) (InstanceResponse, error) { //Compose the return response resp := InstanceResponse{ - ID: id, - Request: InstanceRequest{ - RBName: i.RBName, - RBVersion: i.RBVersion, - ProfileName: i.ProfileName, - CloudRegion: i.CloudRegion, - }, + ID: id, + Request: i, Namespace: profile.Namespace, Resources: createdResources, } @@ -179,9 +174,11 @@ func (v *InstanceClient) Get(id string) (InstanceResponse, error) { // Find returns the instances that match the given criteria // If version is empty, it will return all instances for a given rbName // If profile is empty, it will return all instances for a given rbName+version -func (v *InstanceClient) Find(rbName string, version string, profile string) ([]InstanceResponse, error) { - if rbName == "" { - return []InstanceResponse{}, pkgerrors.New("rbName is required and cannot be empty") +// If labelKeys are provided, the results are filtered based on that. +// It is an AND operation for labelkeys. +func (v *InstanceClient) Find(rbName string, version string, profile string, labelKeys map[string]string) ([]InstanceResponse, error) { + if rbName == "" && len(labelKeys) == 0 { + return []InstanceResponse{}, pkgerrors.New("rbName or labelkeys is required and cannot be empty") } values, err := db.DBconn.ReadAll(v.storeName, v.tagInst) @@ -191,6 +188,7 @@ func (v *InstanceClient) Find(rbName string, version string, profile string) ([] response := []InstanceResponse{} //values is a map[string][]byte +InstanceResponseLoop: for _, value := range values { resp := InstanceResponse{} db.DBconn.Unmarshal(value, &resp) @@ -198,19 +196,45 @@ func (v *InstanceClient) Find(rbName string, version string, profile string) ([] return []InstanceResponse{}, pkgerrors.Wrap(err, "Unmarshaling Instance Value") } - if resp.Request.RBName == rbName { + // Filter by labels provided + if len(labelKeys) != 0 { + for lkey, lvalue := range labelKeys { + //Check if label key exists and get its value + if val, ok := resp.Request.Labels[lkey]; ok { + if lvalue != val { + continue InstanceResponseLoop + } + } else { + continue InstanceResponseLoop + } + } + } + + if rbName != "" { + if resp.Request.RBName == rbName { - //Check if a version is provided and if it matches - if version != "" { - if resp.Request.RBVersion == version { - //Check if a profilename matches or if it is not provided - if profile == "" || resp.Request.ProfileName == profile { - response = append(response, resp) + //Check if a version is provided and if it matches + if version != "" { + if resp.Request.RBVersion == version { + //Check if a profilename matches or if it is not provided + if profile == "" || resp.Request.ProfileName == profile { + response = append(response, resp) + } } + } else { + //Append all versions as version is not provided + response = append(response, resp) } - } else { - //Append all versions as version is not provided - response = append(response, resp) + } + } else { + response = append(response, resp) + } + } + + //filter the list by labelKeys now + for _, value := range response { + for _, label := range labelKeys { + if _, ok := value.Request.Labels[label]; ok { } } } diff --git a/src/k8splugin/internal/app/instance_test.go b/src/k8splugin/internal/app/instance_test.go index 87824d74..ea377482 100644 --- a/src/k8splugin/internal/app/instance_test.go +++ b/src/k8splugin/internal/app/instance_test.go @@ -339,7 +339,10 @@ func TestInstanceFind(t *testing.T) { "profile-name":"profile1", "rb-name":"test-rbdef", "rb-version":"v1", - "cloud-region":"region1" + "cloud-region":"region1", + "labels":{ + "vf_module_id": "test-vf-module-id" + } }, "namespace":"testnamespace", "resources": [ @@ -439,6 +442,9 @@ func TestInstanceFind(t *testing.T) { RBVersion: "v1", ProfileName: "profile1", CloudRegion: "region1", + Labels: map[string]string{ + "vf_module_id": "test-vf-module-id", + }, }, Namespace: "testnamespace", Resources: []helm.KubernetesResource{ @@ -513,7 +519,7 @@ func TestInstanceFind(t *testing.T) { } ic := NewInstanceClient() name := "test-rbdef" - data, err := ic.Find(name, "", "") + data, err := ic.Find(name, "", "", nil) if err != nil { t.Fatalf("TestInstanceFind returned an error (%s)", err) } @@ -548,6 +554,9 @@ func TestInstanceFind(t *testing.T) { RBVersion: "v1", ProfileName: "profile1", CloudRegion: "region1", + Labels: map[string]string{ + "vf_module_id": "test-vf-module-id", + }, }, Namespace: "testnamespace", @@ -598,7 +607,7 @@ func TestInstanceFind(t *testing.T) { } ic := NewInstanceClient() name := "test-rbdef" - data, err := ic.Find(name, "v1", "") + data, err := ic.Find(name, "v1", "", nil) if err != nil { t.Fatalf("TestInstanceFind returned an error (%s)", err) } @@ -633,6 +642,9 @@ func TestInstanceFind(t *testing.T) { RBVersion: "v1", ProfileName: "profile1", CloudRegion: "region1", + Labels: map[string]string{ + "vf_module_id": "test-vf-module-id", + }, }, Namespace: "testnamespace", @@ -656,7 +668,7 @@ func TestInstanceFind(t *testing.T) { } ic := NewInstanceClient() name := "test-rbdef" - data, err := ic.Find(name, "v1", "profile1") + data, err := ic.Find(name, "v1", "profile1", nil) if err != nil { t.Fatalf("TestInstanceFind returned an error (%s)", err) } @@ -715,7 +727,7 @@ func TestInstanceFind(t *testing.T) { ic := NewInstanceClient() name := "non-existing" - resp, _ := ic.Find(name, "", "") + resp, _ := ic.Find(name, "", "", nil) if len(resp) != 0 { t.Fatalf("Expected 0 responses, but got %d", len(resp)) } -- cgit 1.2.3-korg