Skip to content

Commit

Permalink
migrate more structs
Browse files Browse the repository at this point in the history
  • Loading branch information
husniMDB committed Jun 20, 2024
1 parent 5b0c153 commit 0563efb
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 44 deletions.
41 changes: 33 additions & 8 deletions opsmngr/checkpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,45 @@ import (
"context"
"fmt"
"net/http"

atlas "go.mongodb.org/atlas/mongodbatlas"
)

const (
checkpoints = "api/public/v1.0/groups/%s/clusters/%s/checkpoints"
)

// Checkpoint represents MongoDB Checkpoint.
type Checkpoint struct {
ClusterID string `json:"clusterId"`
Completed string `json:"completed,omitempty"`
GroupID string `json:"groupId"`
ID string `json:"id,omitempty"` // Unique identifier of the checkpoint.
Links []*Link `json:"links,omitempty"` // One or more links to sub-resources and/or related resources.
Parts []*Part `json:"parts,omitempty"`
Restorable bool `json:"restorable"`
Started string `json:"started"`
Timestamp string `json:"timestamp"`
}

// CheckpointPart represents the individual parts that comprise the complete checkpoint.
type CheckpointPart struct {
ShardName string `json:"shardName"`
TokenDiscovered bool `json:"tokenDiscovered"`
TokenTimestamp SnapshotTimestamp `json:"tokenTimestamp"`
}

// Checkpoints represents all the backup checkpoints related to a cluster.
type Checkpoints struct {
Results []*Checkpoint `json:"results,omitempty"` // Includes one Checkpoint object for each item detailed in the results array section.
Links []*Link `json:"links,omitempty"` // One or more links to sub-resources and/or related resources.
TotalCount int `json:"totalCount,omitempty"` // Count of the total number of items in the result set. It may be greater than the number of objects in the results array if the entire result set is paginated.
}

// CheckpointsService provides access to the backup related functions in the Ops Manager API.
//
// See more: https://docs.opsmanager.mongodb.com/current/reference/api/checkpoints/
type CheckpointsService interface {
List(context.Context, string, string, *ListOptions) (*atlas.Checkpoints, *Response, error)
Get(context.Context, string, string, string) (*atlas.Checkpoint, *Response, error)
List(context.Context, string, string, *ListOptions) (*Checkpoints, *Response, error)
Get(context.Context, string, string, string) (*Checkpoint, *Response, error)
}

// CheckpointsServiceOp provides an implementation of the CheckpointsService interface.
Expand All @@ -42,7 +67,7 @@ var _ CheckpointsService = &CheckpointsServiceOp{}
// List lists checkpoints.
//
// See https://docs.opsmanager.mongodb.com/current/reference/api/checkpoints/#get-all-checkpoints
func (s *CheckpointsServiceOp) List(ctx context.Context, groupID, clusterName string, listOptions *ListOptions) (*atlas.Checkpoints, *Response, error) {
func (s *CheckpointsServiceOp) List(ctx context.Context, groupID, clusterName string, listOptions *ListOptions) (*Checkpoints, *Response, error) {
if groupID == "" {
return nil, nil, NewArgError("groupId", "must be set")
}
Expand All @@ -61,7 +86,7 @@ func (s *CheckpointsServiceOp) List(ctx context.Context, groupID, clusterName st
return nil, nil, err
}

root := new(atlas.Checkpoints)
root := new(Checkpoints)
resp, err := s.Client.Do(ctx, req, root)

return root, resp, err
Expand All @@ -70,7 +95,7 @@ func (s *CheckpointsServiceOp) List(ctx context.Context, groupID, clusterName st
// Get gets a checkpoint.
//
// See https://docs.opsmanager.mongodb.com/current/reference/api/checkpoints/#get-one-checkpoint
func (s *CheckpointsServiceOp) Get(ctx context.Context, groupID, clusterID, checkpointID string) (*atlas.Checkpoint, *Response, error) {
func (s *CheckpointsServiceOp) Get(ctx context.Context, groupID, clusterID, checkpointID string) (*Checkpoint, *Response, error) {
if groupID == "" {
return nil, nil, NewArgError("groupId", "must be set")
}
Expand All @@ -89,7 +114,7 @@ func (s *CheckpointsServiceOp) Get(ctx context.Context, groupID, clusterID, chec
return nil, nil, err
}

root := new(atlas.Checkpoint)
root := new(Checkpoint)
resp, err := s.Client.Do(ctx, req, root)

return root, resp, err
Expand Down
57 changes: 28 additions & 29 deletions opsmngr/checkpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"testing"

"github.com/go-test/deep"
atlas "go.mongodb.org/atlas/mongodbatlas"
)

const clusterID = "6b8cd61180eef547110159d9" //nolint:gosec // not a credential
Expand Down Expand Up @@ -145,48 +144,48 @@ func TestCheckpoints_List(t *testing.T) {
t.Fatalf("Checkpoints.List returned error: %v", err)
}

expected := &atlas.Checkpoints{
Results: []*atlas.Checkpoint{
expected := &Checkpoints{
Results: []*Checkpoint{
{
ClusterID: clusterID,
Completed: "2018-02-08T23:20:25Z",
GroupID: projectID,
ID: "5a7cdb3980eef53de5bffdcf",
Links: []*atlas.Link{
Links: []*Link{
{
Rel: "self",
Href: "https://cloud.mongodb.com/api/public/v1.0/groups/5c8100bcf2a30b12ff88258f/clusters/Cluster0/checkpoints",
},
},
Parts: []*atlas.Part{
Parts: []*Part{
{
ReplicaSetName: "Cluster0-shard-1",
TypeName: "REPLICA_SET",
CheckpointPart: atlas.CheckpointPart{
CheckpointPart: CheckpointPart{
ShardName: "Cluster0-shard-1",
TokenDiscovered: true,
TokenTimestamp: atlas.SnapshotTimestamp{
TokenTimestamp: SnapshotTimestamp{
Date: "2018-02-08T23:20:25Z",
Increment: 1,
}},
},
{
ReplicaSetName: "Cluster0-shard-0",
TypeName: "REPLICA_SET",
CheckpointPart: atlas.CheckpointPart{
CheckpointPart: CheckpointPart{
ShardName: "Cluster0-shard-0",
TokenDiscovered: true,
TokenTimestamp: atlas.SnapshotTimestamp{
TokenTimestamp: SnapshotTimestamp{
Date: "2018-02-08T23:20:25Z",
Increment: 1,
}},
},
{
ReplicaSetName: "Cluster0-config-0",
TypeName: "CONFIG_SERVER_REPLICA_SET",
CheckpointPart: atlas.CheckpointPart{
CheckpointPart: CheckpointPart{
TokenDiscovered: true,
TokenTimestamp: atlas.SnapshotTimestamp{
TokenTimestamp: SnapshotTimestamp{
Date: "2018-02-08T23:20:25Z",
Increment: 2,
}},
Expand All @@ -201,41 +200,41 @@ func TestCheckpoints_List(t *testing.T) {
Completed: "2018-02-09T14:50:33Z",
GroupID: projectID,
ID: "5a7db53987d9d64fe298ff46",
Links: []*atlas.Link{
Links: []*Link{
{
Rel: "self",
Href: "https://cloud.mongodb.com/api/public/v1.0/groups/5c8100bcf2a30b12ff88258f/clusters/Cluster0/checkpoints?pretty=true",
},
},
Parts: []*atlas.Part{
Parts: []*Part{
{
ReplicaSetName: "Cluster0-shard-1",
TypeName: "REPLICA_SET",
CheckpointPart: atlas.CheckpointPart{
CheckpointPart: CheckpointPart{
ShardName: "Cluster0-shard-1",
TokenDiscovered: true,
TokenTimestamp: atlas.SnapshotTimestamp{
TokenTimestamp: SnapshotTimestamp{
Date: "2018-02-09T14:50:33Z",
Increment: 1,
}},
},
{
ReplicaSetName: "Cluster0-shard-0",
TypeName: "REPLICA_SET",
CheckpointPart: atlas.CheckpointPart{
CheckpointPart: CheckpointPart{
ShardName: "Cluster0-shard-0",
TokenDiscovered: true,
TokenTimestamp: atlas.SnapshotTimestamp{
TokenTimestamp: SnapshotTimestamp{
Date: "2018-02-09T14:50:33Z",
Increment: 2,
}},
},
{
ReplicaSetName: "Cluster0-config-0",
TypeName: "CONFIG_SERVER_REPLICA_SET",
CheckpointPart: atlas.CheckpointPart{
CheckpointPart: CheckpointPart{
TokenDiscovered: true,
TokenTimestamp: atlas.SnapshotTimestamp{
TokenTimestamp: SnapshotTimestamp{
Date: "2018-02-09T14:50:33Z",
Increment: 4,
}},
Expand All @@ -246,7 +245,7 @@ func TestCheckpoints_List(t *testing.T) {
Timestamp: "2018-02-09T14:50:18Z",
},
},
Links: []*atlas.Link{
Links: []*Link{
{
Href: "https://cloud.mongodb.com/api/public/v1.0/groups/5c8100bcf2a30b12ff88258f/clusters/Cluster0/checkpoints?pageNum=1&itemsPerPage=100",
Rel: "self",
Expand Down Expand Up @@ -322,26 +321,26 @@ func TestCheckpoints_Get(t *testing.T) {
t.Fatalf("Checkpoints.Get returned error: %v", err)
}

expected := &atlas.Checkpoint{
expected := &Checkpoint{
ClusterID: clusterID,
Completed: "2018-02-08T23:20:25Z",
GroupID: projectID,
ID: "5a7cdb3980eef53de5bffdcf",
Links: []*atlas.Link{
Links: []*Link{
{
Rel: "self",
Href: "https://cloud.mongodb.com/api/public/v1.0/groups/5c8100bcf2a30b12ff88258f/clusters/Cluster0/checkpoints",
},
},
Parts: []*atlas.Part{
Parts: []*Part{

{
ReplicaSetName: "Cluster0-shard-1",
TypeName: "REPLICA_SET",
CheckpointPart: atlas.CheckpointPart{
CheckpointPart: CheckpointPart{
ShardName: "Cluster0-shard-1",
TokenDiscovered: true,
TokenTimestamp: atlas.SnapshotTimestamp{
TokenTimestamp: SnapshotTimestamp{
Date: "2018-02-08T23:20:25Z",
Increment: 1,
},
Expand All @@ -350,20 +349,20 @@ func TestCheckpoints_Get(t *testing.T) {
{
ReplicaSetName: "Cluster0-shard-0",
TypeName: "REPLICA_SET",
CheckpointPart: atlas.CheckpointPart{
CheckpointPart: CheckpointPart{
ShardName: "Cluster0-shard-0",
TokenDiscovered: true,
TokenTimestamp: atlas.SnapshotTimestamp{
TokenTimestamp: SnapshotTimestamp{
Date: "2018-02-08T23:20:25Z",
Increment: 1,
}},
},
{
ReplicaSetName: "Cluster0-config-0",
TypeName: "CONFIG_SERVER_REPLICA_SET",
CheckpointPart: atlas.CheckpointPart{
CheckpointPart: CheckpointPart{
TokenDiscovered: true,
TokenTimestamp: atlas.SnapshotTimestamp{
TokenTimestamp: SnapshotTimestamp{
Date: "2018-02-08T23:20:25Z",
Increment: 2,
}},
Expand Down
5 changes: 5 additions & 0 deletions opsmngr/continuous_restore_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ type (
ContinuousJobRequest = atlas.ContinuousJobRequest
)

type SnapshotTimestamp struct {
Date string `json:"date"`
Increment int64 `json:"increment"`
}

var _ atlas.ContinuousRestoreJobsService = &ContinuousRestoreJobsServiceOp{}

// List lists all continuous backup jobs in Atlas
Expand Down
18 changes: 18 additions & 0 deletions opsmngr/continuous_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ type (
ContinuousSnapshot = atlas.ContinuousSnapshot
)

type Part struct {
ReplicaSetName string `json:"replicaSetName"`
TypeName string `json:"typeName"`
SnapshotPart
CheckpointPart
}

type SnapshotPart struct {
ClusterID string `json:"clusterId"`
CompressionSetting string `json:"compressionSetting"`
DataSizeBytes int64 `json:"dataSizeBytes"`
EncryptionEnabled bool `json:"encryptionEnabled"`
FileSizeBytes int64 `json:"fileSizeBytes"`
MasterKeyUUID string `json:"masterKeyUUID,omitempty"`

Check failure on line 53 in opsmngr/continuous_snapshots.go

View workflow job for this annotation

GitHub Actions / golangci-lint

json(camel): got 'masterKeyUUID' want 'masterKeyUuid' (tagliatelle)
MongodVersion string `json:"mongodVersion"`
StorageSizeBytes int64 `json:"storageSizeBytes"`
}

// List lists continuous snapshots for the given cluster
//
// See more: https://docs.opsmanager.mongodb.com/current/reference/api/snapshots/get-all-snapshots-for-one-cluster/
Expand Down
4 changes: 1 addition & 3 deletions opsmngr/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"fmt"
"io"
"net/http"

atlas "go.mongodb.org/atlas/mongodbatlas"
)

const (
Expand Down Expand Up @@ -56,7 +54,7 @@ type LogsService interface {
// LogsServiceOp handles communication with the Log Collection Jobs download method of the
// MongoDB Ops Manager API.
type LogsServiceOp struct {
Client atlas.GZipRequestDoer
Client GZipRequestDoer
}

var _ LogsService = &LogsServiceOp{}
Expand Down
21 changes: 17 additions & 4 deletions opsmngr/opsmngr.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ type (
APIKey = atlas.APIKey
APIKeyInput = atlas.APIKeyInput
AssignAPIKey = atlas.AssignAPIKey
Part = atlas.Part
Checkpoint = atlas.Checkpoint
Checkpoints = atlas.Checkpoints
SnapshotTimestamp = atlas.SnapshotTimestamp
IndexOptions = atlas.IndexOptions
CollationOptions = atlas.CollationOptions
Team = atlas.Team
Expand All @@ -78,6 +74,23 @@ type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}

// Doer basic interface of a client to be able to do a request.
type Doer interface {
Do(context.Context, *http.Request, interface{}) (*Response, error)
}

// Completer interface for clients with callback.
type Completer interface {
OnRequestCompleted(RequestCompletionCallback)
}

// GZipRequestDoer minimum interface for any service of the client that should handle gzip downloads.
type GZipRequestDoer interface {
Doer
Completer
NewGZipRequest(context.Context, string, string) (*http.Request, error)
}

// Client manages communication with Ops Manager API.
type Client struct {
client HTTPClient
Expand Down

0 comments on commit 0563efb

Please sign in to comment.