-
Notifications
You must be signed in to change notification settings - Fork 28
/
service_broker.go
117 lines (99 loc) · 3.13 KB
/
service_broker.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
/*
* 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"
"strconv"
)
const maxNameLength = 255
//go:generate smgen api ServiceBroker
// ServiceBroker broker struct
type ServiceBroker struct {
Base
Secured `json:"-"`
Strip `json:"-"`
Name string `json:"name"`
Description string `json:"description"`
BrokerURL string `json:"broker_url"`
Credentials *Credentials `json:"credentials,omitempty"`
Catalog json.RawMessage `json:"-"`
Services []*ServiceOffering `json:"-"`
LastOperation *Operation `json:"last_operation,omitempty"`
}
func (e *ServiceBroker) Sanitize() {
e.Credentials = nil
}
func (e *ServiceBroker) Encrypt(ctx context.Context, encryptionFunc func(context.Context, []byte) ([]byte, error)) error {
return e.transform(ctx, encryptionFunc)
}
func (e *ServiceBroker) Decrypt(ctx context.Context, decryptionFunc func(context.Context, []byte) ([]byte, error)) error {
return e.transform(ctx, decryptionFunc)
}
func (e *ServiceBroker) transform(ctx context.Context, transformationFunc func(context.Context, []byte) ([]byte, error)) error {
if e.Credentials == nil || e.Credentials.Basic == nil {
return nil
}
transformedPassword, err := transformationFunc(ctx, []byte(e.Credentials.Basic.Password))
if err != nil {
return err
}
e.Credentials.Basic.Password = string(transformedPassword)
return nil
}
func (e *ServiceBroker) SetLastOperation(lastOp *Operation) {
e.LastOperation = lastOp
}
func (e *ServiceBroker) GetLastOperation() *Operation {
return e.LastOperation
}
// Validate implements InputValidator and verifies all mandatory fields are populated
func (e *ServiceBroker) Validate() error {
if e.Name == "" {
return errors.New("missing broker name")
}
if len(e.Name) > maxNameLength {
return fmt.Errorf("broker name cannot exceed %s symbols", strconv.Itoa(maxNameLength))
}
if e.BrokerURL == "" {
return errors.New("missing broker url")
}
if err := e.Labels.Validate(); err != nil {
return err
}
if e.Credentials == nil {
return errors.New("missing credentials")
}
return e.Credentials.Validate()
}
func (e *ServiceBroker) Equals(obj Object) bool {
if !Equals(e, obj) {
return false
}
broker := obj.(*ServiceBroker)
if e.Name != broker.Name ||
e.BrokerURL != broker.BrokerURL ||
e.Description != broker.Description ||
!reflect.DeepEqual(e.Catalog, broker.Catalog) ||
!reflect.DeepEqual(e.Credentials, broker.Credentials) {
return false
}
return true
}