@@ -331,3 +331,124 @@ func GetArgoCDServerEndpoint(k8sClient KubeClient) (string, error) {
331
331
332
332
return argoEndpoint , nil
333
333
}
334
+
335
+ // CreateApplication creates a new application in ArgoCD
336
+ func (c * ArgoRestClient ) CreateApplication (app * v1alpha1.Application ) (* v1alpha1.Application , error ) {
337
+ reqURL := c .url ()
338
+ reqURL .Path = "/api/v1/applications"
339
+ appBytes , err := json .Marshal (app )
340
+ if err != nil {
341
+ return nil , fmt .Errorf ("failed to marshal application: %w" , err )
342
+ }
343
+
344
+ req , err := http .NewRequest (http .MethodPost , reqURL .String (), bytes .NewBuffer (appBytes ))
345
+ if err != nil {
346
+ return nil , fmt .Errorf ("failed to create request: %w" , err )
347
+ }
348
+ req .Header .Set ("Content-Type" , "application/json" )
349
+
350
+ resp , err := c .Do (req )
351
+ if err != nil {
352
+ return nil , fmt .Errorf ("failed to create application: %w" , err )
353
+ }
354
+ defer resp .Body .Close ()
355
+
356
+ if resp .StatusCode != http .StatusOK {
357
+ body , _ := io .ReadAll (resp .Body )
358
+ return nil , fmt .Errorf ("failed to create application: status %d, body: %s" , resp .StatusCode , string (body ))
359
+ }
360
+
361
+ var createdApp v1alpha1.Application
362
+ if err := json .NewDecoder (resp .Body ).Decode (& createdApp ); err != nil {
363
+ return nil , fmt .Errorf ("failed to unmarshal created application: %w" , err )
364
+ }
365
+
366
+ return & createdApp , nil
367
+ }
368
+
369
+ // GetApplication retrieves an application by its name
370
+ func (c * ArgoRestClient ) GetApplication (name string ) (* v1alpha1.Application , error ) {
371
+ reqURL := c .url ()
372
+ reqURL .Path = fmt .Sprintf ("/api/v1/applications/%s" , name )
373
+
374
+ req , err := http .NewRequest (http .MethodGet , reqURL .String (), nil )
375
+ if err != nil {
376
+ return nil , fmt .Errorf ("failed to create request: %w" , err )
377
+ }
378
+
379
+ resp , err := c .Do (req )
380
+ if err != nil {
381
+ return nil , fmt .Errorf ("failed to get application: %w" , err )
382
+ }
383
+ defer resp .Body .Close ()
384
+
385
+ if resp .StatusCode == http .StatusNotFound {
386
+ return nil , fmt .Errorf ("application '%s' not found" , name )
387
+ }
388
+
389
+ if resp .StatusCode != http .StatusOK {
390
+ body , _ := io .ReadAll (resp .Body )
391
+ return nil , fmt .Errorf ("failed to get application: status %d, body: %s" , resp .StatusCode , string (body ))
392
+ }
393
+
394
+ var app v1alpha1.Application
395
+ if err := json .NewDecoder (resp .Body ).Decode (& app ); err != nil {
396
+ return nil , fmt .Errorf ("failed to unmarshal application: %w" , err )
397
+ }
398
+
399
+ return & app , nil
400
+ }
401
+
402
+ // ListApplications lists all applications
403
+ func (c * ArgoRestClient ) ListApplications () (* v1alpha1.ApplicationList , error ) {
404
+ reqURL := c .url ()
405
+ reqURL .Path = "/api/v1/applications"
406
+
407
+ req , err := http .NewRequest (http .MethodGet , reqURL .String (), nil )
408
+ if err != nil {
409
+ return nil , fmt .Errorf ("failed to create request: %w" , err )
410
+ }
411
+
412
+ resp , err := c .Do (req )
413
+ if err != nil {
414
+ return nil , fmt .Errorf ("failed to list applications: %w" , err )
415
+ }
416
+ defer resp .Body .Close ()
417
+
418
+ if resp .StatusCode != http .StatusOK {
419
+ body , _ := io .ReadAll (resp .Body )
420
+ return nil , fmt .Errorf ("failed to list applications: status %d, body: %s" , resp .StatusCode , string (body ))
421
+ }
422
+
423
+ var appList v1alpha1.ApplicationList
424
+ if err := json .NewDecoder (resp .Body ).Decode (& appList ); err != nil {
425
+ return nil , fmt .Errorf ("failed to unmarshal application list: %w" , err )
426
+ }
427
+
428
+ return & appList , nil
429
+ }
430
+
431
+ // DeleteApplication deletes an application by its name
432
+ func (c * ArgoRestClient ) DeleteApplication (name string ) error {
433
+ reqURL := c .url ()
434
+ reqURL .Path = fmt .Sprintf ("/api/v1/applications/%s" , name )
435
+
436
+ req , err := http .NewRequest (http .MethodDelete , reqURL .String (), nil )
437
+ if err != nil {
438
+ return fmt .Errorf ("failed to create request: %w" , err )
439
+ }
440
+ req .Header .Set ("Content-Type" , "application/json" ) // content-type
441
+
442
+ resp , err := c .Do (req )
443
+ if err != nil {
444
+ return fmt .Errorf ("failed to delete application: %w" , err )
445
+ }
446
+ defer resp .Body .Close ()
447
+
448
+ if resp .StatusCode != http .StatusOK {
449
+ body , _ := io .ReadAll (resp .Body )
450
+ return fmt .Errorf ("failed to delete application: status %d, body: %s" , resp .StatusCode , string (body ))
451
+ }
452
+
453
+ return nil
454
+ }
0 commit comments