Skip to content

Commit 1fd4851

Browse files
committed
dashboard folders
1 parent 2dd2e18 commit 1fd4851

File tree

4 files changed

+46
-44
lines changed

4 files changed

+46
-44
lines changed

api/coralogix/v1alpha1/dashboard_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func expandDashboardFolder(ctx context.Context, namespace string, in *DashboardS
9393
if err != nil {
9494
return nil, err
9595
}
96-
folderID = &dashboards.UUID{Value: &id}
96+
folderID = &dashboards.UUID{Value: id}
9797
} else {
9898
return nil, fmt.Errorf("folderRef.BackendRef or folderRef.ResourceRef is required")
9999
}

api/coralogix/v1alpha1/dashboardsfolder_types.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ import (
1818
"context"
1919
"fmt"
2020

21-
"google.golang.org/protobuf/types/known/wrapperspb"
2221
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2322
"sigs.k8s.io/controller-runtime/pkg/client"
2423

25-
cxsdk "github.com/coralogix/coralogix-management-sdk/go"
24+
dashboardsfolders "github.com/coralogix/coralogix-management-sdk/go/openapi/gen/dashboard_folders_service"
2625

2726
"github.com/coralogix/coralogix-operator/internal/config"
2827
)
@@ -45,35 +44,36 @@ type DashboardsFolderSpec struct {
4544
ParentFolderRef *ResourceRef `json:"parentFolderRef,omitempty"`
4645
}
4746

48-
func (in *DashboardsFolderSpec) ExtractDashboardsFolderFromSpec(ctx context.Context, namespace string) (*cxsdk.DashboardFolder, error) {
49-
dashboardFolder := new(cxsdk.DashboardFolder)
50-
dashboardFolder.Name = wrapperspb.String(in.Name)
47+
func (in *DashboardsFolderSpec) ExtractDashboardsFolderFromSpec(ctx context.Context, namespace string) (*dashboardsfolders.DashboardFolder, error) {
48+
folder := &dashboardsfolders.DashboardFolder{
49+
Name: dashboardsfolders.PtrString(in.Name),
50+
}
5151

5252
if parentID := in.ParentFolderID; parentID != nil {
53-
dashboardFolder.ParentId = wrapperspb.String(*parentID)
53+
folder.ParentId = parentID
5454
} else if parentRef := in.ParentFolderRef; parentRef != nil {
5555
var err error
56-
parentId, err := GetFolderIdFromFolderCR(ctx, namespace, *parentRef)
56+
folder.ParentId, err = GetFolderIdFromFolderCR(ctx, namespace, *parentRef)
5757
if err != nil {
5858
return nil, err
5959
}
60-
dashboardFolder.ParentId = wrapperspb.String(parentId)
6160
}
62-
return dashboardFolder, nil
61+
62+
return folder, nil
6363
}
6464

65-
func GetFolderIdFromFolderCR(ctx context.Context, namespace string, parentRef ResourceRef) (string, error) {
65+
func GetFolderIdFromFolderCR(ctx context.Context, namespace string, parentRef ResourceRef) (*string, error) {
6666
df := &DashboardsFolder{}
6767
if parentRef.Namespace != nil {
6868
namespace = *parentRef.Namespace
6969
}
7070
if err := config.GetClient().Get(ctx, client.ObjectKey{Name: parentRef.Name, Namespace: namespace}, df); err != nil {
71-
return "", fmt.Errorf("failed to get DashboardsFolder: %w", err)
71+
return nil, fmt.Errorf("failed to get DashboardsFolder: %w", err)
7272
}
7373
if df.Status.ID == nil || *df.Status.ID == "" {
74-
return "", fmt.Errorf("failed to get DashboardsFolder ID")
74+
return nil, fmt.Errorf("failed to get DashboardsFolder ID")
7575
}
76-
return *df.Status.ID, nil
76+
return df.Status.ID, nil
7777
}
7878

7979
type DashboardFolderRefBackendRef struct {

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func main() {
239239
os.Exit(1)
240240
}
241241
if err = (&v1alpha1controllers.DashboardsFolderReconciler{
242-
DashboardsFoldersClient: clientSet.DashboardsFolders(),
242+
DashboardsFoldersClient: oapiClientSet.DashboardFolders(),
243243
Interval: cfg.ReconcileIntervals[utils.DashboardsFolderKind],
244244
}).SetupWithManager(mgr); err != nil {
245245
setupLog.Error(err, "unable to create controller", "controller", "DashboardsFolder")

internal/controller/coralogix/v1alpha1/dashboardsfolder_controller.go

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,16 @@ package v1alpha1
1717
import (
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
3937
type 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 created", "folder", utils.FormatJSON(createResponse))
8580

86-
folder.Status = coralogixv1alpha1.DashboardsFolderStatus{ID: ptr.To(folderToCreate.Id.GetValue())}
81+
folder.Status = coralogixv1alpha1.DashboardsFolderStatus{ID: createResponse.FolderId}
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

Comments
 (0)