@@ -60,7 +60,7 @@ type ApiDefinitionStatus struct {
60
60
base.Status `json:",inline"`
61
61
}
62
62
63
- var _ custom.ApiDefinition = & ApiDefinition {}
63
+ var _ custom.ApiDefinitionResource = & ApiDefinition {}
64
64
var _ custom.Status = & ApiDefinitionStatus {}
65
65
var _ custom.Spec = & ApiDefinitionV2Spec {}
66
66
@@ -81,39 +81,6 @@ type ApiDefinition struct {
81
81
Status ApiDefinitionStatus `json:"status,omitempty"`
82
82
}
83
83
84
- // PickID returns the ID of the API definition, when a context has been defined at the spec level.
85
- // The ID might be returned from the API status, meaning that the API is already known.
86
- // If the API is unknown, the ID is either given from the spec if given,
87
- // or generated from the API UID and the context key to ensure uniqueness
88
- // in case the API is replicated on a same APIM instance.
89
- func (api * ApiDefinition ) PickID () string {
90
- if api .Status .ID != "" {
91
- return api .Status .ID
92
- }
93
-
94
- if api .Spec .ID != "" {
95
- return api .Spec .ID
96
- }
97
-
98
- return string (api .UID )
99
- }
100
-
101
- func (api * ApiDefinition ) PickCrossID () string {
102
- if api .Status .CrossID != "" {
103
- return api .Status .CrossID
104
- }
105
-
106
- return api .GetOrGenerateCrossID ()
107
- }
108
-
109
- func (api * ApiDefinition ) GetOrGenerateCrossID () string {
110
- if api .Spec .CrossID != "" {
111
- return api .Spec .CrossID
112
- }
113
-
114
- return uuid .FromStrings (api .GetNamespacedName ().String ())
115
- }
116
-
117
84
func (api * ApiDefinition ) GetNamespacedName () * refs.NamespacedName {
118
85
return & refs.NamespacedName {Namespace : api .Namespace , Name : api .Name }
119
86
}
@@ -169,6 +136,79 @@ func (api *ApiDefinition) GetContextPaths() ([]string, error) {
169
136
return api .Spec .GetContextPaths ()
170
137
}
171
138
139
+ func (api * ApiDefinition ) GetDefinition () custom.ApiDefinition {
140
+ return & api .Spec .Api
141
+ }
142
+
143
+ func (api * ApiDefinition ) PopulateIDs (_ custom.Context ) {
144
+ api .Spec .ID = api .pickID ()
145
+ api .Spec .CrossID = api .pickCrossID ()
146
+ api .generateEmptyPlanCrossIds ()
147
+ api .generatePageIDs ()
148
+ }
149
+
150
+ // For each plan, generate a CrossId from Api Id & Plan Name if not defined.
151
+ func (api * ApiDefinition ) generateEmptyPlanCrossIds () {
152
+ plans := api .Spec .Plans
153
+
154
+ for _ , plan := range plans {
155
+ if plan .CrossId == "" {
156
+ plan .CrossId = uuid .FromStrings (api .Spec .ID , separator , plan .Name )
157
+ }
158
+ }
159
+ }
160
+
161
+ func (api * ApiDefinition ) generatePageIDs () {
162
+ spec := & api .Spec
163
+ pages := spec .Pages
164
+ for name , page := range pages {
165
+ page .API = spec .ID
166
+ apiName := api .GetNamespacedName ().String ()
167
+ if page .CrossID == "" {
168
+ page .CrossID = uuid .FromStrings (apiName , separator , name )
169
+ }
170
+ if page .ID == "" {
171
+ page .ID = uuid .FromStrings (spec .ID , separator , name )
172
+ }
173
+ if page .Parent != "" {
174
+ page .ParentID = uuid .FromStrings (spec .ID , separator , page .Parent )
175
+ }
176
+ }
177
+ }
178
+
179
+ // PickID returns the ID of the API definition, when a context has been defined at the spec level.
180
+ // The ID might be returned from the API status, meaning that the API is already known.
181
+ // If the API is unknown, the ID is either given from the spec if given,
182
+ // or generated from the API UID and the context key to ensure uniqueness
183
+ // in case the API is replicated on a same APIM instance.
184
+ func (api * ApiDefinition ) pickID () string {
185
+ if api .Status .ID != "" {
186
+ return api .Status .ID
187
+ }
188
+
189
+ if api .Spec .ID != "" {
190
+ return api .Spec .ID
191
+ }
192
+
193
+ return string (api .UID )
194
+ }
195
+
196
+ func (api * ApiDefinition ) pickCrossID () string {
197
+ if api .Status .CrossID != "" {
198
+ return api .Status .CrossID
199
+ }
200
+
201
+ return api .getOrGenerateCrossID ()
202
+ }
203
+
204
+ func (api * ApiDefinition ) getOrGenerateCrossID () string {
205
+ if api .Spec .CrossID != "" {
206
+ return api .Spec .CrossID
207
+ }
208
+
209
+ return uuid .FromStrings (api .GetNamespacedName ().String ())
210
+ }
211
+
172
212
func (spec * ApiDefinitionV2Spec ) Hash () string {
173
213
return hash .Calculate (spec )
174
214
}
0 commit comments