File: rule_method.go

package info (click to toggle)
golang-github-mimuret-golang-iij-dpf 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,340 kB
  • sloc: makefile: 55
file content (135 lines) | stat: -rw-r--r-- 3,977 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package lb_domains

import (
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/mimuret/golang-iij-dpf/pkg/api"
	"github.com/mimuret/golang-iij-dpf/pkg/apis"
)

var _ Spec = &RuleMethod{}

type RuleAttributeMeta struct {
	AttributeMeta
	RuleResourceName string
}

type RuleMethodProps interface {
	Fix()
	GetMType() RuleMethodMType
	GetMethodResourceName() string
	SetMethodResourceName(string)
	DeepCopyRuleMethodProps() RuleMethodProps
}

// +k8s:deepcopy-gen:interfaces=github.com/mimuret/golang-iij-dpf/pkg/api.Object
type RuleMethod struct {
	RuleAttributeMeta
	Priority *uint           `create:"priority,omitempty" update:"priority,omitempty" apply:"priority,omitempty"`
	Method   RuleMethodProps `create:"method" update:"method" apply:"method"`
}

func (c *RuleMethod) Fix() {
	if c.Method != nil {
		c.Method.Fix()
	}
}

func (c *RuleMethod) UnmarshalJSON(bs []byte) error {
	r := struct {
		Priority *uint           `read:"priority"`
		Method   json.RawMessage `read:"method"`
	}{}
	if err := api.UnmarshalRead(bs, &r); err != nil {
		return fmt.Errorf("failed to parse RuleMethod: %w", err)
	}
	propsCommon := &RuleMethodPropsCommon{}
	if err := api.UnmarshalRead(r.Method, propsCommon); err != nil {
		return fmt.Errorf("failed to parse Method: %w", err)
	}

	var props RuleMethodProps
	switch propsCommon.MType {
	case "entry_a":
		props = &RuleMethodEntryA{}
	case "entry_aaaa":
		props = &RuleMethodEntryAAAA{}
	case "entry_cname":
		props = &RuleMethodEntryCNAME{}
	case "exit_site":
		props = &RuleMethodExitSite{}
	case "exit_sorry":
		props = &RuleMethodExitSorry{}
	case "failover":
		props = &RuleMethodFailover{}
	default:
		return fmt.Errorf("unknown mtype `%s`", propsCommon.MType)
	}
	if err := api.UnmarshalRead(r.Method, props); err != nil {
		return fmt.Errorf("failed to parse props: %w", err)
	}
	c.Priority = r.Priority
	c.Method = props
	return nil
}

func (c *RuleMethod) GetName() string               { return "rule_methods" }
func (c *RuleMethod) GetMethodResourceName() string { return c.Method.GetMethodResourceName() }
func (c *RuleMethod) SetMethodResourceName(resourceName string) {
	c.Method.SetMethodResourceName(resourceName)
}

func (c *RuleMethod) GetPathMethod(action api.Action) (string, string) {
	switch action {
	case api.ActionCreate:
		return action.ToMethod(), fmt.Sprintf("/lb_domains/%s/rules/%s/rule_methods", c.GetLBDoaminID(), c.RuleResourceName)
	case api.ActionRead, api.ActionUpdate, api.ActionDelete:
		return action.ToMethod(), fmt.Sprintf("/lb_domains/%s/rules/%s/rule_methods/%s", c.GetLBDoaminID(), c.RuleResourceName, c.Method.GetMethodResourceName())
	}
	return "", ""
}

func (c *RuleMethod) SetPathParams(args ...interface{}) error {
	var methodResourceName string
	if err := apis.SetPathParams(args, &c.LBDomainID, &c.RuleResourceName, &methodResourceName); err != nil {
		return err
	}
	c.SetMethodResourceName(methodResourceName)
	return nil
}

var _ ListSpec = &MonitoringList{}

// +k8s:deepcopy-gen:interfaces=github.com/mimuret/golang-iij-dpf/pkg/api.Object
type RuleMethodList struct {
	RuleAttributeMeta
	Items []RuleMethod `read:"items"`
}

func (c *RuleMethodList) GetName() string         { return "rule_methods" }
func (c *RuleMethodList) GetItems() interface{}   { return &c.Items }
func (c *RuleMethodList) Len() int                { return len(c.Items) }
func (c *RuleMethodList) Index(i int) interface{} { return c.Items[i] }

func (c *RuleMethodList) GetPathMethod(action api.Action) (string, string) {
	if action == api.ActionList {
		return http.MethodGet, fmt.Sprintf("/lb_domains/%s/rules/%s/rule_methods", c.GetLBDoaminID(), c.RuleResourceName)
	}
	return "", ""
}

func (c *RuleMethodList) Init() {
	for i := range c.Items {
		c.Items[i].RuleAttributeMeta = c.RuleAttributeMeta
	}
}

func (c *RuleMethodList) SetPathParams(args ...interface{}) error {
	return apis.SetPathParams(args, &c.LBDomainID, &c.RuleResourceName)
}

func init() {
	register(&RuleMethod{}, &RuleMethodList{})
}