-
Notifications
You must be signed in to change notification settings - Fork 28
/
service_binding.go
114 lines (99 loc) · 3.63 KB
/
service_binding.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
/*
* Copyright 2018 The Service Manager Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package types contains the Service Manager web entities
package types
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"github.com/Peripli/service-manager/pkg/util"
)
//go:generate smgen api ServiceBinding
// ServiceBinding struct
type ServiceBinding struct {
Base
Secured `json:"-"`
Name string `json:"name"`
ServiceInstanceID string `json:"service_instance_id"`
SyslogDrainURL string `json:"syslog_drain_url,omitempty"`
RouteServiceURL string `json:"route_service_url,omitempty"`
VolumeMounts json.RawMessage `json:"volume_mounts,omitempty"`
Endpoints json.RawMessage `json:"endpoints,omitempty"`
Context json.RawMessage `json:"context,omitempty"`
BindResource json.RawMessage `json:"bind_resource,omitempty"`
Credentials json.RawMessage `json:"credentials,omitempty"`
Parameters map[string]interface{} `json:"parameters,omitempty"`
LastOperation *Operation `json:"last_operation,omitempty"`
}
func (e *ServiceBinding) Encrypt(ctx context.Context, encryptionFunc func(context.Context, []byte) ([]byte, error)) error {
return e.transform(ctx, encryptionFunc)
}
func (e *ServiceBinding) Decrypt(ctx context.Context, decryptionFunc func(context.Context, []byte) ([]byte, error)) error {
return e.transform(ctx, decryptionFunc)
}
func (e *ServiceBinding) transform(ctx context.Context, transformationFunc func(context.Context, []byte) ([]byte, error)) error {
if len(e.Credentials) == 0 {
return nil
}
transformedCredentials, err := transformationFunc(ctx, e.Credentials)
if err != nil {
return err
}
e.Credentials = transformedCredentials
return nil
}
func (e *ServiceBinding) Equals(obj Object) bool {
if !Equals(e, obj) {
return false
}
binding := obj.(*ServiceBinding)
if e.Name != binding.Name ||
e.ServiceInstanceID != binding.ServiceInstanceID ||
e.SyslogDrainURL != binding.SyslogDrainURL ||
e.RouteServiceURL != binding.RouteServiceURL ||
!reflect.DeepEqual(e.VolumeMounts, binding.VolumeMounts) ||
!reflect.DeepEqual(e.Endpoints, binding.Endpoints) ||
!reflect.DeepEqual(e.Context, binding.Context) ||
!reflect.DeepEqual(e.BindResource, binding.BindResource) ||
!reflect.DeepEqual(e.Credentials, binding.Credentials) {
return false
}
return true
}
func (e *ServiceBinding) SetLastOperation(lastOp *Operation) {
e.LastOperation = lastOp
}
func (e *ServiceBinding) GetLastOperation() *Operation {
return e.LastOperation
}
// Validate implements InputValidator and verifies all mandatory fields are populated
func (e *ServiceBinding) Validate() error {
if util.HasRFC3986ReservedSymbols(e.ID) {
return fmt.Errorf("%s contains invalid character(s)", e.ID)
}
if e.Name == "" {
return errors.New("missing service binding name")
}
if e.ServiceInstanceID == "" {
return errors.New("missing service binding service instance ID")
}
if err := e.Labels.Validate(); err != nil {
return err
}
return nil
}