diff --git a/internal/cli/streams/instance/create.go b/internal/cli/streams/instance/create.go index 872f1c5bb7..7f0694d153 100644 --- a/internal/cli/streams/instance/create.go +++ b/internal/cli/streams/instance/create.go @@ -38,16 +38,19 @@ type CreateOpts struct { cli.ProjectOpts cli.OutputOpts cli.InputOpts - name string - provider string - region string - tier string - store StreamsCreator + name string + provider string + region string + tier string + defaultTier string + maxTierSize string + store StreamsCreator } const ( - createTemplate = "Atlas Streams Processor Instance '{{.Name}}' successfully created.\n" - defaultTier = "SP30" + createTemplate = "Atlas Streams Processor Instance '{{.Name}}' successfully created.\n" + createWorkspace = "Atlas Streams Processor Workspace '{{.Name}}' successfully created.\n" + defaultTier = "SP30" ) func (opts *CreateOpts) Run() error { @@ -124,3 +127,46 @@ func CreateBuilder() *cobra.Command { return cmd } + +func WorkspaceCreateBuilder() *cobra.Command { + opts := &CreateOpts{} + cmd := &cobra.Command{ + Use: "create ", + Short: "Create an Atlas Stream Processing workspace for your project", + Long: `To get started quickly, specify a name, a cloud provider, and a region to configure an Atlas Stream Processing workspace.` + fmt.Sprintf(usage.RequiredRole, "Project Owner"), + Example: ` # Deploy an Atlas Stream Processing workspace called myProcessor for the project with the ID 5e2211c17a3e5a48f5497de3: + atlas streams instance create myProcessor --projectId 5e2211c17a3e5a48f5497de3 --provider AWS --region VIRGINIA_USA --tier SP10 --defaultTier SP30 --maxTierSize SP50`, + Args: require.ExactArgs(1), + Annotations: map[string]string{ + "nameDesc": "Name of the Atlas Stream Processing workspace. After creation, you can't change the name of the workspace. The name can contain ASCII letters, numbers, and hyphens.", + "output": createWorkspace, + }, + PreRunE: func(cmd *cobra.Command, args []string) error { + opts.name = args[0] + + return opts.PreRunE( + opts.ValidateProjectID, + opts.initStore(cmd.Context()), + opts.InitOutput(cmd.OutOrStdout(), createWorkspace), + ) + }, + RunE: func(_ *cobra.Command, _ []string) error { + return opts.Run() + }, + } + + cmd.Flags().StringVar(&opts.provider, flag.Provider, "AWS", usage.StreamsProvider) + cmd.Flags().StringVarP(&opts.region, flag.Region, flag.RegionShort, "", usage.StreamsRegion) + + opts.AddProjectOptsFlags(cmd) + opts.AddOutputOptFlags(cmd) + + cmd.Flags().StringVar(&opts.tier, flag.Tier, "SP30", usage.StreamsWorkspaceTier) + cmd.Flags().StringVar(&opts.defaultTier, flag.DefaultTier, "", usage.StreamsWorkspaceDefaultTier) + cmd.Flags().StringVar(&opts.maxTierSize, flag.MaxTierSize, "", usage.StreamsWorkspaceMaxTierSize) + + _ = cmd.MarkFlagRequired(flag.Provider) + _ = cmd.MarkFlagRequired(flag.Region) + + return cmd +} diff --git a/internal/cli/streams/instance/create_test.go b/internal/cli/streams/instance/create_test.go index 8b0b71945f..5cb158851a 100644 --- a/internal/cli/streams/instance/create_test.go +++ b/internal/cli/streams/instance/create_test.go @@ -98,4 +98,73 @@ func TestCreateOpts_Run(t *testing.T) { t.Log(buf.String()) test.VerifyOutputTemplate(t, createTemplate, expected) }) + + t.Run("stream workspaces create --tier", func(t *testing.T) { + buf := new(bytes.Buffer) + opts := &CreateOpts{ + store: mockStore, + name: "ExampleStreamWorkspaces", + provider: "AWS", + region: "VIRGINIA_USA", + tier: "SP30", + } + opts.ProjectID = testProjectID + + expected := &atlasv2.StreamsTenant{ + Name: &opts.name, + GroupId: &opts.ProjectID, + DataProcessRegion: &atlasv2.StreamsDataProcessRegion{CloudProvider: "AWS", Region: "VIRGINIA_USA"}, + StreamConfig: &atlasv2.StreamConfig{ + Tier: &opts.tier, + }, + } + + mockStore. + EXPECT(). + CreateStream(opts.ProjectID, expected). + Return(expected, nil). + Times(1) + + if err := opts.Run(); err != nil { + t.Fatalf("Run() unexpected error: %v", err) + } + t.Log(buf.String()) + test.VerifyOutputTemplate(t, createWorkspace, expected) + }) + + // Testing the parsing of flags but not passing into StreamConfig object + t.Run("stream workspaces create --tier --defaultTier --maxTierSize", func(t *testing.T) { + buf := new(bytes.Buffer) + opts := &CreateOpts{ + store: mockStore, + name: "ExampleStreamWorkspaces", + provider: "AWS", + region: "VIRGINIA_USA", + tier: "SP30", + defaultTier: "SP30", + maxTierSize: "SP50", + } + opts.ProjectID = testProjectID + + expected := &atlasv2.StreamsTenant{ + Name: &opts.name, + GroupId: &opts.ProjectID, + DataProcessRegion: &atlasv2.StreamsDataProcessRegion{CloudProvider: "AWS", Region: "VIRGINIA_USA"}, + StreamConfig: &atlasv2.StreamConfig{ + Tier: &opts.tier, + }, + } + + mockStore. + EXPECT(). + CreateStream(opts.ProjectID, expected). + Return(expected, nil). + Times(1) + + if err := opts.Run(); err != nil { + t.Fatalf("Run() unexpected error: %v", err) + } + t.Log(buf.String()) + test.VerifyOutputTemplate(t, createWorkspace, expected) + }) } diff --git a/internal/cli/streams/instance/workspaces.go b/internal/cli/streams/instance/workspaces.go new file mode 100644 index 0000000000..ac75216e43 --- /dev/null +++ b/internal/cli/streams/instance/workspaces.go @@ -0,0 +1,33 @@ +// 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 instance + +import ( + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli" + "github.com/spf13/cobra" +) + +func WorkspaceBuilder() *cobra.Command { + const use = "workspaces" + cmd := &cobra.Command{ + Use: use, + Aliases: cli.GenerateAliases(use), + Short: "Manage Atlas Stream Processing workspaces.", + Long: `Create, list, update, and delete your Atlas Stream Processing workspaces.`, + } + cmd.AddCommand(WorkspaceCreateBuilder()) + + return cmd +} diff --git a/internal/flag/flags.go b/internal/flag/flags.go index 3cee4fff9d..3a58edd692 100644 --- a/internal/flag/flags.go +++ b/internal/flag/flags.go @@ -51,6 +51,8 @@ const ( MembersShort = "m" // MembersShort flag ShardsShort = "s" // ShardsShort flag Tier = "tier" // Tier flag + DefaultTier = "defaultTier" // DefaultTier flag + MaxTierSize = "maxTierSize" // MaxTierSize flag Forever = "forever" // Forever flag ForeverShort = "F" // ForeverShort flag DiskSizeGB = "diskSizeGB" // DiskSizeGB flag diff --git a/internal/usage/usage.go b/internal/usage/usage.go index 96f44045b4..460aeb9043 100644 --- a/internal/usage/usage.go +++ b/internal/usage/usage.go @@ -242,9 +242,13 @@ dbName and collection are required only for built-in roles.` StreamsRegion = "Human-readable label that identifies the physical location of your Atlas Stream Processing instance. The region can affect network latency and performance if it is far from your source or sink. For AWS, region name must be in the following format: VIRGINIA_USA. For a list of valid values, see https://www.mongodb.com/docs/atlas/reference/amazon-aws/#std-label-aws-stream-processing-regions. For Azure, region name must be in the following format: eastus. For a list of valid values, see https://www.mongodb.com/docs/atlas/reference/microsoft-azure/#std-label-azure-stream-processing-regions." StreamsProvider = "Cloud service provider that applies to the provisioned Atlas Stream Processing instance. Valid values are AWS or AZURE." StreamsInstance = "Name of your Atlas Stream Processing instance." + StreamsWorkspace = "Name of your Atlas Stream Procesing workspace." StreamsConnectionFilename = "Path to a JSON configuration file that defines an Atlas Stream Processing connection. Note: Unsupported fields in the JSON file are ignored." StreamsPrivateLinkFilename = "Path to a JSON configuration file that defines an Atlas Stream Processing PrivateLink endpoint. Note: Unsupported fields in the JSON file are ignored." StreamsInstanceTier = "Tier for your Stream Instance." + StreamsWorkspaceTier = "Tier for your Stream Workspace." + StreamsWorkspaceDefaultTier = "Default Tier for your Stream Workspace." + StreamsWorkspaceMaxTierSize = "Max Tier Size for your Stream Workspace." WithoutDefaultAlertSettings = "Flag that creates the new project without the default alert settings enabled. This flag defaults to false. This option is useful if you create projects programmatically and want to create your own alerts instead of using the default alert settings." FormatOut = "Output format. Valid values are json, json-path, go-template, or go-template-file. To see the full output, use the -o json option." TargetClusterName = "Name of the target cluster. For use only with automated restore jobs. You must specify a targetClusterName for automated restores."