2023-11-01 08:44:04 -05:00
|
|
|
package playlist
|
2023-10-12 10:29:06 -05:00
|
|
|
|
|
|
|
import (
|
2023-10-31 12:26:39 -05:00
|
|
|
"encoding/json"
|
2023-10-12 10:29:06 -05:00
|
|
|
"fmt"
|
2023-10-31 12:26:39 -05:00
|
|
|
"strconv"
|
2023-10-12 10:29:06 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2023-10-31 12:26:39 -05:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2023-10-12 10:29:06 -05:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
|
2023-11-03 10:07:55 -05:00
|
|
|
playlist "github.com/grafana/grafana/pkg/apis/playlist/v0alpha1"
|
2024-02-01 16:27:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
|
|
|
"github.com/grafana/grafana/pkg/services/apiserver/utils"
|
2023-11-03 10:07:55 -05:00
|
|
|
playlistsvc "github.com/grafana/grafana/pkg/services/playlist"
|
2023-10-12 10:29:06 -05:00
|
|
|
)
|
|
|
|
|
2023-11-03 11:25:29 -05:00
|
|
|
func LegacyUpdateCommandToUnstructured(cmd playlistsvc.UpdatePlaylistCommand) unstructured.Unstructured {
|
|
|
|
items := []map[string]string{}
|
|
|
|
for _, item := range cmd.Items {
|
|
|
|
items = append(items, map[string]string{
|
|
|
|
"type": item.Type,
|
|
|
|
"value": item.Value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
obj := unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"spec": map[string]interface{}{
|
|
|
|
"title": cmd.Name,
|
|
|
|
"interval": cmd.Interval,
|
|
|
|
"items": items,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
obj.SetName(cmd.UID)
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
2023-11-03 10:07:55 -05:00
|
|
|
func UnstructuredToLegacyPlaylist(item unstructured.Unstructured) *playlistsvc.Playlist {
|
2023-10-31 12:26:39 -05:00
|
|
|
spec := item.Object["spec"].(map[string]any)
|
2023-11-03 10:07:55 -05:00
|
|
|
return &playlistsvc.Playlist{
|
2023-10-31 12:26:39 -05:00
|
|
|
UID: item.GetName(),
|
|
|
|
Name: spec["title"].(string),
|
|
|
|
Interval: spec["interval"].(string),
|
|
|
|
Id: getLegacyID(&item),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-03 10:07:55 -05:00
|
|
|
func UnstructuredToLegacyPlaylistDTO(item unstructured.Unstructured) *playlistsvc.PlaylistDTO {
|
2023-10-31 12:26:39 -05:00
|
|
|
spec := item.Object["spec"].(map[string]any)
|
2023-11-03 10:07:55 -05:00
|
|
|
dto := &playlistsvc.PlaylistDTO{
|
2023-10-31 12:26:39 -05:00
|
|
|
Uid: item.GetName(),
|
|
|
|
Name: spec["title"].(string),
|
|
|
|
Interval: spec["interval"].(string),
|
|
|
|
Id: getLegacyID(&item),
|
|
|
|
}
|
|
|
|
items := spec["items"]
|
|
|
|
if items != nil {
|
|
|
|
b, err := json.Marshal(items)
|
|
|
|
if err == nil {
|
|
|
|
_ = json.Unmarshal(b, &dto.Items)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dto
|
|
|
|
}
|
|
|
|
|
2023-11-03 10:07:55 -05:00
|
|
|
func convertToK8sResource(v *playlistsvc.PlaylistDTO, namespacer request.NamespaceMapper) *playlist.Playlist {
|
|
|
|
spec := playlist.Spec{
|
2023-10-12 10:29:06 -05:00
|
|
|
Title: v.Name,
|
|
|
|
Interval: v.Interval,
|
|
|
|
}
|
|
|
|
for _, item := range v.Items {
|
2023-11-03 10:07:55 -05:00
|
|
|
spec.Items = append(spec.Items, playlist.Item{
|
|
|
|
Type: playlist.ItemType(item.Type),
|
2023-10-12 10:29:06 -05:00
|
|
|
Value: item.Value,
|
|
|
|
})
|
|
|
|
}
|
2023-10-31 12:26:39 -05:00
|
|
|
|
2023-12-20 12:28:56 -06:00
|
|
|
p := &playlist.Playlist{
|
2023-10-12 10:29:06 -05:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: v.Uid,
|
|
|
|
UID: types.UID(v.Uid),
|
|
|
|
ResourceVersion: fmt.Sprintf("%d", v.UpdatedAt),
|
|
|
|
CreationTimestamp: metav1.NewTime(time.UnixMilli(v.CreatedAt)),
|
|
|
|
Namespace: namespacer(v.OrgID),
|
|
|
|
},
|
|
|
|
Spec: spec,
|
|
|
|
}
|
2024-01-12 15:18:14 -06:00
|
|
|
meta, err := utils.MetaAccessor(p)
|
|
|
|
if err == nil {
|
|
|
|
meta.SetUpdatedTimestampMillis(v.UpdatedAt)
|
|
|
|
if v.Id > 0 {
|
2024-05-02 09:06:51 -05:00
|
|
|
createdAt := time.UnixMilli(v.CreatedAt)
|
2024-01-12 15:18:14 -06:00
|
|
|
meta.SetOriginInfo(&utils.ResourceOriginInfo{
|
2024-05-02 09:06:51 -05:00
|
|
|
Name: "SQL",
|
|
|
|
Key: fmt.Sprintf("%d", v.Id),
|
|
|
|
Timestamp: &createdAt,
|
2024-01-12 15:18:14 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 12:28:56 -06:00
|
|
|
p.UID = utils.CalculateClusterWideUID(p)
|
|
|
|
return p
|
2023-10-12 10:29:06 -05:00
|
|
|
}
|
2023-10-31 12:26:39 -05:00
|
|
|
|
2023-11-03 10:07:55 -05:00
|
|
|
func convertToLegacyUpdateCommand(p *playlist.Playlist, orgId int64) (*playlistsvc.UpdatePlaylistCommand, error) {
|
2023-11-01 14:32:24 -05:00
|
|
|
spec := p.Spec
|
2023-11-03 10:07:55 -05:00
|
|
|
cmd := &playlistsvc.UpdatePlaylistCommand{
|
2023-11-01 14:32:24 -05:00
|
|
|
UID: p.Name,
|
|
|
|
Name: spec.Title,
|
|
|
|
Interval: spec.Interval,
|
|
|
|
OrgId: orgId,
|
|
|
|
}
|
|
|
|
for _, item := range spec.Items {
|
2023-11-03 10:07:55 -05:00
|
|
|
if item.Type == playlist.ItemTypeDashboardById {
|
2023-11-01 14:32:24 -05:00
|
|
|
return nil, fmt.Errorf("unsupported item type: %s", item.Type)
|
|
|
|
}
|
2023-11-03 10:07:55 -05:00
|
|
|
cmd.Items = append(cmd.Items, playlistsvc.PlaylistItem{
|
2023-11-01 14:32:24 -05:00
|
|
|
Type: string(item.Type),
|
|
|
|
Value: item.Value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return cmd, nil
|
|
|
|
}
|
|
|
|
|
2023-10-31 12:26:39 -05:00
|
|
|
// Read legacy ID from metadata annotations
|
|
|
|
func getLegacyID(item *unstructured.Unstructured) int64 {
|
2024-01-12 15:18:14 -06:00
|
|
|
meta, err := utils.MetaAccessor(item)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
2023-10-31 12:26:39 -05:00
|
|
|
}
|
2023-12-12 14:55:10 -06:00
|
|
|
info, _ := meta.GetOriginInfo()
|
2023-10-31 12:26:39 -05:00
|
|
|
if info != nil && info.Name == "SQL" {
|
|
|
|
i, err := strconv.ParseInt(info.Key, 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|