diff --git a/src/api/rest/docs/docs.go b/src/api/rest/docs/docs.go index 537088718..64e39fae0 100644 --- a/src/api/rest/docs/docs.go +++ b/src/api/rest/docs/docs.go @@ -2455,6 +2455,288 @@ const docTemplate = `{ } } }, + "/ns/{nsId}/nlb": { + "get": { + "description": "List all NLBs or NLBs' ID", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "[Infra resource] NLB management" + ], + "summary": "List all NLBs or NLBs' ID", + "parameters": [ + { + "type": "string", + "default": "ns01", + "description": "Namespace ID", + "name": "nsId", + "in": "path", + "required": true + }, + { + "enum": [ + "id" + ], + "type": "string", + "description": "Option", + "name": "option", + "in": "query" + }, + { + "type": "string", + "description": "Field key for filtering (ex: cspNLBName)", + "name": "filterKey", + "in": "query" + }, + { + "type": "string", + "description": "Field value for filtering (ex: ns01-alibaba-ap-northeast-1-vpc)", + "name": "filterVal", + "in": "query" + } + ], + "responses": { + "200": { + "description": "Different return structures by the given option param", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/mcis.JSONResult" + }, + { + "type": "object", + "properties": { + "[DEFAULT]": { + "$ref": "#/definitions/mcis.RestGetAllNLBResponse" + }, + "[ID]": { + "$ref": "#/definitions/common.IdList" + } + } + } + ] + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + } + } + }, + "post": { + "description": "Create NLB", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "[Infra resource] NLB management" + ], + "summary": "Create NLB", + "parameters": [ + { + "type": "string", + "default": "ns01", + "description": "Namespace ID", + "name": "nsId", + "in": "path", + "required": true + }, + { + "enum": [ + "register" + ], + "type": "string", + "description": "Option: [required params for register] connectionName, name, cspNLBId", + "name": "option", + "in": "query" + }, + { + "description": "Details of the NLB object", + "name": "nlbReq", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/mcis.TbNLBReq" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/mcis.TbNLBInfo" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + } + } + }, + "delete": { + "description": "Delete all NLBs", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "[Infra resource] NLB management" + ], + "summary": "Delete all NLBs", + "parameters": [ + { + "type": "string", + "default": "ns01", + "description": "Namespace ID", + "name": "nsId", + "in": "path", + "required": true + }, + { + "type": "string", + "default": "", + "description": "Delete resources containing matched ID-substring only", + "name": "match", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/common.IdList" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + } + } + } + }, + "/ns/{nsId}/nlb/{nlbId}": { + "get": { + "description": "Get NLB", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "[Infra resource] NLB management" + ], + "summary": "Get NLB", + "parameters": [ + { + "type": "string", + "default": "ns01", + "description": "Namespace ID", + "name": "nsId", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "NLB ID", + "name": "nlbId", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/mcis.TbNLBInfo" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + } + } + }, + "delete": { + "description": "Delete NLB", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "[Infra resource] NLB management" + ], + "summary": "Delete NLB", + "parameters": [ + { + "type": "string", + "default": "ns01", + "description": "Namespace ID", + "name": "nsId", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "NLB ID", + "name": "nlbId", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + } + } + } + }, "/ns/{nsId}/policy/mcis": { "get": { "description": "List all MCIS policies", @@ -6821,6 +7103,97 @@ const docTemplate = `{ } } }, + "mcis.NLBHealthCheckerInfo": { + "type": "object", + "properties": { + "cspID": { + "description": "Optional, May be Used by Driver.", + "type": "string" + }, + "interval": { + "description": "secs, Interval time between health checks.", + "type": "integer" + }, + "keyValueList": { + "type": "array", + "items": { + "$ref": "#/definitions/common.KeyValue" + } + }, + "port": { + "description": "Listener Port or 1-65535", + "type": "string" + }, + "protocol": { + "description": "TCP|HTTP|HTTPS", + "type": "string" + }, + "threshold": { + "description": "num, The number of continuous health checks to change the VM status.", + "type": "integer" + }, + "timeout": { + "description": "secs, Waiting time to decide an unhealthy VM when no response.", + "type": "integer" + } + } + }, + "mcis.NLBHealthCheckerReq": { + "type": "object", + "properties": { + "interval": { + "description": "secs, Interval time between health checks.", + "type": "string" + }, + "port": { + "description": "Listener Port or 1-65535", + "type": "string" + }, + "protocol": { + "description": "TCP|HTTP|HTTPS", + "type": "string" + }, + "threshold": { + "description": "num, The number of continuous health checks to change the VM status.", + "type": "string" + }, + "timeout": { + "description": "secs, Waiting time to decide an unhealthy VM when no response.", + "type": "string" + } + } + }, + "mcis.NLBListenerInfo": { + "type": "object", + "properties": { + "cspID": { + "description": "Optional, May be Used by Driver.", + "type": "string" + }, + "dnsname": { + "description": "Optional, Auto Generated and attached", + "type": "string" + }, + "ip": { + "description": "Auto Generated and attached", + "type": "string" + }, + "keyValueList": { + "type": "array", + "items": { + "$ref": "#/definitions/common.KeyValue" + } + }, + "port": { + "description": "1-65535", + "type": "string" + }, + "protocol": { + "description": "TCP|UDP", + "type": "string" + } + } + }, "mcis.NetworkReq": { "type": "object", "properties": { @@ -7042,6 +7415,17 @@ const docTemplate = `{ } } }, + "mcis.RestGetAllNLBResponse": { + "type": "object", + "properties": { + "nlb": { + "type": "array", + "items": { + "$ref": "#/definitions/mcis.TbNLBInfo" + } + } + } + }, "mcis.RestGetBenchmarkRequest": { "type": "object", "properties": { @@ -7254,6 +7638,38 @@ const docTemplate = `{ } } }, + "mcis.TBNLBVMGroup": { + "type": "object", + "properties": { + "cspID": { + "description": "Optional, May be Used by Driver.", + "type": "string" + }, + "keyValueList": { + "type": "array", + "items": { + "$ref": "#/definitions/common.KeyValue" + } + }, + "mcis": { + "type": "string" + }, + "port": { + "description": "Listener Port or 1-65535", + "type": "string" + }, + "protocol": { + "description": "TCP|HTTP|HTTPS", + "type": "string" + }, + "vms": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, "mcis.TbMcisDynamicReq": { "type": "object", "required": [ @@ -7408,6 +7824,126 @@ const docTemplate = `{ } } }, + "mcis.TbNLBInfo": { + "type": "object", + "properties": { + "associatedObjectList": { + "type": "array", + "items": { + "type": "string" + } + }, + "connectionName": { + "type": "string" + }, + "createdTime": { + "type": "string" + }, + "cspNLBId": { + "type": "string" + }, + "cspNLBName": { + "type": "string" + }, + "description": { + "type": "string" + }, + "healthChecker": { + "$ref": "#/definitions/mcis.NLBHealthCheckerInfo" + }, + "id": { + "type": "string" + }, + "isAutoGenerated": { + "type": "boolean" + }, + "keyValueList": { + "type": "array", + "items": { + "$ref": "#/definitions/common.KeyValue" + } + }, + "listener": { + "$ref": "#/definitions/mcis.NLBListenerInfo" + }, + "name": { + "type": "string" + }, + "scope": { + "description": "REGION(V) | GLOBAL", + "type": "string" + }, + "status": { + "type": "string" + }, + "systemLabel": { + "description": "SystemLabel is for describing the MCIR in a keyword (any string can be used) for special System purpose", + "type": "string", + "example": "Managed by CB-Tumblebug" + }, + "type": { + "description": "PUBLIC(V) | INTERNAL", + "type": "string" + }, + "vmGroup": { + "$ref": "#/definitions/mcis.TBNLBVMGroup" + } + } + }, + "mcis.TbNLBReq": { + "type": "object", + "required": [ + "connectionName", + "healthChecker", + "listener", + "name", + "scope", + "type", + "vNetId" + ], + "properties": { + "connectionName": { + "type": "string" + }, + "cspNLBId": { + "type": "string" + }, + "description": { + "type": "string" + }, + "healthChecker": { + "$ref": "#/definitions/mcis.NLBHealthCheckerReq" + }, + "listener": { + "$ref": "#/definitions/mcis.NLBListenerInfo" + }, + "name": { + "type": "string" + }, + "scope": { + "description": "REGION(V) | GLOBAL", + "type": "string", + "enum": [ + "REGION", + "GLOBAL" + ] + }, + "type": { + "description": "PUBLIC(V) | INTERNAL", + "type": "string", + "enum": [ + "PUBLIC", + "INTERNAL" + ] + }, + "vNetId": { + "type": "string" + }, + "vmGroup": { + "$ref": "#/definitions/mcis.TBNLBVMGroup" + } + } + }, "mcis.TbVmDynamicReq": { "type": "object", "required": [ diff --git a/src/api/rest/docs/swagger.json b/src/api/rest/docs/swagger.json index 6568d4e42..ed89ef6ff 100644 --- a/src/api/rest/docs/swagger.json +++ b/src/api/rest/docs/swagger.json @@ -2447,6 +2447,288 @@ } } }, + "/ns/{nsId}/nlb": { + "get": { + "description": "List all NLBs or NLBs' ID", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "[Infra resource] NLB management" + ], + "summary": "List all NLBs or NLBs' ID", + "parameters": [ + { + "type": "string", + "default": "ns01", + "description": "Namespace ID", + "name": "nsId", + "in": "path", + "required": true + }, + { + "enum": [ + "id" + ], + "type": "string", + "description": "Option", + "name": "option", + "in": "query" + }, + { + "type": "string", + "description": "Field key for filtering (ex: cspNLBName)", + "name": "filterKey", + "in": "query" + }, + { + "type": "string", + "description": "Field value for filtering (ex: ns01-alibaba-ap-northeast-1-vpc)", + "name": "filterVal", + "in": "query" + } + ], + "responses": { + "200": { + "description": "Different return structures by the given option param", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/mcis.JSONResult" + }, + { + "type": "object", + "properties": { + "[DEFAULT]": { + "$ref": "#/definitions/mcis.RestGetAllNLBResponse" + }, + "[ID]": { + "$ref": "#/definitions/common.IdList" + } + } + } + ] + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + } + } + }, + "post": { + "description": "Create NLB", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "[Infra resource] NLB management" + ], + "summary": "Create NLB", + "parameters": [ + { + "type": "string", + "default": "ns01", + "description": "Namespace ID", + "name": "nsId", + "in": "path", + "required": true + }, + { + "enum": [ + "register" + ], + "type": "string", + "description": "Option: [required params for register] connectionName, name, cspNLBId", + "name": "option", + "in": "query" + }, + { + "description": "Details of the NLB object", + "name": "nlbReq", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/mcis.TbNLBReq" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/mcis.TbNLBInfo" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + } + } + }, + "delete": { + "description": "Delete all NLBs", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "[Infra resource] NLB management" + ], + "summary": "Delete all NLBs", + "parameters": [ + { + "type": "string", + "default": "ns01", + "description": "Namespace ID", + "name": "nsId", + "in": "path", + "required": true + }, + { + "type": "string", + "default": "", + "description": "Delete resources containing matched ID-substring only", + "name": "match", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/common.IdList" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + } + } + } + }, + "/ns/{nsId}/nlb/{nlbId}": { + "get": { + "description": "Get NLB", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "[Infra resource] NLB management" + ], + "summary": "Get NLB", + "parameters": [ + { + "type": "string", + "default": "ns01", + "description": "Namespace ID", + "name": "nsId", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "NLB ID", + "name": "nlbId", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/mcis.TbNLBInfo" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + } + } + }, + "delete": { + "description": "Delete NLB", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "[Infra resource] NLB management" + ], + "summary": "Delete NLB", + "parameters": [ + { + "type": "string", + "default": "ns01", + "description": "Namespace ID", + "name": "nsId", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "NLB ID", + "name": "nlbId", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/common.SimpleMsg" + } + } + } + } + }, "/ns/{nsId}/policy/mcis": { "get": { "description": "List all MCIS policies", @@ -6813,6 +7095,97 @@ } } }, + "mcis.NLBHealthCheckerInfo": { + "type": "object", + "properties": { + "cspID": { + "description": "Optional, May be Used by Driver.", + "type": "string" + }, + "interval": { + "description": "secs, Interval time between health checks.", + "type": "integer" + }, + "keyValueList": { + "type": "array", + "items": { + "$ref": "#/definitions/common.KeyValue" + } + }, + "port": { + "description": "Listener Port or 1-65535", + "type": "string" + }, + "protocol": { + "description": "TCP|HTTP|HTTPS", + "type": "string" + }, + "threshold": { + "description": "num, The number of continuous health checks to change the VM status.", + "type": "integer" + }, + "timeout": { + "description": "secs, Waiting time to decide an unhealthy VM when no response.", + "type": "integer" + } + } + }, + "mcis.NLBHealthCheckerReq": { + "type": "object", + "properties": { + "interval": { + "description": "secs, Interval time between health checks.", + "type": "string" + }, + "port": { + "description": "Listener Port or 1-65535", + "type": "string" + }, + "protocol": { + "description": "TCP|HTTP|HTTPS", + "type": "string" + }, + "threshold": { + "description": "num, The number of continuous health checks to change the VM status.", + "type": "string" + }, + "timeout": { + "description": "secs, Waiting time to decide an unhealthy VM when no response.", + "type": "string" + } + } + }, + "mcis.NLBListenerInfo": { + "type": "object", + "properties": { + "cspID": { + "description": "Optional, May be Used by Driver.", + "type": "string" + }, + "dnsname": { + "description": "Optional, Auto Generated and attached", + "type": "string" + }, + "ip": { + "description": "Auto Generated and attached", + "type": "string" + }, + "keyValueList": { + "type": "array", + "items": { + "$ref": "#/definitions/common.KeyValue" + } + }, + "port": { + "description": "1-65535", + "type": "string" + }, + "protocol": { + "description": "TCP|UDP", + "type": "string" + } + } + }, "mcis.NetworkReq": { "type": "object", "properties": { @@ -7034,6 +7407,17 @@ } } }, + "mcis.RestGetAllNLBResponse": { + "type": "object", + "properties": { + "nlb": { + "type": "array", + "items": { + "$ref": "#/definitions/mcis.TbNLBInfo" + } + } + } + }, "mcis.RestGetBenchmarkRequest": { "type": "object", "properties": { @@ -7246,6 +7630,38 @@ } } }, + "mcis.TBNLBVMGroup": { + "type": "object", + "properties": { + "cspID": { + "description": "Optional, May be Used by Driver.", + "type": "string" + }, + "keyValueList": { + "type": "array", + "items": { + "$ref": "#/definitions/common.KeyValue" + } + }, + "mcis": { + "type": "string" + }, + "port": { + "description": "Listener Port or 1-65535", + "type": "string" + }, + "protocol": { + "description": "TCP|HTTP|HTTPS", + "type": "string" + }, + "vms": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, "mcis.TbMcisDynamicReq": { "type": "object", "required": [ @@ -7400,6 +7816,126 @@ } } }, + "mcis.TbNLBInfo": { + "type": "object", + "properties": { + "associatedObjectList": { + "type": "array", + "items": { + "type": "string" + } + }, + "connectionName": { + "type": "string" + }, + "createdTime": { + "type": "string" + }, + "cspNLBId": { + "type": "string" + }, + "cspNLBName": { + "type": "string" + }, + "description": { + "type": "string" + }, + "healthChecker": { + "$ref": "#/definitions/mcis.NLBHealthCheckerInfo" + }, + "id": { + "type": "string" + }, + "isAutoGenerated": { + "type": "boolean" + }, + "keyValueList": { + "type": "array", + "items": { + "$ref": "#/definitions/common.KeyValue" + } + }, + "listener": { + "$ref": "#/definitions/mcis.NLBListenerInfo" + }, + "name": { + "type": "string" + }, + "scope": { + "description": "REGION(V) | GLOBAL", + "type": "string" + }, + "status": { + "type": "string" + }, + "systemLabel": { + "description": "SystemLabel is for describing the MCIR in a keyword (any string can be used) for special System purpose", + "type": "string", + "example": "Managed by CB-Tumblebug" + }, + "type": { + "description": "PUBLIC(V) | INTERNAL", + "type": "string" + }, + "vmGroup": { + "$ref": "#/definitions/mcis.TBNLBVMGroup" + } + } + }, + "mcis.TbNLBReq": { + "type": "object", + "required": [ + "connectionName", + "healthChecker", + "listener", + "name", + "scope", + "type", + "vNetId" + ], + "properties": { + "connectionName": { + "type": "string" + }, + "cspNLBId": { + "type": "string" + }, + "description": { + "type": "string" + }, + "healthChecker": { + "$ref": "#/definitions/mcis.NLBHealthCheckerReq" + }, + "listener": { + "$ref": "#/definitions/mcis.NLBListenerInfo" + }, + "name": { + "type": "string" + }, + "scope": { + "description": "REGION(V) | GLOBAL", + "type": "string", + "enum": [ + "REGION", + "GLOBAL" + ] + }, + "type": { + "description": "PUBLIC(V) | INTERNAL", + "type": "string", + "enum": [ + "PUBLIC", + "INTERNAL" + ] + }, + "vNetId": { + "type": "string" + }, + "vmGroup": { + "$ref": "#/definitions/mcis.TBNLBVMGroup" + } + } + }, "mcis.TbVmDynamicReq": { "type": "object", "required": [ diff --git a/src/api/rest/docs/swagger.yaml b/src/api/rest/docs/swagger.yaml index 2f59b2e80..6b1e2c681 100644 --- a/src/api/rest/docs/swagger.yaml +++ b/src/api/rest/docs/swagger.yaml @@ -1091,6 +1091,73 @@ definitions: nsId: type: string type: object + mcis.NLBHealthCheckerInfo: + properties: + cspID: + description: Optional, May be Used by Driver. + type: string + interval: + description: secs, Interval time between health checks. + type: integer + keyValueList: + items: + $ref: '#/definitions/common.KeyValue' + type: array + port: + description: Listener Port or 1-65535 + type: string + protocol: + description: TCP|HTTP|HTTPS + type: string + threshold: + description: num, The number of continuous health checks to change the VM + status. + type: integer + timeout: + description: secs, Waiting time to decide an unhealthy VM when no response. + type: integer + type: object + mcis.NLBHealthCheckerReq: + properties: + interval: + description: secs, Interval time between health checks. + type: string + port: + description: Listener Port or 1-65535 + type: string + protocol: + description: TCP|HTTP|HTTPS + type: string + threshold: + description: num, The number of continuous health checks to change the VM + status. + type: string + timeout: + description: secs, Waiting time to decide an unhealthy VM when no response. + type: string + type: object + mcis.NLBListenerInfo: + properties: + cspID: + description: Optional, May be Used by Driver. + type: string + dnsname: + description: Optional, Auto Generated and attached + type: string + ip: + description: Auto Generated and attached + type: string + keyValueList: + items: + $ref: '#/definitions/common.KeyValue' + type: array + port: + description: 1-65535 + type: string + protocol: + description: TCP|UDP + type: string + type: object mcis.NetworkReq: properties: etcdEndpoints: @@ -1242,6 +1309,13 @@ definitions: $ref: '#/definitions/mcis.McisStatusInfo' type: array type: object + mcis.RestGetAllNLBResponse: + properties: + nlb: + items: + $ref: '#/definitions/mcis.TbNLBInfo' + type: array + type: object mcis.RestGetBenchmarkRequest: properties: host: @@ -1390,6 +1464,28 @@ definitions: description: CountUndefined is for counting Undefined type: integer type: object + mcis.TBNLBVMGroup: + properties: + cspID: + description: Optional, May be Used by Driver. + type: string + keyValueList: + items: + $ref: '#/definitions/common.KeyValue' + type: array + mcis: + type: string + port: + description: Listener Port or 1-65535 + type: string + protocol: + description: TCP|HTTP|HTTPS + type: string + vms: + items: + type: string + type: array + type: object mcis.TbMcisDynamicReq: properties: description: @@ -1513,6 +1609,91 @@ definitions: - name - vm type: object + mcis.TbNLBInfo: + properties: + associatedObjectList: + items: + type: string + type: array + connectionName: + type: string + createdTime: + type: string + cspNLBId: + type: string + cspNLBName: + type: string + description: + type: string + healthChecker: + $ref: '#/definitions/mcis.NLBHealthCheckerInfo' + id: + type: string + isAutoGenerated: + type: boolean + keyValueList: + items: + $ref: '#/definitions/common.KeyValue' + type: array + listener: + $ref: '#/definitions/mcis.NLBListenerInfo' + name: + type: string + scope: + description: REGION(V) | GLOBAL + type: string + status: + type: string + systemLabel: + description: SystemLabel is for describing the MCIR in a keyword (any string + can be used) for special System purpose + example: Managed by CB-Tumblebug + type: string + type: + description: PUBLIC(V) | INTERNAL + type: string + vmGroup: + $ref: '#/definitions/mcis.TBNLBVMGroup' + type: object + mcis.TbNLBReq: + properties: + connectionName: + type: string + cspNLBId: + type: string + description: + type: string + healthChecker: + $ref: '#/definitions/mcis.NLBHealthCheckerReq' + listener: + $ref: '#/definitions/mcis.NLBListenerInfo' + name: + type: string + scope: + description: REGION(V) | GLOBAL + enum: + - REGION + - GLOBAL + type: string + type: + description: PUBLIC(V) | INTERNAL + enum: + - PUBLIC + - INTERNAL + type: string + vNetId: + type: string + vmGroup: + $ref: '#/definitions/mcis.TBNLBVMGroup' + required: + - connectionName + - healthChecker + - listener + - name + - scope + - type + - vNetId + type: object mcis.TbVmDynamicReq: properties: commonImage: @@ -3531,6 +3712,194 @@ paths: summary: Inject Cloud Information For Cloud Adaptive Network tags: - '[Infra service] MCIS Cloud Adaptive Network (for developer)' + /ns/{nsId}/nlb: + delete: + consumes: + - application/json + description: Delete all NLBs + parameters: + - default: ns01 + description: Namespace ID + in: path + name: nsId + required: true + type: string + - default: "" + description: Delete resources containing matched ID-substring only + in: query + name: match + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/common.IdList' + "404": + description: Not Found + schema: + $ref: '#/definitions/common.SimpleMsg' + summary: Delete all NLBs + tags: + - '[Infra resource] NLB management' + get: + consumes: + - application/json + description: List all NLBs or NLBs' ID + parameters: + - default: ns01 + description: Namespace ID + in: path + name: nsId + required: true + type: string + - description: Option + enum: + - id + in: query + name: option + type: string + - description: 'Field key for filtering (ex: cspNLBName)' + in: query + name: filterKey + type: string + - description: 'Field value for filtering (ex: ns01-alibaba-ap-northeast-1-vpc)' + in: query + name: filterVal + type: string + produces: + - application/json + responses: + "200": + description: Different return structures by the given option param + schema: + allOf: + - $ref: '#/definitions/mcis.JSONResult' + - properties: + '[DEFAULT]': + $ref: '#/definitions/mcis.RestGetAllNLBResponse' + '[ID]': + $ref: '#/definitions/common.IdList' + type: object + "404": + description: Not Found + schema: + $ref: '#/definitions/common.SimpleMsg' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/common.SimpleMsg' + summary: List all NLBs or NLBs' ID + tags: + - '[Infra resource] NLB management' + post: + consumes: + - application/json + description: Create NLB + parameters: + - default: ns01 + description: Namespace ID + in: path + name: nsId + required: true + type: string + - description: 'Option: [required params for register] connectionName, name, + cspNLBId' + enum: + - register + in: query + name: option + type: string + - description: Details of the NLB object + in: body + name: nlbReq + required: true + schema: + $ref: '#/definitions/mcis.TbNLBReq' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/mcis.TbNLBInfo' + "404": + description: Not Found + schema: + $ref: '#/definitions/common.SimpleMsg' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/common.SimpleMsg' + summary: Create NLB + tags: + - '[Infra resource] NLB management' + /ns/{nsId}/nlb/{nlbId}: + delete: + consumes: + - application/json + description: Delete NLB + parameters: + - default: ns01 + description: Namespace ID + in: path + name: nsId + required: true + type: string + - description: NLB ID + in: path + name: nlbId + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/common.SimpleMsg' + "404": + description: Not Found + schema: + $ref: '#/definitions/common.SimpleMsg' + summary: Delete NLB + tags: + - '[Infra resource] NLB management' + get: + consumes: + - application/json + description: Get NLB + parameters: + - default: ns01 + description: Namespace ID + in: path + name: nsId + required: true + type: string + - description: NLB ID + in: path + name: nlbId + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/mcis.TbNLBInfo' + "404": + description: Not Found + schema: + $ref: '#/definitions/common.SimpleMsg' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/common.SimpleMsg' + summary: Get NLB + tags: + - '[Infra resource] NLB management' /ns/{nsId}/policy/mcis: delete: consumes: diff --git a/src/api/rest/server/mcis/nlb.go b/src/api/rest/server/mcis/nlb.go new file mode 100644 index 000000000..a11ab58b9 --- /dev/null +++ b/src/api/rest/server/mcis/nlb.go @@ -0,0 +1,219 @@ +/* +Copyright 2019 The Cloud-Barista Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package mcis is to handle REST API for mcis +package mcis + +import ( + "fmt" + "net/http" + + "github.com/cloud-barista/cb-tumblebug/src/core/common" + "github.com/cloud-barista/cb-tumblebug/src/core/mcis" + "github.com/labstack/echo/v4" +) + +// RestPostNLB godoc +// @Summary Create NLB +// @Description Create NLB +// @Tags [Infra resource] NLB management +// @Accept json +// @Produce json +// @Param nsId path string true "Namespace ID" default(ns01) +// @Param option query string false "Option: [required params for register] connectionName, name, cspNLBId" Enums(register) +// @Param nlbReq body mcis.TbNLBReq true "Details of the NLB object" +// @Success 200 {object} mcis.TbNLBInfo +// @Failure 404 {object} common.SimpleMsg +// @Failure 500 {object} common.SimpleMsg +// @Router /ns/{nsId}/nlb [post] +func RestPostNLB(c echo.Context) error { + + nsId := c.Param("nsId") + + optionFlag := c.QueryParam("option") + + u := &mcis.TbNLBReq{} + if err := c.Bind(u); err != nil { + return err + } + + fmt.Println("[POST NLB]") + + content, err := mcis.CreateNLB(nsId, u, optionFlag) + + if err != nil { + common.CBLog.Error(err) + mapA := map[string]string{"message": err.Error()} + return c.JSON(http.StatusInternalServerError, &mapA) + } + return c.JSON(http.StatusCreated, content) +} + +/* function RestPutNLB not yet implemented +// RestPutNLB godoc +// @Summary Update NLB +// @Description Update NLB +// @Tags [Infra resource] NLB management +// @Accept json +// @Produce json +// @Param nlbInfo body mcis.TbNLBInfo true "Details of the NLB object" +// @Success 200 {object} mcis.TbNLBInfo +// @Failure 404 {object} common.SimpleMsg +// @Failure 500 {object} common.SimpleMsg +// @Router /ns/{nsId}/nlb/{nlbId} [put] +*/ +func RestPutNLB(c echo.Context) error { + //nsId := c.Param("nsId") + + return nil +} + +// RestGetNLB godoc +// @Summary Get NLB +// @Description Get NLB +// @Tags [Infra resource] NLB management +// @Accept json +// @Produce json +// @Param nsId path string true "Namespace ID" default(ns01) +// @Param nlbId path string true "NLB ID" +// @Success 200 {object} mcis.TbNLBInfo +// @Failure 404 {object} common.SimpleMsg +// @Failure 500 {object} common.SimpleMsg +// @Router /ns/{nsId}/nlb/{nlbId} [get] +func RestGetNLB(c echo.Context) error { + + nsId := c.Param("nsId") + + resourceId := c.Param("resourceId") + + res, err := mcis.GetNLB(nsId, resourceId) + if err != nil { + mapA := map[string]string{"message": "Failed to find the NLB " + resourceId} + return c.JSON(http.StatusNotFound, &mapA) + } else { + return c.JSON(http.StatusOK, &res) + } +} + +// Response structure for RestGetAllNLB +type RestGetAllNLBResponse struct { + NLB []mcis.TbNLBInfo `json:"nlb"` +} + +// RestGetAllNLB godoc +// @Summary List all NLBs or NLBs' ID +// @Description List all NLBs or NLBs' ID +// @Tags [Infra resource] NLB management +// @Accept json +// @Produce json +// @Param nsId path string true "Namespace ID" default(ns01) +// @Param option query string false "Option" Enums(id) +// @Param filterKey query string false "Field key for filtering (ex: cspNLBName)" +// @Param filterVal query string false "Field value for filtering (ex: ns01-alibaba-ap-northeast-1-vpc)" +// @Success 200 {object} JSONResult{[DEFAULT]=RestGetAllNLBResponse,[ID]=common.IdList} "Different return structures by the given option param" +// @Failure 404 {object} common.SimpleMsg +// @Failure 500 {object} common.SimpleMsg +// @Router /ns/{nsId}/nlb [get] +func RestGetAllNLB(c echo.Context) error { + + nsId := c.Param("nsId") + + optionFlag := c.QueryParam("option") + filterKey := c.QueryParam("filterKey") + filterVal := c.QueryParam("filterVal") + + if optionFlag == "id" { + content := common.IdList{} + var err error + content.IdList, err = mcis.ListNLBId(nsId) + if err != nil { + mapA := map[string]string{"message": "Failed to list NLBs' ID; " + err.Error()} + return c.JSON(http.StatusNotFound, &mapA) + } + + return c.JSON(http.StatusOK, &content) + } else { + + resourceList, err := mcis.ListNLB(nsId, filterKey, filterVal) + if err != nil { + mapA := map[string]string{"message": "Failed to list NLBs; " + err.Error()} + return c.JSON(http.StatusNotFound, &mapA) + } + + var content struct { + NLB []mcis.TbNLBInfo `json:"nlb"` + } + + content.NLB = resourceList.([]mcis.TbNLBInfo) // type assertion (interface{} -> array) + return c.JSON(http.StatusOK, &content) + // return c.JSON(http.StatusBadRequest, nil) + } +} + +// RestDelNLB godoc +// @Summary Delete NLB +// @Description Delete NLB +// @Tags [Infra resource] NLB management +// @Accept json +// @Produce json +// @Param nsId path string true "Namespace ID" default(ns01) +// @Param nlbId path string true "NLB ID" +// @Success 200 {object} common.SimpleMsg +// @Failure 404 {object} common.SimpleMsg +// @Router /ns/{nsId}/nlb/{nlbId} [delete] +func RestDelNLB(c echo.Context) error { + + nsId := c.Param("nsId") + + resourceId := c.Param("resourceId") + + forceFlag := c.QueryParam("force") + + err := mcis.DelNLB(nsId, resourceId, forceFlag) + if err != nil { + common.CBLog.Error(err) + mapA := map[string]string{"message": err.Error()} + return c.JSON(http.StatusInternalServerError, &mapA) + } + + mapA := map[string]string{"message": "The NLB " + resourceId + " has been deleted"} + return c.JSON(http.StatusOK, &mapA) +} + +// RestDelAllNLB godoc +// @Summary Delete all NLBs +// @Description Delete all NLBs +// @Tags [Infra resource] NLB management +// @Accept json +// @Produce json +// @Param nsId path string true "Namespace ID" default(ns01) +// @Param match query string false "Delete resources containing matched ID-substring only" default() +// @Success 200 {object} common.IdList +// @Failure 404 {object} common.SimpleMsg +// @Router /ns/{nsId}/nlb [delete] +func RestDelAllNLB(c echo.Context) error { + + nsId := c.Param("nsId") + + forceFlag := c.QueryParam("force") + subString := c.QueryParam("match") + + output, err := mcis.DelAllNLB(nsId, subString, forceFlag) + if err != nil { + common.CBLog.Error(err) + mapA := map[string]string{"message": err.Error()} + return c.JSON(http.StatusConflict, &mapA) + } + + return c.JSON(http.StatusOK, output) +} diff --git a/src/api/rest/server/server.go b/src/api/rest/server/server.go index ddcfb08bf..9b7971428 100644 --- a/src/api/rest/server/server.go +++ b/src/api/rest/server/server.go @@ -220,6 +220,14 @@ func RunServer(port string) { g.POST("/:nsId/network/mcis/:mcisId", rest_mcis.RestPostConfigureCloudAdaptiveNetworkToMcis) g.PUT("/:nsId/network/mcis/:mcisId", rest_mcis.RestPutInjectCloudInformationForCloudAdaptiveNetwork) + // Network Load Balancer + g.POST("/:nsId/nlb", rest_mcis.RestPostNLB) + g.GET("/:nsId/nlb/:resourceId", rest_mcis.RestGetNLB) + g.GET("/:nsId/nlb", rest_mcis.RestGetAllNLB) + // g.PUT("/:nsId/nlb/:resourceId", rest_mcis.RestPutNLB) + g.DELETE("/:nsId/nlb/:resourceId", rest_mcis.RestDelNLB) + g.DELETE("/:nsId/nlb", rest_mcis.RestDelAllNLB) + //MCIR Management g.POST("/:nsId/resources/image", rest_mcir.RestPostImage) g.GET("/:nsId/resources/image/:resourceId", rest_mcir.RestGetResource) diff --git a/src/core/common/common.go b/src/core/common/common.go index 987664379..32b27a65d 100644 --- a/src/core/common/common.go +++ b/src/core/common/common.go @@ -66,6 +66,7 @@ const ( StrSpec string = "spec" StrVNet string = "vNet" StrSubnet string = "subnet" + StrNLB string = "nlb" StrVM string = "vm" StrDefaultResourceName string = "-systemdefault-" // StrFirewallRule string = "firewallRule" diff --git a/src/core/mcis/nlb.go b/src/core/mcis/nlb.go new file mode 100644 index 000000000..39732d9c8 --- /dev/null +++ b/src/core/mcis/nlb.go @@ -0,0 +1,866 @@ +/* +Copyright 2019 The Cloud-Barista Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package mcis is to manage multi-cloud infra service +package mcis + +import ( + "encoding/json" + "fmt" + "os" + "strconv" + "strings" + "time" + + cbstore_utils "github.com/cloud-barista/cb-store/utils" + "github.com/cloud-barista/cb-tumblebug/src/core/common" + "github.com/cloud-barista/cb-tumblebug/src/core/mcir" + validator "github.com/go-playground/validator/v10" + "github.com/go-resty/resty/v2" +) + +// 2022-07-15 https://github.com/cloud-barista/cb-spider/blob/master/cloud-control-manager/cloud-driver/interfaces/resources/NLBHandler.go + +// SpiderNLBReqInfoWrapper is a wrapper struct to create JSON body of 'Create NLB request' +type SpiderNLBReqInfoWrapper struct { + ConnectionName string + ReqInfo SpiderNLBReqInfo +} + +// 2022-07-15 https://github.com/cloud-barista/cb-spider/blob/a3ee3030e4b956bcb27691203f286265ab3a926e/api-runtime/rest-runtime/CCMRest.go#L1895 + +// SpiderNLBReqInfo is a struct to create JSON body of 'Create NLB request' +type SpiderNLBReqInfo struct { + Name string + VPCName string + Type string // PUBLIC(V) | INTERNAL + Scope string // REGION(V) | GLOBAL + + //------ Frontend + + Listener NLBListenerInfo + + //------ Backend + + VMGroup SpiderNLBVMGroupReq + HealthChecker NLBHealthCheckerReq +} + +// +type NLBHealthCheckerReq struct { + Protocol string // TCP|HTTP|HTTPS + Port string // Listener Port or 1-65535 + Interval string // secs, Interval time between health checks. + Timeout string // secs, Waiting time to decide an unhealthy VM when no response. + Threshold string // num, The number of continuous health checks to change the VM status. +} + +// +type SpiderNLBVMGroupReq struct { + Protocol string // TCP|HTTP|HTTPS + Port string // Listener Port or 1-65535 + VMs []string +} + +// SpiderNLBInfo is a struct to handle NLB information from the CB-Spider's REST API response +type SpiderNLBInfo struct { + IId common.IID // {NameId, SystemId} + VpcIID common.IID // {NameId, SystemId} + + Type string // PUBLIC(V) | INTERNAL + Scope string // REGION(V) | GLOBAL + + //------ Frontend + Listener NLBListenerInfo + + //------ Backend + VMGroup NLBVMGroupInfo + HealthChecker NLBHealthCheckerInfo + + CreatedTime time.Time + KeyValueList []common.KeyValue +} + +// SpiderSubnetInfo is a struct to handle subnet information from the CB-Spider's REST API response +type NLBListenerInfo struct { + Protocol string // TCP|UDP + IP string // Auto Generated and attached + Port string // 1-65535 + DNSName string // Optional, Auto Generated and attached + + CspID string // Optional, May be Used by Driver. + KeyValueList []common.KeyValue +} + +type NLBVMGroupInfo struct { // Spider + Protocol string // TCP|UDP|HTTP|HTTPS + Port string // 1-65535 + VMs *[]common.IID + + CspID string // Optional, May be Used by Driver. + KeyValueList []common.KeyValue +} + +type NLBHealthCheckerInfo struct { + Protocol string // TCP|HTTP|HTTPS + Port string // Listener Port or 1-65535 + Interval int // secs, Interval time between health checks. + Timeout int // secs, Waiting time to decide an unhealthy VM when no response. + Threshold int // num, The number of continuous health checks to change the VM status. + + CspID string // Optional, May be Used by Driver. + KeyValueList []common.KeyValue +} + +type SpiderHealthInfo struct { + AllVMs *[]common.IID + HealthyVMs *[]common.IID + UnHealthyVMs *[]common.IID +} + +type TBNLBVMGroup struct { + Protocol string // TCP|HTTP|HTTPS + Port string // Listener Port or 1-65535 + MCIS string + VMs []string + + CspID string // Optional, May be Used by Driver. + KeyValueList []common.KeyValue +} + +// TbNLBReq is a struct to handle 'Create nlb' request toward CB-Tumblebug. +type TbNLBReq struct { // Tumblebug + Name string `json:"name" validate:"required"` + ConnectionName string `json:"connectionName" validate:"required"` + VNetId string `json:"vNetId" validate:"required"` + Description string `json:"description"` + CspNLBId string `json:"cspNLBId"` + + Type string `json:"type" validate:"required" enums:"PUBLIC,INTERNAL"` // PUBLIC(V) | INTERNAL + Scope string `json:"scope" validate:"required" enums:"REGION,GLOBAL"` // REGION(V) | GLOBAL + + //------ Frontend + + Listener NLBListenerInfo `json:"listener" validate:"required"` + + //------ Backend + + VMGroup TBNLBVMGroup `json:"vmGroup"` + HealthChecker NLBHealthCheckerReq `json:"healthChecker" validate:"required"` +} + +// TbNLBReqStructLevelValidation is a function to validate 'TbNLBReq' object. +func TbNLBReqStructLevelValidation(sl validator.StructLevel) { + + u := sl.Current().Interface().(TbNLBReq) + + err := common.CheckString(u.Name) + if err != nil { + // ReportError(field interface{}, fieldName, structFieldName, tag, param string) + sl.ReportError(u.Name, "name", "Name", err.Error(), "") + } +} + +// TbNLBInfo is a struct that represents TB nlb object. +type TbNLBInfo struct { // Tumblebug + Id string `json:"id"` + Name string `json:"name"` + ConnectionName string `json:"connectionName"` + + Type string // PUBLIC(V) | INTERNAL + Scope string // REGION(V) | GLOBAL + + //------ Frontend + + Listener NLBListenerInfo + + //------ Backend + + VMGroup TBNLBVMGroup `json:"vmGroup"` + HealthChecker NLBHealthCheckerInfo + + CreatedTime time.Time + + Description string `json:"description"` + CspNLBId string `json:"cspNLBId"` + CspNLBName string `json:"cspNLBName"` + Status string `json:"status"` + KeyValueList []common.KeyValue `json:"keyValueList"` + AssociatedObjectList []string `json:"associatedObjectList"` + IsAutoGenerated bool `json:"isAutoGenerated"` + + // SystemLabel is for describing the MCIR in a keyword (any string can be used) for special System purpose + SystemLabel string `json:"systemLabel" example:"Managed by CB-Tumblebug" default:""` + + // Disabled for now + //Region string `json:"region"` + //ResourceGroupName string `json:"resourceGroupName"` +} + +// CreateNLB accepts nlb creation request, creates and returns an TB nlb object +func CreateNLB(nsId string, u *TbNLBReq, option string) (TbNLBInfo, error) { + fmt.Println("=========================== CreateNLB") + + err := common.CheckString(nsId) + if err != nil { + temp := TbNLBInfo{} + common.CBLog.Error(err) + return temp, err + } + + err = validate.Struct(u) + if err != nil { + if _, ok := err.(*validator.InvalidValidationError); ok { + fmt.Println(err) + temp := TbNLBInfo{} + return temp, err + } + + temp := TbNLBInfo{} + return temp, err + } + + check, err := CheckNLB(nsId, u.Name) + + if check { + temp := TbNLBInfo{} + err := fmt.Errorf("The nlb " + u.Name + " already exists.") + return temp, err + } + + if err != nil { + temp := TbNLBInfo{} + err := fmt.Errorf("Failed to check the existence of the nlb " + u.Name + ".") + return temp, err + } + + vNetInfo := mcir.TbVNetInfo{} + tempInterface, err := mcir.GetResource(nsId, common.StrVNet, u.VNetId) + if err != nil { + err := fmt.Errorf("Failed to get the TbVNetInfo " + u.VNetId + ".") + return TbNLBInfo{}, err + } + err = common.CopySrcToDest(&tempInterface, &vNetInfo) + if err != nil { + err := fmt.Errorf("Failed to get the TbVNetInfo-CopySrcToDest() " + u.VNetId + ".") + return TbNLBInfo{}, err + } + + tempReq := SpiderNLBReqInfoWrapper{} + tempReq.ConnectionName = u.ConnectionName + tempReq.ReqInfo.Name = fmt.Sprintf("%s-%s", nsId, u.Name) + tempReq.ReqInfo.VPCName = vNetInfo.CspVNetName + tempReq.ReqInfo.Type = u.Type + tempReq.ReqInfo.Scope = u.Scope + + tempReq.ReqInfo.Listener = u.Listener + + tempReq.ReqInfo.HealthChecker = u.HealthChecker + + tempReq.ReqInfo.VMGroup.Port = u.VMGroup.Port + tempReq.ReqInfo.VMGroup.Protocol = u.VMGroup.Protocol + + for _, v := range u.VMGroup.VMs { + vm, err := GetVmObject(nsId, u.VMGroup.MCIS, v) + if err != nil { + common.CBLog.Error(err) + return TbNLBInfo{}, err + } + // fmt.Println("vm:") // for debug + // payload, _ := json.MarshalIndent(vm, "", " ") // for debug + // fmt.Print(string(payload)) // for debug + tempReq.ReqInfo.VMGroup.VMs = append(tempReq.ReqInfo.VMGroup.VMs, vm.CspViewVmDetail.IId.NameId) + } + + // fmt.Printf("u.VMGroup.VMs: %s \n", u.VMGroup.VMs) // for debug + // fmt.Printf("tempReq.ReqInfo.VMGroup.VMs: %s \n", tempReq.ReqInfo.VMGroup.VMs) // for debug + /* + for _, v := range u.VMIDList { + mcisId_vmId := strings.Split(v, "/") + if len(mcisId_vmId) != 2 { + err := fmt.Errorf("Cannot retrieve VM info: " + v) + common.CBLog.Error(err) + return TbNLBInfo{}, err + } + + vm, err := mcis.GetVmObject(nsId, mcisId_vmId[0], mcisId_vmId[1]) + if err != nil { + common.CBLog.Error(err) + return TbNLBInfo{}, err + } + + tempReq.ReqInfo.VMGroup = append(tempReq.ReqInfo.VMGroup, vm.IdByCSP) + } + */ + + var tempSpiderNLBInfo *SpiderNLBInfo + + if os.Getenv("SPIDER_CALL_METHOD") == "REST" { + + client := resty.New().SetCloseConnection(true) + client.SetAllowGetMethodPayload(true) + + // fmt.Println("tempReq:") // for debug + // payload, _ := json.MarshalIndent(tempReq, "", " ") // for debug + // fmt.Print(string(payload)) // for debug + + req := client.R(). + SetHeader("Content-Type", "application/json"). + SetBody(tempReq). + SetResult(&SpiderNLBInfo{}) // or SetResult(AuthSuccess{}). + //SetError(&AuthError{}). // or SetError(AuthError{}). + + var resp *resty.Response + var err error + + var url string + if option == "register" && u.CspNLBId == "" { + url = fmt.Sprintf("%s/nlb/%s", common.SpiderRestUrl, u.Name) + resp, err = req.Get(url) + } else if option == "register" && u.CspNLBId != "" { + url = fmt.Sprintf("%s/regnlb", common.SpiderRestUrl) + resp, err = req.Post(url) + } else { // option != "register" + url = fmt.Sprintf("%s/nlb", common.SpiderRestUrl) + resp, err = req.Post(url) + } + + if err != nil { + common.CBLog.Error(err) + content := TbNLBInfo{} + err := fmt.Errorf("an error occurred while requesting to CB-Spider") + return content, err + } + + fmt.Println("HTTP Status code: " + strconv.Itoa(resp.StatusCode())) + switch { + case resp.StatusCode() >= 400 || resp.StatusCode() < 200: + err := fmt.Errorf(string(resp.Body())) + common.CBLog.Error(err) + content := TbNLBInfo{} + return content, err + } + + tempSpiderNLBInfo = resp.Result().(*SpiderNLBInfo) + + } + /* + else { + + // Set CCM API + ccm := api.NewCloudResourceHandler() + err := ccm.SetConfigPath(os.Getenv("CBTUMBLEBUG_ROOT") + "/conf/grpc_conf.yaml") + if err != nil { + common.CBLog.Error("ccm failed to set config : ", err) + return TbNLBInfo{}, err + } + err = ccm.Open() + if err != nil { + common.CBLog.Error("ccm api open failed : ", err) + return TbNLBInfo{}, err + } + defer ccm.Close() + + payload, _ := json.MarshalIndent(tempReq, "", " ") + + var result string + + if option == "register" { + result, err = ccm.CreateNLB(string(payload)) + } else { + result, err = ccm.GetNLB(string(payload)) + } + + if err != nil { + common.CBLog.Error(err) + return TbNLBInfo{}, err + } + + tempSpiderNLBInfo = &SpiderNLBInfo{} + err = json.Unmarshal([]byte(result), &tempSpiderNLBInfo) + if err != nil { + common.CBLog.Error(err) + return TbNLBInfo{}, err + } + + } + */ + + content := TbNLBInfo{} + //content.Id = common.GenUid() + content.Id = u.Name + content.Name = u.Name + content.ConnectionName = u.ConnectionName + content.Type = tempSpiderNLBInfo.Type + content.Scope = tempSpiderNLBInfo.Scope + content.Listener = tempSpiderNLBInfo.Listener + content.HealthChecker = tempSpiderNLBInfo.HealthChecker + content.CspNLBId = tempSpiderNLBInfo.IId.SystemId + content.CspNLBName = tempSpiderNLBInfo.IId.NameId + content.Description = u.Description + content.KeyValueList = tempSpiderNLBInfo.KeyValueList + content.AssociatedObjectList = []string{} + + content.VMGroup.Port = tempSpiderNLBInfo.VMGroup.Port + content.VMGroup.Protocol = tempSpiderNLBInfo.VMGroup.Protocol + content.VMGroup.MCIS = u.VMGroup.MCIS + content.VMGroup.VMs = u.VMGroup.VMs + content.VMGroup.CspID = u.VMGroup.CspID + content.VMGroup.KeyValueList = u.VMGroup.KeyValueList + + if option == "register" && u.CspNLBId == "" { + content.SystemLabel = "Registered from CB-Spider resource" + } else if option == "register" && u.CspNLBId != "" { + content.SystemLabel = "Registered from CSP resource" + } + + // cb-store + // Key := common.GenResourceKey(nsId, common.StrNLB, content.Id) + Key := GenNLBKey(nsId, content.Id) + Val, _ := json.Marshal(content) + + err = common.CBStore.Put(Key, string(Val)) + if err != nil { + common.CBLog.Error(err) + return content, err + } + + keyValue, err := common.CBStore.Get(Key) + if err != nil { + common.CBLog.Error(err) + err = fmt.Errorf("In CreateNLB(); CBStore.Get() returned an error.") + common.CBLog.Error(err) + // return nil, err + } + + fmt.Println("<" + keyValue.Key + "> \n" + keyValue.Value) + fmt.Println("===========================") + + result := TbNLBInfo{} + err = json.Unmarshal([]byte(keyValue.Value), &result) + if err != nil { + common.CBLog.Error(err) + } + return result, nil +} + +// GetNLB returns the requested TB NLB object +func GetNLB(nsId string, resourceId string) (TbNLBInfo, error) { + res := TbNLBInfo{} + + err := common.CheckString(nsId) + if err != nil { + common.CBLog.Error(err) + return res, err + } + + err = common.CheckString(resourceId) + if err != nil { + common.CBLog.Error(err) + return res, err + } + check, err := CheckNLB(nsId, resourceId) + if err != nil { + common.CBLog.Error(err) + return res, err + } + + if !check { + errString := "The NLB " + resourceId + " does not exist." + err := fmt.Errorf(errString) + return res, err + } + + fmt.Println("[Get NLB] " + resourceId) + + // key := common.GenResourceKey(nsId, resourceType, resourceId) + key := GenNLBKey(nsId, resourceId) + + keyValue, err := common.CBStore.Get(key) + if err != nil { + common.CBLog.Error(err) + return res, err + } + if keyValue != nil { + + err = json.Unmarshal([]byte(keyValue.Value), &res) + if err != nil { + common.CBLog.Error(err) + return res, err + } + return res, nil + } + errString := "Cannot get the NLB " + resourceId + "." + err = fmt.Errorf(errString) + return res, err +} + +// CheckNLB returns the existence of the TB NLB object in bool form. +func CheckNLB(nsId string, resourceId string) (bool, error) { + + // Check parameters' emptiness + if nsId == "" { + err := fmt.Errorf("CheckResource failed; nsId given is null.") + return false, err + } else if resourceId == "" { + err := fmt.Errorf("CheckResource failed; resourceId given is null.") + return false, err + } + + err := common.CheckString(nsId) + if err != nil { + common.CBLog.Error(err) + return false, err + } + + err = common.CheckString(resourceId) + if err != nil { + common.CBLog.Error(err) + return false, err + } + + fmt.Println("[Check NLB] " + resourceId) + + // key := common.GenResourceKey(nsId, resourceType, resourceId) + key := GenNLBKey(nsId, resourceId) + + keyValue, err := common.CBStore.Get(key) + if err != nil { + common.CBLog.Error(err) + return false, err + } + if keyValue != nil { + return true, nil + } + return false, nil + +} + +// GenNLBKey is func to generate a key from NLB id +func GenNLBKey(nsId string, resourceId string) string { + err := common.CheckString(nsId) + if err != nil { + common.CBLog.Error(err) + return "/invalidKey" + } + + err = common.CheckString(resourceId) + if err != nil { + common.CBLog.Error(err) + return "/invalidKey" + } + + return fmt.Sprintf("/ns/%s/nlb/%s", nsId, resourceId) +} + +// ListNLBId returns the list of TB NLB object IDs of given nsId +func ListNLBId(nsId string) ([]string, error) { + + err := common.CheckString(nsId) + if err != nil { + common.CBLog.Error(err) + return nil, err + } + + fmt.Println("[ListNLBId] ns: " + nsId) + key := "/ns/" + nsId + "/" + fmt.Println(key) + + keyValue, err := common.CBStore.GetList(key, true) + + if err != nil { + common.CBLog.Error(err) + return nil, err + } + + /* if keyValue == nil, then for-loop below will not be executed, and the empty array will be returned in `resourceList` placeholder. + if keyValue == nil { + err = fmt.Errorf("ListResourceId(); %s is empty.", key) + common.CBLog.Error(err) + return nil, err + } + */ + + var resourceList []string + for _, v := range keyValue { + trimmedString := strings.TrimPrefix(v.Key, (key + "nlb/")) + // prevent malformed key (if key for resource id includes '/', the key does not represent resource ID) + if !strings.Contains(trimmedString, "/") { + resourceList = append(resourceList, trimmedString) + } + } + + return resourceList, nil + +} + +// ListNLB returns the list of TB NLB objects of given nsId +func ListNLB(nsId string, filterKey string, filterVal string) (interface{}, error) { + + err := common.CheckString(nsId) + if err != nil { + common.CBLog.Error(err) + return nil, err + } + + fmt.Println("[Get] NLB list") + key := "/ns/" + nsId + "/nlb" + fmt.Println(key) + + keyValue, err := common.CBStore.GetList(key, true) + keyValue = cbstore_utils.GetChildList(keyValue, key) + + if err != nil { + common.CBLog.Error(err) + return nil, err + } + if keyValue != nil { + res := []TbNLBInfo{} + for _, v := range keyValue { + + tempObj := TbNLBInfo{} + err = json.Unmarshal([]byte(v.Value), &tempObj) + if err != nil { + common.CBLog.Error(err) + return nil, err + } + // Check the JSON body inclues both filterKey and filterVal strings. (assume key and value) + if filterKey != "" { + // If not inclues both, do not append current item to the list result. + itemValueForCompare := strings.ToLower(v.Value) + if !(strings.Contains(itemValueForCompare, strings.ToLower(filterKey)) && strings.Contains(itemValueForCompare, strings.ToLower(filterVal))) { + continue + } + } + res = append(res, tempObj) + } + return res, nil + + } else { //return empty object according to resourceType + res := []TbNLBInfo{} + return res, nil + + } + + err = fmt.Errorf("Some exceptional case happened. Please check the references of " + common.GetFuncName()) + return nil, err // if interface{} == nil, make err be returned. Should not come this part if there is no err. +} + +// DelNLB deletes the TB NLB object +func DelNLB(nsId string, resourceId string, forceFlag string) error { + + err := common.CheckString(nsId) + if err != nil { + common.CBLog.Error(err) + return err + } + + err = common.CheckString(resourceId) + if err != nil { + common.CBLog.Error(err) + return err + } + check, err := CheckNLB(nsId, resourceId) + + if err != nil { + common.CBLog.Error(err) + return err + } + + if !check { + errString := "The NLB " + resourceId + " does not exist." + err := fmt.Errorf(errString) + return err + } + + key := GenNLBKey(nsId, resourceId) + fmt.Println("key: " + key) + + keyValue, _ := common.CBStore.Get(key) + // In CheckResource() above, calling 'CBStore.Get()' and checking err parts exist. + // So, in here, we don't need to check whether keyValue == nil or err != nil. + + // Deleting NLB should be possible, even if backend VMs still exist. + // So here 'associated object' codes are commented. + /* + associatedList, _ := GetAssociatedObjectList(nsId, resourceType, resourceId) + if len(associatedList) == 0 { + // continue + } else { + errString := " [Failed]" + " Associated with [" + strings.Join(associatedList[:], ", ") + "]" + err := fmt.Errorf(errString) + common.CBLog.Error(err) + return err + } + */ + + //cspType := common.GetResourcesCspType(nsId, resourceType, resourceId) + + // NLB has no childResources, so below line is commented. + // var childResources interface{} + + // Disable gRPC calling for NLB + // if os.Getenv("SPIDER_CALL_METHOD") == "REST" { + + var url string + + // Create Req body + type JsonTemplate struct { + ConnectionName string + } + tempReq := JsonTemplate{} + + temp := TbNLBInfo{} + err = json.Unmarshal([]byte(keyValue.Value), &temp) + if err != nil { + common.CBLog.Error(err) + return err + } + tempReq.ConnectionName = temp.ConnectionName + url = common.SpiderRestUrl + "/nlb/" + temp.CspNLBName + + fmt.Println("url: " + url) + + client := resty.New().SetCloseConnection(true) + + resp, err := client.R(). + SetHeader("Content-Type", "application/json"). + SetBody(tempReq). + //SetResult(&SpiderSpecInfo{}). // or SetResult(AuthSuccess{}). + //SetError(&AuthError{}). // or SetError(AuthError{}). + Delete(url) + + if err != nil { + common.CBLog.Error(err) + err := fmt.Errorf("an error occurred while requesting to CB-Spider") + return err + } + + fmt.Println("HTTP Status code: " + strconv.Itoa(resp.StatusCode())) + switch { + case forceFlag == "true": + url += "?force=true" + fmt.Println("forceFlag == true; url: " + url) + + _, err := client.R(). + SetHeader("Content-Type", "application/json"). + SetBody(tempReq). + //SetResult(&SpiderSpecInfo{}). // or SetResult(AuthSuccess{}). + //SetError(&AuthError{}). // or SetError(AuthError{}). + Delete(url) + + if err != nil { + common.CBLog.Error(err) + err := fmt.Errorf("an error occurred while requesting to CB-Spider") + return err + } + + case resp.StatusCode() >= 400 || resp.StatusCode() < 200: + err := fmt.Errorf(string(resp.Body())) + common.CBLog.Error(err) + return err + default: + + } + + // Disable gRPC calling for NLB + /* + } else { + + // Set CCM gRPC API + ccm := api.NewCloudResourceHandler() + err := ccm.SetConfigPath(os.Getenv("CBTUMBLEBUG_ROOT") + "/conf/grpc_conf.yaml") + if err != nil { + common.CBLog.Error("ccm failed to set config : ", err) + return err + } + err = ccm.Open() + if err != nil { + common.CBLog.Error("ccm api open failed : ", err) + return err + } + defer ccm.Close() + + temp := TbNLBInfo{} + err = json.Unmarshal([]byte(keyValue.Value), &temp) + if err != nil { + common.CBLog.Error(err) + return err + } + + _, err = ccm.DeleteNLBByParam(temp.ConnectionName, temp.Name, forceFlag) + if err != nil { + common.CBLog.Error(err) + return err + } + + // err = common.CBStore.Delete(key) + // if err != nil { + // common.CBLog.Error(err) + // return err + // } + // return nil + + } + */ + + err = common.CBStore.Delete(key) + if err != nil { + common.CBLog.Error(err) + return err + } + return nil +} + +// DelAllNLB deletes all TB NLB object of given nsId +func DelAllNLB(nsId string, subString string, forceFlag string) (common.IdList, error) { + + deletedResources := common.IdList{} + deleteStatus := "" + + err := common.CheckString(nsId) + if err != nil { + common.CBLog.Error(err) + return deletedResources, err + } + + resourceIdList, err := ListNLBId(nsId) + if err != nil { + return deletedResources, err + } + + if len(resourceIdList) == 0 { + errString := "There is no NLB in " + nsId + err := fmt.Errorf(errString) + common.CBLog.Error(err) + return deletedResources, err + } + + for _, v := range resourceIdList { + // if subSting is provided, check the resourceId contains the subString. + if subString == "" || strings.Contains(v, subString) { + deleteStatus = "" + + err := DelNLB(nsId, v, forceFlag) + + if err != nil { + deleteStatus = err.Error() + } else { + deleteStatus = " [Done]" + } + + deletedResources.IdList = append(deletedResources.IdList, "NLB: "+v+deleteStatus) + } + } + return deletedResources, nil +} diff --git a/src/testclient/scripts/10.nlb/create-NLB.sh b/src/testclient/scripts/10.nlb/create-NLB.sh new file mode 100755 index 000000000..6df229d99 --- /dev/null +++ b/src/testclient/scripts/10.nlb/create-NLB.sh @@ -0,0 +1,81 @@ +#!/bin/bash + +function CallTB() { + echo "- Create nlb in ${MCIRRegionName}" + + resp=$( + curl -H "${AUTH}" -sX POST http://$TumblebugServer/tumblebug/ns/$NSID/nlb -H 'Content-Type: application/json' -d @- <