/
table.go
57 lines (49 loc) · 1.59 KB
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package odoostorage
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
billingv1 "github.com/appuio/control-api/apis/billing/v1"
)
// ConvertToTable translates the given object to a table for kubectl printing
func (s *billingEntityStorage) ConvertToTable(ctx context.Context, obj runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
var table metav1.Table
bes := []billingv1.BillingEntity{}
if meta.IsListType(obj) {
beList, ok := obj.(*billingv1.BillingEntityList)
if !ok {
return nil, fmt.Errorf("not an billing entity: %#v", obj)
}
bes = beList.Items
} else {
be, ok := obj.(*billingv1.BillingEntity)
if !ok {
return nil, fmt.Errorf("not an billing entity: %#v", obj)
}
bes = append(bes, *be)
}
for _, be := range bes {
table.Rows = append(table.Rows, beToTableRow(&be))
}
if opt, ok := tableOptions.(*metav1.TableOptions); !ok || !opt.NoHeaders {
desc := metav1.ObjectMeta{}.SwaggerDoc()
table.ColumnDefinitions = []metav1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name", Description: desc["name"]},
{Name: "Display Name", Type: "string", Description: "Name of the billing entity"},
{Name: "Invoice Contact", Type: "string", Description: "Name of the contact person for invoices"},
}
}
return &table, nil
}
func beToTableRow(be *billingv1.BillingEntity) metav1.TableRow {
return metav1.TableRow{
Cells: []any{
be.GetName(),
be.Spec.Name,
be.Spec.AccountingContact.Name,
},
Object: runtime.RawExtension{Object: be},
}
}