-
Notifications
You must be signed in to change notification settings - Fork 18
/
apiclient.go
850 lines (722 loc) · 25.5 KB
/
apiclient.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
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
* SPDX-License-Identifier: MIT
*/
package utils
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"strings"
"github.com/eclipse-symphony/symphony/api/constants"
"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/model"
"github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2"
"github.com/fsnotify/fsnotify"
"github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2/observability"
observ_utils "github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2/observability/utils"
)
type (
apiClient struct {
baseUrl string
tokenProvider TokenProvider
client *http.Client
caCertPath string
}
ApiClientOption func(*apiClient)
TokenProvider func(ctx context.Context, baseUrl string, client *http.Client, user string, password string) (string, error)
SummaryGetter interface {
GetSummary(ctx context.Context, id string, namespace string, user string, password string) (*model.SummaryResult, error)
}
Dispatcher interface {
QueueJob(ctx context.Context, id string, namespace string, isDelete bool, isTarget bool, user string, password string) error
QueueDeploymentJob(ctx context.Context, namespace string, isDelete bool, deployment model.DeploymentSpec, user string, password string) error
}
ApiClient interface {
SummaryGetter
Dispatcher
GetInstancesForAllNamespaces(ctx context.Context, user string, password string) ([]model.InstanceState, error)
GetInstances(ctx context.Context, namespace string, user string, password string) ([]model.InstanceState, error)
GetInstance(ctx context.Context, instance string, namespace string, user string, password string) (model.InstanceState, error)
CreateInstance(ctx context.Context, instance string, payload []byte, namespace string, user string, password string) error
DeleteInstance(ctx context.Context, instance string, namespace string, user string, password string) error
DeleteTarget(ctx context.Context, target string, namespace string, user string, password string) error
GetSolutions(ctx context.Context, namespace string, user string, password string) ([]model.SolutionState, error)
GetSolution(ctx context.Context, solution string, namespace string, user string, password string) (model.SolutionState, error)
CreateSolution(ctx context.Context, solution string, payload []byte, namespace string, user string, password string) error
DeleteSolution(ctx context.Context, solution string, namespace string, user string, password string) error
GetTargetsForAllNamespaces(ctx context.Context, user string, password string) ([]model.TargetState, error)
GetTarget(ctx context.Context, target string, namespace string, user string, password string) (model.TargetState, error)
GetTargets(ctx context.Context, namespace string, user string, password string) ([]model.TargetState, error)
CreateTarget(ctx context.Context, target string, payload []byte, namespace string, user string, password string) error
Reconcile(ctx context.Context, deployment model.DeploymentSpec, isDelete bool, namespace string, user string, password string) (model.SummarySpec, error)
CatalogHook(ctx context.Context, payload []byte, user string, password string) error
PublishActivationEvent(ctx context.Context, event v1alpha2.ActivationData, user string, password string) error
GetCatalog(ctx context.Context, catalog string, namespace string, user string, password string) (model.CatalogState, error)
UpsertCatalog(ctx context.Context, catalog string, payload []byte, user string, password string) error
DeleteCatalog(ctx context.Context, catalog string, user string, password string) error
UpsertSolution(ctx context.Context, solution string, payload []byte, namespace string, user string, password string) error
GetSites(ctx context.Context, user string, password string) ([]model.SiteState, error)
GetCatalogs(ctx context.Context, namespace string, user string, password string) ([]model.CatalogState, error)
GetCatalogsWithFilter(ctx context.Context, namespace string, filterType string, filterValue string, user string, password string) ([]model.CatalogState, error)
UpdateSite(ctx context.Context, site string, payload []byte, user string, password string) error
GetABatchForSite(ctx context.Context, site string, user string, password string) (model.SyncPackage, error)
SyncActivationStatus(ctx context.Context, status model.ActivationStatus, user string, password string) error
SendVisualizationPacket(ctx context.Context, payload []byte, user string, password string) error
ReportCatalogs(ctx context.Context, instance string, components []model.ComponentSpec, user string, password string) error
}
)
func noTokenProvider(ctx context.Context, baseUrl string, client *http.Client, user string, passowrd string) (string, error) {
return "", nil
}
func WithUserPassword(ctx context.Context, user string, password string) ApiClientOption {
return func(a *apiClient) {
a.tokenProvider = func(ctx context.Context, baseUrl string, _ *http.Client, user string, password string) (string, error) {
request := authRequest{Username: user, Password: password}
requestData, _ := json.Marshal(request)
ret, err := a.callRestAPI(ctx, "users/auth", "POST", requestData, "")
if err != nil {
return "", err
}
var response authResponse
err = json.Unmarshal(ret, &response)
if err != nil {
return "", err
}
return response.AccessToken, nil
}
}
}
func WithServiceAccountToken() ApiClientOption {
return func(a *apiClient) {
a.tokenProvider = func(ctx context.Context, _ string, _ *http.Client, _ string, _ string) (string, error) {
path := os.Getenv(constants.SATokenPathName)
if path == "" {
path = constants.SATokenPath
}
token, err := os.ReadFile(path)
if err != nil {
return "", v1alpha2.NewCOAError(nil, "Token creation error: unable to read from volume.", v1alpha2.InternalError)
}
return string(token), nil
}
}
}
func WithCertAuth(caCertPath string) ApiClientOption {
return func(a *apiClient) {
a.caCertPath = caCertPath
}
}
func NewApiClient(ctx context.Context, baseUrl string, opts ...ApiClientOption) (*apiClient, error) {
rUrl, err := url.Parse(baseUrl)
if err != nil {
return nil, err
}
isSecure := rUrl.Scheme == "https"
client, err := newHttpClient(ctx, isSecure)
if err != nil {
return nil, err
}
a := &apiClient{
baseUrl: baseUrl,
tokenProvider: noTokenProvider,
client: client,
}
for _, opt := range opts {
opt(a)
}
return a, nil
}
func (a *apiClient) GetInstances(ctx context.Context, namespace string, user string, password string) ([]model.InstanceState, error) {
ret := make([]model.InstanceState, 0)
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return ret, err
}
response, err := a.callRestAPI(ctx, "instances?namespace="+url.QueryEscape(namespace), "GET", nil, token)
if err != nil {
return ret, err
}
err = json.Unmarshal(response, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
func (a *apiClient) GetInstancesForAllNamespaces(ctx context.Context, user string, password string) ([]model.InstanceState, error) {
ret := make([]model.InstanceState, 0)
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return ret, err
}
response, err := a.callRestAPI(ctx, "instances", "GET", nil, token)
if err != nil {
return ret, err
}
err = json.Unmarshal(response, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
func (a *apiClient) GetInstance(ctx context.Context, instance string, namespace string, user string, password string) (model.InstanceState, error) {
ret := model.InstanceState{}
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return ret, err
}
response, err := a.callRestAPI(ctx, "instances/"+url.QueryEscape(instance)+"?namespace="+url.QueryEscape(namespace), "GET", nil, token)
if err != nil {
return ret, err
}
err = json.Unmarshal(response, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
func (a *apiClient) CreateInstance(ctx context.Context, instance string, payload []byte, namespace string, user string, password string) error {
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return err
}
//use proper url encoding in the following statement
_, err = a.callRestAPI(ctx, "instances/"+url.QueryEscape(instance)+"?namespace="+url.QueryEscape(namespace), "POST", payload, token)
if err != nil {
return err
}
return nil
}
func (a *apiClient) DeleteInstance(ctx context.Context, instance string, namespace string, user string, password string) error {
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return err
}
_, err = a.callRestAPI(ctx, "instances/"+url.QueryEscape(instance)+"?direct=true&namespace="+url.QueryEscape(namespace), "DELETE", nil, token)
if err != nil {
return err
}
return nil
}
func (a *apiClient) DeleteTarget(ctx context.Context, target string, namespace string, user string, password string) error {
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return err
}
_, err = a.callRestAPI(ctx, "targets/registry/"+url.QueryEscape(target)+"?direct=true&namespace="+url.QueryEscape(namespace), "DELETE", nil, token)
if err != nil {
return err
}
return nil
}
func (a *apiClient) GetSolutions(ctx context.Context, namespace string, user string, password string) ([]model.SolutionState, error) {
ret := make([]model.SolutionState, 0)
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return ret, err
}
response, err := a.callRestAPI(ctx, "solutions?namespace="+url.QueryEscape(namespace), "GET", nil, token)
if err != nil {
return ret, err
}
err = json.Unmarshal(response, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
func (a *apiClient) GetSolution(ctx context.Context, solution string, namespace string, user string, password string) (model.SolutionState, error) {
ret := model.SolutionState{}
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return ret, err
}
response, err := a.callRestAPI(ctx, "solutions/"+url.QueryEscape(solution)+"?namespace="+url.QueryEscape(namespace), "GET", nil, token)
if err != nil {
return ret, err
}
err = json.Unmarshal(response, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
func (a *apiClient) CreateSolution(ctx context.Context, solution string, payload []byte, namespace string, user string, password string) error {
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return err
}
_, err = a.callRestAPI(ctx, "solutions/"+url.QueryEscape(solution)+"?namespace="+url.QueryEscape(namespace), "POST", payload, token)
if err != nil {
return err
}
return nil
}
func (a *apiClient) DeleteSolution(ctx context.Context, solution string, namespace string, user string, password string) error {
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return err
}
_, err = a.callRestAPI(ctx, "solutions/"+url.QueryEscape(solution)+"?namespace="+url.QueryEscape(namespace), "DELETE", nil, token)
if err != nil {
return err
}
return nil
}
func (a *apiClient) GetTarget(ctx context.Context, target string, namespace string, user string, password string) (model.TargetState, error) {
ret := model.TargetState{}
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return ret, err
}
response, err := a.callRestAPI(ctx, "targets/registry/"+url.QueryEscape(target)+"?namespace="+url.QueryEscape(namespace), "GET", nil, token)
if err != nil {
return ret, err
}
err = json.Unmarshal(response, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
func (a *apiClient) GetTargets(ctx context.Context, namespace string, user string, password string) ([]model.TargetState, error) {
ret := []model.TargetState{}
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return ret, err
}
response, err := a.callRestAPI(ctx, "targets/registry?namespace="+url.QueryEscape(namespace), "GET", nil, token)
if err != nil {
return ret, err
}
err = json.Unmarshal(response, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
func (a *apiClient) GetTargetsForAllNamespaces(ctx context.Context, user string, password string) ([]model.TargetState, error) {
ret := []model.TargetState{}
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return ret, err
}
response, err := a.callRestAPI(ctx, "targets/registry", "GET", nil, token)
if err != nil {
return ret, err
}
err = json.Unmarshal(response, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
func (a *apiClient) CreateTarget(ctx context.Context, target string, payload []byte, namespace string, user string, password string) error {
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return err
}
_, err = a.callRestAPI(ctx, "targets/registry/"+url.QueryEscape(target)+"?namespace="+url.QueryEscape(namespace), "POST", payload, token)
if err != nil {
return err
}
return nil
}
func (a *apiClient) GetSummary(ctx context.Context, id string, namespace string, user string, password string) (*model.SummaryResult, error) {
result := model.SummaryResult{}
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return nil, err
}
log.Debugf("apiClient.GetSummary: id: %s, namespace: %s", id, namespace)
ret, err := a.callRestAPI(ctx, "solution/queue?instance="+url.QueryEscape(id)+"&namespace="+url.QueryEscape(namespace), "GET", nil, token)
// callRestApi Does a weird thing where it returns nil if the status code is 404 so we'll recreate the error here
if err == nil && ret == nil {
log.Debugf("apiClient.GetSummary: Not found")
return nil, v1alpha2.NewCOAError(nil, "Not found", v1alpha2.NotFound)
}
if err != nil {
return nil, err
}
if ret != nil {
log.Debugf("apiClient.GetSummary: ret: %s", string(ret))
err = json.Unmarshal(ret, &result)
if err != nil {
return nil, err
}
}
return &result, nil
}
func (a *apiClient) QueueDeploymentJob(ctx context.Context, namespace string, isDelete bool, deployment model.DeploymentSpec, user string, password string) error {
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
path := "solution/queue"
query := url.Values{
"namespace": []string{namespace},
"delete": []string{fmt.Sprintf("%t", isDelete)},
"objectType": []string{"deployment"},
}
var payload []byte
if err != nil {
return err
}
payload, err = json.Marshal(deployment)
log.Debugf("apiClient.QueueDeploymentJob: Deployment payload: %s", string(payload))
if err != nil {
return err
}
_, err = a.callRestAPI(ctx, fmt.Sprintf("%s?%s", path, query.Encode()), "POST", payload, token)
if err != nil {
return err
}
return nil
}
// Deprecated: Use QueueDeploymentJob instead
func (a *apiClient) QueueJob(ctx context.Context, id string, namespace string, isDelete bool, isTarget bool, user string, password string) error {
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return err
}
path := "solution/queue"
query := url.Values{
"instance": []string{id},
"namespace": []string{namespace},
"delete": []string{fmt.Sprintf("%t", isDelete)},
"objectType": []string{"instance"},
}
if isTarget {
query.Set("objectType", "target")
}
_, err = a.callRestAPI(ctx, fmt.Sprintf("%s?%s", path, query.Encode()), "POST", nil, token) // TODO: We can pass empty token now because is path is a "back-door", as it was designed to be invoked from a trusted environment, which should be also protected with auth
if err != nil {
return err
}
return nil
}
func (a *apiClient) Reconcile(ctx context.Context, deployment model.DeploymentSpec, isDelete bool, namespace string, user string, password string) (model.SummarySpec, error) {
summary := model.SummarySpec{}
payload, _ := json.Marshal(deployment)
path := "solution/reconcile" + "?namespace=" + namespace
if isDelete {
path = path + "&delete=true"
}
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return summary, err
}
ret, err := a.callRestAPI(ctx, path, "POST", payload, token) // TODO: We can pass empty token now because is path is a "back-door", as it was designed to be invoked from a trusted environment, which should be also protected with auth
if err != nil {
return summary, err
}
if ret != nil {
err = json.Unmarshal(ret, &summary)
if err != nil {
return summary, err
}
}
return summary, nil
}
func (a *apiClient) CatalogHook(ctx context.Context, payload []byte, user string, password string) error {
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return err
}
path := "federation/k8shook?objectType=catalog"
_, err = a.callRestAPI(ctx, path, "POST", payload, token)
if err != nil {
return err
}
return nil
}
func (a *apiClient) PublishActivationEvent(ctx context.Context, event v1alpha2.ActivationData, user string, password string) error {
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return err
}
jData, _ := json.Marshal(event)
_, err = a.callRestAPI(ctx, "jobs", "POST", jData, token)
if err != nil {
return err
}
return nil
}
func (a *apiClient) GetCatalog(ctx context.Context, catalog string, namespace string, user string, password string) (model.CatalogState, error) {
ret := model.CatalogState{}
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return ret, err
}
catalogName := catalog
if strings.HasPrefix(catalogName, "<") && strings.HasSuffix(catalogName, ">") {
catalogName = catalogName[1 : len(catalogName)-1]
}
path := "catalogs/registry/" + url.QueryEscape(catalogName)
if namespace != "" {
path = path + "?namespace=" + url.QueryEscape(namespace)
}
response, err := a.callRestAPI(ctx, path, "GET", nil, token)
if err != nil {
return ret, err
}
err = json.Unmarshal(response, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
func (a *apiClient) GetCatalogsWithFilter(ctx context.Context, namespace string, filterType string, filterValue string, user string, password string) ([]model.CatalogState, error) {
ret := make([]model.CatalogState, 0)
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return ret, err
}
path := "catalogs/registry"
if filterType != "" && filterValue != "" {
path = path + "?filterType=" + url.QueryEscape(filterType) + "&filterValue=" + url.QueryEscape(filterValue)
if namespace != "" {
path = path + "&namespace=" + url.QueryEscape(namespace)
}
} else if namespace != "" {
path = path + "?namespace=" + url.QueryEscape(namespace)
}
response, err := a.callRestAPI(ctx, path, "GET", nil, token)
if err != nil {
return ret, err
}
err = json.Unmarshal(response, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
func (a *apiClient) GetCatalogs(ctx context.Context, namespace string, user string, password string) ([]model.CatalogState, error) {
return a.GetCatalogsWithFilter(ctx, namespace, "", "", user, password)
}
func (a *apiClient) UpsertCatalog(ctx context.Context, catalog string, payload []byte, user string, password string) error {
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return err
}
_, err = a.callRestAPI(ctx, "catalogs/registry/"+url.QueryEscape(catalog), "POST", payload, token)
if err != nil {
return err
}
return nil
}
func (a *apiClient) DeleteCatalog(ctx context.Context, catalog string, user string, password string) error {
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return err
}
_, err = a.callRestAPI(ctx, "catalogs/registry/"+url.QueryEscape(catalog), "DELETE", nil, token)
if err != nil {
return err
}
return nil
}
func (a *apiClient) ReportCatalogs(ctx context.Context, instance string, components []model.ComponentSpec, user string, password string) error {
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return err
}
path := "catalogs/status/" + url.QueryEscape(instance)
jData, _ := json.Marshal(components)
_, err = a.callRestAPI(ctx, path, "POST", jData, token)
if err != nil {
return err
}
return nil
}
func (a *apiClient) UpsertSolution(ctx context.Context, solution string, payload []byte, namespace string, user string, password string) error {
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return err
}
path := "solutions/" + url.QueryEscape(solution)
path = path + "?namespace=" + url.QueryEscape(namespace)
_, err = a.callRestAPI(ctx, path, "POST", payload, token)
if err != nil {
return err
}
return nil
}
func (a *apiClient) GetSites(ctx context.Context, user string, password string) ([]model.SiteState, error) {
ret := make([]model.SiteState, 0)
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return ret, err
}
response, err := a.callRestAPI(ctx, "federation/registry", "GET", nil, token)
if err != nil {
return ret, err
}
err = json.Unmarshal(response, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
func (a *apiClient) UpdateSite(ctx context.Context, site string, payload []byte, user string, password string) error {
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return err
}
_, err = a.callRestAPI(ctx, "federation/status/"+url.QueryEscape(site), "POST", payload, token)
if err != nil {
return err
}
return nil
}
func (a *apiClient) GetABatchForSite(ctx context.Context, site string, user string, password string) (model.SyncPackage, error) {
ret := model.SyncPackage{}
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return ret, err
}
response, err := a.callRestAPI(ctx, "federation/sync/"+url.QueryEscape(site)+"?count=10", "GET", nil, token)
if err != nil {
return ret, err
}
err = json.Unmarshal(response, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
func (a *apiClient) SyncActivationStatus(ctx context.Context, status model.ActivationStatus, user string, password string) error {
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return err
}
jData, _ := json.Marshal(status)
_, err = a.callRestAPI(ctx, "federation/sync", "POST", jData, token)
if err != nil {
return err
}
return nil
}
func (a *apiClient) SendVisualizationPacket(ctx context.Context, payload []byte, user string, password string) error {
token, err := a.tokenProvider(ctx, a.baseUrl, a.client, user, password)
if err != nil {
return err
}
_, err = a.callRestAPI(ctx, "visualization", "POST", payload, token)
if err != nil {
return err
}
return nil
}
func (a *apiClient) callRestAPI(ctx context.Context, route string, method string, payload []byte, token string) ([]byte, error) {
urlString := fmt.Sprintf("%s%s", a.baseUrl, path.Clean(route))
ctx, span := observability.StartSpan("Symphony-API-Client", ctx, &map[string]string{
"method": "callRestAPI",
"http.method": method,
"http.url": urlString,
})
var err error = nil
defer observ_utils.CloseSpanWithError(span, &err)
var rUrl *url.URL
rUrl, err = url.Parse(urlString)
if err != nil {
return nil, err
}
var req *http.Request
var reqBody io.Reader
if payload != nil {
reqBody = bytes.NewBuffer(payload)
}
req, err = http.NewRequestWithContext(ctx, method, rUrl.String(), reqBody)
observ_utils.PropagateSpanContextToHttpRequestHeader(req)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
var resp *http.Response
resp, err = a.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var bodyBytes []byte
bodyBytes, err = io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 300 {
if resp.StatusCode == 404 { // API service is already gone
return nil, nil
}
object := &SummarySpecError{
Code: fmt.Sprintf("Symphony API: [%d]", resp.StatusCode),
Message: string(bodyBytes),
}
return nil, object
}
return bodyBytes, nil
}
func newHttpClient(ctx context.Context, secure bool) (*http.Client, error) {
client := &http.Client{}
if !secure {
return client, nil
}
certBytes, err := os.ReadFile(apiCertPath)
if err != nil {
return nil, err
}
updateTransport := func(certBytes []byte) {
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(certBytes)
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
InsecureSkipVerify: false,
},
}
}
updateTransport(certBytes)
// setup a file watcher to reload the cert pool when the symphony cert changes
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
// watch for cert changes
go func() {
defer watcher.Close()
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Has(fsnotify.Write) {
newCertBytes, readErr := os.ReadFile(apiCertPath)
if readErr != nil {
continue
}
updateTransport(newCertBytes)
}
case _, ok := <-watcher.Errors:
if !ok {
return
}
case <-ctx.Done():
return
}
}
}()
err = watcher.Add(apiCertPath)
if err != nil {
return nil, err
}
return client, nil
}