Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task: move the time parser to the convert package #2733

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/cli/atlas/accesslists/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/convert"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/time"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/spf13/cobra"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115007/admin"
Expand Down Expand Up @@ -79,7 +79,7 @@ func (opts *CreateOpts) newProjectIPAccessList() ([]*atlasv2.NetworkPermissionEn
Comment: &opts.comment,
}
if opts.deleteAfter != "" {
deleteAfterDate, err := time.ParseTimestamp(opts.deleteAfter)
deleteAfterDate, err := convert.ParseTimestamp(opts.deleteAfter)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions internal/cli/atlas/alerts/acknowledge.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/convert"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
store "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store/atlas"
customTime "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/time"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/spf13/cobra"
"go.mongodb.org/atlas-sdk/v20231115007/admin"
Expand Down Expand Up @@ -76,12 +76,12 @@ func (opts *AcknowledgeOpts) newAcknowledgeRequest() (*admin.AlertViewForNdsGrou
const years = 100
opts.until = time.Now().AddDate(years, 1, 1).Format(time.RFC3339)
}
time, err := customTime.ParseTimestamp(opts.until)
until, err := convert.ParseTimestamp(opts.until)
if err != nil {
return nil, err
}
return &admin.AlertViewForNdsGroup{
AcknowledgedUntil: &time,
AcknowledgedUntil: &until,
AcknowledgementComment: &opts.comment,
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/atlas/alerts/acknowledge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"github.com/golang/mock/gomock"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
mocks "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks/atlas"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/test"
"github.com/stretchr/testify/require"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115007/admin"
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/atlas/alerts/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
store "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store/atlas"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/spf13/cobra"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115007/admin"
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/atlas/alerts/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
mocks "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks/atlas"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/test"
"github.com/stretchr/testify/assert"
Expand Down
7 changes: 4 additions & 3 deletions internal/cli/atlas/alerts/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
store "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store/atlas"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/spf13/cobra"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115007/admin"
Expand Down Expand Up @@ -54,7 +53,9 @@ func (opts *ListOpts) Run() error {
GroupId: opts.ConfigProjectID(),
ItemsPerPage: &opts.ItemsPerPage,
PageNum: &opts.PageNum,
Status: pointer.GetStringPointerIfNotEmpty(opts.status),
}
if opts.status != "" {
params.Status = &opts.status
}

r, err := opts.store.Alerts(params)
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/atlas/alerts/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"github.com/golang/mock/gomock"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
mocks "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks/atlas"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/test"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115007/admin"
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/atlas/alerts/unacknowledge.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
store "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store/atlas"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/spf13/cobra"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115007/admin"
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/atlas/alerts/unacknowledge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"testing"

"github.com/golang/mock/gomock"
mocks "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks/atlas"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115007/admin"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/convert"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
store "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store/atlas"
customTime "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/time"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -57,14 +57,14 @@ func (opts *ListOpts) initStore(ctx context.Context) func() error {

func convertTime(value string) *time.Time {
var result *time.Time
if completedAfter, err := customTime.ParseTimestamp(value); err == nil {
if completedAfter, err := convert.ParseTimestamp(value); err == nil {
result = &completedAfter
}
return result
}

func (opts *ListOpts) validate() error {
if _, err := customTime.ParseTimestamp(opts.completedAfter); opts.completedAfter != "" && err != nil {
if _, err := convert.ParseTimestamp(opts.completedAfter); opts.completedAfter != "" && err != nil {
return fmt.Errorf("%w: expected format 'YYYY-MM-DD' got '%s'", ErrCompletedAfterInvalidFormat, opts.completedAfter)
}
return nil
Expand Down
26 changes: 18 additions & 8 deletions internal/cli/atlas/dbusers/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/convert"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/telemetry"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
Expand Down Expand Up @@ -106,18 +105,29 @@ func (opts *CreateOpts) newDatabaseUser() *atlasv2.CloudDatabaseUser {
authDB = convert.ExternalAuthDB
}

return &atlasv2.CloudDatabaseUser{
Roles: pointer.Get(convert.BuildAtlasRoles(opts.roles)),
Scopes: pointer.Get(convert.BuildAtlasScopes(opts.scopes)),
roles := convert.BuildAtlasRoles(opts.roles)
scopes := convert.BuildAtlasScopes(opts.scopes)
u := &atlasv2.CloudDatabaseUser{
Roles: &roles,
Scopes: &scopes,
GroupId: opts.ConfigProjectID(),
Username: opts.username,
Password: pointer.GetStringPointerIfNotEmpty(opts.password),
X509Type: pointer.GetStringPointerIfNotEmpty(opts.x509Type),
AwsIAMType: pointer.GetStringPointerIfNotEmpty(opts.awsIamType),
LdapAuthType: pointer.GetStringPointerIfNotEmpty(opts.ldapType),
DeleteAfterDate: convert.ParseDeleteAfter(opts.deleteAfter),
DatabaseName: authDB,
}
if opts.password != "" {
u.Password = &opts.password
}
if opts.x509Type != "" {
u.X509Type = &opts.x509Type
}
if opts.awsIamType != "" {
u.AwsIAMType = &opts.awsIamType
}
if opts.ldapType != "" {
u.LdapAuthType = &opts.ldapType
}
return u
}

func (opts *CreateOpts) Prompt() error {
Expand Down
14 changes: 9 additions & 5 deletions internal/cli/atlas/dbusers/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/convert"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/validate"
Expand Down Expand Up @@ -80,15 +79,20 @@ func (opts *UpdateOpts) update(out *admin.CloudDatabaseUser) {
out.Username = opts.currentUsername
}
if opts.password != "" {
out.Password = pointer.GetStringPointerIfNotEmpty(opts.password)
out.Password = &opts.password
}
out.Scopes = pointer.Get(convert.BuildAtlasScopes(opts.scopes))
out.Roles = pointer.Get(convert.BuildAtlasRoles(opts.roles))

roles := convert.BuildAtlasRoles(opts.roles)
out.Roles = &roles
scopes := convert.BuildAtlasScopes(opts.scopes)
out.Scopes = &scopes
out.DatabaseName = opts.authDB
if opts.authDB == "" {
out.DatabaseName = convert.GetAuthDB(out)
}
out.X509Type = pointer.GetStringPointerIfNotEmpty(opts.x509Type)
if opts.x509Type != "" {
out.X509Type = &opts.x509Type
}
}

func (opts *UpdateOpts) validateAuthDB() error {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/atlas/deployments/pause_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestPause_RunAtlas(t *testing.T) {
PauseCluster(projectID, deploymentName).
Return(
&admin.AdvancedClusterDescription{
Name: pointer.GetStringPointerIfNotEmpty(deploymentName),
Name: pointer.Get(deploymentName),
}, nil).
Times(1)

Expand Down
13 changes: 5 additions & 8 deletions internal/cli/atlas/deployments/search/indexes/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ func TestDescribe_RunLocal(t *testing.T) {
ctx := context.Background()

const (
expectedIndexName = "idx1"
expectedLocalDeployment = "localDeployment1"
expectedDB = "db1"
expectedCollection = "col1"
expectedStatus = "STEADY"
expectedType = "search"
)
Expand Down Expand Up @@ -108,11 +105,11 @@ func TestDescribe_RunLocal(t *testing.T) {

expected := &atlasv2.ClusterSearchIndex{
Name: "name",
IndexID: pointer.GetStringPointerIfNotEmpty("test"),
IndexID: pointer.Get("test"),
CollectionName: "coll",
Database: "db",
Status: pointer.GetStringPointerIfNotEmpty(expectedStatus),
Type: pointer.GetStringPointerIfNotEmpty(expectedType),
Status: pointer.Get(expectedStatus),
Type: pointer.Get(expectedType),
}

mockMongodbClient.
Expand Down Expand Up @@ -170,13 +167,13 @@ func TestDescribe_RunAtlas(t *testing.T) {
Name: "name",
Database: "db",
CollectionName: "coll",
IndexID: pointer.GetStringPointerIfNotEmpty("test"),
IndexID: pointer.Get("test"),
}, nil).
Times(1)

expected := &atlasv2.ClusterSearchIndex{
Name: "name",
IndexID: pointer.GetStringPointerIfNotEmpty("test"),
IndexID: pointer.Get("test"),
CollectionName: "coll",
Database: "db",
}
Expand Down
10 changes: 5 additions & 5 deletions internal/cli/atlas/deployments/search/indexes/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ func TestList_RunLocal(t *testing.T) {
expected := []*atlasv2.ClusterSearchIndex{
{
Name: expectedName,
IndexID: pointer.GetStringPointerIfNotEmpty(expectedID),
IndexID: pointer.Get(expectedID),
CollectionName: expectedCollection,
Database: expectedDB,
Status: pointer.GetStringPointerIfNotEmpty(expectedStatus),
Type: pointer.GetStringPointerIfNotEmpty(expectedType),
Status: pointer.Get(expectedStatus),
Type: pointer.Get(expectedType),
},
}

Expand Down Expand Up @@ -189,15 +189,15 @@ func TestList_RunAtlas(t *testing.T) {
Name: expectedName,
Database: expectedDB,
CollectionName: expectedCollection,
IndexID: pointer.GetStringPointerIfNotEmpty(expectedID),
IndexID: pointer.Get(expectedID),
},
}, nil).
Times(1)

expected := []*atlasv2.ClusterSearchIndex{
{
Name: expectedName,
IndexID: pointer.GetStringPointerIfNotEmpty(expectedID),
IndexID: pointer.Get(expectedID),
CollectionName: expectedCollection,
Database: expectedDB,
},
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/atlas/deployments/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func TestStart_RunAtlas(t *testing.T) {
StartCluster(projectID, deploymentName).
Return(
&admin.AdvancedClusterDescription{
Name: pointer.GetStringPointerIfNotEmpty(deploymentName),
Name: pointer.Get(deploymentName),
}, nil).
Times(1)

Expand Down
21 changes: 16 additions & 5 deletions internal/cli/atlas/events/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/convert"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
Expand Down Expand Up @@ -82,15 +83,22 @@ func (opts *ListOpts) NewOrgListOptions() admin.ListOrganizationEventsApiParams
p := admin.ListOrganizationEventsApiParams{
OrgId: opts.orgID,
EventType: eventType,
MaxDate: pointer.StringToTimePointer(opts.MaxDate),
MinDate: pointer.StringToTimePointer(opts.MinDate),
}
if maxDate, err := convert.ParseTimestamp(opts.MaxDate); err == nil {
p.MaxDate = pointer.Get(maxDate)
}
if minDate, err := convert.ParseTimestamp(opts.MinDate); err == nil {
p.MinDate = pointer.Get(minDate)
}
if opts.ItemsPerPage > 0 {
p.ItemsPerPage = &opts.ItemsPerPage
}
if opts.PageNum > 0 {
p.PageNum = &opts.PageNum
}
if opts.OmitCount {
p.IncludeCount = pointer.Get(false)
}
return p
}

Expand All @@ -102,16 +110,19 @@ func (opts *ListOpts) NewProjectListOptions() admin.ListProjectEventsApiParams {
p := admin.ListProjectEventsApiParams{
GroupId: opts.projectID,
EventType: eventType,
MaxDate: pointer.StringToTimePointer(opts.MaxDate),
MinDate: pointer.StringToTimePointer(opts.MinDate),
}
if maxDate, err := convert.ParseTimestamp(opts.MaxDate); err == nil {
p.MaxDate = pointer.Get(maxDate)
}
if minDate, err := convert.ParseTimestamp(opts.MinDate); err == nil {
p.MinDate = pointer.Get(minDate)
}
if opts.ItemsPerPage > 0 {
p.ItemsPerPage = &opts.ItemsPerPage
}
if opts.PageNum > 0 {
p.PageNum = &opts.PageNum
}

if opts.OmitCount {
p.IncludeCount = pointer.Get(false)
}
Expand Down
Loading