Skip to content

Commit 9752f36

Browse files
feat: Implement the possibility to move folder from parent or at root level
1 parent b6fdf3b commit 9752f36

File tree

6 files changed

+42
-2
lines changed

6 files changed

+42
-2
lines changed

api/v1beta1/grafanafolder_types.go

+7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ type GrafanaFolderStatus struct {
6666
NoMatchingInstances bool `json:"NoMatchingInstances,omitempty"`
6767
// Last time the folder was resynced
6868
LastResync metav1.Time `json:"lastResync,omitempty"`
69+
// UID of the parent folder where the folder is created.
70+
// Will be empty if the folder is deployed at the root level
71+
ParentFolderUID string `json:"parentFolderUID,omitempty"`
6972
}
7073

7174
//+kubebuilder:object:root=true
@@ -115,6 +118,10 @@ func (in *GrafanaFolder) Unchanged() bool {
115118
return in.Hash() == in.Status.Hash
116119
}
117120

121+
func (in *GrafanaFolder) Moved() bool {
122+
return in.Spec.ParentFolderUID != in.Status.ParentFolderUID
123+
}
124+
118125
func (in *GrafanaFolder) IsAllowCrossNamespaceImport() bool {
119126
if in.Spec.AllowCrossNamespaceImport != nil {
120127
return *in.Spec.AllowCrossNamespaceImport

config/crd/bases/grafana.integreatly.org_grafanafolders.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ spec:
134134
description: Last time the folder was resynced
135135
format: date-time
136136
type: string
137+
parentFolderUID:
138+
description: |-
139+
UID of the parent folder where the folder is created.
140+
Will be empty if the folder is deployed at the root level
141+
type: string
137142
type: object
138143
type: object
139144
served: true

controllers/grafanafolder_controller.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func (r *GrafanaFolderReconciler) onFolderCreated(ctx context.Context, grafana *
301301
}
302302

303303
// always update after resync period has elapsed even if cr is unchanged.
304-
if exists && cr.Unchanged() && !cr.ResyncPeriodHasElapsed() {
304+
if exists && cr.Unchanged() && !cr.ResyncPeriodHasElapsed() && !cr.Moved() {
305305
return nil
306306
}
307307

@@ -327,6 +327,15 @@ func (r *GrafanaFolderReconciler) onFolderCreated(ctx context.Context, grafana *
327327
return err
328328
}
329329
}
330+
331+
if cr.Moved() {
332+
_, err = grafanaClient.Folders.MoveFolder(remoteUID, &models.MoveFolderCommand{ //nolint
333+
ParentUID: cr.Spec.ParentFolderUID,
334+
})
335+
if err != nil {
336+
return err
337+
}
338+
}
330339
} else {
331340
body := &models.CreateFolderCommand{
332341
Title: title,
@@ -365,6 +374,7 @@ func (r *GrafanaFolderReconciler) onFolderCreated(ctx context.Context, grafana *
365374

366375
func (r *GrafanaFolderReconciler) UpdateStatus(ctx context.Context, cr *grafanav1beta1.GrafanaFolder) error {
367376
cr.Status.Hash = cr.Hash()
377+
cr.Status.ParentFolderUID = cr.Spec.ParentFolderUID
368378
return r.Client.Status().Update(ctx, cr)
369379
}
370380

@@ -375,7 +385,7 @@ func (r *GrafanaFolderReconciler) Exists(client *genapi.GrafanaHTTPAPI, cr *graf
375385
page := int64(1)
376386
limit := int64(10000)
377387
for {
378-
params := folders.NewGetFoldersParams().WithPage(&page).WithLimit(&limit).WithParentUID(&cr.Spec.ParentFolderUID)
388+
params := folders.NewGetFoldersParams().WithPage(&page).WithLimit(&limit).WithParentUID(&cr.Status.ParentFolderUID)
379389

380390
foldersResp, err := client.Folders.GetFolders(params)
381391
if err != nil {

deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanafolders.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ spec:
134134
description: Last time the folder was resynced
135135
format: date-time
136136
type: string
137+
parentFolderUID:
138+
description: |-
139+
UID of the parent folder where the folder is created.
140+
Will be empty if the folder is deployed at the root level
141+
type: string
137142
type: object
138143
type: object
139144
served: true

deploy/kustomize/base/crds.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,11 @@ spec:
12791279
description: Last time the folder was resynced
12801280
format: date-time
12811281
type: string
1282+
parentFolderUID:
1283+
description: |-
1284+
UID of the parent folder where the folder is created.
1285+
Will be empty if the folder is deployed at the root level
1286+
type: string
12821287
type: object
12831288
type: object
12841289
served: true

docs/docs/api.md

+8
Original file line numberDiff line numberDiff line change
@@ -2629,6 +2629,14 @@ Important: Run "make" to regenerate code after modifying this file<br/>
26292629
<i>Format</i>: date-time<br/>
26302630
</td>
26312631
<td>false</td>
2632+
</tr><tr>
2633+
<td><b>parentFolderUID</b></td>
2634+
<td>string</td>
2635+
<td>
2636+
UID of the parent folder where the folder is created.
2637+
Will be empty if the folder is deployed at the root level<br/>
2638+
</td>
2639+
<td>false</td>
26322640
</tr></tbody>
26332641
</table>
26342642

0 commit comments

Comments
 (0)