-
Notifications
You must be signed in to change notification settings - Fork 88
/
license_types.go
146 lines (118 loc) · 4.22 KB
/
license_types.go
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
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright 2019 Replicated, Inc..
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1beta1
import (
"encoding/json"
"fmt"
"strconv"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
Int Type = iota
String
Bool
)
type Type int
type EntitlementValue struct {
Type Type `json:"-"`
IntVal int64 `json:"-"`
StrVal string `json:"-"`
BoolVal bool `json:"-"`
}
func (entitlementValue *EntitlementValue) Value() interface{} {
if entitlementValue.Type == Int {
return entitlementValue.IntVal
} else if entitlementValue.Type == Bool {
return entitlementValue.BoolVal
}
return entitlementValue.StrVal
}
func (entitlementValue *EntitlementValue) MarshalJSON() ([]byte, error) {
switch entitlementValue.Type {
case Int:
return json.Marshal(entitlementValue.IntVal)
case String:
return json.Marshal(entitlementValue.StrVal)
case Bool:
return json.Marshal(entitlementValue.BoolVal)
default:
return []byte{}, fmt.Errorf("impossible EntitlementValue.Type")
}
}
func (entitlementValue *EntitlementValue) UnmarshalJSON(value []byte) error {
if value[0] == '"' {
entitlementValue.Type = String
return json.Unmarshal(value, &entitlementValue.StrVal)
}
intValue, err := strconv.ParseInt(string(value), 10, 64)
if err == nil {
entitlementValue.Type = Int
entitlementValue.IntVal = intValue
return nil
}
boolValue, err := strconv.ParseBool(string(value))
if err == nil {
entitlementValue.Type = Bool
entitlementValue.BoolVal = boolValue
return nil
}
return errors.New("unknown license value type")
}
type EntitlementField struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Value EntitlementValue `json:"value,omitempty"`
ValueType string `json:"valueType,omitempty"`
IsHidden bool `json:"isHidden,omitempty"`
}
// LicenseSpec defines the desired state of LicenseSpec
type LicenseSpec struct {
Signature []byte `json:"signature"`
AppSlug string `json:"appSlug"`
Endpoint string `json:"endpoint,omitempty"`
CustomerName string `json:"customerName,omitempty"`
ChannelID string `json:"channelID,omitempty"`
ChannelName string `json:"channelName,omitempty"`
LicenseSequence int64 `json:"licenseSequence,omitempty"`
LicenseID string `json:"licenseID"`
LicenseType string `json:"licenseType,omitempty"`
IsAirgapSupported bool `json:"isAirgapSupported,omitempty"`
IsGitOpsSupported bool `json:"isGitOpsSupported,omitempty"`
IsSnapshotSupported bool `json:"isSnapshotSupported,omitempty"`
Entitlements map[string]EntitlementField `json:"entitlements,omitempty"`
}
// LicenseStatus defines the observed state of License
type LicenseStatus struct {
}
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// License is the Schema for the license API
// +k8s:openapi-gen=true
// +kubebuilder:subresource:status
type License struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec LicenseSpec `json:"spec,omitempty"`
Status LicenseStatus `json:"status,omitempty"`
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// LicenseList contains a list of Licenses
type LicenseList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []License `json:"items"`
}
func init() {
SchemeBuilder.Register(&License{}, &LicenseList{})
}