summaryrefslogtreecommitdiff
path: root/external/evcli/identities.go
blob: f89ae1b48a3cd3daa80f871b4ec1a46d60d9b343 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package evcli

import (
	"encoding/json"
	"fmt"
	"time"

	"github.com/exograd/eventline/pkg/eventline"
	"github.com/exograd/eventline/pkg/utils"
)

type IdentityPage struct {
	Elements Identities        `json:"elements"`
	Previous *eventline.Cursor `json:"previous,omitempty"`
	Next     *eventline.Cursor `json:"next,omitempty"`
}

type Identity struct {
	Id           eventline.Id             `json:"id"`
	ProjectId    *eventline.Id            `json:"project_id"`
	Name         string                   `json:"name"`
	Status       eventline.IdentityStatus `json:"status"`
	ErrorMessage string                   `json:"error_message,omitempty"`
	CreationTime time.Time                `json:"creation_time"`
	UpdateTime   time.Time                `json:"update_time"`
	LastUseTime  *time.Time               `json:"last_use_time,omitempty"`
	RefreshTime  *time.Time               `json:"refresh_time,omitempty"`
	Connector    string                   `json:"connector"`
	Type         string                   `json:"type"`
	Data         eventline.IdentityData   `json:"-"`
	RawData      json.RawMessage          `json:"data"`
}

type Identities []*Identity

func (i *Identity) SortKey(sort string) (key string) {
	switch sort {
	case "id":
		key = i.Id.String()
	case "name":
		key = i.Name
	default:
		utils.Panicf("unknown identity sort %q", sort)
	}

	return
}

func (pi *Identity) MarshalJSON() ([]byte, error) {
	type Identity2 Identity

	i := Identity2(*pi)
	data, err := json.Marshal(i.Data)
	if err != nil {
		return nil, fmt.Errorf("cannot encode data: %w", err)
	}

	i.RawData = data

	return json.Marshal(i)
}

func (pi *Identity) UnmarshalJSON(data []byte) error {
	type Identity2 Identity

	i := Identity2(*pi)
	if err := json.Unmarshal(data, &i); err != nil {
		return err
	}

	*pi = Identity(i)
	return nil
}