diff options
author | Rajamohan Raj <rajamohan.raj@intel.com> | 2020-08-26 00:28:12 +0000 |
---|---|---|
committer | Rajamohan Raj <rajamohan.raj@intel.com> | 2020-09-10 22:12:17 +0000 |
commit | b2c9f4c61d00bf18e7a253c410479f8124640106 (patch) | |
tree | f2c3df18a5cb904f3b57b858cff7d889a36837c6 /src/clm/api/clusterhandler.go | |
parent | 4759e43ff7f29727477b0d928047bf5ca283cef1 (diff) |
Adding validations for all JSON data across modules
Issue-ID: MULTICLOUD-1198
Signed-off-by: Rajamohan Raj <rajamohan.raj@intel.com>
Change-Id: Idc2fbfd6e875bd7b662c7d60925aeb776b3f7bea
Diffstat (limited to 'src/clm/api/clusterhandler.go')
-rw-r--r-- | src/clm/api/clusterhandler.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/clm/api/clusterhandler.go b/src/clm/api/clusterhandler.go index 84dd3230..75fcc561 100644 --- a/src/clm/api/clusterhandler.go +++ b/src/clm/api/clusterhandler.go @@ -28,10 +28,16 @@ import ( "net/textproto" clusterPkg "github.com/onap/multicloud-k8s/src/clm/pkg/cluster" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/validation" "github.com/gorilla/mux" ) +var cpJSONFile string = "json-schemas/metadata.json" +var ckvJSONFile string = "json-schemas/cluster-kv.json" +var clJSONFile string = "json-schemas/cluster-label.json" + + // Used to store backend implementations objects // Also simplifies mocking for unit testing purposes type clusterHandler struct { @@ -55,6 +61,12 @@ func (h clusterHandler) createClusterProviderHandler(w http.ResponseWriter, r *h return } + err, httpError := validation.ValidateJsonSchemaData(cpJSONFile, p) + if err != nil { + http.Error(w, err.Error(), httpError) + return + } + // Name is required. if p.Metadata.Name == "" { http.Error(w, "Missing name in POST request", http.StatusBadRequest) @@ -148,6 +160,12 @@ func (h clusterHandler) createClusterHandler(w http.ResponseWriter, r *http.Requ return } + err, httpError := validation.ValidateJsonSchemaData(cpJSONFile, p) + if err != nil { + http.Error(w, err.Error(), httpError) + return + } + //Read the file section and ignore the header file, _, err := r.FormFile("file") if err != nil { @@ -333,6 +351,12 @@ func (h clusterHandler) createClusterLabelHandler(w http.ResponseWriter, r *http err := json.NewDecoder(r.Body).Decode(&p) + err, httpError := validation.ValidateJsonSchemaData(clJSONFile, p) + if err != nil { + http.Error(w, err.Error(), httpError) + return + } + // LabelName is required. if p.LabelName == "" { http.Error(w, "Missing label name in POST request", http.StatusBadRequest) @@ -413,6 +437,13 @@ func (h clusterHandler) createClusterKvPairsHandler(w http.ResponseWriter, r *ht err := json.NewDecoder(r.Body).Decode(&p) + // Verify JSON Body + err, httpError := validation.ValidateJsonSchemaData(ckvJSONFile, p) + if err != nil { + http.Error(w, err.Error(), httpError) + return + } + // KvPairsName is required. if p.Metadata.Name == "" { http.Error(w, "Missing Key Value pair name in POST request", http.StatusBadRequest) |