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
15package mongodbatlas
16
17import (
18	"context"
19	"fmt"
20	"net/http"
21)
22
23const (
24	backupCheckpoints = "groups/%s/clusters/%s/backupCheckpoints"
25)
26
27// CheckpointsService is an interface for interfacing with the Checkpoint
28// endpoints of the MongoDB Atlas API.
29type CheckpointsService interface {
30	List(context.Context, string, string, *ListOptions) (*Checkpoints, *Response, error)
31	Get(context.Context, string, string, string) (*Checkpoint, *Response, error)
32}
33
34// CheckpointsServiceOp handles communication with the checkpoint related methods of the
35// MongoDB Atlas API
36type CheckpointsServiceOp service
37
38var _ CheckpointsService = &CheckpointsServiceOp{}
39
40// Checkpoint represents MongoDB Checkpoint
41type Checkpoint struct {
42	ClusterID  string  `json:"clusterId"`
43	Completed  string  `json:"completed,omitempty"`
44	GroupID    string  `json:"groupId"`
45	ID         string  `json:"id,omitempty"`    // Unique identifier of the checkpoint.
46	Links      []*Link `json:"links,omitempty"` // One or more links to sub-resources and/or related resources.
47	Parts      []*Part `json:"parts,omitempty"`
48	Restorable bool    `json:"restorable"`
49	Started    string  `json:"started"`
50	Timestamp  string  `json:"timestamp"`
51}
52
53// CheckpointPart represents the individual parts that comprise the complete checkpoint.
54type CheckpointPart struct {
55	ShardName       string            `json:"shardName"`
56	TokenDiscovered bool              `json:"tokenDiscovered"`
57	TokenTimestamp  SnapshotTimestamp `json:"tokenTimestamp"`
58}
59
60// Checkpoints represents all the backup checkpoints related to a cluster.
61type Checkpoints struct {
62	Results    []*Checkpoint `json:"results,omitempty"`    // Includes one Checkpoint object for each item detailed in the results array section.
63	Links      []*Link       `json:"links,omitempty"`      // One or more links to sub-resources and/or related resources.
64	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.
65}
66
67// List all checkpoints for the specified sharded cluster.
68// See more: https://docs.atlas.mongodb.com/reference/api/checkpoints-get-all/
69func (s CheckpointsServiceOp) List(ctx context.Context, groupID, clusterName string, listOptions *ListOptions) (*Checkpoints, *Response, error) {
70	if groupID == "" {
71		return nil, nil, NewArgError("groupId", "must be set")
72	}
73	if clusterName == "" {
74		return nil, nil, NewArgError("clusterName", "must be set")
75	}
76
77	basePath := fmt.Sprintf(backupCheckpoints, groupID, clusterName)
78	path, err := setListOptions(basePath, listOptions)
79	if err != nil {
80		return nil, nil, err
81	}
82
83	req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
84	if err != nil {
85		return nil, nil, err
86	}
87
88	root := new(Checkpoints)
89	resp, err := s.Client.Do(ctx, req, root)
90
91	return root, resp, err
92}
93
94// Get one checkpoint for the specified sharded cluster.
95// See more: https://docs.atlas.mongodb.com/reference/api/checkpoints-get-one/
96func (s CheckpointsServiceOp) Get(ctx context.Context, groupID, clusterName, checkpointID string) (*Checkpoint, *Response, error) {
97	if groupID == "" {
98		return nil, nil, NewArgError("groupId", "must be set")
99	}
100	if clusterName == "" {
101		return nil, nil, NewArgError("clusterName", "must be set")
102	}
103	if checkpointID == "" {
104		return nil, nil, NewArgError("checkpointID", "must be set")
105	}
106
107	basePath := fmt.Sprintf(backupCheckpoints, groupID, clusterName)
108	path := fmt.Sprintf("%s/%s", basePath, checkpointID)
109	req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
110
111	if err != nil {
112		return nil, nil, err
113	}
114
115	root := new(Checkpoint)
116	resp, err := s.Client.Do(ctx, req, root)
117
118	return root, resp, err
119}
120