diff options
author | Igor D.C <igor.duarte.cardoso@intel.com> | 2020-09-25 00:04:58 +0000 |
---|---|---|
committer | Igor D.C <igor.duarte.cardoso@intel.com> | 2020-09-25 05:28:52 +0000 |
commit | c585d2b091875dbc9575a960f4f42c1f14ec3366 (patch) | |
tree | aba0039b6477dfbc2790cd5efc365a6d9ccb4390 /src/dcm | |
parent | ab8142dfc0f54bfc140e240fe558ac5be8d60ea5 (diff) |
Enhance error handling and HTTP codes in DCM
This improves error handling between DCM and the database resources and
adds/corrects a lot more HTTP return codes in the API, respectively.
Issue-ID: MULTICLOUD-1143
Change-Id: I3abc8025660e042f4c946f8bbfd280e1eb4c9583
Signed-off-by: Igor D.C <igor.duarte.cardoso@intel.com>
Diffstat (limited to 'src/dcm')
-rw-r--r-- | src/dcm/api/clusterHandler.go | 10 | ||||
-rw-r--r-- | src/dcm/api/keyValueHandler.go | 10 | ||||
-rw-r--r-- | src/dcm/api/logicalCloudHandler.go | 20 | ||||
-rw-r--r-- | src/dcm/api/quotaHandler.go | 10 | ||||
-rw-r--r-- | src/dcm/api/userPermissionsHandler.go | 12 | ||||
-rw-r--r-- | src/dcm/pkg/module/cluster.go | 9 | ||||
-rw-r--r-- | src/dcm/pkg/module/keyvalue.go | 4 | ||||
-rw-r--r-- | src/dcm/pkg/module/logicalcloud.go | 10 | ||||
-rw-r--r-- | src/dcm/pkg/module/quota.go | 4 | ||||
-rw-r--r-- | src/dcm/pkg/module/userpermissions.go | 4 |
10 files changed, 74 insertions, 19 deletions
diff --git a/src/dcm/api/clusterHandler.go b/src/dcm/api/clusterHandler.go index f4a3abdc..d0c1e62c 100644 --- a/src/dcm/api/clusterHandler.go +++ b/src/dcm/api/clusterHandler.go @@ -91,6 +91,10 @@ func (h clusterHandler) getHandler(w http.ResponseWriter, r *http.Request) { } else { ret, err = h.client.GetCluster(project, logicalCloud, name) if err != nil { + if err.Error() == "Cluster Reference does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) + return + } http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -131,12 +135,16 @@ func (h clusterHandler) updateHandler(w http.ResponseWriter, r *http.Request) { ret, err := h.client.UpdateCluster(project, logicalCloud, name, v) if err != nil { + if err.Error() == "Cluster Reference does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) + return + } http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) + w.WriteHeader(http.StatusOK) err = json.NewEncoder(w).Encode(ret) if err != nil { http.Error(w, err.Error(), diff --git a/src/dcm/api/keyValueHandler.go b/src/dcm/api/keyValueHandler.go index c67504f2..a4a4f14a 100644 --- a/src/dcm/api/keyValueHandler.go +++ b/src/dcm/api/keyValueHandler.go @@ -90,6 +90,10 @@ func (h keyValueHandler) getHandler(w http.ResponseWriter, r *http.Request) { } else { ret, err = h.client.GetKVPair(project, logicalCloud, name) if err != nil { + if err.Error() == "KV Pair does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) + return + } http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -130,12 +134,16 @@ func (h keyValueHandler) updateHandler(w http.ResponseWriter, r *http.Request) { ret, err := h.client.UpdateKVPair(project, logicalCloud, name, v) if err != nil { + if err.Error() == "KV Pair does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) + return + } http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) + w.WriteHeader(http.StatusOK) err = json.NewEncoder(w).Encode(ret) if err != nil { http.Error(w, err.Error(), diff --git a/src/dcm/api/logicalCloudHandler.go b/src/dcm/api/logicalCloudHandler.go index d9a3e5f5..36ec4e05 100644 --- a/src/dcm/api/logicalCloudHandler.go +++ b/src/dcm/api/logicalCloudHandler.go @@ -91,6 +91,10 @@ func (h logicalCloudHandler) getHandler(w http.ResponseWriter, r *http.Request) } else { ret, err = h.client.Get(project, name) if err != nil { + if err.Error() == "Logical Cloud does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) + return + } http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -129,6 +133,10 @@ func (h logicalCloudHandler) updateHandler(w http.ResponseWriter, r *http.Reques ret, err := h.client.Update(project, name, v) if err != nil { + if err.Error() == "Logical Cloud does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) + return + } http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -150,6 +158,10 @@ func (h logicalCloudHandler) deleteHandler(w http.ResponseWriter, r *http.Reques err := h.client.Delete(project, name) if err != nil { + if err.Error() == "Logical Cloud does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) + return + } http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -165,6 +177,10 @@ func (h logicalCloudHandler) applyHandler(w http.ResponseWriter, r *http.Request // Get logical cloud lc, err := h.client.Get(project, name) if err != nil { + if err.Error() == "Logical Cloud does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) + return + } http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -173,6 +189,10 @@ func (h logicalCloudHandler) applyHandler(w http.ResponseWriter, r *http.Request clusters, err := h.clusterClient.GetAllClusters(project, name) if err != nil { + if err.Error() == "No Cluster References associated" { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } http.Error(w, err.Error(), http.StatusInternalServerError) return } diff --git a/src/dcm/api/quotaHandler.go b/src/dcm/api/quotaHandler.go index deb18e18..fd9b40f8 100644 --- a/src/dcm/api/quotaHandler.go +++ b/src/dcm/api/quotaHandler.go @@ -91,6 +91,10 @@ func (h quotaHandler) getHandler(w http.ResponseWriter, r *http.Request) { } else { ret, err = h.client.GetQuota(project, logicalCloud, name) if err != nil { + if err.Error() == "Cluster Quota does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) + return + } http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -131,12 +135,16 @@ func (h quotaHandler) updateHandler(w http.ResponseWriter, r *http.Request) { ret, err := h.client.UpdateQuota(project, logicalCloud, name, v) if err != nil { + if err.Error() == "Cluster Quota does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) + return + } http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) + w.WriteHeader(http.StatusOK) err = json.NewEncoder(w).Encode(ret) if err != nil { http.Error(w, err.Error(), diff --git a/src/dcm/api/userPermissionsHandler.go b/src/dcm/api/userPermissionsHandler.go index 156c390f..3ac955fa 100644 --- a/src/dcm/api/userPermissionsHandler.go +++ b/src/dcm/api/userPermissionsHandler.go @@ -89,8 +89,12 @@ func (h userPermissionHandler) getHandler(w http.ResponseWriter, r *http.Request return } } else { - ret, err = h.client.GetAllUserPerms(project, logicalCloud) + ret, err = h.client.GetUserPerm(project, logicalCloud, name) if err != nil { + if err.Error() == "User Permission does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) + return + } http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -131,12 +135,16 @@ func (h userPermissionHandler) updateHandler(w http.ResponseWriter, r *http.Requ ret, err := h.client.UpdateUserPerm(project, logicalCloud, name, v) if err != nil { + if err.Error() == "User Permission does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) + return + } http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) + w.WriteHeader(http.StatusOK) err = json.NewEncoder(w).Encode(ret) if err != nil { http.Error(w, err.Error(), diff --git a/src/dcm/pkg/module/cluster.go b/src/dcm/pkg/module/cluster.go index 206d79a6..85b20117 100644 --- a/src/dcm/pkg/module/cluster.go +++ b/src/dcm/pkg/module/cluster.go @@ -133,7 +133,7 @@ func (v *ClusterClient) GetCluster(project, logicalCloud, clusterReference strin return cl, nil } - return Cluster{}, pkgerrors.New("Error getting Cluster") + return Cluster{}, pkgerrors.New("Cluster Reference does not exist") } // GetAll returns all cluster references in the logical cloud @@ -149,6 +149,9 @@ func (v *ClusterClient) GetAllClusters(project, logicalCloud string) ([]Cluster, if err != nil { return []Cluster{}, pkgerrors.Wrap(err, "Get All Cluster references") } + if len(values) == 0 { + return []Cluster{}, pkgerrors.New("No Cluster References associated") + } for _, value := range values { cl := Cluster{} @@ -188,12 +191,12 @@ func (v *ClusterClient) UpdateCluster(project, logicalCloud, clusterReference st //Check for name mismatch in cluster reference if c.MetaData.ClusterReference != clusterReference { - return Cluster{}, pkgerrors.New("Update Error - Cluster reference mismatch") + return Cluster{}, pkgerrors.New("Cluster Reference mismatch") } //Check if this Cluster reference exists _, err := v.GetCluster(project, logicalCloud, clusterReference) if err != nil { - return Cluster{}, pkgerrors.New("Update Error - Cluster reference doesn't exist") + return Cluster{}, pkgerrors.New("Cluster Reference does not exist") } err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) if err != nil { diff --git a/src/dcm/pkg/module/keyvalue.go b/src/dcm/pkg/module/keyvalue.go index 37c74a84..0127a6f4 100644 --- a/src/dcm/pkg/module/keyvalue.go +++ b/src/dcm/pkg/module/keyvalue.go @@ -133,7 +133,7 @@ func (v *KeyValueClient) GetKVPair(project, logicalCloud, kvPairName string) (Ke return kv, nil } - return KeyValue{}, pkgerrors.New("Error getting Key Value") + return KeyValue{}, pkgerrors.New("Key Value does not exist") } // Get All lists all key value pairs @@ -194,7 +194,7 @@ func (v *KeyValueClient) UpdateKVPair(project, logicalCloud, kvPairName string, //Check if this Key Value exists _, err := v.GetKVPair(project, logicalCloud, kvPairName) if err != nil { - return KeyValue{}, pkgerrors.New("Update Error - Key Value Pair doesn't exist") + return KeyValue{}, pkgerrors.New("KV Pair does not exist") } err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) if err != nil { diff --git a/src/dcm/pkg/module/logicalcloud.go b/src/dcm/pkg/module/logicalcloud.go index 51ee387d..7d3c806b 100644 --- a/src/dcm/pkg/module/logicalcloud.go +++ b/src/dcm/pkg/module/logicalcloud.go @@ -145,7 +145,7 @@ func (v *LogicalCloudClient) Get(project, logicalCloudName string) (LogicalCloud } value, err := v.util.DBFind(v.storeName, key, v.tagMeta) if err != nil { - return LogicalCloud{}, pkgerrors.Wrap(err, "Get Logical Cloud") + return LogicalCloud{}, pkgerrors.Wrap(err, "Error getting Logical Cloud") } //value is a byte array @@ -153,12 +153,12 @@ func (v *LogicalCloudClient) Get(project, logicalCloudName string) (LogicalCloud lc := LogicalCloud{} err = v.util.DBUnmarshal(value[0], &lc) if err != nil { - return LogicalCloud{}, pkgerrors.Wrap(err, "Unmarshaling value") + return LogicalCloud{}, pkgerrors.Wrap(err, "Error unmarshaling value") } return lc, nil } - return LogicalCloud{}, pkgerrors.New("Error getting Logical Cloud") + return LogicalCloud{}, pkgerrors.New("Logical Cloud does not exist") } // GetAll returns Logical Clouds in the project @@ -213,12 +213,12 @@ func (v *LogicalCloudClient) Update(project, logicalCloudName string, c LogicalC } // Check for mismatch, logicalCloudName and payload logical cloud name if c.MetaData.LogicalCloudName != logicalCloudName { - return LogicalCloud{}, pkgerrors.New("Update Error - Logical Cloud name mismatch") + return LogicalCloud{}, pkgerrors.New("Logical Cloud name mismatch") } //Check if this Logical Cloud exists _, err := v.Get(project, logicalCloudName) if err != nil { - return LogicalCloud{}, pkgerrors.New("Update Error - Logical Cloud doesn't exist") + return LogicalCloud{}, pkgerrors.New("Logical Cloud does not exist") } err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) if err != nil { diff --git a/src/dcm/pkg/module/quota.go b/src/dcm/pkg/module/quota.go index cbd9c8b7..8e15a794 100644 --- a/src/dcm/pkg/module/quota.go +++ b/src/dcm/pkg/module/quota.go @@ -152,7 +152,7 @@ func (v *QuotaClient) GetQuota(project, logicalCloud, quotaName string) (Quota, return q, nil } - return Quota{}, pkgerrors.New("Error getting Quota") + return Quota{}, pkgerrors.New("Cluster Quota does not exist") } // GetAll returns all cluster quotas in the logical cloud @@ -211,7 +211,7 @@ func (v *QuotaClient) UpdateQuota(project, logicalCloud, quotaName string, c Quo //Check if this Quota exists _, err := v.GetQuota(project, logicalCloud, quotaName) if err != nil { - return Quota{}, pkgerrors.New("Update Error - Quota doesn't exist") + return Quota{}, pkgerrors.New("Cluster Quota does not exist") } err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) if err != nil { diff --git a/src/dcm/pkg/module/userpermissions.go b/src/dcm/pkg/module/userpermissions.go index 2cff712b..4c918f0f 100644 --- a/src/dcm/pkg/module/userpermissions.go +++ b/src/dcm/pkg/module/userpermissions.go @@ -124,7 +124,7 @@ func (v *UserPermissionClient) GetUserPerm(project, logicalCloud, userPermName s return up, nil } - return UserPermission{}, pkgerrors.New("Error getting User Permission") + return UserPermission{}, pkgerrors.New("User Permission does not exist") } // GetAll lists all user permissions @@ -184,7 +184,7 @@ func (v *UserPermissionClient) UpdateUserPerm(project, logicalCloud, userPermNam _, err := v.GetUserPerm(project, logicalCloud, userPermName) if err != nil { return UserPermission{}, pkgerrors.New( - "Update Error - User Permission doesn't exist") + "User Permission does not exist") } err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) if err != nil { |