-
Notifications
You must be signed in to change notification settings - Fork 289
/
target.go
173 lines (148 loc) · 4.66 KB
/
target.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Package tcp provides a Target subtype for a TCP Target.
// Importing this package will register it with the target package and
// allow the target.Repository to support tcp.Targets.
package tcp
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/db/timestamp"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/oplog"
"github.com/hashicorp/boundary/internal/target"
"github.com/hashicorp/boundary/internal/target/tcp/store"
"github.com/hashicorp/boundary/internal/types/subtypes"
"google.golang.org/protobuf/proto"
)
const (
defaultTableName = "target_tcp"
Subtype = subtypes.Subtype("tcp")
)
// Target is a resources that represets a networked service
// that can be accessed via TCP. It is a subtype of target.Target.
type Target struct {
*store.Target
tableName string `gorm:"-"`
}
// Ensure Target implements interfaces
var (
_ target.Target = (*Target)(nil)
_ db.VetForWriter = (*Target)(nil)
_ oplog.ReplayableMessage = (*Target)(nil)
)
// NewTarget creates a new in memory tcp target. WithName, WithDescription and
// WithDefaultPort options are supported
func (h targetHooks) NewTarget(scopeId string, opt ...target.Option) (target.Target, error) {
const op = "tcp.NewTarget"
opts := target.GetOpts(opt...)
if scopeId == "" {
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing scope id")
}
t := &Target{
Target: &store.Target{
ScopeId: scopeId,
Name: opts.WithName,
Description: opts.WithDescription,
DefaultPort: opts.WithDefaultPort,
SessionConnectionLimit: opts.WithSessionConnectionLimit,
SessionMaxSeconds: opts.WithSessionMaxSeconds,
WorkerFilter: opts.WithWorkerFilter,
},
}
return t, nil
}
// AllocTarget will allocate a tcp target
func (h targetHooks) AllocTarget() target.Target {
return &Target{
Target: &store.Target{},
}
}
// Clone creates a clone of the Target
func (t *Target) Clone() target.Target {
cp := proto.Clone(t.Target)
return &Target{
Target: cp.(*store.Target),
}
}
// VetForWrite implements db.VetForWrite() interface and validates the tcp target
// before it's written.
func (t *Target) VetForWrite(ctx context.Context, _ db.Reader, opType db.OpType, _ ...db.Option) error {
const op = "tcp.(Target).VetForWrite"
if t.PublicId == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing public id")
}
if opType == db.CreateOp {
if t.ScopeId == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing scope id")
}
if t.Name == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing name")
}
}
return nil
}
// TableName returns the tablename to override the default gorm table name
func (t *Target) TableName() string {
if t.tableName != "" {
return t.tableName
}
return defaultTableName
}
// SetTableName sets the tablename and satisfies the ReplayableMessage
// interface. If the caller attempts to set the name to "" the name will be
// reset to the default name.
func (t *Target) SetTableName(n string) {
t.tableName = n
}
// Oplog provides the oplog.Metadata for recording operations taken on a Target.
func (t *Target) Oplog(op oplog.OpType) oplog.Metadata {
metadata := oplog.Metadata{
"resource-public-id": []string{t.PublicId},
"resource-type": []string{"tcp target"},
"op-type": []string{op.String()},
"scope-id": []string{t.ScopeId},
}
return metadata
}
func (t *Target) GetType() subtypes.Subtype {
return Subtype
}
func (t *Target) SetPublicId(ctx context.Context, publicId string) error {
const op = "tcp.(Target).SetPublicId"
if !strings.HasPrefix(publicId, TargetPrefix+"_") {
return errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("passed-in public ID %q has wrong prefix, should be %q", publicId, TargetPrefix))
}
t.PublicId = publicId
return nil
}
func (t *Target) SetScopeId(scopeId string) {
t.ScopeId = scopeId
}
func (t *Target) SetName(name string) {
t.Name = name
}
func (t *Target) SetDescription(description string) {
t.Description = description
}
func (t *Target) SetVersion(v uint32) {
t.Version = v
}
func (t *Target) SetDefaultPort(port uint32) {
t.DefaultPort = port
}
func (t *Target) SetCreateTime(ts *timestamp.Timestamp) {
t.CreateTime = ts
}
func (t *Target) SetUpdateTime(ts *timestamp.Timestamp) {
t.UpdateTime = ts
}
func (t *Target) SetSessionMaxSeconds(s uint32) {
t.SessionMaxSeconds = s
}
func (t *Target) SetSessionConnectionLimit(limit int32) {
t.SessionConnectionLimit = limit
}
func (t *Target) SetWorkerFilter(filter string) {
t.WorkerFilter = filter
}