forked from bf2fc6cc711aee1a0c2a/kas-fleet-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_server.go
1207 lines (1077 loc) · 54.2 KB
/
api_server.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package mocks
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httptest"
"reflect"
"sync"
amsv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
authorizationsv1 "github.com/openshift-online/ocm-sdk-go/authorizations/v1"
"k8s.io/apimachinery/pkg/util/wait"
"time"
ocmErrors "github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/pkg/errors"
"github.com/gorilla/mux"
clustersmgmtv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
v1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
)
const (
// EndpointPathClusters ocm clusters management service clusters endpoint
EndpointPathClusters = "/api/clusters_mgmt/v1/clusters"
// EndpointPathCluster ocm clusters management service clusters endpoint
EndpointPathCluster = "/api/clusters_mgmt/v1/clusters/{id}"
// EndpointPathClusterIdentityProviders ocm clusters management service clusters identity provider create endpoint
EndpointPathClusterIdentityProviders = "/api/clusters_mgmt/v1/clusters/{id}/identity_providers"
// EndpointPathClusterIdentityProvider ocm clusters management service clusters identity provider update endpoint
EndpointPathClusterIdentityProvider = "/api/clusters_mgmt/v1/clusters/{id}/identity_providers/{idp_id}"
// EndpointPathSyncsets ocm clusters management service syncset endpoint
EndpointPathSyncsets = "/api/clusters_mgmt/v1/clusters/{id}/external_configuration/syncsets"
// EndpointPathSyncset ocm clusters management service syncset endpoint
EndpointPathSyncset = "/api/clusters_mgmt/v1/clusters/{id}/external_configuration/syncsets/{syncsetID}"
// EndpointPathIngresses ocm cluster management ingress endpoint
EndpointPathIngresses = "/api/clusters_mgmt/v1/clusters/{id}/ingresses"
// EndpointPathCloudProviders ocm cluster management cloud providers endpoint
EndpointPathCloudProviders = "/api/clusters_mgmt/v1/cloud_providers"
// EndpointPathCloudProvider ocm cluster management cloud provider endpoint
EndpointPathCloudProvider = "/api/clusters_mgmt/v1/cloud_providers/{id}"
// EndpointPathCloudProviderRegions ocm cluster management cloud provider regions endpoint
EndpointPathCloudProviderRegions = "/api/clusters_mgmt/v1/cloud_providers/{id}/regions"
// EndpointPathCloudProviderRegion ocm cluster management cloud provider region endpoint
EndpointPathCloudProviderRegion = "/api/clusters_mgmt/v1/cloud_providers/{providerID}/regions/{regionID}"
// EndpointPathClusterStatus ocm cluster management cluster status endpoint
EndpointPathClusterStatus = "/api/clusters_mgmt/v1/clusters/{id}/status"
// EndpointPathClusterAddons ocm cluster management cluster addons endpoint
EndpointPathClusterAddons = "/api/clusters_mgmt/v1/clusters/{id}/addons"
// EndpointPathMachinePools ocm cluster management machine pools endpoint
EndpointPathMachinePools = "/api/clusters_mgmt/v1/clusters/{id}/machine_pools"
// EndpointPathMachinePool ocm cluster management machine pool endpoint
EndpointPathMachinePool = "/api/clusters_mgmt/v1/clusters/{id}/machine_pools/{machinePoolId}"
// EndpointPathAddonInstallations ocm cluster addon installations endpoint
EndpointPathAddonInstallations = "/api/clusters_mgmt/v1/clusters/{id}/addons"
// EndpointPathAddonInstallation ocm cluster addon installation endpoint
EndpointPathAddonInstallation = "/api/clusters_mgmt/v1/clusters/{id}/addons/{addoninstallationId}"
// EndpointPathKasFleetshardOperatorAddonInstallation ocm cluster kas-fleetshard-operator-qe addon installation endpoint
EndpointPathKasFleetshardOperatorAddonInstallation = "/api/clusters_mgmt/v1/clusters/{id}/addons/kas-fleetshard-operator-qe"
// EndpointPathClusterLoggingOperatorAddonInstallation ocm cluster cluster-logging-operator addon installation endpoint
EndpointPathClusterLoggingOperatorAddonInstallation = "/api/clusters_mgmt/v1/clusters/{id}/addons/cluster-logging-operator"
EndpointPathClusterAuthorization = "/api/accounts_mgmt/v1/cluster_authorizations"
EndpointPathSubscription = "/api/accounts_mgmt/v1/subscriptions/{id}"
EndpointPathSubscriptionSearch = "/api/accounts_mgmt/v1/subscriptions"
EndpointPathServiceAccount = "/api/accounts_mgmt/v1/current_account"
EndpointPathOrganisationQuotaCost = "/api/accounts_mgmt/v1/organizations/{orgId}/quota_cost"
EndpointPathOrganisationList = "/api/accounts_mgmt/v1/organizations"
EndpointPathTermsReview = "/api/authorizations/v1/terms_review"
// Default values for getX functions
// MockClusterID default mock cluster id used in the mock ocm server
MockClusterID = "2aad9fc1-c40e-471f-8d57-fdaecc7d3686"
// MockCloudProviderID default mock provider ID
MockCloudProviderID = "aws"
// MockClusterExternalID default mock cluster external ID
MockClusterExternalID = "2aad9fc1-c40e-471f-8d57-fdaecc7d3686"
// MockClusterState default mock cluster state
MockClusterState = clustersmgmtv1.ClusterStateReady
// MockCloudProviderDisplayName default mock provider display name
MockCloudProviderDisplayName = "AWS"
// MockCloudRegionID default mock cluster region
MockCloudRegionID = "us-east-1"
// MockCloudRegionDisplayName default mock cloud region display name
MockCloudRegionDisplayName = "US East, N. Virginia"
// MockSyncsetID default mock syncset id used in the mock ocm server
MockSyncsetID = "ext-8a41f783-b5e4-4692-a7cd-c0b9c8eeede9"
// MockIngressID default mock ingress id used in the mock ocm server
MockIngressID = "s1h5"
// MockIngressDNS default mock ingress dns used in the mock ocm server
MockIngressDNS = "apps.mk-btq2d1h8d3b1.b3k3.s1.devshift.org"
// MockIngressHref default mock ingress HREF used in the mock ocm server
MockIngressHref = "/api/clusters_mgmt/v1/clusters/000/ingresses/i8y1"
// MockIngressListening default mock ingress listening used in the mock ocm server
MockIngressListening = clustersmgmtv1.ListeningMethodExternal
// MockClusterAddonID default mock cluster addon ID
MockClusterAddonID = "managed-kafka-qe"
// MockKasFleetshardAddonID default mock ID for the KAS Fleetshard Operator
MockKasFleetshardAddonID = "kas-fleetshard-operator-qe"
// MockClusterLoggingOperatorAddonID default mock ID for the Cluster Logging Operator
MockClusterLoggingOperatorAddonID = "cluster-logging-operator"
// MockClusterAddonState default mock cluster addon state
MockClusterAddonState = clustersmgmtv1.AddOnInstallationStateReady
// MockClusterAddonDescription default mock cluster addon description
MockClusterAddonDescription = "InstallWaiting"
// MockMachinePoolID default machine pool ID
MockMachinePoolID = "managed"
// MockMachinePoolReplicas default number of machine pool replicas
MockMachinePoolReplicas = 3
// MockOpenshiftVersion default cluster openshift version
MockOpenshiftVersion = "openshift-v4.6.1"
//MockMultiAZ default value
MockMultiAZ = true
//MockClusterComputeNodes default nodes
MockClusterComputeNodes = 3
// MockIdentityProviderID default identity provider ID
MockIdentityProviderID = "identity-provider-id"
//
MockSubID = "pphCb6sIQPqtjMtL0GQaX6i4bP"
// MockQuotaConsumed default quota consumed value returned by the get quota costs by organisation endpoint
MockQuotaConsumed = 1
// MockQuotaMaxAllowed default quota max allowed value returned by the get quota costs by organisation endpoint
MockQuotaMaxAllowed = 10
// MockQuotaId default quota id value returned by the get quota costs by organisation endpoint
MockQuotaId = "quota-id"
)
// variables for endpoints
var (
EndpointClusterGet = Endpoint{EndpointPathCluster, http.MethodGet}
EndpointClusterPatch = Endpoint{EndpointPathCluster, http.MethodPatch}
EndpointKafkaDelete = Endpoint{EndpointPathSyncset, http.MethodDelete}
EndpointClustersGet = Endpoint{EndpointPathClusters, http.MethodGet}
EndpointClustersPost = Endpoint{EndpointPathClusters, http.MethodPost}
EndpointClusterDelete = Endpoint{EndpointPathCluster, http.MethodDelete}
EndpointClusterSyncsetsPost = Endpoint{EndpointPathSyncsets, http.MethodPost}
EndpointClusterSyncsetGet = Endpoint{EndpointPathSyncset, http.MethodGet}
EndpointClusterSyncsetPatch = Endpoint{EndpointPathSyncset, http.MethodPatch}
EndpointClusterIngressGet = Endpoint{EndpointPathIngresses, http.MethodGet}
EndpointCloudProvidersGet = Endpoint{EndpointPathCloudProviders, http.MethodGet}
EndpointCloudProviderGet = Endpoint{EndpointPathCloudProvider, http.MethodGet}
EndpointCloudProviderRegionsGet = Endpoint{EndpointPathCloudProviderRegions, http.MethodGet}
EndpointCloudProviderRegionGet = Endpoint{EndpointPathCloudProviderRegion, http.MethodGet}
EndpointClusterStatusGet = Endpoint{EndpointPathClusterStatus, http.MethodGet}
EndpointClusterAddonsGet = Endpoint{EndpointPathClusterAddons, http.MethodGet}
EndpointClusterAddonPost = Endpoint{EndpointPathClusterAddons, http.MethodPost}
EndpointMachinePoolsGet = Endpoint{EndpointPathMachinePools, http.MethodGet}
EndpointMachinePoolPost = Endpoint{EndpointPathMachinePools, http.MethodPost}
EndpointMachinePoolPatch = Endpoint{EndpointPathMachinePool, http.MethodPatch}
EndpointMachinePoolGet = Endpoint{EndpointPathMachinePool, http.MethodGet}
EndpointServiceAccountGet = Endpoint{EndpointPathServiceAccount, http.MethodGet}
EndpointOrganizationQuotaCostGet = Endpoint{EndpointPathOrganisationQuotaCost, http.MethodGet}
EndpointOrganizationList = Endpoint{EndpointPathOrganisationList, http.MethodGet}
EndpointIdentityProviderPost = Endpoint{EndpointPathClusterIdentityProviders, http.MethodPost}
EndpointIdentityProviderPatch = Endpoint{EndpointPathClusterIdentityProvider, http.MethodPatch}
EndpointAddonInstallationsPost = Endpoint{EndpointPathAddonInstallations, http.MethodPost}
EndpointAddonInstallationGet = Endpoint{EndpointPathAddonInstallation, http.MethodGet}
EndpointAddonInstallationPatch = Endpoint{EndpointPathAddonInstallation, http.MethodPatch}
EndpointKasFleetshardOperatorAddonInstallationGet = Endpoint{EndpointPathKasFleetshardOperatorAddonInstallation, http.MethodGet}
EndpointKasFleetshardOperatorAddonInstallationPatch = Endpoint{EndpointPathKasFleetshardOperatorAddonInstallation, http.MethodPatch}
EndpointKasFleetshardOperatorAddonInstallationPost = Endpoint{EndpointPathKasFleetshardOperatorAddonInstallation, http.MethodPost}
EndpointClusterLoggingOperatorAddonInstallationGet = Endpoint{EndpointPathClusterLoggingOperatorAddonInstallation, http.MethodGet}
EndpointClusterLoggingOperatorAddonInstallationPatch = Endpoint{EndpointPathClusterLoggingOperatorAddonInstallation, http.MethodPatch}
EndpointClusterLoggingOperatorAddonInstallationPost = Endpoint{EndpointPathClusterLoggingOperatorAddonInstallation, http.MethodPost}
EndpointClusterAuthorizationPost = Endpoint{EndpointPathClusterAuthorization, http.MethodPost}
EndpointSubscriptionDelete = Endpoint{EndpointPathSubscription, http.MethodDelete}
EndpointSubscriptionSearch = Endpoint{EndpointPathSubscriptionSearch, http.MethodGet}
EndpointTermsReviewPost = Endpoint{EndpointPathTermsReview, http.MethodPost}
)
// variables for mocked ocm types
//
// these are the default types that will be returned by the emulated ocm api
// to override these values, do not set them directly e.g. mocks.MockSyncset = ...
// instead use the Set*Response functions provided by MockConfigurableServerBuilder e.g. SetClusterGetResponse(...)
var (
MockIdentityProvider *clustersmgmtv1.IdentityProvider
MockSyncset *clustersmgmtv1.Syncset
MockIngressList *clustersmgmtv1.IngressList
MockCloudProvider *clustersmgmtv1.CloudProvider
MockCloudProviderList *clustersmgmtv1.CloudProviderList
MockCloudProviderRegion *clustersmgmtv1.CloudRegion
MockCloudProviderRegionList *clustersmgmtv1.CloudRegionList
MockClusterStatus *clustersmgmtv1.ClusterStatus
MockClusterAddonInstallation *clustersmgmtv1.AddOnInstallation
MockClusterAddonInstallationList *clustersmgmtv1.AddOnInstallationList
MockKasFleetshardOperatorAddonInstallation *clustersmgmtv1.AddOnInstallation
MockKasClusterLoggingOperatorAddonInstallation *clustersmgmtv1.AddOnInstallation
MockMachinePoolList *clustersmgmtv1.MachinePoolList
MockMachinePool *clustersmgmtv1.MachinePool
MockCluster *clustersmgmtv1.Cluster
MockClusterAuthorization *amsv1.ClusterAuthorizationResponse
MockSubscription *amsv1.Subscription
MockOrganizationQuotaCost *amsv1.QuotaCostList
MockServiceAccount *amsv1.Account
MockSubscriptionSearch []*amsv1.Subscription
MockTermsReview *authorizationsv1.TermsReviewResponse
)
// routerSwapper is an http.Handler that allows you to swap mux routers.
type routerSwapper struct {
mu sync.Mutex
router *mux.Router
}
// Swap changes the old router with the new one.
func (rs *routerSwapper) Swap(newRouter *mux.Router) {
rs.mu.Lock()
rs.router = newRouter
rs.mu.Unlock()
}
var router *mux.Router
// rSwapper is required if any change to the Router for mocked OCM server is needed
var rSwapper *routerSwapper
// Endpoint is a wrapper around an endpoint and the method used to interact with that endpoint e.g. GET /clusters
type Endpoint struct {
Path string
Method string
}
// HandlerRegister is a cache that maps Endpoints to their handlers
type HandlerRegister map[Endpoint]func(w http.ResponseWriter, r *http.Request)
// MockConfigurableServerBuilder allows mock ocm api servers to be built
type MockConfigurableServerBuilder struct {
// handlerRegister cache of endpoints and handlers to be used when the mock ocm api server is built
handlerRegister HandlerRegister
}
// NewMockConfigurableServerBuilder returns a new builder that can be used to define a mock ocm api server
func NewMockConfigurableServerBuilder() *MockConfigurableServerBuilder {
// get the default endpoint handlers that'll be used if they're not overridden
handlerRegister, err := getDefaultHandlerRegister()
if err != nil {
panic(err)
}
return &MockConfigurableServerBuilder{
handlerRegister: handlerRegister,
}
}
// SetClusterGetResponse set a mock response cluster or error for the POST /api/clusters_mgmt/v1/clusters endpoint
func (b *MockConfigurableServerBuilder) SetClusterGetResponse(cluster *clustersmgmtv1.Cluster, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointClusterGet] = buildMockRequestHandler(cluster, err)
}
// SetKafkaDeleteResponse set a mock response cluster or error for the DELETE /api/clusters_mgmt/v1/clusters/{id}/external_configuration/syncsets/{syncsetID} endpoint
func (b *MockConfigurableServerBuilder) SetKafkaDeleteResponse(syncset *clustersmgmtv1.Syncset, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointKafkaDelete] = buildMockRequestHandler(syncset, err)
}
// SetClusterPatchResponse set a mock response cluster or error for the PATCH /api/clusters_mgmt/v1/clusters/{id} endpoint
func (b *MockConfigurableServerBuilder) SetClusterPatchResponse(cluster *clustersmgmtv1.Cluster, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointClusterPatch] = buildMockRequestHandler(cluster, err)
}
// SetClustersPostResponse set a mock response cluster or error for the POST /api/clusters_mgmt/v1/clusters endpoint
func (b *MockConfigurableServerBuilder) SetClustersPostResponse(cluster *clustersmgmtv1.Cluster, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointClustersPost] = buildMockRequestHandler(cluster, err)
}
// SetClustersGetResponse set a mock response cluster or error for the GET /api/clusters_mgmt/v1/clusters endpoint
func (b *MockConfigurableServerBuilder) SetClustersGetResponse(cluster *clustersmgmtv1.Cluster, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointClustersGet] = buildMockRequestHandler(cluster, err)
}
// SetClusterDeleteResponse set a mock response cluster or error for the DELETE /api/clusters_mgmt/v1/clusters/{id} endpoint
func (b *MockConfigurableServerBuilder) SetClusterDeleteResponse(cluster *clustersmgmtv1.Cluster, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointClusterDelete] = buildMockRequestHandler(cluster, err)
}
// SetClusterSyncsetGetResponse set a mock response syncset or error for the GET /api/clusters_mgmt/v1/clusters/{id}/external_configuration/syncsets/{syncsetID}
func (b *MockConfigurableServerBuilder) SetClusterSyncsetGetResponse(syncset *clustersmgmtv1.Syncset, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointClusterSyncsetGet] = buildMockRequestHandler(syncset, err)
}
// SetClusterSyncsetPostResponse set a mock response syncset or error for the POST /api/clusters_mgmt/v1/clusters/{id}/syncsets endpoint
func (b *MockConfigurableServerBuilder) SetClusterSyncsetPostResponse(syncset *clustersmgmtv1.Syncset, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointClusterSyncsetsPost] = buildMockRequestHandler(syncset, err)
}
// SetClusterSyncsetPatchResponse set a mock response syncset or error for the Patch /api/clusters_mgmt/v1/clusters/{id}/syncsets endpoint
func (b *MockConfigurableServerBuilder) SetClusterSyncsetPatchResponse(syncset *clustersmgmtv1.Syncset, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointClusterSyncsetPatch] = buildMockRequestHandler(syncset, err)
}
func (b *MockConfigurableServerBuilder) SetClusterSyncsetPostRequestHandler(customMockRequestHandler func() func(w http.ResponseWriter, r *http.Request)) {
b.handlerRegister[EndpointClusterSyncsetsPost] = customMockRequestHandler()
}
// SetClusterIngressGetResponse set a mock response ingress or error for the GET /api/clusters_mgmt/v1/clusters/{id}/ingresses endpoint
func (b *MockConfigurableServerBuilder) SetClusterIngressGetResponse(ingress *clustersmgmtv1.Ingress, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointClusterIngressGet] = buildMockRequestHandler(ingress, err)
}
// SetCloudProvidersGetResponse set a mock response provider list or error for GET /api/clusters_mgmt/v1/cloud_providers
func (b *MockConfigurableServerBuilder) SetCloudProvidersGetResponse(providers *clustersmgmtv1.CloudProviderList, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointCloudProvidersGet] = buildMockRequestHandler(providers, err)
}
// SetCloudRegionsGetResponse set a mock response region list or error for GET /api/clusters_mgmt/v1/cloud_providers/{id}/regions
func (b *MockConfigurableServerBuilder) SetCloudRegionsGetResponse(regions *clustersmgmtv1.CloudRegionList, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointCloudProviderRegionsGet] = buildMockRequestHandler(regions, err)
}
// SetCloudRegionGetResponse set a mock response region or error for GET /api/clusters_mgmt/v1/cloud_providers/{id}/regions/{regionId}
func (b *MockConfigurableServerBuilder) SetCloudRegionGetResponse(region *clustersmgmtv1.CloudRegion, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointCloudProviderRegionGet] = buildMockRequestHandler(region, err)
}
// SetClusterStatusGetResponse set a mock response cluster status or error for GET /api/clusters_mgmt/v1/clusters/{id}/status
func (b *MockConfigurableServerBuilder) SetClusterStatusGetResponse(status *clustersmgmtv1.ClusterStatus, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointClusterStatusGet] = buildMockRequestHandler(status, err)
}
// SetClusterAddonsGetResponse set a mock response addon list or error for GET /api/clusters_mgmt/v1/clusters/{id}/addons
func (b *MockConfigurableServerBuilder) SetClusterAddonsGetResponse(addons *clustersmgmtv1.AddOnInstallationList, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointClusterAddonsGet] = buildMockRequestHandler(addons, err)
}
// SetClusterAddonPostResponse set a mock response addon or error for POST /api/clusters_mgmt/v1/clusters/{id}/addons
func (b *MockConfigurableServerBuilder) SetClusterAddonPostResponse(addon *clustersmgmtv1.AddOnInstallation, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointClusterAddonPost] = buildMockRequestHandler(addon, err)
}
// SetMachinePoolsGetResponse set a mock response machine pool or error for Get /api/clusters_mgmt/v1/clusters/{id}/machine_pools
func (b *MockConfigurableServerBuilder) SetMachinePoolsGetResponse(mp *clustersmgmtv1.MachinePoolList, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointMachinePoolsGet] = buildMockRequestHandler(mp, err)
}
// SetMachinePoolGetResponse set a mock response machine pool list or error for Get /api/clusters_mgmt/v1/clusters/{id}/machine_pools/{machinePoolId}
func (b *MockConfigurableServerBuilder) SetMachinePoolGetResponse(mp *clustersmgmtv1.MachinePoolList, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointMachinePoolGet] = buildMockRequestHandler(mp, err)
}
// SetMachinePoolPostResponse set a mock response for Post /api/clusters_mgmt/v1/clusters/{id}/machine_pools
func (b *MockConfigurableServerBuilder) SetMachinePoolPostResponse(mp *clustersmgmtv1.MachinePool, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointMachinePoolPost] = buildMockRequestHandler(mp, err)
}
// SetMachinePoolPatchResponse set a mock response for Patch /api/clusters_mgmt/v1/clusters/{id}/machine_pools/{machinePoolId}
func (b *MockConfigurableServerBuilder) SetMachinePoolPatchResponse(mp *clustersmgmtv1.MachinePool, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointMachinePoolPatch] = buildMockRequestHandler(mp, err)
}
// SetIdentityProviderPostResponse set a mock response for Post /api/clusters_mgmt/v1/clusters/{id}/identity_providers
func (b *MockConfigurableServerBuilder) SetIdentityProviderPostResponse(idp *clustersmgmtv1.IdentityProvider, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointIdentityProviderPost] = buildMockRequestHandler(idp, err)
}
// SetGetServiceAccountResponse set a mock response for GET /api/accounts_mgmt/v1/accounts/{id}
func (b *MockConfigurableServerBuilder) SetGetServiceAccountResponse(acc *amsv1.Account, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointServiceAccountGet] = buildMockRequestHandler(acc, err)
}
// SetGetOrganizationQuotaCostResponse set a mock response for GET /api/accounts_mgmt/v1/organizations/{id}/quota_cost
func (b *MockConfigurableServerBuilder) SetGetOrganizationQuotaCostResponse(list *amsv1.QuotaCostList, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointOrganizationQuotaCostGet] = buildMockRequestHandler(list, err)
}
// SetOrganizationsGetResponse set a mock response for GET /api/accounts_mgmt/v1/organizations
func (b *MockConfigurableServerBuilder) SetOrganizationsGetResponse(list *amsv1.OrganizationList, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointOrganizationList] = buildMockRequestHandler(list, err)
}
// SetIdentityProviderPatchResponse set a mock response for Patch /api/clusters_mgmt/v1/clusters/{id}/identity_providers/{idp_id}
func (b *MockConfigurableServerBuilder) SetIdentityProviderPatchResponse(idp *clustersmgmtv1.IdentityProvider, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointIdentityProviderPatch] = buildMockRequestHandler(idp, err)
}
// SetClusterAuthorizationResponse set a mock response for Post /api/accounts_mgmt/v1/cluster_authorizations
func (b *MockConfigurableServerBuilder) SetClusterAuthorizationResponse(idp *amsv1.ClusterAuthorizationResponse, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointClusterAuthorizationPost] = buildMockRequestHandler(idp, err)
}
// SetAddonInstallationsPostResponse set a mock response for Post /api/clusters_mgmt/v1/clusters/{id}/addons
func (b *MockConfigurableServerBuilder) SetAddonInstallationsPostResponse(ai *clustersmgmtv1.AddOnInstallation, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointAddonInstallationsPost] = buildMockRequestHandler(ai, err)
}
// SetAddonInstallationGetResponse set a mock response for Get /api/clusters_mgmt/v1/clusters/{id}/addons/{addoninstallationId}
func (b *MockConfigurableServerBuilder) SetAddonInstallationGetResponse(ai *clustersmgmtv1.AddOnInstallation, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointAddonInstallationGet] = buildMockRequestHandler(ai, err)
}
// SetAddonInstallationPatchResponse set a mock response for Patch /api/clusters_mgmt/v1/clusters/{id}/addons/{addoninstallationId}
func (b *MockConfigurableServerBuilder) SetAddonInstallationPatchResponse(ai *clustersmgmtv1.AddOnInstallation, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointAddonInstallationPatch] = buildMockRequestHandler(ai, err)
}
func (b *MockConfigurableServerBuilder) SetKasFleetshardOperatorAddonInstallationGetResponse(ai *clustersmgmtv1.AddOnInstallation, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointKasFleetshardOperatorAddonInstallationGet] = buildMockRequestHandler(ai, err)
}
func (b *MockConfigurableServerBuilder) SetKasFleetshardOperatorAddonInstallationPatchResponse(ai *clustersmgmtv1.AddOnInstallation, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointKasFleetshardOperatorAddonInstallationPatch] = buildMockRequestHandler(ai, err)
}
func (b *MockConfigurableServerBuilder) SetKasFleetshardOperatorAddonInstallationPostResponse(ai *clustersmgmtv1.AddOnInstallation, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointKasFleetshardOperatorAddonInstallationPost] = buildMockRequestHandler(ai, err)
}
func (b *MockConfigurableServerBuilder) SetClusterLoggingOperatorAddonInstallationGetResponse(ai *clustersmgmtv1.AddOnInstallation, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointClusterLoggingOperatorAddonInstallationGet] = buildMockRequestHandler(ai, err)
}
func (b *MockConfigurableServerBuilder) SetClusterLoggingOperatorAddonInstallationPatchResponse(ai *clustersmgmtv1.AddOnInstallation, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointClusterLoggingOperatorAddonInstallationPatch] = buildMockRequestHandler(ai, err)
}
func (b *MockConfigurableServerBuilder) SetClusterLoggingOperatorAddonInstallationPostResponse(ai *clustersmgmtv1.AddOnInstallation, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointClusterLoggingOperatorAddonInstallationPost] = buildMockRequestHandler(ai, err)
}
func (b *MockConfigurableServerBuilder) SetSubscriptionPathDeleteResponse(idp *amsv1.Subscription, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointSubscriptionDelete] = buildMockRequestHandler(idp, err)
}
func (b *MockConfigurableServerBuilder) SetSubscriptionSearchResponse(sl *amsv1.SubscriptionList, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointSubscriptionSearch] = buildMockRequestHandler(sl, err)
}
func (b *MockConfigurableServerBuilder) SetTermsReviewPostResponse(idp *authorizationsv1.TermsReviewResponse, err *ocmErrors.ServiceError) {
b.handlerRegister[EndpointTermsReviewPost] = buildMockRequestHandler(idp, err)
}
// Build builds the mock ocm api server using the endpoint handlers that have been set in the builder
func (b *MockConfigurableServerBuilder) Build() *httptest.Server {
router = mux.NewRouter()
rSwapper = &routerSwapper{sync.Mutex{}, router}
// set up handlers from the builder
for endpoint, handleFn := range b.handlerRegister {
router.HandleFunc(endpoint.Path, handleFn).Methods(endpoint.Method)
}
server := httptest.NewUnstartedServer(rSwapper)
l, err := net.Listen("tcp", "127.0.0.1:9876")
if err != nil {
log.Fatal(err)
}
server.Listener = l
server.Start()
err = wait.PollImmediate(time.Second, 10*time.Second, func() (done bool, err error) {
resp, err := http.Get("http://127.0.0.1:9876/api/clusters_mgmt/v1/cloud_providers/aws/regions")
if resp != nil {
_ = resp.Body.Close()
}
return err == nil, nil
})
if err != nil {
log.Fatal("Timed out waiting for mock server to start.")
panic(err)
}
return server
}
// SwapRouterResponse and update the router to handle this response
func (b *MockConfigurableServerBuilder) SwapRouterResponse(path string, method string, successType interface{}, serviceErr *ocmErrors.ServiceError) {
b.handlerRegister[Endpoint{
Path: path,
Method: method,
}] = buildMockRequestHandler(successType, serviceErr)
router = mux.NewRouter()
for endpoint, handleFn := range b.handlerRegister {
router.HandleFunc(endpoint.Path, handleFn).Methods(endpoint.Method)
}
rSwapper.Swap(router)
}
// ServeHTTP makes the routerSwapper to implement the http.Handler interface
// so that routerSwapper can be used by httptest.NewServer()
func (rs *routerSwapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rs.mu.Lock()
router := rs.router
rs.mu.Unlock()
router.ServeHTTP(w, r)
}
// getDefaultHandlerRegister returns a set of default endpoints and handlers used in the mock ocm api server
func getDefaultHandlerRegister() (HandlerRegister, error) {
// define a list of default endpoints and handlers in the mock ocm api server, when new endpoints are used in the
// managed-services-api service, a default ocm response should also be added here
return HandlerRegister{
EndpointClusterGet: buildMockRequestHandler(MockCluster, nil),
EndpointClusterPatch: buildMockRequestHandler(MockCluster, nil),
EndpointKafkaDelete: buildMockRequestHandler(MockSyncset, nil),
EndpointClustersGet: buildMockRequestHandler(MockCluster, nil),
EndpointClustersPost: buildMockRequestHandler(MockCluster, nil),
EndpointClusterDelete: buildMockRequestHandler(MockCluster, ocmErrors.NotFound("setting this to not found to mimick a successul deletion")),
EndpointClusterSyncsetsPost: buildMockRequestHandler(MockSyncset, nil),
EndpointClusterSyncsetGet: buildMockRequestHandler(MockSyncset, nil),
EndpointClusterSyncsetPatch: buildMockRequestHandler(MockSyncset, nil),
EndpointClusterIngressGet: buildMockRequestHandler(MockIngressList, nil),
EndpointCloudProvidersGet: buildMockRequestHandler(MockCloudProviderList, nil),
EndpointCloudProviderGet: buildMockRequestHandler(MockCloudProvider, nil),
EndpointCloudProviderRegionsGet: buildMockRequestHandler(MockCloudProviderRegionList, nil),
EndpointCloudProviderRegionGet: buildMockRequestHandler(MockCloudProviderRegion, nil),
EndpointClusterStatusGet: buildMockRequestHandler(MockClusterStatus, nil),
EndpointClusterAddonsGet: buildMockRequestHandler(MockClusterAddonInstallationList, nil),
EndpointClusterAddonPost: buildMockRequestHandler(MockClusterAddonInstallation, nil),
EndpointMachinePoolsGet: buildMockRequestHandler(MockMachinePoolList, nil),
EndpointMachinePoolGet: buildMockRequestHandler(MockMachinePool, nil),
EndpointMachinePoolPatch: buildMockRequestHandler(MockMachinePool, nil),
EndpointMachinePoolPost: buildMockRequestHandler(MockMachinePool, nil),
EndpointServiceAccountGet: buildMockRequestHandler(MockServiceAccount, nil),
EndpointOrganizationQuotaCostGet: buildMockRequestHandler(MockOrganizationQuotaCost, nil),
EndpointIdentityProviderPatch: buildMockRequestHandler(MockIdentityProvider, nil),
EndpointIdentityProviderPost: buildMockRequestHandler(MockIdentityProvider, nil),
EndpointAddonInstallationsPost: buildMockRequestHandler(MockClusterAddonInstallation, nil),
EndpointAddonInstallationGet: buildMockRequestHandler(MockClusterAddonInstallation, nil),
EndpointAddonInstallationPatch: buildMockRequestHandler(MockClusterAddonInstallation, nil),
EndpointKasFleetshardOperatorAddonInstallationGet: buildMockRequestHandler(MockKasFleetshardOperatorAddonInstallation, nil),
EndpointKasFleetshardOperatorAddonInstallationPatch: buildMockRequestHandler(MockKasFleetshardOperatorAddonInstallation, nil),
EndpointKasFleetshardOperatorAddonInstallationPost: buildMockRequestHandler(MockKasFleetshardOperatorAddonInstallation, nil),
EndpointClusterLoggingOperatorAddonInstallationGet: buildMockRequestHandler(MockKasClusterLoggingOperatorAddonInstallation, nil),
EndpointClusterLoggingOperatorAddonInstallationPatch: buildMockRequestHandler(MockKasClusterLoggingOperatorAddonInstallation, nil),
EndpointClusterLoggingOperatorAddonInstallationPost: buildMockRequestHandler(MockKasClusterLoggingOperatorAddonInstallation, nil),
EndpointClusterAuthorizationPost: buildMockRequestHandler(MockClusterAuthorization, nil),
EndpointSubscriptionDelete: buildMockRequestHandler(MockSubscription, nil),
EndpointSubscriptionSearch: buildMockRequestHandler(MockSubscriptionSearch, nil),
EndpointTermsReviewPost: buildMockRequestHandler(MockTermsReview, nil),
}, nil
}
// buildMockRequestHandler builds a generic handler for all ocm api server responses
// one of successType of serviceErr should be defined
// if serviceErr is defined, it will be provided as an ocm error response
// if successType is defined, it will be provided as an ocm success response
func buildMockRequestHandler(successType interface{}, serviceErr *ocmErrors.ServiceError) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if serviceErr != nil {
w.WriteHeader(serviceErr.HttpCode)
if err := marshalOCMType(serviceErr, w); err != nil {
panic(err)
}
} else if successType != nil {
if err := marshalOCMType(successType, w); err != nil {
panic(err)
}
} else {
panic("no response was defined")
}
}
}
// marshalOCMType marshals known ocm types to a provided io.Writer using the ocm sdk marshallers
func marshalOCMType(t interface{}, w io.Writer) error {
switch v := t.(type) {
// handle cluster types
case *clustersmgmtv1.Cluster:
return clustersmgmtv1.MarshalCluster(v, w)
// handle cluster status types
case *clustersmgmtv1.ClusterStatus:
return clustersmgmtv1.MarshalClusterStatus(v, w)
// handle syncset types
case *clustersmgmtv1.Syncset:
return clustersmgmtv1.MarshalSyncset(v, w)
// handle identiy provider types
case *clustersmgmtv1.IdentityProvider:
return clustersmgmtv1.MarshalIdentityProvider(v, w)
// handle account types
case *amsv1.Account:
return amsv1.MarshalAccount(v, w)
//handle qoutaCostList types
case *amsv1.QuotaCost:
return amsv1.MarshalQuotaCost(v, w)
case []*amsv1.QuotaCost:
return amsv1.MarshalQuotaCostList(v, w)
case *amsv1.QuotaCostList:
quotaCostList, err := NewOCMList().WithItems(v.Slice())
if err != nil {
return err
}
return json.NewEncoder(w).Encode(quotaCostList)
// handle ingress types
case *clustersmgmtv1.Ingress:
return clustersmgmtv1.MarshalIngress(v, w)
case []*clustersmgmtv1.Ingress:
return clustersmgmtv1.MarshalIngressList(v, w)
// for any <type>List ocm type we'll need to follow this pattern to ensure the array of objects
// is wrapped with an OCMList object
case *clustersmgmtv1.IngressList:
ocmList, err := NewOCMList().WithItems(v.Slice())
if err != nil {
return err
}
return json.NewEncoder(w).Encode(ocmList)
// handle cloud provider types
case *clustersmgmtv1.CloudProvider:
return clustersmgmtv1.MarshalCloudProvider(v, w)
case []*clustersmgmtv1.CloudProvider:
return clustersmgmtv1.MarshalCloudProviderList(v, w)
case *clustersmgmtv1.CloudProviderList:
ocmList, err := NewOCMList().WithItems(v.Slice())
if err != nil {
return err
}
return json.NewEncoder(w).Encode(ocmList)
// handle cloud region types
case *clustersmgmtv1.CloudRegion:
return clustersmgmtv1.MarshalCloudRegion(v, w)
case []*clustersmgmtv1.CloudRegion:
return clustersmgmtv1.MarshalCloudRegionList(v, w)
case *clustersmgmtv1.CloudRegionList:
ocmList, err := NewOCMList().WithItems(v.Slice())
if err != nil {
return err
}
return json.NewEncoder(w).Encode(ocmList)
// handle cluster addon installations
case *clustersmgmtv1.AddOnInstallation:
return clustersmgmtv1.MarshalAddOnInstallation(v, w)
case []*clustersmgmtv1.AddOnInstallation:
return clustersmgmtv1.MarshalAddOnInstallationList(v, w)
case *clustersmgmtv1.AddOnInstallationList:
ocmList, err := NewOCMList().WithItems(v.Slice())
if err != nil {
return err
}
return json.NewEncoder(w).Encode(ocmList)
case *clustersmgmtv1.MachinePool:
return clustersmgmtv1.MarshalMachinePool(v, w)
case []*clustersmgmtv1.MachinePool:
return clustersmgmtv1.MarshalMachinePoolList(v, w)
case *clustersmgmtv1.MachinePoolList:
ocmList, err := NewOCMList().WithItems(v.Slice())
if err != nil {
return err
}
return json.NewEncoder(w).Encode(ocmList)
// handle the generic ocm list type
case *ocmList:
return json.NewEncoder(w).Encode(t)
case *amsv1.ClusterAuthorizationResponse:
return amsv1.MarshalClusterAuthorizationResponse(v, w)
case *amsv1.Subscription:
return amsv1.MarshalSubscription(t.(*amsv1.Subscription), w)
case *amsv1.Organization:
return amsv1.MarshalOrganization(t.(*amsv1.Organization), w)
case *authorizationsv1.TermsReviewResponse:
return authorizationsv1.MarshalTermsReviewResponse(v, w)
case []*amsv1.Subscription:
return amsv1.MarshalSubscriptionList(v, w)
case []*amsv1.Organization:
return amsv1.MarshalOrganizationList(v, w)
case *amsv1.OrganizationList:
organizationList, err := NewOrganizationList().WithItems(v.Slice())
if err != nil {
return err
}
return json.NewEncoder(w).Encode(organizationList)
case *amsv1.SubscriptionList:
subscList, err := NewSubscriptionList().WithItems(v.Slice())
if err != nil {
return err
}
return json.NewEncoder(w).Encode(subscList)
//list := t.(*amsv1.SubscriptionList)
//return amsv1.MarshalSubscriptionList(list.Slice(), w)
// handle ocm error type
case *ocmErrors.ServiceError:
return json.NewEncoder(w).Encode(v.AsOpenapiError("", ""))
}
return fmt.Errorf("could not recognise type %s in ocm type marshaller", reflect.TypeOf(t).String())
}
// basic wrapper to emulate the the ocm list types as they're private
type ocmList struct {
HREF *string `json:"href"`
Link bool `json:"link"`
Items json.RawMessage `json:"items"`
}
func NewOCMList() *ocmList {
return &ocmList{
HREF: nil,
Link: false,
Items: nil,
}
}
func (l *ocmList) WithHREF(href string) *ocmList {
l.HREF = &href
return l
}
func (l *ocmList) WithLink(link bool) *ocmList {
l.Link = link
return l
}
func (l *ocmList) WithItems(items interface{}) (*ocmList, error) {
var b bytes.Buffer
if err := marshalOCMType(items, &b); err != nil {
return l, err
}
l.Items = b.Bytes()
return l, nil
}
type subscriptionList struct {
Page int `json:"page"`
Size int `json:"size"`
Total int `json:"total"`
Items json.RawMessage `json:"items"`
}
func (l *subscriptionList) WithItems(items interface{}) (*subscriptionList, error) {
var b bytes.Buffer
if err := marshalOCMType(items, &b); err != nil {
return l, err
}
l.Items = b.Bytes()
return l, nil
}
func NewSubscriptionList() *subscriptionList {
return &subscriptionList{
Page: 0,
Size: 0,
Total: 0,
Items: nil,
}
}
type organizationList struct {
Page int `json:"page"`
Size int `json:"size"`
Total int `json:"total"`
Items json.RawMessage `json:"items"`
}
func (l *organizationList) WithItems(items any) (*organizationList, error) {
var b bytes.Buffer
if err := marshalOCMType(items, &b); err != nil {
return l, err
}
l.Items = b.Bytes()
return l, nil
}
func NewOrganizationList() *organizationList {
return &organizationList{
Page: 0,
Size: 0,
Total: 0,
Items: nil,
}
}
// init the shared mock types, panic if we fail, this should never fail
func init() {
var err error
// mock syncsets
mockMockSyncsetBuilder := GetMockSyncsetBuilder(nil)
MockSyncset, err = GetMockSyncset(mockMockSyncsetBuilder)
if err != nil {
panic(err)
}
// mock ingresses
MockIngressList, err = GetMockIngressList(nil)
if err != nil {
panic(err)
}
// mock cloud providers
MockCloudProvider, err = GetMockCloudProvider(nil)
if err != nil {
panic(err)
}
MockCloudProviderList, err = GetMockCloudProviderList(nil)
if err != nil {
panic(err)
}
// mock cloud provider regions/cloud regions
MockCloudProviderRegion, err = GetMockCloudProviderRegion(nil)
if err != nil {
panic(err)
}
MockCloudProviderRegionList, err = GetMockCloudProviderRegionList(nil)
if err != nil {
panic(err)
}
// mock cluster status
MockClusterStatus, err = GetMockClusterStatus(nil)
if err != nil {
panic(err)
}
MockClusterAddonInstallation, err = GetMockClusterAddonInstallation(nil, "")
if err != nil {
panic(err)
}
MockClusterAddonInstallationList, err = GetMockClusterAddonInstallationList(nil)
if err != nil {
panic(err)
}
MockKasFleetshardOperatorAddonInstallation, err = GetMockClusterAddonInstallation(nil, MockKasFleetshardAddonID)
if err != nil {
panic(err)
}
MockKasClusterLoggingOperatorAddonInstallation, err = GetMockClusterAddonInstallation(nil, MockClusterLoggingOperatorAddonID)
if err != nil {
panic(err)
}
MockCluster, err = GetMockCluster(nil)
if err != nil {
panic(err)
}
// Mock machine pools
MockMachinePoolList, err = GetMachinePoolList(nil)
if err != nil {
panic(err)
}
MockMachinePool, err = GetMockMachinePool(nil)
if err != nil {
panic(err)
}
// Service Account
MockServiceAccount, err = GetMockServiceAccount(nil)
if err != nil {
panic(err)
}
// Organization Quota Cost
MockOrganizationQuotaCost, err = GetMockOrganizationQuotaCost(nil)
if err != nil {
panic(err)
}
// Identity provider
MockIdentityProvider, err = GetMockIdentityProvider(nil)
if err != nil {
panic(err)
}
MockClusterAuthorization, err = GetMockClusterAuthorization(nil)
if err != nil {
panic(err)
}
MockSubscription, err = GetMockSubscription(nil)
if err != nil {
panic(err)
}
}
func GetMockSubscription(modifyFn func(b *amsv1.Subscription)) (*amsv1.Subscription, error) {
builder, err := amsv1.NewSubscription().ID(MockSubID).Build()
if modifyFn != nil {
modifyFn(builder)
}
return builder, err
}
func GetMockClusterAuthorization(modifyFn func(b *amsv1.ClusterAuthorizationResponse)) (*amsv1.ClusterAuthorizationResponse, error) {
sub := amsv1.SubscriptionBuilder{}
sub.ID(MockSubID)
sub.ClusterID(MockClusterExternalID)
sub.Status("Active")
builder, err := amsv1.NewClusterAuthorizationResponse().Subscription(&sub).Allowed(true).Build()
if modifyFn != nil {
modifyFn(builder)
}
return builder, err
}
func GetMockTermsReview(modifyFn func(b *authorizationsv1.TermsReviewResponse)) (*authorizationsv1.TermsReviewResponse, error) {
return authorizationsv1.NewTermsReviewResponse().TermsRequired(true).Build()
}
// GetMockSyncsetBuilder for emulated OCM server
func GetMockSyncsetBuilder(modifyFn func(b *clustersmgmtv1.SyncsetBuilder)) *clustersmgmtv1.SyncsetBuilder {
builder := clustersmgmtv1.NewSyncset().
ID(MockSyncsetID).
HREF(fmt.Sprintf("/api/clusters_mgmt/v1/clusters/%s/external_configuration/syncsets/%s", MockClusterID, MockSyncsetID))
if modifyFn != nil {
modifyFn(builder)
}
return builder
}
// GetMockSyncset for emulated OCM server
func GetMockSyncset(syncsetBuilder *clustersmgmtv1.SyncsetBuilder) (*clustersmgmtv1.Syncset, error) {
return syncsetBuilder.Build()
}
// GetMockIngressList for emulated OCM server
func GetMockIngressList(modifyFn func(l *v1.IngressList, err error)) (*clustersmgmtv1.IngressList, error) {
list, err := clustersmgmtv1.NewIngressList().Items(
clustersmgmtv1.NewIngress().ID(MockIngressID).DNSName(MockIngressDNS).Default(true).Listening(MockIngressListening).HREF(MockIngressHref)).Build()
if modifyFn != nil {
modifyFn(list, err)
}
return list, err
}
// GetMockCloudProviderBuilder for emulated OCM server
func GetMockCloudProviderBuilder(modifyFn func(builder *clustersmgmtv1.CloudProviderBuilder)) *clustersmgmtv1.CloudProviderBuilder {
builder := clustersmgmtv1.NewCloudProvider().
ID(MockCloudProviderID).
Name(MockCloudProviderID).
DisplayName(MockCloudProviderDisplayName).
HREF(fmt.Sprintf("/api/clusters_mgmt/v1/cloud_providers/%s", MockCloudProviderID))
if modifyFn != nil {
modifyFn(builder)
}
return builder
}
// GetMockCloudProvider for emulated OCM server
func GetMockCloudProvider(modifyFn func(*clustersmgmtv1.CloudProvider, error)) (*clustersmgmtv1.CloudProvider, error) {
cloudProvider, err := GetMockCloudProviderBuilder(nil).Build()
if modifyFn != nil {
modifyFn(cloudProvider, err)
}
return cloudProvider, err
}
// GetMockCloudProviderList for emulated OCM server
func GetMockCloudProviderList(modifyFn func(*clustersmgmtv1.CloudProviderList, error)) (*clustersmgmtv1.CloudProviderList, error) {
list, err := clustersmgmtv1.NewCloudProviderList().
Items(GetMockCloudProviderBuilder(nil)).
Build()
if modifyFn != nil {
modifyFn(list, err)
}
return list, err
}
// GetMockCloudProviderRegionBuilder for emulated OCM server
func GetMockCloudProviderRegionBuilder(modifyFn func(*clustersmgmtv1.CloudRegionBuilder)) *clustersmgmtv1.CloudRegionBuilder {
builder := clustersmgmtv1.NewCloudRegion().
ID(MockCloudRegionID).
HREF(fmt.Sprintf("/api/clusters_mgmt/v1/cloud_providers/%s/regions/%s", MockCloudProviderID, MockCloudRegionID)).
DisplayName(MockCloudRegionDisplayName).
CloudProvider(GetMockCloudProviderBuilder(nil)).
Enabled(true).
SupportsMultiAZ(true)
if modifyFn != nil {
modifyFn(builder)
}
return builder
}
// GetMockCloudProviderRegion for emulated OCM server
func GetMockCloudProviderRegion(modifyFn func(*clustersmgmtv1.CloudRegion, error)) (*clustersmgmtv1.CloudRegion, error) {
cloudRegion, err := GetMockCloudProviderRegionBuilder(nil).Build()
if modifyFn != nil {
modifyFn(cloudRegion, err)
}
return cloudRegion, err
}
// GetMockCloudProviderRegionList for emulated OCM server
func GetMockCloudProviderRegionList(modifyFn func(*clustersmgmtv1.CloudRegionList, error)) (*clustersmgmtv1.CloudRegionList, error) {
list, err := clustersmgmtv1.NewCloudRegionList().Items(GetMockCloudProviderRegionBuilder(nil)).Build()
if modifyFn != nil {
modifyFn(list, err)
}
return list, err
}
// GetMockClusterStatus for emulated OCM server
func GetMockClusterStatus(modifyFn func(*clustersmgmtv1.ClusterStatus, error)) (*clustersmgmtv1.ClusterStatus, error) {
status, err := GetMockClusterStatusBuilder(nil).Build()
if modifyFn != nil {
modifyFn(status, err)
}
return status, err
}
// GetMockClusterStatusBuilder for emulated OCM server
func GetMockClusterStatusBuilder(modifyFn func(*clustersmgmtv1.ClusterStatusBuilder)) *clustersmgmtv1.ClusterStatusBuilder {
builder := clustersmgmtv1.NewClusterStatus().
ID(MockClusterID).
HREF(fmt.Sprintf("/api/clusters_mgmt/v1/clusters/%s/status", MockClusterID)).