File size: 4,092 Bytes
d6f631f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package subscription

import (
	"errors"

	"github.com/samber/lo"

	"github.com/openmeterio/openmeter/pkg/models"
)

const (
	// AnnotationSubscriptionID is the ID of the subscription that created this entitlement
	AnnotationSubscriptionID = "subscription.id"

	AnnotationOwnerSubSystem = "subscription.owner"

	AnnotationBooleanEntitlementCount = "subscription.entitlement.boolean.count"

	// AnnotationPreviousSubscriptionID is the ID of the subscription that was superseded by this subscription
	AnnotationPreviousSubscriptionID = "subscription.previous.id"

	// AnnotationSupersedingSubscriptionID is the ID of the subscription that supersedes this subscription
	AnnotationSupersedingSubscriptionID = "subscription.superseding.id"
)

const OwnerSubscriptionSubSystem = "subscription"

type annotationParser struct{}

var AnnotationParser = annotationParser{}

func (a annotationParser) HasSubscription(annotations models.Annotations) bool {
	subId, ok := annotations[AnnotationSubscriptionID]
	return ok && subId != nil
}

func (a annotationParser) ListOwnerSubSystems(annotations models.Annotations) []string {
	if annotations == nil {
		return nil
	}

	systems, ok := annotations[AnnotationOwnerSubSystem]
	if !ok {
		return nil
	}

	var systemsStr []string
	systemsArr, ok := systems.([]interface{})
	if !ok {
		return nil
	}

	for _, system := range systemsArr {
		systemStr, ok := system.(string)
		if !ok {
			return nil
		}
		systemsStr = append(systemsStr, systemStr)
	}

	return systemsStr
}

func (a annotationParser) AddOwnerSubSystem(annotations models.Annotations, system string) (models.Annotations, error) {
	if annotations == nil {
		return nil, errors.New("annotations are nil")
	}

	systems := a.ListOwnerSubSystems(annotations)
	if lo.Contains(systems, system) {
		return annotations, nil
	}

	systems = append(systems, system)
	annotations[AnnotationOwnerSubSystem] = systems

	return annotations, nil
}

func (a annotationParser) GetBooleanEntitlementCount(annotations models.Annotations) int {
	count, ok := annotations[AnnotationBooleanEntitlementCount]
	if !ok {
		return 0
	}

	countInt, ok := count.(int)
	if !ok {
		countFloat, ok := count.(float64)
		if !ok {
			return 0
		}

		countInt = int(countFloat)
	}

	return countInt
}

func (a annotationParser) SetBooleanEntitlementCount(annotations models.Annotations, count int) (models.Annotations, error) {
	if annotations == nil {
		return nil, errors.New("annotations are nil")
	}
	annotations[AnnotationBooleanEntitlementCount] = count
	return annotations, nil
}

func (a annotationParser) GetPreviousSubscriptionID(annotations models.Annotations) *string {
	if annotations == nil {
		return nil
	}

	prevID, ok := annotations[AnnotationPreviousSubscriptionID]
	if !ok {
		return nil
	}

	prevIDStr, ok := prevID.(string)
	if !ok {
		return nil
	}

	return &prevIDStr
}

func (a annotationParser) SetPreviousSubscriptionID(annotations models.Annotations, subscriptionID string) (models.Annotations, error) {
	if annotations == nil {
		return nil, errors.New("annotations are nil")
	}
	annotations[AnnotationPreviousSubscriptionID] = subscriptionID
	return annotations, nil
}

func (a annotationParser) GetSupersedingSubscriptionID(annotations models.Annotations) *string {
	if annotations == nil {
		return nil
	}

	supersedingID, ok := annotations[AnnotationSupersedingSubscriptionID]
	if !ok {
		return nil
	}

	supersedingIDStr, ok := supersedingID.(string)
	if !ok {
		return nil
	}

	return &supersedingIDStr
}

func (a annotationParser) SetSupersedingSubscriptionID(annotations models.Annotations, subscriptionID string) (models.Annotations, error) {
	if annotations == nil {
		return nil, errors.New("annotations are nil")
	}
	annotations[AnnotationSupersedingSubscriptionID] = subscriptionID
	return annotations, nil
}

func (a annotationParser) ClearSupersedingSubscriptionID(annotations models.Annotations) (models.Annotations, error) {
	if annotations == nil {
		return nil, errors.New("annotations are nil")
	}
	delete(annotations, AnnotationSupersedingSubscriptionID)
	return annotations, nil
}