|
| 1 | +package mongodbatlas |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | +) |
| 8 | + |
| 9 | +const indexesPath = "groups/%s/clusters/%s/index" |
| 10 | + |
| 11 | +// IndexesService is an interface for interfacing with the clusters indexes |
| 12 | +// endpoints of the MongoDB Atlas API. |
| 13 | +// See more: https://docs.atlas.mongodb.com/reference/api/indexes/ |
| 14 | +type IndexesService interface { |
| 15 | + Create(context.Context, string, string, *IndexConfiguration) (*Response, error) |
| 16 | +} |
| 17 | + |
| 18 | +// IndexesServiceOp handles communication with the Cluster related methods |
| 19 | +// of the MongoDB Atlas API |
| 20 | +type IndexesServiceOp struct { |
| 21 | + Client RequestDoer |
| 22 | +} |
| 23 | + |
| 24 | +var _ IndexesService = &IndexesServiceOp{} |
| 25 | + |
| 26 | +// IndexConfiguration represents a new index requests for a given database and collection. |
| 27 | +type IndexConfiguration struct { |
| 28 | + DB string `json:"db"` // DB the database of the index |
| 29 | + Collection string `json:"collection"` // Collection the collection of the index |
| 30 | + Keys []map[string]string `json:"keys"` // Keys array of keys to index and their type, sorting of keys is important for an index |
| 31 | + Options *IndexOptions `json:"options,omitempty"` // Options MongoDB index options |
| 32 | + Collation *CollationOptions `json:"collation,omitempty"` // Collation Mongo collation index options |
| 33 | +} |
| 34 | + |
| 35 | +// IndexOptions, represents mdb index options |
| 36 | +// See: https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/#options |
| 37 | +type IndexOptions struct { |
| 38 | + Background bool `json:"background,omitempty"` |
| 39 | + PartialFilterExpression *map[string]interface{} `json:"partialFilterExpression,omitempty"` |
| 40 | + StorageEngine *map[string]interface{} `json:"storageEngine,omitempty"` |
| 41 | + Weights *map[string]int `json:"weights,omitempty"` |
| 42 | + DefaultLanguage string `json:"default_language,omitempty"` |
| 43 | + LanguageOverride string `json:"language_override,omitempty"` |
| 44 | + TextIndexVersion int `json:"textIndexVersion,omitempty"` |
| 45 | + TwodsphereIndexVersion int `json:"2dsphereIndexVersion,omitempty"` |
| 46 | + Bits int `json:"bits,omitempty"` |
| 47 | + Unique bool `json:"unique,omitempty"` |
| 48 | + Sparse bool `json:"sparse,omitempty"` |
| 49 | + GeoMin int `json:"min,omitempty"` |
| 50 | + GeoMax int `json:"max,omitempty"` |
| 51 | + BucketSize int `json:"bucketSize,omitempty"` |
| 52 | + Name string `json:"name,omitempty"` |
| 53 | + ExpireAfterSeconds int `json:"expireAfterSeconds,omitempty"` |
| 54 | +} |
| 55 | + |
| 56 | +// CollationOptions represents options for collation indexes |
| 57 | +// See: https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/#option-for-collation |
| 58 | +type CollationOptions struct { |
| 59 | + Locale string `json:"locale,omitempty"` |
| 60 | + CaseLevel bool `json:"caseLevel,omitempty"` |
| 61 | + CaseFirst string `json:"caseFirst,omitempty"` |
| 62 | + Strength int `json:"strength,omitempty"` |
| 63 | + NumericOrdering bool `json:"numericOrdering,omitempty"` |
| 64 | + Alternate string `json:"alternate,omitempty"` |
| 65 | + MaxVariable string `json:"maxVariable,omitempty"` |
| 66 | + Normalization bool `json:"normalization,omitempty"` |
| 67 | + Backwards bool `json:"backwards,omitempty"` |
| 68 | +} |
| 69 | + |
| 70 | +// Create creates a request for a rolling index creation for the project associated to {GROUP-ID} and the {CLUSTER-NAME}. |
| 71 | +// See more: https://docs.atlas.mongodb.com/reference/api/rolling-index-create-one/ |
| 72 | +func (s *IndexesServiceOp) Create(ctx context.Context, groupID string, clusterName string, createReq *IndexConfiguration) (*Response, error) { |
| 73 | + if groupID == "" { |
| 74 | + return nil, NewArgError("groupID", "must be set") |
| 75 | + } |
| 76 | + if clusterName == "" { |
| 77 | + return nil, NewArgError("clusterName", "must be set") |
| 78 | + } |
| 79 | + if createReq == nil { |
| 80 | + return nil, NewArgError("createReq", "must be set") |
| 81 | + } |
| 82 | + |
| 83 | + path := fmt.Sprintf(indexesPath, groupID, clusterName) |
| 84 | + |
| 85 | + req, err := s.Client.NewRequest(ctx, http.MethodPost, path, createReq) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + resp, err := s.Client.Do(ctx, req, nil) |
| 91 | + |
| 92 | + return resp, err |
| 93 | +} |
0 commit comments