-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathstream_activation.go
166 lines (135 loc) · 5.05 KB
/
stream_activation.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
package datastream
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// ActivationHistoryEntry contains single ActivationHistory item
ActivationHistoryEntry struct {
ModifiedBy string `json:"modifiedBy"`
ModifiedDate string `json:"modifiedDate"`
Status StreamStatus `json:"status"`
StreamID int64 `json:"streamId"`
StreamVersion int64 `json:"streamVersion"`
}
// ActivateStreamRequest contains parameters necessary to send a ActivateStream request
ActivateStreamRequest struct {
StreamID int64
}
// DeactivateStreamRequest contains parameters necessary to send a DeactivateStream request
DeactivateStreamRequest ActivateStreamRequest
// GetActivationHistoryRequest contains parameters necessary to send a GetActivationHistory request
GetActivationHistoryRequest ActivateStreamRequest
)
// Validate performs validation on ActivateStreamRequest
func (r ActivateStreamRequest) Validate() error {
return validation.Errors{
"streamId": validation.Validate(r.StreamID, validation.Required),
}.Filter()
}
// Validate performs validation on DeactivateStreamRequest
func (r DeactivateStreamRequest) Validate() error {
return validation.Errors{
"streamId": validation.Validate(r.StreamID, validation.Required),
}.Filter()
}
// Validate performs validation on DeactivateStreamRequest
func (r GetActivationHistoryRequest) Validate() error {
return validation.Errors{
"streamId": validation.Validate(r.StreamID, validation.Required),
}.Filter()
}
var (
// ErrActivateStream is returned when ActivateStream fails
ErrActivateStream = errors.New("activate stream")
// ErrDeactivateStream is returned when DeactivateStream fails
ErrDeactivateStream = errors.New("deactivate stream")
// ErrGetActivationHistory is returned when DeactivateStream fails
ErrGetActivationHistory = errors.New("view activation history")
)
func (d *ds) ActivateStream(ctx context.Context, params ActivateStreamRequest) (*DetailedStreamVersion, error) {
logger := d.Log(ctx)
logger.Debug("ActivateStream")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrActivateStream, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf(
"/datastream-config-api/v2/log/streams/%d/activate",
params.StreamID))
if err != nil {
return nil, fmt.Errorf("%w: parsing URL: %s", ErrActivateStream, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrActivateStream, err)
}
var rval DetailedStreamVersion
resp, err := d.Exec(req, &rval)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrActivateStream, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrActivateStream, d.Error(resp))
}
return &rval, nil
}
func (d *ds) DeactivateStream(ctx context.Context, params DeactivateStreamRequest) (*DetailedStreamVersion, error) {
logger := d.Log(ctx)
logger.Debug("DeactivateStream")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrDeactivateStream, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf(
"/datastream-config-api/v2/log/streams/%d/deactivate",
params.StreamID))
if err != nil {
return nil, fmt.Errorf("%w: parsing URL: %s", ErrDeactivateStream, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrDeactivateStream, err)
}
var rval DetailedStreamVersion
resp, err := d.Exec(req, &rval)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrDeactivateStream, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrDeactivateStream, d.Error(resp))
}
return &rval, nil
}
func (d *ds) GetActivationHistory(ctx context.Context, params GetActivationHistoryRequest) ([]ActivationHistoryEntry, error) {
logger := d.Log(ctx)
logger.Debug("GetActivationHistory")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetActivationHistory, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf(
"/datastream-config-api/v2/log/streams/%d/activation-history",
params.StreamID))
if err != nil {
return nil, fmt.Errorf("%w: parsing URL: %s", ErrGetActivationHistory, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetActivationHistory, err)
}
var rval []ActivationHistoryEntry
resp, err := d.Exec(req, &rval)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetActivationHistory, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetActivationHistory, d.Error(resp))
}
return rval, nil
}