-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfighub.go
More file actions
906 lines (781 loc) · 30.5 KB
/
confighub.go
File metadata and controls
906 lines (781 loc) · 30.5 KB
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
package sdk
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
"github.com/google/uuid"
)
// Real ConfigHub API types based on actual source code
// Space represents a ConfigHub space
type Space struct {
SpaceID uuid.UUID `json:"SpaceID,omitempty"`
OrganizationID uuid.UUID `json:"OrganizationID,omitempty"`
Slug string `json:"Slug"`
DisplayName string `json:"DisplayName,omitempty"`
Labels map[string]string `json:"Labels,omitempty"`
Annotations map[string]string `json:"Annotations,omitempty"`
CreatedAt time.Time `json:"CreatedAt,omitempty"`
UpdatedAt time.Time `json:"UpdatedAt,omitempty"`
Version int64 `json:"Version,omitempty"`
EntityType string `json:"EntityType,omitempty"`
}
// Unit represents a ConfigHub configuration unit
type Unit struct {
UnitID uuid.UUID `json:"UnitID,omitempty"`
SpaceID uuid.UUID `json:"SpaceID,omitempty"`
OrganizationID uuid.UUID `json:"OrganizationID,omitempty"`
Slug string `json:"Slug"`
DisplayName string `json:"DisplayName,omitempty"`
Data string `json:"Data,omitempty"`
Labels map[string]string `json:"Labels,omitempty"`
Annotations map[string]string `json:"Annotations,omitempty"`
UpstreamUnitID *uuid.UUID `json:"UpstreamUnitID,omitempty"` // For upstream/downstream
SetIDs []uuid.UUID `json:"SetIDs,omitempty"` // Sets this unit belongs to
TargetID *uuid.UUID `json:"TargetID,omitempty"`
BridgeWorkerID *uuid.UUID `json:"BridgeWorkerID,omitempty"`
ApplyGates map[string]bool `json:"ApplyGates,omitempty"`
CreatedAt time.Time `json:"CreatedAt,omitempty"`
UpdatedAt time.Time `json:"UpdatedAt,omitempty"`
Version int64 `json:"Version,omitempty"`
EntityType string `json:"EntityType,omitempty"`
}
// Set represents a group of related Units (REAL ConfigHub feature)
type Set struct {
SetID uuid.UUID `json:"SetID,omitempty"`
SpaceID uuid.UUID `json:"SpaceID,omitempty"`
OrganizationID uuid.UUID `json:"OrganizationID,omitempty"`
Slug string `json:"Slug"`
DisplayName string `json:"DisplayName,omitempty"`
Labels map[string]string `json:"Labels,omitempty"`
Annotations map[string]string `json:"Annotations,omitempty"`
CreatedAt time.Time `json:"CreatedAt,omitempty"`
UpdatedAt time.Time `json:"UpdatedAt,omitempty"`
Version int64 `json:"Version,omitempty"`
EntityType string `json:"EntityType,omitempty"`
}
// Filter represents a ConfigHub filter with WHERE clauses (REAL feature)
type Filter struct {
FilterID uuid.UUID `json:"FilterID,omitempty"`
SpaceID uuid.UUID `json:"SpaceID,omitempty"`
OrganizationID uuid.UUID `json:"OrganizationID,omitempty"`
Slug string `json:"Slug"`
DisplayName string `json:"DisplayName,omitempty"`
From string `json:"From"` // Entity type to filter (e.g., "Unit")
FromSpaceID *uuid.UUID `json:"FromSpaceID,omitempty"`
Where string `json:"Where"` // WHERE clause
Select []string `json:"Select,omitempty"`
Labels map[string]string `json:"Labels,omitempty"`
Annotations map[string]string `json:"Annotations,omitempty"`
Hash string `json:"Hash,omitempty"`
CreatedAt time.Time `json:"CreatedAt,omitempty"`
UpdatedAt time.Time `json:"UpdatedAt,omitempty"`
Version int64 `json:"Version,omitempty"`
EntityType string `json:"EntityType,omitempty"`
}
// LiveState represents the live deployment state (READ-ONLY)
type LiveState struct {
UnitID uuid.UUID `json:"UnitID"`
SpaceID uuid.UUID `json:"SpaceID"`
Status string `json:"Status"`
DriftDetected bool `json:"DriftDetected"`
LastAppliedAt time.Time `json:"LastAppliedAt"`
LastError string `json:"LastError,omitempty"`
}
// Target represents a deployment target
type Target struct {
TargetID uuid.UUID `json:"TargetID,omitempty"`
OrganizationID uuid.UUID `json:"OrganizationID,omitempty"`
Slug string `json:"Slug"`
DisplayName string `json:"DisplayName,omitempty"`
TargetType string `json:"TargetType"` // e.g., "kubernetes"
Config map[string]string `json:"Config,omitempty"`
Labels map[string]string `json:"Labels,omitempty"`
Annotations map[string]string `json:"Annotations,omitempty"`
CreatedAt time.Time `json:"CreatedAt,omitempty"`
UpdatedAt time.Time `json:"UpdatedAt,omitempty"`
Version int64 `json:"Version,omitempty"`
}
// Request/Response types
type CreateSpaceRequest struct {
Slug string `json:"Slug"`
DisplayName string `json:"DisplayName,omitempty"`
Labels map[string]string `json:"Labels,omitempty"`
Annotations map[string]string `json:"Annotations,omitempty"`
}
type CreateUnitRequest struct {
Slug string `json:"Slug"`
DisplayName string `json:"DisplayName,omitempty"`
Data string `json:"Data"`
Labels map[string]string `json:"Labels,omitempty"`
Annotations map[string]string `json:"Annotations,omitempty"`
UpstreamUnitID *uuid.UUID `json:"UpstreamUnitID,omitempty"`
SetIDs []uuid.UUID `json:"SetIDs,omitempty"`
TargetID *uuid.UUID `json:"TargetID,omitempty"`
ChangeSetID *uuid.UUID `json:"ChangeSetID,omitempty"`
}
type CreateSetRequest struct {
Slug string `json:"Slug"`
DisplayName string `json:"DisplayName,omitempty"`
Labels map[string]string `json:"Labels,omitempty"`
Annotations map[string]string `json:"Annotations,omitempty"`
}
type CreateFilterRequest struct {
Slug string `json:"Slug"`
DisplayName string `json:"DisplayName,omitempty"`
From string `json:"From"` // "Unit", "Space", etc.
Where string `json:"Where"` // WHERE clause
Select []string `json:"Select,omitempty"`
Labels map[string]string `json:"Labels,omitempty"`
Annotations map[string]string `json:"Annotations,omitempty"`
}
type ListUnitsParams struct {
SpaceID uuid.UUID `json:"SpaceID,omitempty"`
FilterID *uuid.UUID `json:"FilterID,omitempty"`
SetID *uuid.UUID `json:"SetID,omitempty"`
Where string `json:"Where,omitempty"`
Limit int `json:"Limit,omitempty"`
Offset int `json:"Offset,omitempty"`
}
type BulkApplyParams struct {
SpaceID uuid.UUID `json:"SpaceID"`
Where string `json:"Where"` // e.g., "SetID = 'xxx'"
DryRun bool `json:"DryRun,omitempty"`
}
type BulkPatchParams struct {
SpaceID uuid.UUID `json:"SpaceID"`
Where string `json:"Where"`
Patch map[string]interface{} `json:"Patch"`
Upgrade bool `json:"Upgrade,omitempty"` // For push-upgrade pattern
}
// ConfigHubClient provides interface to real ConfigHub API
type ConfigHubClient struct {
baseURL string
token string
client *http.Client
}
// NewConfigHubClient creates a new ConfigHub API client
func NewConfigHubClient(baseURL, token string) *ConfigHubClient {
if baseURL == "" {
// Use environment variable or default to ConfigHub API
baseURL = os.Getenv("CUB_API_URL")
if baseURL == "" {
baseURL = "https://hub.confighub.com/api"
}
}
return &ConfigHubClient{
baseURL: baseURL,
token: token,
client: &http.Client{
Timeout: 30 * time.Second,
},
}
}
// Space operations
func (c *ConfigHubClient) CreateSpace(req CreateSpaceRequest) (*Space, error) {
result, err := c.doRequest("POST", "/space", req, &Space{})
if err != nil {
return nil, err
}
return result.(*Space), nil
}
func (c *ConfigHubClient) GetSpace(spaceID uuid.UUID) (*Space, error) {
result, err := c.doRequest("GET", fmt.Sprintf("/space/%s", spaceID), nil, &Space{})
if err != nil {
return nil, err
}
return result.(*Space), nil
}
// SpaceSummary is the wrapper returned by the /space endpoint
type SpaceSummary struct {
Space *Space `json:"Space"`
TotalUnitCount int `json:"TotalUnitCount"`
TotalLinkCount int `json:"TotalLinkCount"`
GatedUnitCount int `json:"GatedUnitCount"`
IncompleteApplyUnitCount int `json:"IncompleteApplyUnitCount"`
RecentChangeUnitCount int `json:"RecentChangeUnitCount"`
TotalBridgeWorkerCount int `json:"TotalBridgeWorkerCount"`
UnlinkedUnitCount int `json:"UnlinkedUnitCount"`
TargetCountByToolchainType map[string]int `json:"TargetCountByToolchainType"`
TriggerCountByEventType map[string]int `json:"TriggerCountByEventType"`
}
func (c *ConfigHubClient) ListSpaces() ([]*Space, error) {
var summaries []SpaceSummary
if err := c.doRequestList("GET", "/space", nil, &summaries); err != nil {
return nil, err
}
// Extract just the Space objects
spaces := make([]*Space, len(summaries))
for i, summary := range summaries {
spaces[i] = summary.Space
}
return spaces, nil
}
func (c *ConfigHubClient) DeleteSpace(spaceID uuid.UUID) error {
_, err := c.doRequest("DELETE", fmt.Sprintf("/space/%s", spaceID), nil, nil)
return err
}
// Unit operations
func (c *ConfigHubClient) CreateUnit(spaceID uuid.UUID, req CreateUnitRequest) (*Unit, error) {
result, err := c.doRequest("POST", fmt.Sprintf("/space/%s/unit", spaceID), req, &Unit{})
if err != nil {
return nil, err
}
return result.(*Unit), nil
}
func (c *ConfigHubClient) GetUnit(spaceID, unitID uuid.UUID) (*Unit, error) {
result, err := c.doRequest("GET", fmt.Sprintf("/space/%s/unit/%s", spaceID, unitID), nil, &Unit{})
if err != nil {
return nil, err
}
return result.(*Unit), nil
}
func (c *ConfigHubClient) UpdateUnit(spaceID, unitID uuid.UUID, req CreateUnitRequest) (*Unit, error) {
result, err := c.doRequest("PUT", fmt.Sprintf("/space/%s/unit/%s", spaceID, unitID), req, &Unit{})
if err != nil {
return nil, err
}
return result.(*Unit), nil
}
func (c *ConfigHubClient) ListUnits(params ListUnitsParams) ([]*Unit, error) {
// API returns wrapped format: [{"Unit": {...}}, ...]
var response []struct {
Unit *Unit `json:"Unit"`
}
endpoint := fmt.Sprintf("/space/%s/unit", params.SpaceID)
if params.Where != "" {
endpoint += fmt.Sprintf("?where=%s", params.Where)
}
err := c.doRequestList("GET", endpoint, nil, &response)
if err != nil {
return nil, err
}
// Extract units from wrapped response
units := make([]*Unit, 0, len(response))
for _, wrapper := range response {
if wrapper.Unit != nil {
units = append(units, wrapper.Unit)
}
}
if os.Getenv("CUB_DEBUG") == "true" && len(units) > 0 {
fmt.Printf("[DEBUG] ListUnits returned %d units for space %s\n", len(units), params.SpaceID)
for i, u := range units {
dataPreview := u.Data
if len(dataPreview) > 100 {
dataPreview = dataPreview[:100] + "..."
}
fmt.Printf("[DEBUG] Unit[%d]: Slug=%s, Data length=%d, Preview=%q\n", i, u.Slug, len(u.Data), dataPreview)
}
}
return units, nil
}
func (c *ConfigHubClient) ApplyUnit(spaceID, unitID uuid.UUID) error {
_, err := c.doRequest("POST", fmt.Sprintf("/space/%s/unit/%s/apply", spaceID, unitID), nil, nil)
return err
}
func (c *ConfigHubClient) DestroyUnit(spaceID, unitID uuid.UUID) error {
_, err := c.doRequest("POST", fmt.Sprintf("/space/%s/unit/%s/destroy", spaceID, unitID), nil, nil)
return err
}
// Set operations (REAL)
func (c *ConfigHubClient) CreateSet(spaceID uuid.UUID, req CreateSetRequest) (*Set, error) {
result, err := c.doRequest("POST", fmt.Sprintf("/space/%s/set", spaceID), req, &Set{})
if err != nil {
return nil, err
}
return result.(*Set), nil
}
func (c *ConfigHubClient) GetSet(spaceID, setID uuid.UUID) (*Set, error) {
result, err := c.doRequest("GET", fmt.Sprintf("/space/%s/set/%s", spaceID, setID), nil, &Set{})
if err != nil {
return nil, err
}
return result.(*Set), nil
}
func (c *ConfigHubClient) UpdateSet(spaceID, setID uuid.UUID, req CreateSetRequest) (*Set, error) {
result, err := c.doRequest("PUT", fmt.Sprintf("/space/%s/set/%s", spaceID, setID), req, &Set{})
if err != nil {
return nil, err
}
return result.(*Set), nil
}
func (c *ConfigHubClient) ListSets(spaceID uuid.UUID) ([]*Set, error) {
var sets []*Set
return sets, c.doRequestList("GET", fmt.Sprintf("/space/%s/set", spaceID), nil, &sets)
}
// Filter operations (REAL)
func (c *ConfigHubClient) CreateFilter(spaceID uuid.UUID, req CreateFilterRequest) (*Filter, error) {
result, err := c.doRequest("POST", fmt.Sprintf("/space/%s/filter", spaceID), req, &Filter{})
if err != nil {
return nil, err
}
return result.(*Filter), nil
}
func (c *ConfigHubClient) GetFilter(spaceID, filterID uuid.UUID) (*Filter, error) {
result, err := c.doRequest("GET", fmt.Sprintf("/space/%s/filter/%s", spaceID, filterID), nil, &Filter{})
if err != nil {
return nil, err
}
return result.(*Filter), nil
}
// Bulk operations (REAL)
func (c *ConfigHubClient) BulkApplyUnits(params BulkApplyParams) error {
_, err := c.doRequest("POST", fmt.Sprintf("/space/%s/unit/bulk-apply", params.SpaceID), params, nil)
return err
}
func (c *ConfigHubClient) BulkPatchUnits(params BulkPatchParams) error {
_, err := c.doRequest("PATCH", fmt.Sprintf("/space/%s/unit/bulk-patch", params.SpaceID), params, nil)
return err
}
// Live State (READ-ONLY)
func (c *ConfigHubClient) GetUnitLiveState(spaceID, unitID uuid.UUID) (*LiveState, error) {
result, err := c.doRequest("GET", fmt.Sprintf("/space/%s/unit/%s/live-state", spaceID, unitID), nil, &LiveState{})
if err != nil {
return nil, err
}
return result.(*LiveState), nil
}
// Target operations
func (c *ConfigHubClient) CreateTarget(req Target) (*Target, error) {
result, err := c.doRequest("POST", "/target", req, &Target{})
if err != nil {
return nil, err
}
return result.(*Target), nil
}
func (c *ConfigHubClient) GetTarget(targetID uuid.UUID) (*Target, error) {
result, err := c.doRequest("GET", fmt.Sprintf("/target/%s", targetID), nil, &Target{})
if err != nil {
return nil, err
}
return result.(*Target), nil
}
// GetNewSpacePrefix calls ConfigHub to generate a unique space prefix
// Returns something like "chubby-paws" or "whisker-tail"
func (c *ConfigHubClient) GetNewSpacePrefix() (string, error) {
// This would typically call: cub space new-prefix
// Since we don't have direct CLI access, we'd need to call the API endpoint
// For now, this is a placeholder that would need the actual API endpoint
// In practice, this would be:
// result, err := c.doRequest("POST", "/space/new-prefix", nil, &struct{Prefix string})
// return result.Prefix, err
// For demonstration, generate a readable prefix
adjectives := []string{"happy", "clever", "swift", "bright", "gentle"}
nouns := []string{"paws", "tail", "whisker", "cloud", "star"}
adj := adjectives[time.Now().UnixNano()%int64(len(adjectives))]
noun := nouns[time.Now().UnixNano()%int64(len(nouns))]
return fmt.Sprintf("%s-%s", adj, noun), nil
}
// Helper methods
func (c *ConfigHubClient) doRequest(method, endpoint string, body interface{}, result interface{}) (interface{}, error) {
url := c.baseURL + endpoint
var reqBody io.Reader
if body != nil {
jsonData, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("marshal request: %w", err)
}
reqBody = bytes.NewBuffer(jsonData)
}
req, err := http.NewRequest(method, url, reqBody)
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
req.Header.Set("Content-Type", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("send request: %w", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read response: %w", err)
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("API error %d: %s", resp.StatusCode, string(respBody))
}
if result != nil && len(respBody) > 0 {
if err := json.Unmarshal(respBody, result); err != nil {
return nil, fmt.Errorf("unmarshal response: %w", err)
}
return result, nil
}
return nil, nil
}
func (c *ConfigHubClient) doRequestList(method, endpoint string, body interface{}, result interface{}) error {
url := c.baseURL + endpoint
var reqBody io.Reader
if body != nil {
jsonData, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("marshal request: %w", err)
}
reqBody = bytes.NewBuffer(jsonData)
}
req, err := http.NewRequest(method, url, reqBody)
if err != nil {
return fmt.Errorf("create request: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
req.Header.Set("Content-Type", "application/json")
// Debug logging
if os.Getenv("CUB_DEBUG") == "true" {
log.Printf("DEBUG: %s %s", method, url)
log.Printf("DEBUG: Authorization: Bearer %s...", c.token[:20])
}
resp, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("send request: %w", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("read response: %w", err)
}
// Debug logging
if os.Getenv("CUB_DEBUG") == "true" {
log.Printf("DEBUG: Response status: %d", resp.StatusCode)
log.Printf("DEBUG: Response body preview: %s", string(respBody[:min(200, len(respBody))]))
}
if resp.StatusCode >= 400 {
return fmt.Errorf("API error %d: %s", resp.StatusCode, string(respBody))
}
if len(respBody) > 0 {
if err := json.Unmarshal(respBody, result); err != nil {
return fmt.Errorf("unmarshal response (preview: %s...): %w", string(respBody[:min(100, len(respBody))]), err)
}
}
return nil
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
// High-level convenience helpers
// GetSpaceBySlug finds a space by its slug name
func (c *ConfigHubClient) GetSpaceBySlug(slug string) (*Space, error) {
spaces, err := c.ListSpaces()
if err != nil {
return nil, fmt.Errorf("list spaces: %w", err)
}
// Filter by slug
for i, space := range spaces {
if space.Slug == slug {
return spaces[i], nil
}
}
return nil, fmt.Errorf("space not found: %s", slug)
}
// CreateSpaceWithUniquePrefix creates a space with a unique prefix + suffix
func (c *ConfigHubClient) CreateSpaceWithUniquePrefix(suffix string, displayName string, labels map[string]string) (*Space, string, error) {
prefix, err := c.GetNewSpacePrefix()
if err != nil {
return nil, "", fmt.Errorf("get unique prefix: %w", err)
}
slug := fmt.Sprintf("%s-%s", prefix, suffix)
space, err := c.CreateSpace(CreateSpaceRequest{
Slug: slug,
DisplayName: displayName,
Labels: labels,
})
if err != nil {
return nil, "", fmt.Errorf("create space: %w", err)
}
return space, slug, nil
}
// EnsureSpaceRecreated implements the delete-then-create pattern for spaces.
// If a space with the given slug exists, it deletes it completely first.
// Then creates a fresh space with the same slug.
// This ensures we always start with a clean slate and avoid stale configurations.
func (c *ConfigHubClient) EnsureSpaceRecreated(req CreateSpaceRequest) (*Space, error) {
// First, try to find existing space by slug
existingSpace, err := c.GetSpaceBySlug(req.Slug)
if err == nil && existingSpace != nil {
// Space exists, delete it first
fmt.Printf("Deleting existing space: %s\n", req.Slug)
if err := c.DeleteSpace(existingSpace.SpaceID); err != nil {
return nil, fmt.Errorf("delete existing space %s: %w", req.Slug, err)
}
fmt.Printf("Successfully deleted space: %s\n", req.Slug)
}
// Now create the space (whether it's new or we just deleted the old one)
fmt.Printf("Creating space: %s\n", req.Slug)
space, err := c.CreateSpace(req)
if err != nil {
return nil, fmt.Errorf("create space %s: %w", req.Slug, err)
}
fmt.Printf("Successfully created space: %s\n", req.Slug)
return space, nil
}
// CloneUnitWithUpstream creates a unit in the target space with an upstream relationship
func (c *ConfigHubClient) CloneUnitWithUpstream(sourceSpaceID, targetSpaceID uuid.UUID, unitSlug string, additionalLabels map[string]string) (*Unit, error) {
// Get the source unit
sourceUnits, err := c.ListUnits(ListUnitsParams{
SpaceID: sourceSpaceID,
Where: fmt.Sprintf("Slug = '%s'", unitSlug),
})
if err != nil {
return nil, fmt.Errorf("list source units: %w", err)
}
if len(sourceUnits) == 0 {
return nil, fmt.Errorf("source unit not found: %s", unitSlug)
}
sourceUnit := sourceUnits[0]
// Merge labels
labels := make(map[string]string)
for k, v := range sourceUnit.Labels {
labels[k] = v
}
for k, v := range additionalLabels {
labels[k] = v
}
// Create downstream unit with upstream relationship
return c.CreateUnit(targetSpaceID, CreateUnitRequest{
Slug: sourceUnit.Slug,
DisplayName: sourceUnit.DisplayName,
Data: sourceUnit.Data,
Labels: labels,
UpstreamUnitID: &sourceUnit.UnitID,
})
}
// BulkCloneUnitsWithUpstream clones multiple units from source to target space
func (c *ConfigHubClient) BulkCloneUnitsWithUpstream(sourceSpaceID, targetSpaceID uuid.UUID, unitSlugs []string, additionalLabels map[string]string) ([]*Unit, error) {
var clonedUnits []*Unit
for _, slug := range unitSlugs {
unit, err := c.CloneUnitWithUpstream(sourceSpaceID, targetSpaceID, slug, additionalLabels)
if err != nil {
return nil, fmt.Errorf("clone unit %s: %w", slug, err)
}
clonedUnits = append(clonedUnits, unit)
}
return clonedUnits, nil
}
// ApplyUnitsInOrder applies units in the correct dependency order
func (c *ConfigHubClient) ApplyUnitsInOrder(spaceID uuid.UUID, unitSlugs []string) error {
for _, slug := range unitSlugs {
units, err := c.ListUnits(ListUnitsParams{
SpaceID: spaceID,
Where: fmt.Sprintf("Slug = '%s'", slug),
})
if err != nil {
return fmt.Errorf("list units for %s: %w", slug, err)
}
if len(units) > 0 {
err = c.ApplyUnit(spaceID, units[0].UnitID)
if err != nil {
return fmt.Errorf("apply unit %s: %w", slug, err)
}
}
}
return nil
}
// ListFilters lists filters in a space
// TODO: Implement when ConfigHub API supports filter listing
func (c *ConfigHubClient) ListFilters(spaceID uuid.UUID) ([]*Filter, error) {
// Placeholder implementation - would call actual ConfigHub API
return []*Filter{}, nil
}
// FunctionInvocationRequest represents a request to invoke a ConfigHub function
type FunctionInvocationRequest struct {
FunctionName string `json:"FunctionName"`
ToolchainType string `json:"ToolchainType"`
Arguments []FunctionArgument `json:"Arguments,omitempty"`
Where string `json:"Where,omitempty"`
FilterID *uuid.UUID `json:"FilterID,omitempty"`
DryRun bool `json:"DryRun"`
ChangeSetID *uuid.UUID `json:"ChangeSetID,omitempty"`
}
type FunctionArgument struct {
ParameterName string `json:"ParameterName"`
Value interface{} `json:"Value"`
}
type FunctionInvocationResponse struct {
Results []FunctionResult `json:"Results"`
}
type FunctionResult struct {
UnitID uuid.UUID `json:"UnitID"`
UnitSlug string `json:"UnitSlug"`
Success bool `json:"Success"`
Error string `json:"Error,omitempty"`
Output interface{} `json:"Output,omitempty"`
Value interface{} `json:"Value,omitempty"`
Passed bool `json:"Passed,omitempty"` // For validation functions
}
// ExecuteFunction runs a ConfigHub function on units
func (c *ConfigHubClient) ExecuteFunction(spaceID uuid.UUID, req FunctionInvocationRequest) (*FunctionInvocationResponse, error) {
endpoint := fmt.Sprintf("/space/%s/function/invoke", spaceID)
var result FunctionInvocationResponse
_, err := c.doRequest("POST", endpoint, req, &result)
return &result, err
}
// SetImageVersion uses the set-image function to update container image
func (c *ConfigHubClient) SetImageVersion(spaceID, unitID uuid.UUID, containerName, image string) error {
req := FunctionInvocationRequest{
FunctionName: "set-image",
ToolchainType: "Kubernetes/YAML",
Where: fmt.Sprintf("UnitID = '%s'", unitID),
Arguments: []FunctionArgument{
{ParameterName: "container-name", Value: containerName},
{ParameterName: "image", Value: image},
},
}
_, err := c.ExecuteFunction(spaceID, req)
return err
}
// SetReplicas uses the set-replicas function to update replica count
func (c *ConfigHubClient) SetReplicas(spaceID, unitID uuid.UUID, replicas int) error {
req := FunctionInvocationRequest{
FunctionName: "set-replicas",
ToolchainType: "Kubernetes/YAML",
Where: fmt.Sprintf("UnitID = '%s'", unitID),
Arguments: []FunctionArgument{
{ParameterName: "replicas", Value: replicas},
},
}
_, err := c.ExecuteFunction(spaceID, req)
return err
}
// ListWorkers lists workers in a space (placeholder for PRINCIPLE #1 requirement)
// TODO: Implement when ConfigHub API supports worker listing
func (c *ConfigHubClient) ListWorkers(spaceID string) ([]interface{}, error) {
// Placeholder implementation - ConfigHub worker API not yet available
// In production, this would call the actual ConfigHub API
// For now, return empty to trigger health check warnings
return []interface{}{}, nil
}
// ListTargets lists targets in a space (placeholder for PRINCIPLE #4 requirement)
// TODO: Implement when ConfigHub API supports target listing
func (c *ConfigHubClient) ListTargets(spaceID string) ([]interface{}, error) {
// Placeholder implementation - ConfigHub target API not yet available
// In production, this would call the actual ConfigHub API
// For now, return empty to trigger health check warnings
return []interface{}{}, nil
}
// ChangeSet operations for grouping related changes
type ChangeSet struct {
ChangeSetID uuid.UUID `json:"changeSetId"`
SpaceID uuid.UUID `json:"spaceId"`
DisplayName string `json:"displayName"`
Description string `json:"description"`
CreatedAt string `json:"createdAt"`
Labels map[string]string `json:"labels,omitempty"`
}
type CreateChangeSetRequest struct {
DisplayName string `json:"displayName"`
Description string `json:"description"`
Labels map[string]string `json:"labels,omitempty"`
}
// CreateChangeSet creates a new ChangeSet for grouping related changes
func (c *ConfigHubClient) CreateChangeSet(spaceID uuid.UUID, req CreateChangeSetRequest) (*ChangeSet, error) {
result, err := c.doRequest("POST", fmt.Sprintf("/space/%s/changeset", spaceID), req, &ChangeSet{})
if err != nil {
return nil, err
}
return result.(*ChangeSet), nil
}
// GetChangeSet retrieves a ChangeSet
func (c *ConfigHubClient) GetChangeSet(spaceID, changeSetID uuid.UUID) (*ChangeSet, error) {
result, err := c.doRequest("GET", fmt.Sprintf("/space/%s/changeset/%s", spaceID, changeSetID), nil, &ChangeSet{})
if err != nil {
return nil, err
}
return result.(*ChangeSet), nil
}
// DeleteChangeSet deletes a ChangeSet
func (c *ConfigHubClient) DeleteChangeSet(spaceID, changeSetID uuid.UUID) error {
_, err := c.doRequest("DELETE", fmt.Sprintf("/space/%s/changeset/%s", spaceID, changeSetID), nil, nil)
return err
}
// ApplyChangeSet applies all changes in a ChangeSet
func (c *ConfigHubClient) ApplyChangeSet(spaceID, changeSetID uuid.UUID) error {
_, err := c.doRequest("POST", fmt.Sprintf("/space/%s/changeset/%s/apply", spaceID, changeSetID), nil, nil)
return err
}
// UpdateUnitWithChangeSet updates a unit and associates it with a ChangeSet
func (c *ConfigHubClient) UpdateUnitWithChangeSet(spaceID, unitID, changeSetID uuid.UUID, data interface{}) (*Unit, error) {
// Convert data to JSON string if it's not already a string
var dataStr string
if str, ok := data.(string); ok {
dataStr = str
} else {
jsonData, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("failed to marshal data: %w", err)
}
dataStr = string(jsonData)
}
req := CreateUnitRequest{
Data: dataStr,
ChangeSetID: &changeSetID,
}
return c.UpdateUnit(spaceID, unitID, req)
}
// Validation Functions - Use ConfigHub's built-in validation
// ValidateNoPlaceholders checks if a unit has any unresolved placeholders
func (c *ConfigHubClient) ValidateNoPlaceholders(spaceID, unitID uuid.UUID) (bool, string, error) {
req := FunctionInvocationRequest{
FunctionName: "no-placeholders",
ToolchainType: "Kubernetes/YAML",
Where: fmt.Sprintf("UnitID = '%s'", unitID),
}
result, err := c.ExecuteFunction(spaceID, req)
if err != nil {
return false, "", err
}
if len(result.Results) > 0 && result.Results[0].Success {
return result.Results[0].Passed, fmt.Sprintf("Unit %s validation", result.Results[0].UnitSlug), nil
}
return false, "Validation failed", nil
}
// ValidateCEL validates units against a CEL (Common Expression Language) expression
func (c *ConfigHubClient) ValidateCEL(spaceID uuid.UUID, where, expression string) ([]FunctionResult, error) {
req := FunctionInvocationRequest{
FunctionName: "cel-validate",
ToolchainType: "Kubernetes/YAML",
Where: where,
Arguments: []FunctionArgument{
{ParameterName: "expression", Value: expression},
},
}
result, err := c.ExecuteFunction(spaceID, req)
if err != nil {
return nil, err
}
return result.Results, nil
}
// GetReplicas uses the get-replicas function to retrieve replica counts
func (c *ConfigHubClient) GetReplicas(spaceID uuid.UUID, where string) ([]FunctionResult, error) {
req := FunctionInvocationRequest{
FunctionName: "get-replicas",
ToolchainType: "Kubernetes/YAML",
Where: where,
}
result, err := c.ExecuteFunction(spaceID, req)
if err != nil {
return nil, err
}
return result.Results, nil
}
// SetIntPath sets an integer value at a specific path in the configuration
func (c *ConfigHubClient) SetIntPath(spaceID, unitID uuid.UUID, apiVersion, kind, path string, value int) error {
req := FunctionInvocationRequest{
FunctionName: "set-int-path",
ToolchainType: "Kubernetes/YAML",
Where: fmt.Sprintf("UnitID = '%s'", unitID),
Arguments: []FunctionArgument{
{ParameterName: "apiVersion", Value: apiVersion},
{ParameterName: "kind", Value: kind},
{ParameterName: "path", Value: path},
{ParameterName: "value", Value: value},
},
}
_, err := c.ExecuteFunction(spaceID, req)
return err
}