File: contract.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 (122 lines) | stat: -rw-r--r-- 3,389 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
package core

import (
	"fmt"
	"net/url"
	"strconv"

	"github.com/google/go-querystring/query"
	"github.com/mimuret/golang-iij-dpf/pkg/api"
	"github.com/mimuret/golang-iij-dpf/pkg/apis"
	"github.com/mimuret/golang-iij-dpf/pkg/types"
)

type Plan int

const (
	PlanBasic   Plan = 1
	PlanPremium Plan = 2
)

func (c Plan) String() string {
	planToString := map[Plan]string{
		PlanBasic:   "basic",
		PlanPremium: "premium",
	}

	return planToString[c]
}

// +k8s:deepcopy-gen=false
type KeywordsPlan []Plan

func (c KeywordsPlan) EncodeValues(key string, v *url.Values) error {
	for _, plan := range c {
		v.Add(key, strconv.Itoa(int(plan)))
	}
	return nil
}

var _ apis.Spec = &Contract{}

// +k8s:deepcopy-gen:interfaces=github.com/mimuret/golang-iij-dpf/pkg/api.Object
type Contract struct {
	AttributeMeta
	ID          string         `read:"id"`
	ServiceCode string         `read:"service_code"`
	State       types.State    `read:"state"`
	Favorite    types.Favorite `read:"favorite" update:"favorite"`
	Plan        Plan           `read:"plan"`
	Description string         `read:"description" update:"description"`
}

func (c *Contract) GetName() string { return "contracts" }
func (c *Contract) GetPathMethod(action api.Action) (string, string) {
	switch action {
	case api.ActionRead, api.ActionUpdate:
		return action.ToMethod(), fmt.Sprintf("/contracts/%s", c.ID)
	}
	return "", ""
}

func (c *Contract) SetPathParams(args ...interface{}) error {
	return apis.SetPathParams(args, &c.ID)
}

var _ apis.CountableListSpec = &ContractList{}

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

func (c *ContractList) GetName() string         { return "contracts" }
func (c *ContractList) GetItems() interface{}   { return &c.Items }
func (c *ContractList) Len() int                { return len(c.Items) }
func (c *ContractList) Index(i int) interface{} { return c.Items[i] }
func (c *ContractList) GetMaxLimit() int32      { return 10000 }
func (c *ContractList) ClearItems()             { c.Items = []Contract{} }
func (c *ContractList) AddItem(v interface{}) bool {
	if a, ok := v.(Contract); ok {
		c.Items = append(c.Items, a)
		return true
	}
	return false
}

func (c *ContractList) GetPathMethod(action api.Action) (string, string) {
	switch action {
	case api.ActionList:
		return action.ToMethod(), "/contracts"
	case api.ActionCount:
		return action.ToMethod(), "/contracts/count"
	}
	return "", ""
}

func (c *ContractList) SetPathParams(args ...interface{}) error {
	return nil
}

func (c *ContractList) Init() {}

var _ api.SearchParams = &ContractListSearchKeywords{}

// +k8s:deepcopy-gen=false
type ContractListSearchKeywords struct {
	api.CommonSearchParams
	FullText    api.KeywordsString   `url:"_keywords_full_text[],omitempty"`
	ServiceCode api.KeywordsString   `url:"_keywords_service_code[],omitempty"`
	Plan        KeywordsPlan         `url:"_keywords_plan[],omitempty"`
	State       api.KeywordsState    `url:"_keywords_state[],omitempty"`
	Favorite    api.KeywordsFavorite `url:"_keywords_favorite[],omitempty"`
	Description api.KeywordsString   `url:"_keywords_description[],omitempty"`
}

func (s *ContractListSearchKeywords) GetValues() (url.Values, error) { return query.Values(s) }

func init() {
	register(&Contract{}, &ContractList{})
}