@@ -17,18 +17,16 @@ package v1alpha1
1717import (
1818 "context"
1919 "fmt"
20+ "net/http"
2021 "time"
2122
23+ "github.com/coralogix/coralogix-operator/internal/utils"
2224 "github.com/go-logr/logr"
23- gouuid "github.com/google/uuid"
24- "google.golang.org/grpc/codes"
25- "google.golang.org/protobuf/encoding/protojson"
26- "google.golang.org/protobuf/types/known/wrapperspb"
27- "k8s.io/utils/ptr"
2825 ctrl "sigs.k8s.io/controller-runtime"
2926 "sigs.k8s.io/controller-runtime/pkg/client"
3027
31- cxsdk "github.com/coralogix/coralogix-management-sdk/go"
28+ "github.com/coralogix/coralogix-management-sdk/go/openapi/cxsdk"
29+ dashboardsfolders "github.com/coralogix/coralogix-management-sdk/go/openapi/gen/dashboard_folders_service"
3230
3331 coralogixv1alpha1 "github.com/coralogix/coralogix-operator/api/coralogix/v1alpha1"
3432 "github.com/coralogix/coralogix-operator/internal/config"
@@ -37,7 +35,7 @@ import (
3735
3836// DashboardsFolderReconciler reconciles a DashboardsFolder object
3937type DashboardsFolderReconciler struct {
40- DashboardsFoldersClient * cxsdk. DashboardsFoldersClient
38+ DashboardsFoldersClient * dashboardsfolders. DashboardFoldersServiceAPIService
4139 Interval time.Duration
4240}
4341
@@ -67,23 +65,20 @@ func (r *DashboardsFolderReconciler) HandleCreation(ctx context.Context, log log
6765 return fmt .Errorf ("error on extracting dashboards-folder from spec: %w" , err )
6866 }
6967
70- if customID := folder .Spec .CustomID ; customID != nil {
71- folderToCreate .Id = wrapperspb .String (* customID )
72- } else {
73- folderToCreate .Id = wrapperspb .String (gouuid .NewString ())
74- }
75-
76- createRequest := & cxsdk.CreateDashboardFolderRequest {
68+ createRequest := & dashboardsfolders.CreateDashboardFolderRequestDataStructure {
7769 Folder : folderToCreate ,
7870 }
79- log .Info ("Creating remote dashboards-folder" , "folder" , protojson .Format (createRequest ))
80- createResponse , err := r .DashboardsFoldersClient .Create (ctx , createRequest )
71+ log .Info ("Creating remote dashboards-folder" , "folder" , utils .FormatJSON (createRequest ))
72+ createResponse , httpResp , err := r .DashboardsFoldersClient .
73+ DashboardFoldersServiceCreateDashboardFolder (ctx ).
74+ CreateDashboardFolderRequestDataStructure (* createRequest ).
75+ Execute ()
8176 if err != nil {
82- return fmt .Errorf ("error on creating remote dashboard-folder: %w" , err )
77+ return fmt .Errorf ("error on creating remote dashboard-folder: %w" , cxsdk . NewAPIError ( httpResp , err ) )
8378 }
84- log .Info ("Remote dashboard dashboards-folder" , "folder" , protojson . Format (createResponse ))
79+ log .Info ("Remote dashboard dashboards-folder" , "folder" , utils . FormatJSON (createResponse ))
8580
86- folder .Status = coralogixv1alpha1.DashboardsFolderStatus {ID : ptr . To ( folderToCreate .Id . GetValue ()) }
81+ folder .Status = coralogixv1alpha1.DashboardsFolderStatus {ID : folderToCreate .Id }
8782 return nil
8883}
8984
@@ -96,16 +91,19 @@ func (r *DashboardsFolderReconciler) HandleUpdate(ctx context.Context, log logr.
9691 if folder .Status .ID == nil {
9792 return fmt .Errorf ("no ID in status, cannot update remote dashboards-folder" )
9893 }
99- folderToUpdate .Id = wrapperspb . String ( * folder .Status .ID )
100- updateRequest := & cxsdk. ReplaceDashboardFolderRequest {
94+ folderToUpdate .Id = folder .Status .ID
95+ updateRequest := & dashboardsfolders. ReplaceDashboardFolderRequestDataStructure {
10196 Folder : folderToUpdate ,
10297 }
103- log .Info ("Updating remote dashboards-folder" , "folder" , protojson .Format (updateRequest ))
104- updateResponse , err := r .DashboardsFoldersClient .Replace (ctx , updateRequest )
98+ log .Info ("Updating remote dashboards-folder" , "folder" , utils .FormatJSON (updateRequest ))
99+ updateResponse , httpResp , err := r .DashboardsFoldersClient .
100+ DashboardFoldersServiceReplaceDashboardFolder (ctx ).
101+ ReplaceDashboardFolderRequestDataStructure (* updateRequest ).
102+ Execute ()
105103 if err != nil {
106- return fmt . Errorf ( "error on updating remote dashboard: %w" , err )
104+ return cxsdk . NewAPIError ( httpResp , err )
107105 }
108- log .Info ("Remote dashboards-folder updated" , "folder" , protojson . Format (updateResponse ))
106+ log .Info ("Remote dashboards-folder updated" , "folder" , utils . FormatJSON (updateResponse ))
109107
110108 return nil
111109}
@@ -118,10 +116,14 @@ func (r *DashboardsFolderReconciler) HandleDeletion(ctx context.Context, log log
118116 return nil
119117 }
120118 log .Info ("Deleting dashboards-folder from remote system" , "id" , id )
121- _ , err := r .DashboardsFoldersClient .Delete (ctx , & cxsdk.DeleteDashboardFolderRequest {FolderId : wrapperspb .String (* id )})
122- if err != nil && cxsdk .Code (err ) != codes .NotFound {
123- log .Error (err , "Error deleting remote dashboards-folder" , "id" , id )
124- return fmt .Errorf ("error deleting remote dashboards-folder %s: %w" , * id , err )
119+ _ , httpResp , err := r .DashboardsFoldersClient .
120+ DashboardFoldersServiceDeleteDashboardFolder (context .Background (), * id ).
121+ Execute ()
122+ if err != nil {
123+ if apiErr := cxsdk .NewAPIError (httpResp , err ); cxsdk .Code (apiErr ) != http .StatusNotFound {
124+ log .Error (err , "Error deleting remote dashboards-folder" , "id" , id )
125+ return fmt .Errorf ("error deleting remote dashboards-folder %s: %w" , * id , err )
126+ }
125127 }
126128 log .Info ("Dashboards-folder deleted from remote" , "id" , id )
127129 return nil
0 commit comments