From b2c9f4c61d00bf18e7a253c410479f8124640106 Mon Sep 17 00:00:00 2001 From: Rajamohan Raj Date: Wed, 26 Aug 2020 00:28:12 +0000 Subject: Adding validations for all JSON data across modules Issue-ID: MULTICLOUD-1198 Signed-off-by: Rajamohan Raj Change-Id: Idc2fbfd6e875bd7b662c7d60925aeb776b3f7bea --- src/ovnaction/api/netcontrolintenthandler.go | 13 ++++ src/ovnaction/api/workloadifintenthandler.go | 8 +++ src/ovnaction/api/workloadintenthandler.go | 8 +++ src/ovnaction/json-schemas/metadata.json | 37 +++++++++++ .../json-schemas/network-load-interface.json | 77 ++++++++++++++++++++++ src/ovnaction/json-schemas/network-workload.json | 67 +++++++++++++++++++ 6 files changed, 210 insertions(+) create mode 100644 src/ovnaction/json-schemas/metadata.json create mode 100644 src/ovnaction/json-schemas/network-load-interface.json create mode 100644 src/ovnaction/json-schemas/network-workload.json (limited to 'src/ovnaction') diff --git a/src/ovnaction/api/netcontrolintenthandler.go b/src/ovnaction/api/netcontrolintenthandler.go index fe2109b6..631f13c4 100644 --- a/src/ovnaction/api/netcontrolintenthandler.go +++ b/src/ovnaction/api/netcontrolintenthandler.go @@ -24,10 +24,16 @@ import ( moduleLib "github.com/onap/multicloud-k8s/src/ovnaction/pkg/module" pkgerrors "github.com/pkg/errors" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/validation" + "github.com/gorilla/mux" ) +var netCntIntJSONFile string = "json-schemas/metadata.json" + + + // Used to store backend implementations objects // Also simplifies mocking for unit testing purposes type netcontrolintentHandler struct { @@ -65,6 +71,13 @@ func (h netcontrolintentHandler) createHandler(w http.ResponseWriter, r *http.Re return } + err, httpError := validation.ValidateJsonSchemaData(netCntIntJSONFile, nci) +if err != nil { + http.Error(w, err.Error(), httpError) + return +} + + // Name is required. if nci.Metadata.Name == "" { http.Error(w, "Missing name in POST request", http.StatusBadRequest) diff --git a/src/ovnaction/api/workloadifintenthandler.go b/src/ovnaction/api/workloadifintenthandler.go index cf8f45bf..e7be6317 100644 --- a/src/ovnaction/api/workloadifintenthandler.go +++ b/src/ovnaction/api/workloadifintenthandler.go @@ -29,6 +29,8 @@ import ( "github.com/gorilla/mux" ) +var netIfJSONFile string = "json-schemas/network-load-interface.json" + // Used to store backend implementations objects // Also simplifies mocking for unit testing purposes type workloadifintentHandler struct { @@ -102,6 +104,12 @@ func (h workloadifintentHandler) createHandler(w http.ResponseWriter, r *http.Re return } + err, httpError := validation.ValidateJsonSchemaData(netIfJSONFile, wif) +if err != nil { + http.Error(w, err.Error(), httpError) + return +} + // Name is required. if wif.Metadata.Name == "" { http.Error(w, "Missing name in POST request", http.StatusBadRequest) diff --git a/src/ovnaction/api/workloadintenthandler.go b/src/ovnaction/api/workloadintenthandler.go index cf7ecebc..acf4edbb 100644 --- a/src/ovnaction/api/workloadintenthandler.go +++ b/src/ovnaction/api/workloadintenthandler.go @@ -29,6 +29,8 @@ import ( "github.com/gorilla/mux" ) +var workloadIntJSONFile string = "json-schemas/network-workload.json" + // Used to store backend implementations objects // Also simplifies mocking for unit testing purposes type workloadintentHandler struct { @@ -82,6 +84,12 @@ func (h workloadintentHandler) createHandler(w http.ResponseWriter, r *http.Requ return } + err, httpError := validation.ValidateJsonSchemaData(workloadIntJSONFile, wi) +if err != nil { + http.Error(w, err.Error(), httpError) + return +} + // Name is required. if wi.Metadata.Name == "" { http.Error(w, "Missing name in POST request", http.StatusBadRequest) diff --git a/src/ovnaction/json-schemas/metadata.json b/src/ovnaction/json-schemas/metadata.json new file mode 100644 index 00000000..960545ee --- /dev/null +++ b/src/ovnaction/json-schemas/metadata.json @@ -0,0 +1,37 @@ + +{ + "$schema": "http://json-schema.org/schema#", + "type": "object", + "properties": { + "metadata": { + "required": ["name"], + "properties": { + "userData2": { + "description": "User relevant data for the resource", + "type": "string", + "example": "Some more data", + "maxLength": 512 + }, + "userData1": { + "description": "User relevant data for the resource", + "type": "string", + "example": "Some data", + "maxLength": 512 + }, + "name": { + "description": "Name of the resource", + "type": "string", + "example": "ResName", + "maxLength": 128, + "pattern": "[-_0-9a-zA-Z]+$" + }, + "description": { + "description": "Description for the resource", + "type": "string", + "example": "Resource description", + "maxLength": 1024 + } + } + } + } + } \ No newline at end of file diff --git a/src/ovnaction/json-schemas/network-load-interface.json b/src/ovnaction/json-schemas/network-load-interface.json new file mode 100644 index 00000000..896f4f2b --- /dev/null +++ b/src/ovnaction/json-schemas/network-load-interface.json @@ -0,0 +1,77 @@ +{ + "$schema": "http://json-schema.org/schema#", + "type": "object", + "properties": { + "spec": { + "required": [ + "interface", + "name" + ], + "type": "object", + "properties": { + "interface": { + "description": "interface Name", + "type": "string", + "example": "eth0", + "maxLength": 128, + "pattern": "[-_0-9a-zA-Z]+$" + }, + "macAddress": { + "description": "Name of the network", + "type": "string", + "example": "x.x.x.x", + "maxLength": 128 + }, + "ipAddress": { + "description": "Name of the network", + "type": "string", + "example": "0.0.0.0", + "maxLength": 128 + }, + "name": { + "description": "Name of the network", + "type": "string", + "example": "provider-1", + "maxLength": 128, + "pattern": "[-_0-9a-zA-Z]+$" + }, + "defaultGateway": { + "description": "Is this interface default gateway", + "type": "string", + "example": "false", + "maxLength": 128 + } + } + }, + "metadata": { + "required": ["name"], + "properties": { + "userData2": { + "description": "User relevant data for the resource", + "type": "string", + "example": "Some more data", + "maxLength": 512 + }, + "userData1": { + "description": "User relevant data for the resource", + "type": "string", + "example": "Some data", + "maxLength": 512 + }, + "name": { + "description": "Name of the resource", + "type": "string", + "example": "ResName", + "maxLength": 128, + "pattern": "[-_0-9a-zA-Z]+$" + }, + "description": { + "description": "Description for the resource", + "type": "string", + "example": "Resource description", + "maxLength": 1024 + } + } + } + } + } \ No newline at end of file diff --git a/src/ovnaction/json-schemas/network-workload.json b/src/ovnaction/json-schemas/network-workload.json new file mode 100644 index 00000000..c5dc14cb --- /dev/null +++ b/src/ovnaction/json-schemas/network-workload.json @@ -0,0 +1,67 @@ +{ + "$schema": "http://json-schema.org/schema#", + "type": "object", + "properties": { + "spec": { + "type": "object", + "description": "Newtwork Workload Intent", + "properties": { + "spec": { + "type": "object", + "properties": { + "workload-resource": { + "description": "Name of the workload", + "type": "string", + "example": "firewall", + "maxLength": 254, + "pattern": "[-_0-9a-zA-Z]+$" + }, + "type": { + "description": "Type of the workload", + "type": "string", + "example": "deployment", + "maxLength": 128 + }, + "application-name": { + "description": "Application Name", + "type": "string", + "example": "Application1", + "maxLength": 128, + "pattern": "[-_0-9a-zA-Z]+$" + } + } + }, + "metadata": { + "required": ["name"], + "properties": { + "userData2": { + "description": "User relevant data for the resource", + "type": "string", + "example": "Some more data", + "maxLength": 512 + }, + "userData1": { + "description": "User relevant data for the resource", + "type": "string", + "example": "Some data", + "maxLength": 512 + }, + "name": { + "description": "Name of the resource", + "type": "string", + "example": "ResName", + "maxLength": 128, + "pattern": "[-_0-9a-zA-Z]+$" + }, + "description": { + "description": "Description for the resource", + "type": "string", + "example": "Resource description", + "maxLength": 1024 + } + } + } + } + } + } +} \ No newline at end of file -- cgit 1.2.3-korg