Skip to content

Commit 0fca1fa

Browse files
INTMDB-587: New Feature: Backup Compliance Policy Support (#453)
Co-authored-by: Andrea Angiolillo <[email protected]>
1 parent 1d51270 commit 0fca1fa

File tree

3 files changed

+403
-0
lines changed

3 files changed

+403
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
BackupCompliancePolicyBasePath = "api/atlas/v1.0/groups/%s/backupCompliancePolicy"
25+
)
26+
27+
// BackupCompliancePolicyService is an interface for interfacing with the Backup Compliance Policy
28+
// endpoints of the MongoDB Atlas API.
29+
//
30+
// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Cloud-Backups/operation/updateDataProtectionSettings
31+
type BackupCompliancePolicyService interface {
32+
Get(context.Context, string) (*BackupCompliancePolicy, *Response, error)
33+
Update(context.Context, string, *BackupCompliancePolicy) (*BackupCompliancePolicy, *Response, error)
34+
}
35+
36+
// CloudProviderSnapshotBackupPolicyServiceOp handles communication with the BackupCompliancePolicyService related methods of the
37+
// MongoDB Atlas API.
38+
type BackupCompliancePolicyServiceOp service
39+
40+
var _ BackupCompliancePolicyService = &BackupCompliancePolicyServiceOp{}
41+
42+
// BackupCompliancePolicy represents a backup compiance policy.
43+
type BackupCompliancePolicy struct {
44+
AuthorizedEmail string `json:"authorizedEmail,omitempty"`
45+
CopyProtectionEnabled *bool `json:"copyProtectionEnabled,omitempty"`
46+
EncryptionAtRestEnabled *bool `json:"encryptionAtRestEnabled,omitempty"`
47+
OnDemandPolicyItem PolicyItem `json:"onDemandPolicyItem,omitempty"`
48+
PitEnabled *bool `json:"pitEnabled,omitempty"`
49+
ProjectID string `json:"projectId,omitempty"`
50+
RestoreWindowDays *int64 `json:"restoreWindowDays,omitempty"`
51+
ScheduledPolicyItems []ScheduledPolicyItem `json:"scheduledPolicyItems,omitempty"`
52+
State string `json:"state,omitempty"`
53+
UpdatedDate string `json:"updatedDate,omitempty"`
54+
UpdatedUser string `json:"updatedUser,omitempty"`
55+
}
56+
57+
// PolicyItem represents a specifications for a scheduled backup policy and on demand policy.
58+
type ScheduledPolicyItem struct {
59+
ID string `json:"id,omitempty"` // Unique identifier of the backup policy item.
60+
FrequencyInterval int `json:"frequencyInterval,omitempty"` // Desired frequency of the new backup policy item specified by frequencyType.
61+
FrequencyType string `json:"frequencyType,omitempty"` // Frequency associated with the backup policy item. One of the following values: hourly, daily, weekly or monthly.
62+
RetentionUnit string `json:"retentionUnit,omitempty"` // Metric of duration of the backup policy item: days, weeks, or months.
63+
RetentionValue int `json:"retentionValue,omitempty"` // Duration for which the backup is kept. Associated with retentionUnit.
64+
}
65+
66+
// Get gets the current snapshot schedule and retention settings for the cluster with {CLUSTER-NAME}.
67+
// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Cloud-Backups/operation/getDataProtectionSettings
68+
func (s *BackupCompliancePolicyServiceOp) Get(ctx context.Context, groupID string) (*BackupCompliancePolicy, *Response, error) {
69+
if groupID == "" {
70+
return nil, nil, NewArgError("groupId", "must be set")
71+
}
72+
73+
path := fmt.Sprintf(BackupCompliancePolicyBasePath, groupID)
74+
75+
req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
76+
if err != nil {
77+
return nil, nil, err
78+
}
79+
80+
root := new(BackupCompliancePolicy)
81+
resp, err := s.Client.Do(ctx, req, root)
82+
if err != nil {
83+
return nil, resp, err
84+
}
85+
86+
return root, resp, err
87+
}
88+
89+
// Update updates the snapshot schedule or retention settings for the cluster with {CLUSTER-NAME}.
90+
// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Cloud-Backups/operation/updateDataProtectionSettings
91+
func (s *BackupCompliancePolicyServiceOp) Update(ctx context.Context, groupID string, createRequest *BackupCompliancePolicy) (*BackupCompliancePolicy, *Response, error) {
92+
if groupID == "" {
93+
return nil, nil, NewArgError("groupId", "must be set")
94+
}
95+
96+
if createRequest == nil {
97+
return nil, nil, NewArgError("createRequest", "cannot be nil")
98+
}
99+
100+
path := fmt.Sprintf(BackupCompliancePolicyBasePath, groupID)
101+
102+
req, err := s.Client.NewRequest(ctx, http.MethodPut, path, createRequest)
103+
if err != nil {
104+
return nil, nil, err
105+
}
106+
107+
root := new(BackupCompliancePolicy)
108+
resp, err := s.Client.Do(ctx, req, root)
109+
if err != nil {
110+
return nil, resp, err
111+
}
112+
113+
return root, resp, err
114+
}

0 commit comments

Comments
 (0)