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-10-31 12:26:39 -05:00
|
|
|
"github.com/grafana/grafana/pkg/kinds"
|
2023-10-25 13:13:46 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/grafana-apiserver/endpoints/request"
|
2023-10-12 10:29:06 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/playlist"
|
|
|
|
)
|
|
|
|
|
2023-10-31 12:26:39 -05:00
|
|
|
func UnstructuredToLegacyPlaylist(item unstructured.Unstructured) *playlist.Playlist {
|
|
|
|
spec := item.Object["spec"].(map[string]any)
|
|
|
|
return &playlist.Playlist{
|
|
|
|
UID: item.GetName(),
|
|
|
|
Name: spec["title"].(string),
|
|
|
|
Interval: spec["interval"].(string),
|
|
|
|
Id: getLegacyID(&item),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnstructuredToLegacyPlaylistDTO(item unstructured.Unstructured) *playlist.PlaylistDTO {
|
|
|
|
spec := item.Object["spec"].(map[string]any)
|
|
|
|
dto := &playlist.PlaylistDTO{
|
|
|
|
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-10-25 13:13:46 -05:00
|
|
|
func convertToK8sResource(v *playlist.PlaylistDTO, namespacer request.NamespaceMapper) *Playlist {
|
2023-10-12 10:29:06 -05:00
|
|
|
spec := Spec{
|
|
|
|
Title: v.Name,
|
|
|
|
Interval: v.Interval,
|
|
|
|
}
|
|
|
|
for _, item := range v.Items {
|
|
|
|
spec.Items = append(spec.Items, Item{
|
|
|
|
Type: ItemType(item.Type),
|
|
|
|
Value: item.Value,
|
|
|
|
})
|
|
|
|
}
|
2023-10-31 12:26:39 -05:00
|
|
|
|
|
|
|
meta := kinds.GrafanaResourceMetadata{}
|
|
|
|
meta.SetUpdatedTimestampMillis(v.UpdatedAt)
|
|
|
|
if v.Id > 0 {
|
|
|
|
meta.SetOriginInfo(&kinds.ResourceOriginInfo{
|
|
|
|
Name: "SQL",
|
|
|
|
Key: fmt.Sprintf("%d", v.Id),
|
|
|
|
})
|
|
|
|
}
|
2023-10-12 10:29:06 -05:00
|
|
|
return &Playlist{
|
|
|
|
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),
|
2023-10-31 12:26:39 -05:00
|
|
|
Annotations: meta.Annotations,
|
2023-10-12 10:29:06 -05:00
|
|
|
},
|
|
|
|
Spec: spec,
|
|
|
|
}
|
|
|
|
}
|
2023-10-31 12:26:39 -05:00
|
|
|
|
|
|
|
// Read legacy ID from metadata annotations
|
|
|
|
func getLegacyID(item *unstructured.Unstructured) int64 {
|
|
|
|
meta := kinds.GrafanaResourceMetadata{
|
|
|
|
Annotations: item.GetAnnotations(),
|
|
|
|
}
|
|
|
|
info := meta.GetOriginInfo()
|
|
|
|
if info != nil && info.Name == "SQL" {
|
|
|
|
i, err := strconv.ParseInt(info.Key, 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|