Skip to content

Commit

Permalink
Merge pull request #718 from jihoon-seo/210908_Adopt_struct_validator…
Browse files Browse the repository at this point in the history
…_more

Adopt struct validator more
  • Loading branch information
seokho-son authored Sep 9, 2021
2 parents 99bbff3 + 7f881fa commit bc8e114
Show file tree
Hide file tree
Showing 12 changed files with 545 additions and 46 deletions.
35 changes: 35 additions & 0 deletions src/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4916,6 +4916,12 @@ var doc = `{
},
"mcir.TbSecurityGroupReq": {
"type": "object",
"required": [
"connectionName",
"firewallRules",
"name",
"vNetId"
],
"properties": {
"connectionName": {
"type": "string"
Expand Down Expand Up @@ -5053,6 +5059,11 @@ var doc = `{
},
"mcir.TbSpecReq": {
"type": "object",
"required": [
"connectionName",
"cspSpecName",
"name"
],
"properties": {
"connectionName": {
"type": "string"
Expand Down Expand Up @@ -5120,6 +5131,10 @@ var doc = `{
},
"mcir.TbSshKeyReq": {
"type": "object",
"required": [
"connectionName",
"name"
],
"properties": {
"connectionName": {
"type": "string"
Expand Down Expand Up @@ -5184,6 +5199,10 @@ var doc = `{
},
"mcir.TbVNetReq": {
"type": "object",
"required": [
"connectionName",
"name"
],
"properties": {
"cidrBlock": {
"type": "string"
Expand Down Expand Up @@ -5392,6 +5411,9 @@ var doc = `{
},
"mcis.McisCmdReq": {
"type": "object",
"required": [
"command"
],
"properties": {
"command": {
"type": "string",
Expand Down Expand Up @@ -5914,6 +5936,10 @@ var doc = `{
},
"mcis.TbMcisReq": {
"type": "object",
"required": [
"name",
"vm"
],
"properties": {
"description": {
"type": "string"
Expand Down Expand Up @@ -6121,6 +6147,15 @@ var doc = `{
},
"mcis.TbVmReq": {
"type": "object",
"required": [
"connectionName",
"imageId",
"name",
"securityGroupIds",
"specId",
"sshKeyId",
"vNetId"
],
"properties": {
"connectionName": {
"type": "string"
Expand Down
35 changes: 35 additions & 0 deletions src/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -4901,6 +4901,12 @@
},
"mcir.TbSecurityGroupReq": {
"type": "object",
"required": [
"connectionName",
"firewallRules",
"name",
"vNetId"
],
"properties": {
"connectionName": {
"type": "string"
Expand Down Expand Up @@ -5038,6 +5044,11 @@
},
"mcir.TbSpecReq": {
"type": "object",
"required": [
"connectionName",
"cspSpecName",
"name"
],
"properties": {
"connectionName": {
"type": "string"
Expand Down Expand Up @@ -5105,6 +5116,10 @@
},
"mcir.TbSshKeyReq": {
"type": "object",
"required": [
"connectionName",
"name"
],
"properties": {
"connectionName": {
"type": "string"
Expand Down Expand Up @@ -5169,6 +5184,10 @@
},
"mcir.TbVNetReq": {
"type": "object",
"required": [
"connectionName",
"name"
],
"properties": {
"cidrBlock": {
"type": "string"
Expand Down Expand Up @@ -5377,6 +5396,9 @@
},
"mcis.McisCmdReq": {
"type": "object",
"required": [
"command"
],
"properties": {
"command": {
"type": "string",
Expand Down Expand Up @@ -5899,6 +5921,10 @@
},
"mcis.TbMcisReq": {
"type": "object",
"required": [
"name",
"vm"
],
"properties": {
"description": {
"type": "string"
Expand Down Expand Up @@ -6106,6 +6132,15 @@
},
"mcis.TbVmReq": {
"type": "object",
"required": [
"connectionName",
"imageId",
"name",
"securityGroupIds",
"specId",
"sshKeyId",
"vNetId"
],
"properties": {
"connectionName": {
"type": "string"
Expand Down
28 changes: 28 additions & 0 deletions src/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,11 @@ definitions:
type: string
vNetId:
type: string
required:
- connectionName
- firewallRules
- name
- vNetId
type: object
mcir.TbSpecInfo:
properties:
Expand Down Expand Up @@ -577,6 +582,10 @@ definitions:
type: string
name:
type: string
required:
- connectionName
- cspSpecName
- name
type: object
mcir.TbSshKeyInfo:
properties:
Expand Down Expand Up @@ -619,6 +628,9 @@ definitions:
type: string
name:
type: string
required:
- connectionName
- name
type: object
mcir.TbVNetInfo:
properties:
Expand Down Expand Up @@ -667,6 +679,9 @@ definitions:
items:
$ref: '#/definitions/mcir.SpiderSubnetReqInfo'
type: array
required:
- connectionName
- name
type: object
mcis.AgentInstallContent:
properties:
Expand Down Expand Up @@ -800,6 +815,8 @@ definitions:
userName:
example: cb-user
type: string
required:
- command
type: object
mcis.McisPolicyInfo:
properties:
Expand Down Expand Up @@ -1181,6 +1198,9 @@ definitions:
items:
$ref: '#/definitions/mcis.TbVmReq'
type: array
required:
- name
- vm
type: object
mcis.TbVmInfo:
properties:
Expand Down Expand Up @@ -1333,6 +1353,14 @@ definitions:
type: string
vmUserPassword:
type: string
required:
- connectionName
- imageId
- name
- securityGroupIds
- specId
- sshKeyId
- vNetId
type: object
mcis.TbVmStatusInfo:
properties:
Expand Down
8 changes: 6 additions & 2 deletions src/core/mcir/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ func init() {
return name
})

// register validation for 'TbImageReq'
// NOTE: only have to register a non-pointer type for 'TbImageReq', validator
// register validation for 'Tb*Req'
// NOTE: only have to register a non-pointer type for 'Tb*Req', validator
// internally dereferences during it's type checks.
validate.RegisterStructValidation(TbImageReqStructLevelValidation, TbImageReq{})
validate.RegisterStructValidation(TbSecurityGroupReqStructLevelValidation, TbSecurityGroupReq{})
validate.RegisterStructValidation(TbSpecReqStructLevelValidation, TbSpecReq{})
validate.RegisterStructValidation(TbSshKeyReqStructLevelValidation, TbSshKeyReq{})
validate.RegisterStructValidation(TbVNetReqStructLevelValidation, TbVNetReq{})
}

// DelAllResources deletes all TB MCIR object of given resourceType
Expand Down
7 changes: 0 additions & 7 deletions src/core/mcir/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,6 @@ func RegisterImageWithId(nsId string, u *TbImageReq) (TbImageInfo, error) {
return temp, err
}

// err = common.CheckString(u.Name)
// if err != nil {
// temp := TbImageInfo{}
// common.CBLog.Error(err)
// return temp, err
// }

// returns InvalidValidationError for bad validation input, nil or ValidationErrors ( []FieldError )
err = validate.Struct(u)
if err != nil {
Expand Down
51 changes: 45 additions & 6 deletions src/core/mcir/securitygroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/cloud-barista/cb-spider/interface/api"
"github.com/cloud-barista/cb-tumblebug/src/core/common"
validator "github.com/go-playground/validator/v10"
"github.com/go-resty/resty/v2"
)

Expand Down Expand Up @@ -51,11 +52,22 @@ type SpiderSecurityInfo struct { // Spider
}

type TbSecurityGroupReq struct { // Tumblebug
Name string `json:"name"`
ConnectionName string `json:"connectionName"`
VNetId string `json:"vNetId"`
Name string `json:"name" validate:"required"`
ConnectionName string `json:"connectionName" validate:"required"`
VNetId string `json:"vNetId" validate:"required"`
Description string `json:"description"`
FirewallRules *[]SpiderSecurityRuleInfo `json:"firewallRules"`
FirewallRules *[]SpiderSecurityRuleInfo `json:"firewallRules" validate:"required"`
}

func TbSecurityGroupReqStructLevelValidation(sl validator.StructLevel) {

u := sl.Current().Interface().(TbSecurityGroupReq)

err := common.CheckString(u.Name)
if err != nil {
// ReportError(field interface{}, fieldName, structFieldName, tag, param string)
sl.ReportError(u.Name, "name", "Name", "NotObeyingNamingConvention", "")
}
}

type TbSecurityGroupInfo struct { // Tumblebug
Expand Down Expand Up @@ -86,12 +98,39 @@ func CreateSecurityGroup(nsId string, u *TbSecurityGroupReq) (TbSecurityGroupInf
common.CBLog.Error(err)
return temp, err
}
err = common.CheckString(u.Name)

// returns InvalidValidationError for bad validation input, nil or ValidationErrors ( []FieldError )
err = validate.Struct(u)
if err != nil {

// this check is only needed when your code could produce
// an invalid value for validation such as interface with nil
// value most including myself do not usually have code like this.
if _, ok := err.(*validator.InvalidValidationError); ok {
fmt.Println(err)
temp := TbSecurityGroupInfo{}
return temp, err
}

// for _, err := range err.(validator.ValidationErrors) {

// fmt.Println(err.Namespace()) // can differ when a custom TagNameFunc is registered or
// fmt.Println(err.Field()) // by passing alt name to ReportError like below
// fmt.Println(err.StructNamespace())
// fmt.Println(err.StructField())
// fmt.Println(err.Tag())
// fmt.Println(err.ActualTag())
// fmt.Println(err.Kind())
// fmt.Println(err.Type())
// fmt.Println(err.Value())
// fmt.Println(err.Param())
// fmt.Println()
// }

temp := TbSecurityGroupInfo{}
common.CBLog.Error(err)
return temp, err
}

check, err := CheckResource(nsId, resourceType, u.Name)

if check {
Expand Down
Loading

0 comments on commit bc8e114

Please sign in to comment.