From ba0c43a757e08ed61889b944a81b81247243c5b4 Mon Sep 17 00:00:00 2001 From: Gustavo Bazan Date: Fri, 3 Apr 2020 12:15:48 +0100 Subject: [PATCH 1/7] CLOUDP-60028: Implement index creation in atlas client --- mongodbatlas/indexes.go | 93 ++++++++++++++++++++++++++++++++++++ mongodbatlas/indexes_test.go | 61 +++++++++++++++++++++++ mongodbatlas/mongodbatlas.go | 2 + 3 files changed, 156 insertions(+) create mode 100644 mongodbatlas/indexes.go create mode 100644 mongodbatlas/indexes_test.go diff --git a/mongodbatlas/indexes.go b/mongodbatlas/indexes.go new file mode 100644 index 000000000..43316a1be --- /dev/null +++ b/mongodbatlas/indexes.go @@ -0,0 +1,93 @@ +package mongodbatlas + +import ( + "context" + "fmt" + "net/http" +) + +const indexesPath = "groups/%s/clusters/%s/indexes" + +// IndexesService is an interface for interfacing with the clusters indexes +// endpoints of the MongoDB Atlas API. +// See more: https://docs.atlas.mongodb.com/reference/api/indexes/ +type IndexesService interface { + Create(context.Context, string, string, *IndexConfiguration) (*Response, error) +} + +// IndexesServiceOp handles communication with the Cluster related methods +// of the MongoDB Atlas API +type IndexesServiceOp struct { + Client RequestDoer +} + +var _ IndexesService = &IndexesServiceOp{} + +// IndexConfiguration represents current value of the metric that triggered the alert. Only present for alerts of type HOST_METRIC. +type IndexConfiguration struct { + DB string `json:"db"` // The value of the metric. + Collection string `json:"collection"` // The units for the value. Depends on the type of metric. + Keys []map[string]string `json:"keys"` + Options *IndexOptions `json:"options,omitempty"` + Collation *CollationOptions `json:"collation,omitempty"` +} + +// IndexOptions, represents mdb index options +// See: https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/#options +type IndexOptions struct { + Background bool `json:"background,omitempty"` + PartialFilterExpression *map[string]interface{} `json:"partialFilterExpression,omitempty"` + StorageEngine *map[string]interface{} `json:"storageEngine,omitempty"` + Weights *map[string]int `json:"weights,omitempty"` + DefaultLanguage string `json:"default_language,omitempty"` + LanguageOverride string `json:"language_override,omitempty"` + TextIndexVersion int `json:"textIndexVersion,omitempty"` + TwodsphereIndexVersion int `json:"2dsphereIndexVersion,omitempty"` + Bits int `json:"bits,omitempty"` + Unique bool `json:"unique,omitempty"` + Sparse bool `json:"sparse,omitempty"` + GeoMin int `json:"min,omitempty"` + GeoMax int `json:"max,omitempty"` + BucketSize int `json:"bucketSize,omitempty"` + Name string `json:"name,omitempty"` + ExpireAfterSeconds int `json:"expireAfterSeconds,omitempty"` +} + +// CollationOptions represents options for collation indexes +// See: https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/#option-for-collation +type CollationOptions struct { + Locale string `json:"locale,omitempty"` + CaseLevel bool `json:"caseLevel,omitempty"` + CaseFirst string `json:"caseFirst,omitempty"` + Strength int `json:"strength,omitempty"` + NumericOrdering bool `json:"numericOrdering,omitempty"` + Alternate string `json:"alternate,omitempty"` + MaxVariable string `json:"maxVariable,omitempty"` + Normalization bool `json:"normalization,omitempty"` + Backwards bool `json:"backwards,omitempty"` +} + +// Create creates a request for a rolling index creation for the project associated to {GROUP-ID} and the {CLUSTER-NAME}. +// See more: https://docs.atlas.mongodb.com/reference/api/rolling-index-create-one/ +func (s *IndexesServiceOp) Create(ctx context.Context, groupID string, clusterName string, createReq *IndexConfiguration) (*Response, error) { + if groupID == "" { + return nil, NewArgError("groupID", "must be set") + } + if clusterName == "" { + return nil, NewArgError("clusterName", "must be set") + } + if createReq == nil { + return nil, NewArgError("createReq", "must be set") + } + + path := fmt.Sprintf(indexesPath, groupID, clusterName) + + req, err := s.Client.NewRequest(ctx, http.MethodPost, path, createReq) + if err != nil { + return nil, err + } + + resp, err := s.Client.Do(ctx, req, nil) + + return resp, err +} diff --git a/mongodbatlas/indexes_test.go b/mongodbatlas/indexes_test.go new file mode 100644 index 000000000..97005f012 --- /dev/null +++ b/mongodbatlas/indexes_test.go @@ -0,0 +1,61 @@ +package mongodbatlas + +import ( + "encoding/json" + "fmt" + "net/http" + "testing" + + "github.com/go-test/deep" +) + +func TestIndexesServiceOp_Create(t *testing.T) { + setup() + defer teardown() + + groupID := "1" + clusterName := "appData" + + createRequest := &IndexConfiguration{ + DB: "test", + Collection: "test", + Keys: []map[string]string{ + { + "name": "1", + }, + }, + Options: &IndexOptions{ + Unique: true, + }, + } + mux.HandleFunc(fmt.Sprintf("/groups/%s/clusters/%s/indexes", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodPost) + expected := map[string]interface{}{ + "db": "test", + "collection": "test", + "keys": []interface{}{map[string]interface{}{ + "name": "1", + }}, + "options": map[string]interface{}{ + "unique": true, + }, + } + + var v map[string]interface{} + err := json.NewDecoder(r.Body).Decode(&v) + if err != nil { + t.Fatalf("decode json: %v", err) + } + + if diff := deep.Equal(v, expected); diff != nil { + t.Errorf("Clusters.Update Request Body = %v", diff) + } + fmt.Fprint(w, "{}") + }) + + _, err := client.Indexes.Create(ctx, groupID, clusterName, createRequest) + + if err != nil { + t.Fatalf("Indexes.Create returned error: %v", err) + } +} diff --git a/mongodbatlas/mongodbatlas.go b/mongodbatlas/mongodbatlas.go index 53fc2d3bc..3c295237f 100644 --- a/mongodbatlas/mongodbatlas.go +++ b/mongodbatlas/mongodbatlas.go @@ -68,6 +68,7 @@ type Client struct { Processes ProcessesService ProcessMeasurements ProcessMeasurementsService ProcessDisks ProcessDisksService + Indexes IndexesService onRequestCompleted RequestCompletionCallback } @@ -189,6 +190,7 @@ func NewClient(httpClient *http.Client) *Client { c.Processes = &ProcessesServiceOp{Client: c} c.ProcessMeasurements = &ProcessMeasurementsServiceOp{Client: c} c.ProcessDisks = &ProcessDisksServiceOp{Client: c} + c.Indexes = &IndexesServiceOp{Client: c} return c } From fae0a3279fb4c6994bb558ccccb52275294bc43b Mon Sep 17 00:00:00 2001 From: Gustavo Bazan Date: Mon, 6 Apr 2020 10:20:19 +0100 Subject: [PATCH 2/7] Update indexes.go --- mongodbatlas/indexes.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mongodbatlas/indexes.go b/mongodbatlas/indexes.go index 43316a1be..f1b75bd52 100644 --- a/mongodbatlas/indexes.go +++ b/mongodbatlas/indexes.go @@ -6,7 +6,7 @@ import ( "net/http" ) -const indexesPath = "groups/%s/clusters/%s/indexes" +const indexesPath = "groups/%s/clusters/%s/index" // IndexesService is an interface for interfacing with the clusters indexes // endpoints of the MongoDB Atlas API. From 7cf1ec8f2efe654e121323b5c83d68bb64473fd3 Mon Sep 17 00:00:00 2001 From: Gustavo Bazan Date: Mon, 6 Apr 2020 10:22:21 +0100 Subject: [PATCH 3/7] Update indexes_test.go --- mongodbatlas/indexes_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mongodbatlas/indexes_test.go b/mongodbatlas/indexes_test.go index 97005f012..53da75e7d 100644 --- a/mongodbatlas/indexes_test.go +++ b/mongodbatlas/indexes_test.go @@ -28,7 +28,7 @@ func TestIndexesServiceOp_Create(t *testing.T) { Unique: true, }, } - mux.HandleFunc(fmt.Sprintf("/groups/%s/clusters/%s/indexes", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc(fmt.Sprintf("/groups/%s/clusters/%s/index", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodPost) expected := map[string]interface{}{ "db": "test", From 75e5346e446463b5cd4a46ea055b741c1df3a63a Mon Sep 17 00:00:00 2001 From: Gustavo Bazan Date: Mon, 6 Apr 2020 10:54:52 +0100 Subject: [PATCH 4/7] Update indexes.go --- mongodbatlas/indexes.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mongodbatlas/indexes.go b/mongodbatlas/indexes.go index f1b75bd52..ab5e7a36a 100644 --- a/mongodbatlas/indexes.go +++ b/mongodbatlas/indexes.go @@ -25,11 +25,11 @@ var _ IndexesService = &IndexesServiceOp{} // IndexConfiguration represents current value of the metric that triggered the alert. Only present for alerts of type HOST_METRIC. type IndexConfiguration struct { - DB string `json:"db"` // The value of the metric. - Collection string `json:"collection"` // The units for the value. Depends on the type of metric. - Keys []map[string]string `json:"keys"` - Options *IndexOptions `json:"options,omitempty"` - Collation *CollationOptions `json:"collation,omitempty"` + DB string `json:"db"` // DB the database of the index + Collection string `json:"collection"` // Collection the collection of the index + Keys []map[string]string `json:"keys"` // Keys array of keys to index and their type, sorting of keys is important for an index + Options *IndexOptions `json:"options,omitempty"` // Options MongoDB index options + Collation *CollationOptions `json:"collation,omitempty"`// Collation Mongo collation index options } // IndexOptions, represents mdb index options From 6533ec983664c853d665ba76d1f915423db178f7 Mon Sep 17 00:00:00 2001 From: Gustavo Bazan Date: Mon, 6 Apr 2020 10:57:38 +0100 Subject: [PATCH 5/7] Update indexes.go --- mongodbatlas/indexes.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mongodbatlas/indexes.go b/mongodbatlas/indexes.go index ab5e7a36a..549985352 100644 --- a/mongodbatlas/indexes.go +++ b/mongodbatlas/indexes.go @@ -25,11 +25,11 @@ var _ IndexesService = &IndexesServiceOp{} // IndexConfiguration represents current value of the metric that triggered the alert. Only present for alerts of type HOST_METRIC. type IndexConfiguration struct { - DB string `json:"db"` // DB the database of the index - Collection string `json:"collection"` // Collection the collection of the index - Keys []map[string]string `json:"keys"` // Keys array of keys to index and their type, sorting of keys is important for an index - Options *IndexOptions `json:"options,omitempty"` // Options MongoDB index options - Collation *CollationOptions `json:"collation,omitempty"`// Collation Mongo collation index options + DB string `json:"db"` // DB the database of the index + Collection string `json:"collection"` // Collection the collection of the index + Keys []map[string]string `json:"keys"` // Keys array of keys to index and their type, sorting of keys is important for an index + Options *IndexOptions `json:"options,omitempty"` // Options MongoDB index options + Collation *CollationOptions `json:"collation,omitempty"` // Collation Mongo collation index options } // IndexOptions, represents mdb index options From cf16fc03e4a078b2b772c0afc92ee265ad48a989 Mon Sep 17 00:00:00 2001 From: Gustavo Bazan Date: Wed, 8 Apr 2020 14:57:35 +0100 Subject: [PATCH 6/7] Update indexes.go --- mongodbatlas/indexes.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mongodbatlas/indexes.go b/mongodbatlas/indexes.go index 549985352..8c688027f 100644 --- a/mongodbatlas/indexes.go +++ b/mongodbatlas/indexes.go @@ -23,7 +23,7 @@ type IndexesServiceOp struct { var _ IndexesService = &IndexesServiceOp{} -// IndexConfiguration represents current value of the metric that triggered the alert. Only present for alerts of type HOST_METRIC. +// IndexConfiguration represents a new index requests for a given database and collection. type IndexConfiguration struct { DB string `json:"db"` // DB the database of the index Collection string `json:"collection"` // Collection the collection of the index From cf6fd244adc06bab6b6be959fb9974591358f44b Mon Sep 17 00:00:00 2001 From: Gustavo Bazan Date: Wed, 8 Apr 2020 16:33:41 +0100 Subject: [PATCH 7/7] Update mongodbatlas.go --- mongodbatlas/mongodbatlas.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mongodbatlas/mongodbatlas.go b/mongodbatlas/mongodbatlas.go index ca42d9e4b..24d0f9817 100644 --- a/mongodbatlas/mongodbatlas.go +++ b/mongodbatlas/mongodbatlas.go @@ -193,7 +193,7 @@ func NewClient(httpClient *http.Client) *Client { c.ProcessMeasurements = &ProcessMeasurementsServiceOp{Client: c} c.ProcessDisks = &ProcessDisksServiceOp{Client: c} c.ProcessDiskMeasurements = &ProcessDiskMeasurementsServiceOp{Client: c} - c.ProcessDatabases = &ProcessDatabasesServiceOp{Client: c} + c.ProcessDatabases = &ProcessDatabasesServiceOp{Client: c} c.Indexes = &IndexesServiceOp{Client: c} return c