Skip to content

Commit

Permalink
feat: Implement the possibility to move folder from parent or at root…
Browse files Browse the repository at this point in the history
… level
  • Loading branch information
aboulay-numspot committed Jun 21, 2024
1 parent 6054405 commit e320737
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 2 deletions.
7 changes: 7 additions & 0 deletions api/v1beta1/grafanafolder_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ type GrafanaFolderStatus struct {
NoMatchingInstances bool `json:"NoMatchingInstances,omitempty"`
// Last time the folder was resynced
LastResync metav1.Time `json:"lastResync,omitempty"`
// UID of the parent folder where the folder is created.
// Will be empty if the folder is deployed at the root level
ParentFolderUID string `json:"parentFolderUID,omitempty"`
}

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

func (in *GrafanaFolder) Moved() bool {
return in.Spec.ParentFolderUID != in.Status.ParentFolderUID
}

func (in *GrafanaFolder) IsAllowCrossNamespaceImport() bool {
if in.Spec.AllowCrossNamespaceImport != nil {
return *in.Spec.AllowCrossNamespaceImport
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/grafana.integreatly.org_grafanafolders.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ spec:
description: Last time the folder was resynced
format: date-time
type: string
parentFolderUID:
description: |-
UID of the parent folder where the folder is created.
Will be empty if the folder is deployed at the root level
type: string
type: object
type: object
served: true
Expand Down
14 changes: 12 additions & 2 deletions controllers/grafanafolder_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (r *GrafanaFolderReconciler) onFolderCreated(ctx context.Context, grafana *
}

// always update after resync period has elapsed even if cr is unchanged.
if exists && cr.Unchanged() && !cr.ResyncPeriodHasElapsed() {
if exists && cr.Unchanged() && !cr.ResyncPeriodHasElapsed() && !cr.Moved() {
return nil
}

Expand All @@ -327,6 +327,15 @@ func (r *GrafanaFolderReconciler) onFolderCreated(ctx context.Context, grafana *
return err
}
}

if cr.Moved() {
_, err = grafanaClient.Folders.MoveFolder(remoteUID, &models.MoveFolderCommand{ //nolint
ParentUID: cr.Spec.ParentFolderUID,
})
if err != nil {
return err
}
}
} else {
body := &models.CreateFolderCommand{
Title: title,
Expand Down Expand Up @@ -365,6 +374,7 @@ func (r *GrafanaFolderReconciler) onFolderCreated(ctx context.Context, grafana *

func (r *GrafanaFolderReconciler) UpdateStatus(ctx context.Context, cr *grafanav1beta1.GrafanaFolder) error {
cr.Status.Hash = cr.Hash()
cr.Status.ParentFolderUID = cr.Spec.ParentFolderUID
return r.Client.Status().Update(ctx, cr)
}

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

foldersResp, err := client.Folders.GetFolders(params)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ spec:
description: Last time the folder was resynced
format: date-time
type: string
parentFolderUID:
description: |-
UID of the parent folder where the folder is created.
Will be empty if the folder is deployed at the root level
type: string
type: object
type: object
served: true
Expand Down
5 changes: 5 additions & 0 deletions deploy/kustomize/base/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,11 @@ spec:
description: Last time the folder was resynced
format: date-time
type: string
parentFolderUID:
description: |-
UID of the parent folder where the folder is created.
Will be empty if the folder is deployed at the root level
type: string
type: object
type: object
served: true
Expand Down
8 changes: 8 additions & 0 deletions docs/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2629,6 +2629,14 @@ Important: Run "make" to regenerate code after modifying this file<br/>
<i>Format</i>: date-time<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>parentFolderUID</b></td>
<td>string</td>
<td>
UID of the parent folder where the folder is created.
Will be empty if the folder is deployed at the root level<br/>
</td>
<td>false</td>
</tr></tbody>
</table>

Expand Down

0 comments on commit e320737

Please sign in to comment.