|
| 1 | +// Copyright 2021 MongoDB Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package mongodbatlas |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "net/http" |
| 21 | + "net/url" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + serverlessPrivateEndpointsPath = "api/atlas/v1.0/groups/%s/privateEndpoint/serverless/instance/%s/endpoint" |
| 26 | +) |
| 27 | + |
| 28 | +// ServerlessPrivateEndpointsService is an interface for interfacing with the Private Endpoints |
| 29 | +// of the MongoDB Atlas API. |
| 30 | +// |
| 31 | +// See more: See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Serverless-Private-Endpoints |
| 32 | +type ServerlessPrivateEndpointsService interface { |
| 33 | + List(context.Context, string, string, *ListOptions) ([]ServerlessPrivateEndpointConnection, *Response, error) |
| 34 | + Create(context.Context, string, string, *ServerlessPrivateEndpointConnection) (*ServerlessPrivateEndpointConnection, *Response, error) |
| 35 | + Get(context.Context, string, string, string) (*ServerlessPrivateEndpointConnection, *Response, error) |
| 36 | + Delete(context.Context, string, string, string) (*Response, error) |
| 37 | + Update(context.Context, string, string, string, *ServerlessPrivateEndpointConnection) (*ServerlessPrivateEndpointConnection, *Response, error) |
| 38 | +} |
| 39 | + |
| 40 | +// PrivateServerlessEndpointsServiceOp handles communication with the PrivateServerlessEndpoints related methods |
| 41 | +// of the MongoDB Atlas API. |
| 42 | +type ServerlessPrivateEndpointsServiceOp service |
| 43 | + |
| 44 | +var _ ServerlessPrivateEndpointsService = &ServerlessPrivateEndpointsServiceOp{} |
| 45 | + |
| 46 | +// PrivateEndpointServerlessConnection represents MongoDB Private Endpoint Connection. |
| 47 | +type ServerlessPrivateEndpointConnection struct { |
| 48 | + ID string `json:"_id,omitempty"` // Unique identifier of the Serverless PrivateLink Service. |
| 49 | + CloudProviderEndpointID string `json:"cloudProviderEndpointId,omitempty"` |
| 50 | + Comment string `json:"comment,omitempty"` |
| 51 | + EndpointServiceName string `json:"endpointServiceName,omitempty"` // Name of the PrivateLink endpoint service in AWS. Returns null while the endpoint service is being created. |
| 52 | + ErrorMessage string `json:"errorMessage,omitempty"` // Error message pertaining to the AWS Service Connect. Returns null if there are no errors. |
| 53 | + Status string `json:"status,omitempty"` // Status of the AWS Serverless PrivateLink connection: INITIATING, WAITING_FOR_USER, FAILED, DELETING, AVAILABLE. |
| 54 | + ProviderName string `json:"providerName,omitempty"` // Human-readable label that identifies the cloud provider. Values include AWS or AZURE. |
| 55 | + PrivateEndpointIPAddress string `json:"privateEndpointIpAddress,omitempty"` // IPv4 address of the private endpoint in your Azure VNet that someone added to this private endpoint service. |
| 56 | + PrivateLinkServiceResourceID string `json:"privateLinkServiceResourceId,omitempty"` // Root-relative path that identifies the Azure Private Link Service that MongoDB Cloud manages. MongoDB Cloud returns null while it creates the endpoint service. |
| 57 | +} |
| 58 | + |
| 59 | +// List retrieve details for all private Serverless endpoint services in one Atlas project. |
| 60 | +// |
| 61 | +// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#operation/returnAllPrivateEndpointsForOneServerlessInstance |
| 62 | +func (s *ServerlessPrivateEndpointsServiceOp) List(ctx context.Context, groupID, instanceName string, listOptions *ListOptions) ([]ServerlessPrivateEndpointConnection, *Response, error) { |
| 63 | + if groupID == "" { |
| 64 | + return nil, nil, NewArgError("groupID", "must be set") |
| 65 | + } |
| 66 | + if instanceName == "" { |
| 67 | + return nil, nil, NewArgError("instanceID", "must be set") |
| 68 | + } |
| 69 | + |
| 70 | + path := fmt.Sprintf(serverlessPrivateEndpointsPath, groupID, instanceName) // Add query params from listOptions |
| 71 | + path, err := setListOptions(path, listOptions) |
| 72 | + if err != nil { |
| 73 | + return nil, nil, err |
| 74 | + } |
| 75 | + |
| 76 | + req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil) |
| 77 | + if err != nil { |
| 78 | + return nil, nil, err |
| 79 | + } |
| 80 | + |
| 81 | + root := new([]ServerlessPrivateEndpointConnection) |
| 82 | + resp, err := s.Client.Do(ctx, req, root) |
| 83 | + if err != nil { |
| 84 | + return nil, resp, err |
| 85 | + } |
| 86 | + |
| 87 | + return *root, resp, nil |
| 88 | +} |
| 89 | + |
| 90 | +// Delete one private serverless endpoint service in an Atlas project. |
| 91 | +// |
| 92 | +// See more https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#operation/removeOnePrivateEndpointFromOneServerlessInstance |
| 93 | +func (s *ServerlessPrivateEndpointsServiceOp) Delete(ctx context.Context, groupID, instanceName, privateEndpointID string) (*Response, error) { |
| 94 | + if groupID == "" { |
| 95 | + return nil, NewArgError("groupID", "must be set") |
| 96 | + } |
| 97 | + if privateEndpointID == "" { |
| 98 | + return nil, NewArgError("PrivateEndpointID", "must be set") |
| 99 | + } |
| 100 | + if instanceName == "" { |
| 101 | + return nil, NewArgError("instanceName", "must be set") |
| 102 | + } |
| 103 | + |
| 104 | + basePath := fmt.Sprintf(serverlessPrivateEndpointsPath, groupID, instanceName) |
| 105 | + path := fmt.Sprintf("%s/%s", basePath, url.PathEscape(privateEndpointID)) |
| 106 | + |
| 107 | + req, err := s.Client.NewRequest(ctx, http.MethodDelete, path, nil) |
| 108 | + if err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + |
| 112 | + return s.Client.Do(ctx, req, nil) |
| 113 | +} |
| 114 | + |
| 115 | +// Create Adds one serverless private endpoint in an Atlas project. |
| 116 | +// |
| 117 | +// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#operation/createOnePrivateEndpointForOneServerlessInstance |
| 118 | +func (s *ServerlessPrivateEndpointsServiceOp) Create(ctx context.Context, groupID, instanceName string, createRequest *ServerlessPrivateEndpointConnection) (*ServerlessPrivateEndpointConnection, *Response, error) { |
| 119 | + if groupID == "" { |
| 120 | + return nil, nil, NewArgError("groupID", "must be set") |
| 121 | + } |
| 122 | + |
| 123 | + if instanceName == "" { |
| 124 | + return nil, nil, NewArgError("instanceName", "must be set") |
| 125 | + } |
| 126 | + if createRequest == nil { |
| 127 | + return nil, nil, NewArgError("createRequest", "cannot be nil") |
| 128 | + } |
| 129 | + |
| 130 | + path := fmt.Sprintf(serverlessPrivateEndpointsPath, groupID, instanceName) |
| 131 | + |
| 132 | + req, err := s.Client.NewRequest(ctx, http.MethodPost, path, createRequest) |
| 133 | + if err != nil { |
| 134 | + return nil, nil, err |
| 135 | + } |
| 136 | + |
| 137 | + root := new(ServerlessPrivateEndpointConnection) |
| 138 | + resp, err := s.Client.Do(ctx, req, root) |
| 139 | + if err != nil { |
| 140 | + return nil, resp, err |
| 141 | + } |
| 142 | + |
| 143 | + return root, resp, err |
| 144 | +} |
| 145 | + |
| 146 | +// Get retrieve details for one private serverless endpoint in an Atlas project. |
| 147 | +// |
| 148 | +// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#operation/returnOnePrivateEndpointForOneServerlessInstance |
| 149 | +func (s *ServerlessPrivateEndpointsServiceOp) Get(ctx context.Context, groupID, instanceName, privateEndpointID string) (*ServerlessPrivateEndpointConnection, *Response, error) { |
| 150 | + if groupID == "" { |
| 151 | + return nil, nil, NewArgError("groupID", "must be set") |
| 152 | + } |
| 153 | + |
| 154 | + if instanceName == "" { |
| 155 | + return nil, nil, NewArgError("instanceName", "must be set") |
| 156 | + } |
| 157 | + if privateEndpointID == "" { |
| 158 | + return nil, nil, NewArgError("privateEndpointID", "must be set") |
| 159 | + } |
| 160 | + |
| 161 | + basePath := fmt.Sprintf(serverlessPrivateEndpointsPath, groupID, instanceName) |
| 162 | + path := fmt.Sprintf("%s/%s", basePath, url.PathEscape(privateEndpointID)) |
| 163 | + |
| 164 | + req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil) |
| 165 | + if err != nil { |
| 166 | + return nil, nil, err |
| 167 | + } |
| 168 | + |
| 169 | + root := new(ServerlessPrivateEndpointConnection) |
| 170 | + resp, err := s.Client.Do(ctx, req, root) |
| 171 | + if err != nil { |
| 172 | + return nil, resp, err |
| 173 | + } |
| 174 | + |
| 175 | + return root, resp, err |
| 176 | +} |
| 177 | + |
| 178 | +// Update updates the private serverless endpoint setting for one Atlas project. |
| 179 | +// |
| 180 | +// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#operation/updateOnePrivateEndpointForOneServerlessInstance |
| 181 | +func (s *ServerlessPrivateEndpointsServiceOp) Update(ctx context.Context, groupID, instanceName, privateEndpointID string, updateRequest *ServerlessPrivateEndpointConnection) (*ServerlessPrivateEndpointConnection, *Response, error) { |
| 182 | + if groupID == "" { |
| 183 | + return nil, nil, NewArgError("groupID", "must be set") |
| 184 | + } |
| 185 | + if instanceName == "" { |
| 186 | + return nil, nil, NewArgError("instanceName", "must be set") |
| 187 | + } |
| 188 | + if privateEndpointID == "" { |
| 189 | + return nil, nil, NewArgError("privateEndpointID", "must be set") |
| 190 | + } |
| 191 | + |
| 192 | + basePath := fmt.Sprintf(serverlessPrivateEndpointsPath, groupID, instanceName) |
| 193 | + path := fmt.Sprintf("%s/%s", basePath, url.PathEscape(privateEndpointID)) |
| 194 | + req, err := s.Client.NewRequest(ctx, http.MethodPatch, path, updateRequest) |
| 195 | + if err != nil { |
| 196 | + return nil, nil, err |
| 197 | + } |
| 198 | + |
| 199 | + root := new(ServerlessPrivateEndpointConnection) |
| 200 | + resp, err := s.Client.Do(ctx, req, root) |
| 201 | + if err != nil { |
| 202 | + return nil, resp, err |
| 203 | + } |
| 204 | + |
| 205 | + return root, resp, err |
| 206 | +} |
0 commit comments