-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
573 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ func TestBuilder(t *testing.T) { | |
test.CmdValidator( | ||
t, | ||
Builder(), | ||
1, | ||
3, | ||
[]string{}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ func TestBuilder(t *testing.T) { | |
test.CmdValidator( | ||
t, | ||
Builder(), | ||
1, | ||
3, | ||
[]string{}, | ||
) | ||
} |
121 changes: 121 additions & 0 deletions
121
internal/cli/atlas/backup/compliancepolicy/policies/scheduled/create.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2023 MongoDB Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package scheduled | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/mongodb/mongodb-atlas-cli/internal/cli" | ||
"github.com/mongodb/mongodb-atlas-cli/internal/config" | ||
"github.com/mongodb/mongodb-atlas-cli/internal/flag" | ||
store "github.com/mongodb/mongodb-atlas-cli/internal/store/atlas" | ||
"github.com/mongodb/mongodb-atlas-cli/internal/usage" | ||
"github.com/spf13/cobra" | ||
atlasv2 "go.mongodb.org/atlas-sdk/v20231115002/admin" | ||
) | ||
|
||
type CreateOpts struct { | ||
cli.GlobalOpts | ||
cli.WatchOpts | ||
store store.CompliancePolicyScheduledPolicyCreator | ||
policy *atlasv2.DataProtectionSettings20231001 | ||
frequencyType string | ||
frequencyInterval int | ||
retentionUnit string | ||
retentionValue int | ||
} | ||
|
||
const ( | ||
active = "ACTIVE" | ||
) | ||
|
||
const updateTemplate = `Your backup compliance policy is being updated | ||
` | ||
const updateWatchTemplate = `Your backup compliance policy has been updated | ||
` | ||
|
||
func (opts *CreateOpts) initStore(ctx context.Context) func() error { | ||
return func() (err error) { | ||
opts.store, err = store.New(store.AuthenticatedPreset(config.Default()), store.WithContext(ctx)) | ||
return | ||
} | ||
} | ||
|
||
func (opts *CreateOpts) watcher() (bool, error) { | ||
res, err := opts.store.DescribeCompliancePolicy(opts.ConfigProjectID()) | ||
if err != nil { | ||
return false, err | ||
} | ||
opts.policy = res | ||
return res.GetState() == active, nil | ||
} | ||
|
||
func (opts *CreateOpts) Run() (err error) { | ||
policyItem := &atlasv2.BackupComplianceScheduledPolicyItem{ | ||
FrequencyType: opts.frequencyType, | ||
FrequencyInterval: opts.frequencyInterval, | ||
RetentionUnit: opts.retentionUnit, | ||
RetentionValue: opts.retentionValue, | ||
} | ||
|
||
if opts.policy, err = opts.store.CreateScheduledPolicy(opts.ProjectID, policyItem); err != nil { | ||
return err | ||
} | ||
|
||
if opts.EnableWatch { | ||
if errW := opts.Watch(opts.watcher); errW != nil { | ||
return fmt.Errorf("received an error while watching for completion: %w", errW) | ||
} | ||
opts.Template = updateWatchTemplate | ||
} | ||
return opts.Print(opts.policy) | ||
} | ||
|
||
func CreateBuilder() *cobra.Command { | ||
opts := &CreateOpts{} | ||
cmd := &cobra.Command{ | ||
Use: "create", | ||
Short: "Create a scheduled policy item for the backup compliance policy for your project.", | ||
Example: ` # Create a backup compliance schedule policy with a weekly frequency, where the snapshot occurs on Monday and has a retention of two months: | ||
atlas backups compliancepolicy policies scheduled create --frequencyType weekly --frequencyInterval 1 --retentionUnit months --retentionValue 2`, | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
return opts.PreRunE( | ||
opts.ValidateProjectID, | ||
opts.initStore(cmd.Context()), | ||
opts.InitOutput(cmd.OutOrStdout(), updateTemplate), | ||
) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return opts.Run() | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVar(&opts.frequencyType, flag.FrequencyType, "", usage.FrequencyType) | ||
cmd.Flags().IntVar(&opts.frequencyInterval, flag.FrequencyInterval, 0, usage.FrequencyInterval) | ||
cmd.Flags().StringVar(&opts.retentionUnit, flag.RetentionUnit, "", usage.RetentionUnit) | ||
cmd.Flags().IntVar(&opts.retentionValue, flag.RetentionValue, 0, usage.RetentionValue) | ||
_ = cmd.MarkFlagRequired(flag.FrequencyType) | ||
_ = cmd.MarkFlagRequired(flag.FrequencyInterval) | ||
_ = cmd.MarkFlagRequired(flag.RetentionUnit) | ||
_ = cmd.MarkFlagRequired(flag.RetentionValue) | ||
|
||
cmd.Flags().BoolVarP(&opts.EnableWatch, flag.EnableWatch, flag.EnableWatchShort, false, usage.EnableWatchDefault) | ||
cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID) | ||
cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut) | ||
_ = cmd.RegisterFlagCompletionFunc(flag.Output, opts.AutoCompleteOutputFlag()) | ||
|
||
return cmd | ||
} |
65 changes: 65 additions & 0 deletions
65
internal/cli/atlas/backup/compliancepolicy/policies/scheduled/create_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2023 MongoDB Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package scheduled | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/mongodb/mongodb-atlas-cli/internal/flag" | ||
"github.com/mongodb/mongodb-atlas-cli/internal/mocks/atlas" | ||
"github.com/mongodb/mongodb-atlas-cli/internal/test" | ||
atlasv2 "go.mongodb.org/atlas-sdk/v20231115002/admin" | ||
) | ||
|
||
func TestCreateOpts_Run(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
mockStore := atlas.NewMockCompliancePolicyScheduledPolicyCreator(ctrl) | ||
|
||
createOpts := &CreateOpts{ | ||
store: mockStore, | ||
frequencyType: "weekly", | ||
frequencyInterval: 1, | ||
retentionUnit: "days", | ||
retentionValue: 30, | ||
} | ||
|
||
policyItem := &atlasv2.BackupComplianceScheduledPolicyItem{ | ||
FrequencyType: createOpts.frequencyType, | ||
FrequencyInterval: createOpts.frequencyInterval, | ||
RetentionUnit: createOpts.retentionUnit, | ||
RetentionValue: createOpts.retentionValue, | ||
} | ||
|
||
expected := &atlasv2.DataProtectionSettings20231001{} | ||
|
||
mockStore. | ||
EXPECT(). | ||
CreateScheduledPolicy("", policyItem).Return(expected, nil). | ||
Times(1) | ||
|
||
if err := createOpts.Run(); err != nil { | ||
t.Fatalf("Run() unexpected error: %v", err) | ||
} | ||
} | ||
|
||
func TestCreateBuilder(t *testing.T) { | ||
test.CmdValidator( | ||
t, | ||
CreateBuilder(), | ||
0, | ||
[]string{flag.FrequencyType, flag.FrequencyInterval, flag.RetentionUnit, flag.RetentionValue, flag.EnableWatch, flag.ProjectID, flag.Output}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ func TestBuilder(t *testing.T) { | |
test.CmdValidator( | ||
t, | ||
Builder(), | ||
1, | ||
2, | ||
[]string{}, | ||
) | ||
} |
116 changes: 116 additions & 0 deletions
116
internal/cli/atlas/backup/compliancepolicy/policies/scheduled/update.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright 2023 MongoDB Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package scheduled | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/mongodb/mongodb-atlas-cli/internal/cli" | ||
"github.com/mongodb/mongodb-atlas-cli/internal/config" | ||
"github.com/mongodb/mongodb-atlas-cli/internal/flag" | ||
store "github.com/mongodb/mongodb-atlas-cli/internal/store/atlas" | ||
"github.com/mongodb/mongodb-atlas-cli/internal/usage" | ||
"github.com/spf13/cobra" | ||
atlasv2 "go.mongodb.org/atlas-sdk/v20231115002/admin" | ||
) | ||
|
||
type UpdateOpts struct { | ||
cli.GlobalOpts | ||
cli.WatchOpts | ||
store store.CompliancePolicyScheduledPolicyUpdater | ||
policy *atlasv2.DataProtectionSettings20231001 | ||
scheduledPolicyID string | ||
frequencyType string | ||
frequencyInterval int | ||
retentionUnit string | ||
retentionValue int | ||
} | ||
|
||
func (opts *UpdateOpts) initStore(ctx context.Context) func() error { | ||
return func() (err error) { | ||
opts.store, err = store.New(store.AuthenticatedPreset(config.Default()), store.WithContext(ctx)) | ||
return | ||
} | ||
} | ||
|
||
func (opts *UpdateOpts) watcher() (bool, error) { | ||
res, err := opts.store.DescribeCompliancePolicy(opts.ConfigProjectID()) | ||
if err != nil { | ||
return false, err | ||
} | ||
opts.policy = res | ||
return res.GetState() == active, nil | ||
} | ||
|
||
func (opts *UpdateOpts) Run() (err error) { | ||
policyItem := &atlasv2.BackupComplianceScheduledPolicyItem{ | ||
FrequencyType: opts.frequencyType, | ||
FrequencyInterval: opts.frequencyInterval, | ||
RetentionUnit: opts.retentionUnit, | ||
RetentionValue: opts.retentionValue, | ||
Id: &opts.scheduledPolicyID, | ||
} | ||
|
||
if opts.policy, err = opts.store.UpdateScheduledPolicy(opts.ProjectID, policyItem); err != nil { | ||
return err | ||
} | ||
|
||
if opts.EnableWatch { | ||
if errW := opts.Watch(opts.watcher); errW != nil { | ||
return fmt.Errorf("received an error while watching for completion: %w", errW) | ||
} | ||
opts.Template = updateWatchTemplate | ||
} | ||
return opts.Print(opts.policy) | ||
} | ||
|
||
func UpdateBuilder() *cobra.Command { | ||
opts := &UpdateOpts{} | ||
cmd := &cobra.Command{ | ||
Use: "update", | ||
Short: "Update a scheduled policy item for the backup compliance policy for your project.", | ||
Example: ` # Update a backup compliance schedule policy with a weekly frequency, where the snapshot occurs on Monday and has a retention of two months: | ||
atlas backups compliancepolicy policies scheduled update --scheduledPolicyId 6567f8ad00e6a55f9e448087 --frequencyType weekly --frequencyInterval 1 --retentionUnit months --retentionValue 2`, | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
return opts.PreRunE( | ||
opts.ValidateProjectID, | ||
opts.initStore(cmd.Context()), | ||
opts.InitOutput(cmd.OutOrStdout(), updateTemplate), | ||
) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return opts.Run() | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVar(&opts.scheduledPolicyID, flag.ScheduledPolicyID, "", usage.ScheduledPolicyID) | ||
cmd.Flags().StringVar(&opts.frequencyType, flag.FrequencyType, "", usage.FrequencyType) | ||
cmd.Flags().IntVar(&opts.frequencyInterval, flag.FrequencyInterval, 0, usage.FrequencyInterval) | ||
cmd.Flags().StringVar(&opts.retentionUnit, flag.RetentionUnit, "", usage.RetentionUnit) | ||
cmd.Flags().IntVar(&opts.retentionValue, flag.RetentionValue, 0, usage.RetentionValue) | ||
_ = cmd.MarkFlagRequired(flag.ScheduledPolicyID) | ||
_ = cmd.MarkFlagRequired(flag.FrequencyType) | ||
_ = cmd.MarkFlagRequired(flag.FrequencyInterval) | ||
_ = cmd.MarkFlagRequired(flag.RetentionUnit) | ||
_ = cmd.MarkFlagRequired(flag.RetentionValue) | ||
|
||
cmd.Flags().BoolVarP(&opts.EnableWatch, flag.EnableWatch, flag.EnableWatchShort, false, usage.EnableWatchDefault) | ||
cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID) | ||
cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut) | ||
_ = cmd.RegisterFlagCompletionFunc(flag.Output, opts.AutoCompleteOutputFlag()) | ||
|
||
return cmd | ||
} |
Oops, something went wrong.