|
| 1 | +// Copyright 2023 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 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + dataFederationBasePath = "api/atlas/v1.0/groups/%s/dataFederation" |
| 25 | +) |
| 26 | + |
| 27 | +// DataFederationService is an interface for interfacing with the Data Federation endpoints of the MongoDB Atlas API. |
| 28 | +// |
| 29 | +// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Data-Federation |
| 30 | +type DataFederationService interface { |
| 31 | + List(context.Context, string) ([]*DataFederationInstance, *Response, error) |
| 32 | + Get(context.Context, string, string) (*DataFederationInstance, *Response, error) |
| 33 | + Create(context.Context, string, *DataFederationInstance) (*DataFederationInstance, *Response, error) |
| 34 | + Update(context.Context, string, string, *DataFederationInstance, *DataFederationUpdateOptions) (*DataFederationInstance, *Response, error) |
| 35 | + Delete(context.Context, string, string) (*Response, error) |
| 36 | +} |
| 37 | + |
| 38 | +// DataFederationServiceOp handles communication with the DataFederationService related methods of the |
| 39 | +// MongoDB Atlas API. |
| 40 | +type DataFederationServiceOp service |
| 41 | + |
| 42 | +var _ DataFederationService = &DataFederationServiceOp{} |
| 43 | + |
| 44 | +// DataFederationInstance is the data federation configuration. |
| 45 | +type DataFederationInstance struct { |
| 46 | + CloudProviderConfig *CloudProviderConfig `json:"cloudProviderConfig,omitempty"` |
| 47 | + DataProcessRegion *DataProcessRegion `json:"dataProcessRegion,omitempty"` |
| 48 | + Storage *DataFederationStorage `json:"storage,omitempty"` |
| 49 | + Name string `json:"name,omitempty"` |
| 50 | +} |
| 51 | + |
| 52 | +// DataFederationStorage represents the storage configuration for a data lake. |
| 53 | +type DataFederationStorage struct { |
| 54 | + Databases []*DataFederationDatabase `json:"databases,omitempty"` |
| 55 | + Stores []*DataFederationStore `json:"stores,omitempty"` |
| 56 | +} |
| 57 | + |
| 58 | +// DataFederationDatabase represents queryable databases and collections for this data federation. |
| 59 | +type DataFederationDatabase struct { |
| 60 | + Collections []*DataFederationCollection `json:"collections,omitempty"` |
| 61 | + Views []*DataFederationDatabaseView `json:"views,omitempty"` |
| 62 | + MaxWildcardCollections int32 `json:"maxWildcardCollections,omitempty"` |
| 63 | + Name string `json:"name,omitempty"` |
| 64 | +} |
| 65 | + |
| 66 | +// DataFederationCollection represents queryable collections for this data federation. |
| 67 | +type DataFederationCollection struct { |
| 68 | + DataSources []*DataFederationDataSource `json:"dataSources,omitempty"` |
| 69 | + Name string `json:"name,omitempty"` |
| 70 | +} |
| 71 | + |
| 72 | +// DataFederationDataSource represents data stores that map to a collection for this data federation. |
| 73 | +type DataFederationDataSource struct { |
| 74 | + AllowInsecure *bool `json:"allowInsecure,omitempty"` |
| 75 | + Collection string `json:"collection,omitempty"` |
| 76 | + CollectionRegex string `json:"collectionRegex,omitempty"` |
| 77 | + Database string `json:"database,omitempty"` |
| 78 | + DatabaseRegex string `json:"databaseRegex,omitempty"` |
| 79 | + DefaultFormat string `json:"defaultFormat,omitempty"` |
| 80 | + Path string `json:"path,omitempty"` |
| 81 | + ProvenanceFieldName string `json:"provenanceFieldName,omitempty"` |
| 82 | + StoreName string `json:"storeName,omitempty"` |
| 83 | + Urls []*string `json:"urls,omitempty"` |
| 84 | +} |
| 85 | + |
| 86 | +// DataFederationDatabaseView represents any view under a DataFederationDatabase. |
| 87 | +type DataFederationDatabaseView struct { |
| 88 | + Name string `json:"name,omitempty"` |
| 89 | + Source string `json:"source,omitempty"` |
| 90 | + Pipeline string `json:"pipeline,omitempty"` |
| 91 | +} |
| 92 | + |
| 93 | +// DataFederationStore represents data stores for the data federation. |
| 94 | +type DataFederationStore struct { |
| 95 | + ReadPreference *ReadPreference `json:"readPreference,omitempty"` |
| 96 | + IncludeTags *bool `json:"includeTags,omitempty"` |
| 97 | + AdditionalStorageClasses []*string `json:"additionalStorageClasses,omitempty"` |
| 98 | + Name string `json:"name,omitempty"` |
| 99 | + Provider string `json:"provider,omitempty"` |
| 100 | + ClusterName string `json:"clusterName,omitempty"` |
| 101 | + Region string `json:"region,omitempty"` |
| 102 | + Bucket string `json:"bucket,omitempty"` |
| 103 | + Prefix string `json:"prefix,omitempty"` |
| 104 | + Delimiter string `json:"delimiter,omitempty"` |
| 105 | + ProjectID string `json:"projectId,omitempty"` |
| 106 | +} |
| 107 | + |
| 108 | +// ReadPreference describes how to route read requests to the cluster. |
| 109 | +type ReadPreference struct { |
| 110 | + MaxStalenessSeconds int32 `json:"maxStalenessSeconds,omitempty"` |
| 111 | + Mode string `json:"mode,omitempty"` |
| 112 | + TagSets []*TagSet `json:"tagSets,omitempty"` |
| 113 | +} |
| 114 | + |
| 115 | +// TagSet describes a tag specification document. |
| 116 | +type TagSet struct { |
| 117 | + Name string `json:"name,omitempty"` |
| 118 | + Value string `json:"value,omitempty"` |
| 119 | +} |
| 120 | + |
| 121 | +// DataFederationUpdateOptions specifies the optional parameters to Update method. |
| 122 | +type DataFederationUpdateOptions struct { |
| 123 | + // Flag that indicates whether this request should check if the requesting IAM role can read from the S3 bucket. |
| 124 | + // AWS checks if the role can list the objects in the bucket before writing to it. |
| 125 | + // Some IAM roles only need write permissions. This flag allows you to skip that check. |
| 126 | + SkipRoleValidation bool `url:"skipRoleValidation"` |
| 127 | +} |
| 128 | + |
| 129 | +// List gets the details of all federated database instances in the specified project. |
| 130 | +// |
| 131 | +// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Data-Federation/operation/listFederatedDatabases |
| 132 | +func (s *DataFederationServiceOp) List(ctx context.Context, groupID string) ([]*DataFederationInstance, *Response, error) { |
| 133 | + if groupID == "" { |
| 134 | + return nil, nil, NewArgError("groupID", "must be set") |
| 135 | + } |
| 136 | + path := fmt.Sprintf(dataFederationBasePath, groupID) |
| 137 | + |
| 138 | + req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil) |
| 139 | + if err != nil { |
| 140 | + return nil, nil, err |
| 141 | + } |
| 142 | + |
| 143 | + var root []*DataFederationInstance |
| 144 | + resp, err := s.Client.Do(ctx, req, &root) |
| 145 | + if err != nil { |
| 146 | + return nil, resp, err |
| 147 | + } |
| 148 | + |
| 149 | + return root, resp, nil |
| 150 | +} |
| 151 | + |
| 152 | +// Get gets the details of one federated database instance within the specified project. |
| 153 | +// |
| 154 | +// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Data-Federation/operation/getFederatedDatabase |
| 155 | +func (s *DataFederationServiceOp) Get(ctx context.Context, groupID, name string) (*DataFederationInstance, *Response, error) { |
| 156 | + if groupID == "" { |
| 157 | + return nil, nil, NewArgError("groupID", "must be set") |
| 158 | + } |
| 159 | + if name == "" { |
| 160 | + return nil, nil, NewArgError("name", "must be set") |
| 161 | + } |
| 162 | + |
| 163 | + basePath := fmt.Sprintf(dataFederationBasePath, groupID) |
| 164 | + path := fmt.Sprintf("%s/%s", basePath, name) |
| 165 | + |
| 166 | + req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil) |
| 167 | + if err != nil { |
| 168 | + return nil, nil, err |
| 169 | + } |
| 170 | + |
| 171 | + root := new(DataFederationInstance) |
| 172 | + resp, err := s.Client.Do(ctx, req, root) |
| 173 | + if err != nil { |
| 174 | + return nil, resp, err |
| 175 | + } |
| 176 | + |
| 177 | + return root, resp, err |
| 178 | +} |
| 179 | + |
| 180 | +// Create creates one federated database instance in the specified project. |
| 181 | +// |
| 182 | +// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Data-Federation/operation/createFederatedDatabase |
| 183 | +func (s *DataFederationServiceOp) Create(ctx context.Context, groupID string, createRequest *DataFederationInstance) (*DataFederationInstance, *Response, error) { |
| 184 | + if groupID == "" { |
| 185 | + return nil, nil, NewArgError("groupID", "must be set") |
| 186 | + } |
| 187 | + if createRequest == nil { |
| 188 | + return nil, nil, NewArgError("createRequest", "must be set") |
| 189 | + } |
| 190 | + |
| 191 | + path := fmt.Sprintf(dataFederationBasePath, groupID) |
| 192 | + req, err := s.Client.NewRequest(ctx, http.MethodPost, path, createRequest) |
| 193 | + if err != nil { |
| 194 | + return nil, nil, err |
| 195 | + } |
| 196 | + |
| 197 | + root := new(DataFederationInstance) |
| 198 | + resp, err := s.Client.Do(ctx, req, root) |
| 199 | + if err != nil { |
| 200 | + return nil, resp, err |
| 201 | + } |
| 202 | + |
| 203 | + return root, resp, err |
| 204 | +} |
| 205 | + |
| 206 | +// Update updates the details of one federated database instance in the specified project. |
| 207 | +// |
| 208 | +// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Data-Federation/operation/updateFederatedDatabase |
| 209 | +func (s *DataFederationServiceOp) Update(ctx context.Context, groupID, name string, updateRequest *DataFederationInstance, option *DataFederationUpdateOptions) (*DataFederationInstance, *Response, error) { |
| 210 | + if groupID == "" { |
| 211 | + return nil, nil, NewArgError("groupID", "must be set") |
| 212 | + } |
| 213 | + if name == "" { |
| 214 | + return nil, nil, NewArgError("name", "must be set") |
| 215 | + } |
| 216 | + if updateRequest == nil { |
| 217 | + return nil, nil, NewArgError("updateRequest", "cannot be nil") |
| 218 | + } |
| 219 | + |
| 220 | + basePath := fmt.Sprintf(dataFederationBasePath, groupID) |
| 221 | + path := fmt.Sprintf("%s/%s", basePath, name) |
| 222 | + |
| 223 | + if option == nil { |
| 224 | + option = &DataFederationUpdateOptions{ |
| 225 | + SkipRoleValidation: true, |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + // Add query params from DataFederationUpdateOptions |
| 230 | + pathWithOptions, err := setListOptions(path, option) |
| 231 | + if err != nil { |
| 232 | + return nil, nil, err |
| 233 | + } |
| 234 | + |
| 235 | + req, err := s.Client.NewRequest(ctx, http.MethodPatch, pathWithOptions, updateRequest) |
| 236 | + if err != nil { |
| 237 | + return nil, nil, err |
| 238 | + } |
| 239 | + |
| 240 | + root := new(DataFederationInstance) |
| 241 | + resp, err := s.Client.Do(ctx, req, root) |
| 242 | + if err != nil { |
| 243 | + return nil, resp, err |
| 244 | + } |
| 245 | + |
| 246 | + return root, resp, err |
| 247 | +} |
| 248 | + |
| 249 | +// Delete removes one federated database instance from the specified project. |
| 250 | +// |
| 251 | +// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Data-Federation/operation/deleteFederatedDatabase |
| 252 | +func (s *DataFederationServiceOp) Delete(ctx context.Context, groupID, name string) (*Response, error) { |
| 253 | + if groupID == "" { |
| 254 | + return nil, NewArgError("groupId", "must be set") |
| 255 | + } |
| 256 | + if name == "" { |
| 257 | + return nil, NewArgError("name", "must be set") |
| 258 | + } |
| 259 | + |
| 260 | + basePath := fmt.Sprintf(dataFederationBasePath, groupID) |
| 261 | + path := fmt.Sprintf("%s/%s", basePath, name) |
| 262 | + |
| 263 | + req, err := s.Client.NewRequest(ctx, http.MethodDelete, path, nil) |
| 264 | + if err != nil { |
| 265 | + return nil, err |
| 266 | + } |
| 267 | + |
| 268 | + resp, err := s.Client.Do(ctx, req, nil) |
| 269 | + |
| 270 | + return resp, err |
| 271 | +} |
0 commit comments