-
Notifications
You must be signed in to change notification settings - Fork 69
/
amqp_mgmt.go
188 lines (163 loc) · 5.96 KB
/
amqp_mgmt.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package eventhub
// MIT License
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE
import (
"context"
"fmt"
"time"
"github.com/Azure/azure-amqp-common-go/v4/rpc"
"github.com/Azure/go-amqp"
"github.com/devigned/tab"
"github.com/mitchellh/mapstructure"
)
const (
// MsftVendor is the Microsoft vendor identifier
MsftVendor = "com.microsoft"
entityTypeKey = "type"
entityNameKey = "name"
partitionNameKey = "partition"
securityTokenKey = "security_token"
eventHubEntityType = MsftVendor + ":eventhub"
partitionEntityType = MsftVendor + ":partition"
operationKey = "operation"
readOperationKey = "READ"
address = "$management"
)
type (
// client communicates with an AMQP management node
client struct {
namespace *namespace
hubName string
}
// HubRuntimeInformation provides management node information about a given Event Hub instance
HubRuntimeInformation struct {
Path string `mapstructure:"name"`
CreatedAt time.Time `mapstructure:"created_at"`
PartitionCount int `mapstructure:"partition_count"`
PartitionIDs []string `mapstructure:"partition_ids"`
}
// HubPartitionRuntimeInformation provides management node information about a given Event Hub partition
HubPartitionRuntimeInformation struct {
HubPath string `mapstructure:"name"`
PartitionID string `mapstructure:"partition"`
BeginningSequenceNumber int64 `mapstructure:"begin_sequence_number"`
LastSequenceNumber int64 `mapstructure:"last_enqueued_sequence_number"`
LastEnqueuedOffset string `mapstructure:"last_enqueued_offset"`
LastEnqueuedTimeUtc time.Time `mapstructure:"last_enqueued_time_utc"`
}
)
// newClient constructs a new AMQP management client
func newClient(namespace *namespace, hubName string) *client {
return &client{
namespace: namespace,
hubName: hubName,
}
}
// GetHubRuntimeInformation requests runtime information for an Event Hub
func (c *client) GetHubRuntimeInformation(ctx context.Context, conn *amqp.Conn) (*HubRuntimeInformation, error) {
ctx, span := tab.StartSpan(ctx, "eh.mgmt.client.GetHubRuntimeInformation")
defer span.End()
rpcLink, err := rpc.NewLink(ctx, conn, address)
if err != nil {
return nil, err
}
msg := &amqp.Message{
ApplicationProperties: map[string]interface{}{
operationKey: readOperationKey,
entityTypeKey: eventHubEntityType,
entityNameKey: c.hubName,
},
}
msg, err = c.addSecurityToken(msg)
if err != nil {
return nil, err
}
res, err := rpcLink.RetryableRPC(ctx, 3, 1*time.Second, msg)
if err != nil {
return nil, err
}
hubRuntimeInfo, err := newHubRuntimeInformation(res.Message)
if err != nil {
return nil, err
}
return hubRuntimeInfo, nil
}
// GetHubPartitionRuntimeInformation fetches runtime information from the AMQP management node for a given partition
func (c *client) GetHubPartitionRuntimeInformation(ctx context.Context, conn *amqp.Conn, partitionID string) (*HubPartitionRuntimeInformation, error) {
ctx, span := tab.StartSpan(ctx, "eh.mgmt.client.GetHubPartitionRuntimeInformation")
defer span.End()
rpcLink, err := rpc.NewLink(ctx, conn, address)
if err != nil {
return nil, err
}
msg := &amqp.Message{
ApplicationProperties: map[string]interface{}{
operationKey: readOperationKey,
entityTypeKey: partitionEntityType,
entityNameKey: c.hubName,
partitionNameKey: partitionID,
},
}
msg, err = c.addSecurityToken(msg)
if err != nil {
return nil, err
}
res, err := rpcLink.RetryableRPC(ctx, 3, 1*time.Second, msg)
if err != nil {
return nil, err
}
hubPartitionRuntimeInfo, err := newHubPartitionRuntimeInformation(res.Message)
if err != nil {
return nil, err
}
return hubPartitionRuntimeInfo, nil
}
func (c *client) addSecurityToken(msg *amqp.Message) (*amqp.Message, error) {
token, err := c.namespace.tokenProvider.GetToken(c.getTokenAudience())
if err != nil {
return nil, err
}
msg.ApplicationProperties[securityTokenKey] = token.Token
return msg, nil
}
func (c *client) getTokenAudience() string {
return c.namespace.getAmqpHostURI() + c.hubName
}
func newHubPartitionRuntimeInformation(msg *amqp.Message) (*HubPartitionRuntimeInformation, error) {
values, ok := msg.Value.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("values were not map[string]interface{}, it was: %v", values)
}
var partitionInfo HubPartitionRuntimeInformation
err := mapstructure.Decode(values, &partitionInfo)
return &partitionInfo, err
}
// newHubRuntimeInformation constructs a new HubRuntimeInformation from an AMQP message
func newHubRuntimeInformation(msg *amqp.Message) (*HubRuntimeInformation, error) {
values, ok := msg.Value.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("values were not map[string]interface{}, it was: %v", values)
}
var runtimeInfo HubRuntimeInformation
err := mapstructure.Decode(values, &runtimeInfo)
return &runtimeInfo, err
}