File: endpoint.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 (127 lines) | stat: -rw-r--r-- 4,690 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
package lb_domains

import (
	"fmt"
	"net/http"

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

var _ ChildSpec = &Site{}

type SiteAttributeMeta struct {
	AttributeMeta
	SiteResourceName string
}

type MonitoringEndpoint struct {
	MonitoringResourceName string `update:"resource_name" create:"resource_name" apply:"resource_name"`
	Enabled                bool   `create:"enabled" update:"enabled" apply:"enabled"`
	Monitoring             *Monitoring
}

func (m *MonitoringEndpoint) UnmarshalJSON(bs []byte) error {
	enabled := struct {
		Enabled bool `read:"enabled"`
	}{}
	var monitoring Monitoring
	if err := api.UnmarshalRead(bs, &enabled); err != nil {
		return fmt.Errorf("failed to parse MonitoringEndpoint")
	}
	if err := api.UnmarshalRead(bs, &monitoring); err != nil {
		return fmt.Errorf("failed to parse MonitoringEndpoint")
	}
	m.MonitoringResourceName = monitoring.ResourceName
	m.Enabled = enabled.Enabled
	m.Monitoring = &monitoring

	return nil
}

type EndpointRdata struct {
	Value string `read:"value" create:"value" update:"value" apply:"value"`
}

// +k8s:deepcopy-gen:interfaces=github.com/mimuret/golang-iij-dpf/pkg/api.Object
type Endpoint struct {
	SiteAttributeMeta
	ResourceName     string               `read:"resource_name" create:"resource_name,omitempty" apply:"resource_name"`
	Name             string               `read:"name" create:"name" update:"name" apply:"name"`
	MonitoringTarget string               `read:"monitoring_target" create:"monitoring_target" update:"monitoring_target" apply:"monitoring_target"`
	Description      string               `read:"description" create:"description" update:"description" apply:"description"`
	Weight           uint8                `read:"weight" create:"weight" update:"weight" apply:"weight"`
	ManualFaillback  bool                 `read:"manual_failback" create:"manual_failback" update:"manual_failback" apply:"manual_failback"`
	ManualFaillOver  bool                 `read:"manual_failover" create:"manual_failover" update:"manual_failover" apply:"manual_failover"`
	Enabled          bool                 `read:"enabled" create:"enabled" update:"enabled" apply:"enabled"`
	LiveStatus       Status               `read:"live_status"`
	ReadyStatus      Status               `read:"ready_status"`
	Rdata            []EndpointRdata      `read:"rdata" create:"rdata" update:"rdata" apply:"rdata"`
	Monitorings      []MonitoringEndpoint `read:"monitorings" create:"monitorings" update:"monitorings" apply:"monitorings"`
}

func (c *Endpoint) Fix() {
	if c.Weight == 0 {
		c.Weight = uint8(1)
	}
}

func (c *Endpoint) Init() {
	for i := range c.Monitorings {
		if c.Monitorings[i].Monitoring != nil {
			c.Monitorings[i].Monitoring.AttributeMeta = c.AttributeMeta
		}
	}
}

func (c *Endpoint) GetName() string                     { return "endpoints" }
func (c *Endpoint) GetResourceName() string             { return c.ResourceName }
func (c *Endpoint) SetResourceName(resourceName string) { c.ResourceName = resourceName }
func (c *Endpoint) GetPathMethod(action api.Action) (string, string) {
	switch action {
	case api.ActionCreate:
		return action.ToMethod(), fmt.Sprintf("/lb_domains/%s/sites/%s/endpoints", c.GetLBDoaminID(), c.SiteResourceName)
	case api.ActionRead, api.ActionUpdate, api.ActionDelete:
		return action.ToMethod(), fmt.Sprintf("/lb_domains/%s/sites/%s/endpoints/%s", c.GetLBDoaminID(), c.SiteResourceName, c.ResourceName)
	}
	return "", ""
}

func (c *Endpoint) SetPathParams(args ...interface{}) error {
	return apis.SetPathParams(args, &c.LBDomainID, &c.SiteResourceName, &c.ResourceName)
}

var _ ListSpec = &EndpointList{}

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

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

func (c *EndpointList) GetPathMethod(action api.Action) (string, string) {
	if action == api.ActionList {
		return http.MethodGet, fmt.Sprintf("/lb_domains/%s/sites/%s/endpoints", c.GetLBDoaminID(), c.SiteResourceName)
	}
	return "", ""
}

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

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

func init() {
	register(&Endpoint{}, &EndpointList{})
}