-
Notifications
You must be signed in to change notification settings - Fork 29
/
service_integration_endpoint.go
133 lines (112 loc) · 4.37 KB
/
service_integration_endpoint.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
package aiven
import (
"context"
"fmt"
)
type (
// ServiceIntegrationEndpoint represents a service integration endpoint,
// like parameters for integration to Datadog
ServiceIntegrationEndpoint struct {
EndpointID string `json:"endpoint_id"`
EndpointName string `json:"endpoint_name"`
EndpointType string `json:"endpoint_type"`
UserConfig map[string]interface{} `json:"user_config"`
EndpointConfig map[string]interface{} `json:"endpoint_config"`
}
// ServiceIntegrationEndpointsHandler is the client that interacts
// with the Service Integration Endpoints API endpoints on Aiven.
ServiceIntegrationEndpointsHandler struct {
client *Client
}
// CreateServiceIntegrationEndpointRequest are the parameters to create
// a Service Integration Endpoint.
CreateServiceIntegrationEndpointRequest struct {
EndpointName string `json:"endpoint_name"`
EndpointType string `json:"endpoint_type"`
UserConfig map[string]interface{} `json:"user_config"`
}
// UpdateServiceIntegrationEndpointRequest are the parameters to update
// a Service Integration Endpoint.
UpdateServiceIntegrationEndpointRequest struct {
UserConfig map[string]interface{} `json:"user_config"`
}
// ServiceIntegrationEndpointResponse represents the response from Aiven
// after interacting with the Service Integration Endpoints API.
ServiceIntegrationEndpointResponse struct {
APIResponse
ServiceIntegrationEndpoint *ServiceIntegrationEndpoint `json:"service_integration_endpoint"`
}
// ServiceIntegrationEndpointListResponse represents the response from Aiven
// for listing service integration endpoints.
ServiceIntegrationEndpointListResponse struct {
APIResponse
ServiceIntegrationEndpoints []*ServiceIntegrationEndpoint `json:"service_integration_endpoints"`
}
)
// Create the given Service Integration Endpoint on Aiven.
func (h *ServiceIntegrationEndpointsHandler) Create(
ctx context.Context,
project string,
req CreateServiceIntegrationEndpointRequest,
) (*ServiceIntegrationEndpoint, error) {
path := buildPath("project", project, "integration_endpoint")
bts, err := h.client.doPostRequest(ctx, path, req)
if err != nil {
return nil, err
}
var r ServiceIntegrationEndpointResponse
errR := checkAPIResponse(bts, &r)
return r.ServiceIntegrationEndpoint, errR
}
// Get a specific service integration endpoint from Aiven.
func (h *ServiceIntegrationEndpointsHandler) Get(ctx context.Context, project, endpointID string) (*ServiceIntegrationEndpoint, error) {
// There's no API for getting integration endpoint by ID. List all endpoints
// and pick the correct one instead. (There shouldn't ever be many endpoints.)
endpoints, err := h.List(ctx, project)
if err != nil {
return nil, err
}
for _, endpoint := range endpoints {
if endpoint.EndpointID == endpointID {
return endpoint, nil
}
}
err = Error{Message: fmt.Sprintf("Integration endpoint with ID %v not found", endpointID), Status: 404}
return nil, err
}
// Update the given service integration endpoint with the given parameters.
func (h *ServiceIntegrationEndpointsHandler) Update(
ctx context.Context,
project string,
endpointID string,
req UpdateServiceIntegrationEndpointRequest,
) (*ServiceIntegrationEndpoint, error) {
path := buildPath("project", project, "integration_endpoint", endpointID)
bts, err := h.client.doPutRequest(ctx, path, req)
if err != nil {
return nil, err
}
var r ServiceIntegrationEndpointResponse
errR := checkAPIResponse(bts, &r)
return r.ServiceIntegrationEndpoint, errR
}
// Delete the given service integration endpoint from Aiven.
func (h *ServiceIntegrationEndpointsHandler) Delete(ctx context.Context, project, endpointID string) error {
path := buildPath("project", project, "integration_endpoint", endpointID)
bts, err := h.client.doDeleteRequest(ctx, path, nil)
if err != nil {
return err
}
return checkAPIResponse(bts, nil)
}
// List all service integration endpoints for a given project.
func (h *ServiceIntegrationEndpointsHandler) List(ctx context.Context, project string) ([]*ServiceIntegrationEndpoint, error) {
path := buildPath("project", project, "integration_endpoint")
bts, err := h.client.doGetRequest(ctx, path, nil)
if err != nil {
return nil, err
}
var r ServiceIntegrationEndpointListResponse
errR := checkAPIResponse(bts, &r)
return r.ServiceIntegrationEndpoints, errR
}