generated from crossplane/provider-template
/
searchattribute.go
121 lines (93 loc) · 3.52 KB
/
searchattribute.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package clients
import (
"context"
"encoding/json"
enums "go.temporal.io/api/enums/v1"
"go.temporal.io/api/operatorservice/v1"
core "github.com/denniskniep/provider-temporal/apis/core/v1alpha1"
)
type SearchAttributeService interface {
DescribeSearchAttributeByName(ctx context.Context, namespace string, name string) (*core.SearchAttributeObservation, error)
CreateSearchAttribute(ctx context.Context, searchAttribute *core.SearchAttributeParameters) error
DeleteSearchAttributeByName(ctx context.Context, namespace string, name string) error
MapToSearchAttributeCompare(searchAttribute interface{}) (*SearchAttributeCompare, error)
}
type SearchAttributeCompare struct {
Name string `json:"name"`
Type string `json:"type"`
TemporalNamespaceName *string `json:"temporalNamespaceName,omitempty"`
}
func (s *TemporalServiceImpl) MapToSearchAttributeCompare(searchAttribute interface{}) (*SearchAttributeCompare, error) {
searchAttributeJson, err := json.Marshal(searchAttribute)
if err != nil {
return nil, err
}
var searchAttributeCompare = SearchAttributeCompare{}
err = json.Unmarshal(searchAttributeJson, &searchAttributeCompare)
if err != nil {
return nil, err
}
return &searchAttributeCompare, nil
}
func (s *TemporalServiceImpl) CreateSearchAttribute(ctx context.Context, searchAttribute *core.SearchAttributeParameters) error {
searchAttributeMap := make(map[string]enums.IndexedValueType)
searchAttributeMap[searchAttribute.Name] = enums.IndexedValueType(enums.IndexedValueType_value[searchAttribute.Type])
createrequest := &operatorservice.AddSearchAttributesRequest{
Namespace: *searchAttribute.TemporalNamespaceName,
SearchAttributes: searchAttributeMap,
}
_, err := s.client.OperatorService().AddSearchAttributes(ctx, createrequest)
if err != nil {
return err
}
return nil
}
func (s *TemporalServiceImpl) DescribeSearchAttributeByName(ctx context.Context, namespace string, name string) (*core.SearchAttributeObservation, error) {
response, err := s.ListSearchAttributesByNamespace(ctx, namespace)
if err != nil {
return nil, err
}
if response == nil {
return nil, nil
}
for _, customAttribute := range response {
if customAttribute.Name == name {
return customAttribute, nil
}
}
return nil, nil
}
func (s *TemporalServiceImpl) ListSearchAttributesByNamespace(ctx context.Context, namespace string) ([]*core.SearchAttributeObservation, error) {
request := &operatorservice.ListSearchAttributesRequest{
Namespace: namespace,
}
response, err := s.client.OperatorService().ListSearchAttributes(ctx, request)
var customAttributes = make([]*core.SearchAttributeObservation, 0, len(response.CustomAttributes))
if err != nil {
return nil, err
}
if response == nil {
return customAttributes, nil
}
for attrName, attrType := range response.CustomAttributes {
customAttribute := core.SearchAttributeObservation{
Name: attrName,
Type: attrType.String(),
TemporalNamespaceName: namespace,
}
customAttributes = append(customAttributes, &customAttribute)
}
return customAttributes, nil
}
func (s *TemporalServiceImpl) DeleteSearchAttributeByName(ctx context.Context, namespace string, name string) error {
searchAttributeNames := []string{name}
deleterequest := &operatorservice.RemoveSearchAttributesRequest{
Namespace: namespace,
SearchAttributes: searchAttributeNames,
}
_, err := s.client.OperatorService().RemoveSearchAttributes(ctx, deleterequest)
if err != nil {
return err
}
return nil
}