| 9 |
| 10 | type VisitorDefinition struct { |
| 11 | Name string `json:"name"` |
| 12 | Type string `json:"type"` |
| 13 |
| 14 | STCP *v1.STCPVisitorConfig `json:"stcp,omitempty"` |
| 15 | SUDP *v1.SUDPVisitorConfig `json:"sudp,omitempty"` |
| 16 | XTCP *v1.XTCPVisitorConfig `json:"xtcp,omitempty"` |
| 17 | } |
| 18 |
| 19 | func (p *VisitorDefinition) Validate(pathName string, isUpdate bool) error { |
| 20 | if strings.TrimSpace(p.Name) == "" { |
| 21 | return fmt.Errorf("visitor name is required") |
| 22 | } |
| 23 | if !IsVisitorType(p.Type) { |
| 24 | return fmt.Errorf("invalid visitor type: %s", p.Type) |
| 25 | } |
| 26 | if isUpdate && pathName != "" && pathName != p.Name { |
| 27 | return fmt.Errorf("visitor name in URL must match name in body") |
| 28 | } |
| 29 |
| 30 | _, blockType, blockCount := p.activeBlock() |
| 31 | if blockCount != 1 { |
| 32 | return fmt.Errorf("exactly one visitor type block is required") |
| 33 | } |
| 34 | if blockType != p.Type { |
| 35 | return fmt.Errorf("visitor type block %q does not match type %q", blockType, p.Type) |
| 36 | } |
| 37 | return nil |
| 38 | } |
| 39 |
| 40 | func (p *VisitorDefinition) ToConfigurer() (v1.VisitorConfigurer, error) { |
| 41 | block, _, _ := p.activeBlock() |
| 42 | if block == nil { |
| 43 | return nil, fmt.Errorf("exactly one visitor type block is required") |
| 44 | } |
| 45 |
| 46 | cfg := block |
| 47 | cfg.GetBaseConfig().Name = p.Name |
| 48 | cfg.GetBaseConfig().Type = p.Type |
| 49 | return cfg, nil |
| 50 | } |
| 51 |
| 52 | func VisitorDefinitionFromConfigurer(cfg v1.VisitorConfigurer) (VisitorDefinition, error) { |
| 53 | if cfg == nil { |
| 54 | return VisitorDefinition{}, fmt.Errorf("visitor config is nil") |
| 55 | } |
| 56 |
| 57 | base := cfg.GetBaseConfig() |
| 58 | payload := VisitorDefinition{ |
| 59 | Name: base.Name, |
| 60 | Type: base.Type, |
| 61 | } |
| 62 |
| 63 | switch c := cfg.(type) { |
| 64 | case *v1.STCPVisitorConfig: |
| 65 | payload.STCP = c |
| 66 | case *v1.SUDPVisitorConfig: |
| 67 | payload.SUDP = c |
| 68 | case *v1.XTCPVisitorConfig: |
| 69 | payload.XTCP = c |
| 70 | default: |
| 71 | return VisitorDefinition{}, fmt.Errorf("unsupported visitor configurer type %T", cfg) |
| 72 | } |
| 73 |
| 74 | return payload, nil |
| 75 | } |
| 76 |
| 77 | func (p *VisitorDefinition) activeBlock() (v1.VisitorConfigurer, string, int) { |
| 78 | count := 0 |
| 79 | var block v1.VisitorConfigurer |
| 80 | var blockType string |
| 81 |
| 82 | if p.STCP != nil { |
| 83 | count++ |
| 84 | block = p.STCP |
| 85 | blockType = "stcp" |
| 86 | } |
| 87 | if p.SUDP != nil { |
| 88 | count++ |
| 89 | block = p.SUDP |
| 90 | blockType = "sudp" |
| 91 | } |
| 92 | if p.XTCP != nil { |
| 93 | count++ |
| 94 | block = p.XTCP |
| 95 | blockType = "xtcp" |
| 96 | } |
| 97 | return block, blockType, count |
| 98 | } |
| 99 |
| 100 | func IsVisitorType(typ string) bool { |
| 101 | switch typ { |
| 102 | case "stcp", "sudp", "xtcp": |
| 103 | return true |
| 104 | default: |
| 105 | return false |
| 106 | } |
| 107 | } |