diff --git a/cmd/base/list_options.go b/cmd/base/list_options.go index edb130c..cbd13ef 100644 --- a/cmd/base/list_options.go +++ b/cmd/base/list_options.go @@ -386,3 +386,18 @@ func (o *DriveInterfaceOption[T]) ApplyToCollection(collection serverscom.Collec collection.SetParam("interface", o.iface) } } + +type SBMFlavorsShowAllOption[T any] struct { + all bool +} + +func (o *SBMFlavorsShowAllOption[T]) AddFlags(cmd *cobra.Command) { + cmd.Flags().BoolVar(&o.all, "show-all", false, + "Filter to show all SBM flavors including unavailable ones") +} + +func (o *SBMFlavorsShowAllOption[T]) ApplyToCollection(collection serverscom.Collection[T]) { + if o.all { + collection.SetParam("show_all", "true") + } +} diff --git a/cmd/entities/sbm_models/get.go b/cmd/entities/sbm_models/get.go new file mode 100644 index 0000000..6f64b22 --- /dev/null +++ b/cmd/entities/sbm_models/get.go @@ -0,0 +1,51 @@ +package sbmmodels + +import ( + "fmt" + "strconv" + + "github.com/serverscom/srvctl/cmd/base" + "github.com/spf13/cobra" +) + +func newGetCmd(cmdContext *base.CmdContext) *cobra.Command { + var locationID int64 + + cmd := &cobra.Command{ + Use: "get ", + Short: "Get an SBM flavor", + Long: "Get an SBM flavor for a location", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + manager := cmdContext.GetManager() + + ctx, cancel := base.SetupContext(cmd, manager) + defer cancel() + + base.SetupProxy(cmd, manager) + + scClient := cmdContext.GetClient().SetVerbose(manager.GetVerbose(cmd)).GetScClient() + + sbmFlavorModelID, err := strconv.Atoi(args[0]) + if err != nil { + return fmt.Errorf("sbm flavor model id should be integer") + } + + model, err := scClient.Locations.GetSBMFlavorOption(ctx, locationID, int64(sbmFlavorModelID)) + if err != nil { + return err + } + + if model != nil { + formatter := cmdContext.GetOrCreateFormatter(cmd) + return formatter.Format(model) + } + return nil + }, + } + + cmd.Flags().Int64Var(&locationID, "location-id", 0, "Location id (int, required)") + _ = cmd.MarkFlagRequired("location-id") + + return cmd +} diff --git a/cmd/entities/sbm_models/list.go b/cmd/entities/sbm_models/list.go new file mode 100644 index 0000000..5b75835 --- /dev/null +++ b/cmd/entities/sbm_models/list.go @@ -0,0 +1,29 @@ +package sbmmodels + +import ( + serverscom "github.com/serverscom/serverscom-go-client/pkg" + "github.com/serverscom/srvctl/cmd/base" + "github.com/spf13/cobra" +) + +func newListCmd(cmdContext *base.CmdContext) *cobra.Command { + var locationID int64 + + factory := func(verbose bool, args ...string) serverscom.Collection[serverscom.SBMFlavor] { + scClient := cmdContext.GetClient().SetVerbose(verbose).GetScClient() + return scClient.Locations.SBMFlavorOptions(locationID) + } + + opts := base.NewListOptions( + &base.BaseListOptions[serverscom.SBMFlavor]{}, + &base.SearchPatternOption[serverscom.SBMFlavor]{}, + &base.SBMFlavorsShowAllOption[serverscom.SBMFlavor]{}, + ) + + cmd := base.NewListCmd("list", "sbm-models", factory, cmdContext, opts...) + + cmd.Flags().Int64Var(&locationID, "location-id", 0, "Location ID (required)") + _ = cmd.MarkFlagRequired("location-id") + + return cmd +} diff --git a/cmd/entities/sbm_models/sbmmodels.go b/cmd/entities/sbm_models/sbmmodels.go new file mode 100644 index 0000000..44d2a3e --- /dev/null +++ b/cmd/entities/sbm_models/sbmmodels.go @@ -0,0 +1,38 @@ +package sbmmodels + +import ( + "log" + + serverscom "github.com/serverscom/serverscom-go-client/pkg" + "github.com/serverscom/srvctl/cmd/base" + "github.com/serverscom/srvctl/internal/output/entities" + "github.com/spf13/cobra" +) + +func NewCmd(cmdContext *base.CmdContext) *cobra.Command { + sbmFlavorEntity, err := entities.Registry.GetEntityFromValue(serverscom.SBMFlavor{}) + if err != nil { + log.Fatal(err) + } + entitiesMap := make(map[string]entities.EntityInterface) + entitiesMap["sbm-models"] = sbmFlavorEntity + cmd := &cobra.Command{ + Use: "sbm-models", + Short: "Manage sbm flavor models", + PersistentPreRunE: base.CombinePreRunE( + base.CheckFormatterFlags(cmdContext, entitiesMap), + base.CheckEmptyContexts(cmdContext), + ), + Args: base.NoArgs, + Run: base.UsageRun, + } + + cmd.AddCommand( + newListCmd(cmdContext), + newGetCmd(cmdContext), + ) + + base.AddFormatFlags(cmd) + + return cmd +} diff --git a/cmd/entities/sbm_models/sbmmodels_test.go b/cmd/entities/sbm_models/sbmmodels_test.go new file mode 100644 index 0000000..92c339a --- /dev/null +++ b/cmd/entities/sbm_models/sbmmodels_test.go @@ -0,0 +1,256 @@ +package sbmmodels + +import ( + "errors" + "fmt" + "path/filepath" + "testing" + + . "github.com/onsi/gomega" + serverscom "github.com/serverscom/serverscom-go-client/pkg" + "github.com/serverscom/srvctl/cmd/testutils" + "github.com/serverscom/srvctl/internal/mocks" + "go.uber.org/mock/gomock" +) + +var ( + fixtureBasePath = filepath.Join("..", "..", "..", "testdata", "entities", "sbm-models") + testSBMFlavorID = int64(10) + testLocationID = int64(1) + testSBMFlavor = serverscom.SBMFlavor{ + ID: testSBMFlavorID, + Name: "P-101", + CPUName: "cpu_name", + CPUCount: 1, + CPUCoresCount: 2, + CPUFrequency: "3.8", + RAMSize: 4096, + DrivesConfiguration: "", + PublicUplinkModelID: 843, + PublicUplinkModelName: "uplink-model-name-30", + PrivateUplinkModelID: 842, + PrivateUplinkModelName: "uplink-model-name-29", + BandwidthID: 844, + BandwidthName: "public-bandwidth-model-35", + } +) + +func TestGetSBMModelCmd(t *testing.T) { + testCases := []struct { + name string + id int64 + output string + flags []string + expectedOutput []byte + expectError bool + }{ + { + name: "get sbm model in default format", + id: testSBMFlavorID, + flags: []string{"--location-id", fmt.Sprint(testLocationID)}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "get.txt")), + }, + { + name: "get sbm model in JSON format", + id: testSBMFlavorID, + output: "json", + flags: []string{"--location-id", fmt.Sprint(testLocationID)}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "get.json")), + }, + { + name: "get sbm model in YAML format", + id: testSBMFlavorID, + output: "yaml", + flags: []string{"--location-id", fmt.Sprint(testLocationID)}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "get.yaml")), + }, + { + name: "get sbm model with service error", + id: testSBMFlavorID, + flags: []string{"--location-id", fmt.Sprint(testLocationID)}, + expectError: true, + }, + { + name: "get sbm model missing required flags", + id: testSBMFlavorID, + expectError: true, + }, + } + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + + locationsServiceHandler := mocks.NewMockLocationsService(mockCtrl) + + scClient := serverscom.NewClientWithEndpoint("", "") + scClient.Locations = locationsServiceHandler + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + g := NewWithT(t) + + var err error + if tc.expectError && len(tc.flags) > 0 { + err = errors.New("some error") + } + + if len(tc.flags) > 0 { + locationsServiceHandler.EXPECT(). + GetSBMFlavorOption(gomock.Any(), testLocationID, tc.id). + Return(&testSBMFlavor, err) + } + + testCmdContext := testutils.NewTestCmdContext(scClient) + sbmCmd := NewCmd(testCmdContext) + + args := []string{"sbm-models", "get", fmt.Sprint(tc.id)} + if len(tc.flags) > 0 { + args = append(args, tc.flags...) + } + if tc.output != "" { + args = append(args, "--output", tc.output) + } + + builder := testutils.NewTestCommandBuilder(). + WithCommand(sbmCmd). + WithArgs(args) + + cmd := builder.Build() + + execErr := cmd.Execute() + + if tc.expectError { + g.Expect(execErr).To(HaveOccurred()) + } else { + g.Expect(execErr).To(BeNil()) + g.Expect(builder.GetOutput()).To(BeEquivalentTo(string(tc.expectedOutput))) + } + }) + } +} + +func TestListUplinkModelsCmd(t *testing.T) { + f1 := testSBMFlavor + f2 := testSBMFlavor + f2.ID = testSBMFlavorID + 1 + f2.Name = "P-102" + + testCases := []struct { + name string + output string + args []string + expectedOutput []byte + expectError bool + configureMock func(*mocks.MockCollection[serverscom.SBMFlavor]) + }{ + { + name: "list all sbm models", + output: "json", + args: []string{"-A", "--location-id", "1"}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "list_all.json")), + configureMock: func(mock *mocks.MockCollection[serverscom.SBMFlavor]) { + mock.EXPECT(). + Collect(gomock.Any()). + Return([]serverscom.SBMFlavor{f1, f2}, nil) + }, + }, + { + name: "list sbm models", + output: "json", + args: []string{"--location-id", "1"}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "list.json")), + configureMock: func(mock *mocks.MockCollection[serverscom.SBMFlavor]) { + mock.EXPECT(). + List(gomock.Any()). + Return([]serverscom.SBMFlavor{f1}, nil) + }, + }, + { + name: "list sbm models with template", + args: []string{"--template", "{{range .}}ID: {{.ID}} Name: {{.Name}}\n{{end}}", "--location-id", "1"}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "list_template.txt")), + configureMock: func(mock *mocks.MockCollection[serverscom.SBMFlavor]) { + mock.EXPECT(). + List(gomock.Any()). + Return([]serverscom.SBMFlavor{f1, f2}, nil) + }, + }, + { + name: "list sbm models with pageView", + args: []string{"--page-view", "--location-id", "1"}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "list_pageview.txt")), + configureMock: func(mock *mocks.MockCollection[serverscom.SBMFlavor]) { + mock.EXPECT(). + List(gomock.Any()). + Return([]serverscom.SBMFlavor{f1, f2}, nil) + }, + }, + { + name: "list sbm models with error", + args: []string{"--location-id", "1"}, + expectError: true, + configureMock: func(mock *mocks.MockCollection[serverscom.SBMFlavor]) { + mock.EXPECT(). + List(gomock.Any()). + Return(nil, errors.New("some error")) + }, + }, + { + name: "list sbm models missing required flags", + expectError: true, + }, + } + + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + + locationsServiceHandler := mocks.NewMockLocationsService(mockCtrl) + collectionHandler := mocks.NewMockCollection[serverscom.SBMFlavor](mockCtrl) + + locationsServiceHandler.EXPECT(). + SBMFlavorOptions(gomock.Any()). + Return(collectionHandler). + AnyTimes() + + collectionHandler.EXPECT(). + SetParam(gomock.Any(), gomock.Any()). + Return(collectionHandler). + AnyTimes() + + scClient := serverscom.NewClientWithEndpoint("", "") + scClient.Locations = locationsServiceHandler + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + g := NewWithT(t) + + if tc.configureMock != nil { + tc.configureMock(collectionHandler) + } + + testCmdContext := testutils.NewTestCmdContext(scClient) + uplinkCmd := NewCmd(testCmdContext) + + args := []string{"sbm-models", "list"} + if len(tc.args) > 0 { + args = append(args, tc.args...) + } + if tc.output != "" { + args = append(args, "--output", tc.output) + } + + builder := testutils.NewTestCommandBuilder(). + WithCommand(uplinkCmd). + WithArgs(args) + + cmd := builder.Build() + err := cmd.Execute() + + if tc.expectError { + g.Expect(err).To(HaveOccurred()) + } else { + g.Expect(err).To(BeNil()) + g.Expect(builder.GetOutput()).To(BeEquivalentTo(string(tc.expectedOutput))) + } + }) + } +} diff --git a/cmd/entities/sbm_os_options/get.go b/cmd/entities/sbm_os_options/get.go new file mode 100644 index 0000000..3a46a29 --- /dev/null +++ b/cmd/entities/sbm_os_options/get.go @@ -0,0 +1,54 @@ +package sbmosoptions + +import ( + "fmt" + "strconv" + + "github.com/serverscom/srvctl/cmd/base" + "github.com/spf13/cobra" +) + +func newGetCmd(cmdContext *base.CmdContext) *cobra.Command { + var locationID int64 + var sbmFlavorModelID int64 + + cmd := &cobra.Command{ + Use: "get ", + Short: "Get an operating system", + Long: "Get an operating system for an SBM server", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + manager := cmdContext.GetManager() + + ctx, cancel := base.SetupContext(cmd, manager) + defer cancel() + + base.SetupProxy(cmd, manager) + + scClient := cmdContext.GetClient().SetVerbose(manager.GetVerbose(cmd)).GetScClient() + + osID, err := strconv.Atoi(args[0]) + if err != nil { + return fmt.Errorf("operating system id should be integer") + } + + model, err := scClient.Locations.GetSBMOperatingSystemOption(ctx, locationID, sbmFlavorModelID, int64(osID)) + if err != nil { + return err + } + + if model != nil { + formatter := cmdContext.GetOrCreateFormatter(cmd) + return formatter.Format(model) + } + return nil + }, + } + + cmd.Flags().Int64Var(&locationID, "location-id", 0, "Location id (int, required)") + cmd.Flags().Int64Var(&sbmFlavorModelID, "model-id", 0, "SBM flavor model id (int, required)") + _ = cmd.MarkFlagRequired("location-id") + _ = cmd.MarkFlagRequired("model-id") + + return cmd +} diff --git a/cmd/entities/sbm_os_options/list.go b/cmd/entities/sbm_os_options/list.go new file mode 100644 index 0000000..3b0bd22 --- /dev/null +++ b/cmd/entities/sbm_os_options/list.go @@ -0,0 +1,30 @@ +package sbmosoptions + +import ( + serverscom "github.com/serverscom/serverscom-go-client/pkg" + "github.com/serverscom/srvctl/cmd/base" + "github.com/spf13/cobra" +) + +func newListCmd(cmdContext *base.CmdContext) *cobra.Command { + var locationID int64 + var sbmFlavorModelID int64 + + factory := func(verbose bool, args ...string) serverscom.Collection[serverscom.OperatingSystemOption] { + scClient := cmdContext.GetClient().SetVerbose(verbose).GetScClient() + return scClient.Locations.SBMOperatingSystemOptions(locationID, sbmFlavorModelID) + } + + opts := base.NewListOptions( + &base.BaseListOptions[serverscom.OperatingSystemOption]{}, + ) + + cmd := base.NewListCmd("list", "sbm-os-options", factory, cmdContext, opts...) + + cmd.Flags().Int64Var(&locationID, "location-id", 0, "Location ID (required)") + cmd.Flags().Int64Var(&sbmFlavorModelID, "model-id", 0, "SBM flavor model id (int, required)") + _ = cmd.MarkFlagRequired("location-id") + _ = cmd.MarkFlagRequired("model-id") + + return cmd +} diff --git a/cmd/entities/sbm_os_options/sbmosoptions.go b/cmd/entities/sbm_os_options/sbmosoptions.go new file mode 100644 index 0000000..15684c9 --- /dev/null +++ b/cmd/entities/sbm_os_options/sbmosoptions.go @@ -0,0 +1,38 @@ +package sbmosoptions + +import ( + "log" + + serverscom "github.com/serverscom/serverscom-go-client/pkg" + "github.com/serverscom/srvctl/cmd/base" + "github.com/serverscom/srvctl/internal/output/entities" + "github.com/spf13/cobra" +) + +func NewCmd(cmdContext *base.CmdContext) *cobra.Command { + osOptionEntity, err := entities.Registry.GetEntityFromValue(serverscom.OperatingSystemOption{}) + if err != nil { + log.Fatal(err) + } + entitiesMap := make(map[string]entities.EntityInterface) + entitiesMap["sbm-os-options"] = osOptionEntity + cmd := &cobra.Command{ + Use: "sbm-os-options", + Short: "Manage sbm os options", + PersistentPreRunE: base.CombinePreRunE( + base.CheckFormatterFlags(cmdContext, entitiesMap), + base.CheckEmptyContexts(cmdContext), + ), + Args: base.NoArgs, + Run: base.UsageRun, + } + + cmd.AddCommand( + newListCmd(cmdContext), + newGetCmd(cmdContext), + ) + + base.AddFormatFlags(cmd) + + return cmd +} diff --git a/cmd/entities/sbm_os_options/sbmosoptions_test.go b/cmd/entities/sbm_os_options/sbmosoptions_test.go new file mode 100644 index 0000000..2910c24 --- /dev/null +++ b/cmd/entities/sbm_os_options/sbmosoptions_test.go @@ -0,0 +1,227 @@ +package sbmosoptions + +import ( + "errors" + "fmt" + "path/filepath" + "testing" + + . "github.com/onsi/gomega" + serverscom "github.com/serverscom/serverscom-go-client/pkg" + "github.com/serverscom/srvctl/cmd/testutils" + "github.com/serverscom/srvctl/internal/mocks" + "go.uber.org/mock/gomock" +) + +var ( + fixtureBasePath = filepath.Join("..", "..", "..", "testdata", "entities", "os-options") + testOSOptionID = int64(10) + testLocationID = int64(1) + testServerModelID = int64(100) + testOSOption = serverscom.OperatingSystemOption{ + ID: testOSOptionID, + FullName: "Ubuntu 18.04-server x86_64", + Name: "Ubuntu", + Version: "18.04-server", + Arch: "x86_64", + Filesystems: []string{"ext2", "ext4", "swap", "xfs", "reiser"}, + } +) + +func TestGetSBMOperatingSystemOptionCmd(t *testing.T) { + testCases := []struct { + name string + id int64 + output string + flags []string + expectedOutput []byte + expectError bool + }{ + { + name: "get os option in default format", + id: testOSOptionID, + flags: []string{"--location-id", fmt.Sprint(testLocationID), "--model-id", fmt.Sprint(testServerModelID)}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "get.txt")), + }, + { + name: "get os option in JSON format", + id: testOSOptionID, + output: "json", + flags: []string{"--location-id", fmt.Sprint(testLocationID), "--model-id", fmt.Sprint(testServerModelID)}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "get.json")), + }, + { + name: "get os option in YAML format", + id: testOSOptionID, + output: "yaml", + flags: []string{"--location-id", fmt.Sprint(testLocationID), "--model-id", fmt.Sprint(testServerModelID)}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "get.yaml")), + }, + { + name: "get os option with service error", + id: testOSOptionID, + flags: []string{"--location-id", fmt.Sprint(testLocationID), "--model-id", fmt.Sprint(testServerModelID)}, + expectError: true, + }, + { + name: "get os option missing required flags", + id: testOSOptionID, + expectError: true, + }, + } + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + locationsServiceHandler := mocks.NewMockLocationsService(mockCtrl) + scClient := serverscom.NewClientWithEndpoint("", "") + scClient.Locations = locationsServiceHandler + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + g := NewWithT(t) + var err error + if tc.expectError && len(tc.flags) > 0 { + err = errors.New("some error") + } + if len(tc.flags) > 0 { + locationsServiceHandler.EXPECT(). + GetSBMOperatingSystemOption(gomock.Any(), testLocationID, testServerModelID, tc.id). + Return(&testOSOption, err) + } + testCmdContext := testutils.NewTestCmdContext(scClient) + osCmd := NewCmd(testCmdContext) + args := []string{"sbm-os-options", "get", fmt.Sprint(tc.id)} + if len(tc.flags) > 0 { + args = append(args, tc.flags...) + } + if tc.output != "" { + args = append(args, "--output", tc.output) + } + builder := testutils.NewTestCommandBuilder(). + WithCommand(osCmd). + WithArgs(args) + cmd := builder.Build() + execErr := cmd.Execute() + if tc.expectError { + g.Expect(execErr).To(HaveOccurred()) + } else { + g.Expect(execErr).To(BeNil()) + g.Expect(builder.GetOutput()).To(BeEquivalentTo(string(tc.expectedOutput))) + } + }) + } +} + +func TestListSBMOperatingSystemOptionsCmd(t *testing.T) { + o1 := testOSOption + o2 := testOSOption + o2.ID = testOSOptionID + 1 + o2.FullName = "CentOS 7 x86_64" + o2.Name = "CentOS" + o2.Version = "7" + testCases := []struct { + name string + output string + args []string + expectedOutput []byte + expectError bool + configureMock func(*mocks.MockCollection[serverscom.OperatingSystemOption]) + }{ + { + name: "list all os options", + output: "json", + args: []string{"-A", "--location-id", "1", "--model-id", "100"}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "list_all.json")), + configureMock: func(mock *mocks.MockCollection[serverscom.OperatingSystemOption]) { + mock.EXPECT(). + Collect(gomock.Any()). + Return([]serverscom.OperatingSystemOption{o1, o2}, nil) + }, + }, + { + name: "list os options", + output: "json", + args: []string{"--location-id", "1", "--model-id", "100"}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "list.json")), + configureMock: func(mock *mocks.MockCollection[serverscom.OperatingSystemOption]) { + mock.EXPECT(). + List(gomock.Any()). + Return([]serverscom.OperatingSystemOption{o1}, nil) + }, + }, + { + name: "list os options with template", + args: []string{"--template", "{{range .}}ID: {{.ID}} Arch: {{.Arch}}\\n{{end}}", "--location-id", "1", "--model-id", "100"}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "list_template.txt")), + configureMock: func(mock *mocks.MockCollection[serverscom.OperatingSystemOption]) { + mock.EXPECT(). + List(gomock.Any()). + Return([]serverscom.OperatingSystemOption{o1, o2}, nil) + }, + }, + { + name: "list os options with pageView", + args: []string{"--page-view", "--location-id", "1", "--model-id", "100"}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "list_pageview.txt")), + configureMock: func(mock *mocks.MockCollection[serverscom.OperatingSystemOption]) { + mock.EXPECT(). + List(gomock.Any()). + Return([]serverscom.OperatingSystemOption{o1, o2}, nil) + }, + }, + { + name: "list os options with error", + args: []string{"--location-id", "1", "--model-id", "100"}, + expectError: true, + configureMock: func(mock *mocks.MockCollection[serverscom.OperatingSystemOption]) { + mock.EXPECT(). + List(gomock.Any()). + Return(nil, errors.New("some error")) + }, + }, + { + name: "list os options missing required flags", + expectError: true, + }, + } + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + locationsServiceHandler := mocks.NewMockLocationsService(mockCtrl) + collectionHandler := mocks.NewMockCollection[serverscom.OperatingSystemOption](mockCtrl) + locationsServiceHandler.EXPECT(). + SBMOperatingSystemOptions(gomock.Any(), gomock.Any()). + Return(collectionHandler). + AnyTimes() + collectionHandler.EXPECT(). + SetParam(gomock.Any(), gomock.Any()). + Return(collectionHandler). + AnyTimes() + scClient := serverscom.NewClientWithEndpoint("", "") + scClient.Locations = locationsServiceHandler + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + g := NewWithT(t) + if tc.configureMock != nil { + tc.configureMock(collectionHandler) + } + testCmdContext := testutils.NewTestCmdContext(scClient) + osCmd := NewCmd(testCmdContext) + args := []string{"sbm-os-options", "list"} + if len(tc.args) > 0 { + args = append(args, tc.args...) + } + if tc.output != "" { + args = append(args, "--output", tc.output) + } + builder := testutils.NewTestCommandBuilder(). + WithCommand(osCmd). + WithArgs(args) + cmd := builder.Build() + err := cmd.Execute() + if tc.expectError { + g.Expect(err).To(HaveOccurred()) + } else { + g.Expect(err).To(BeNil()) + g.Expect(builder.GetOutput()).To(BeEquivalentTo(string(tc.expectedOutput))) + } + }) + } +} diff --git a/cmd/entities/server_os_options/get.go b/cmd/entities/server_os_options/get.go new file mode 100644 index 0000000..e98a641 --- /dev/null +++ b/cmd/entities/server_os_options/get.go @@ -0,0 +1,54 @@ +package serverosoptions + +import ( + "fmt" + "strconv" + + "github.com/serverscom/srvctl/cmd/base" + "github.com/spf13/cobra" +) + +func newGetCmd(cmdContext *base.CmdContext) *cobra.Command { + var locationID int64 + var serverModelID int64 + + cmd := &cobra.Command{ + Use: "get ", + Short: "Get an operating system", + Long: "Get an operating system for a server model", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + manager := cmdContext.GetManager() + + ctx, cancel := base.SetupContext(cmd, manager) + defer cancel() + + base.SetupProxy(cmd, manager) + + scClient := cmdContext.GetClient().SetVerbose(manager.GetVerbose(cmd)).GetScClient() + + osID, err := strconv.Atoi(args[0]) + if err != nil { + return fmt.Errorf("operating system id should be integer") + } + + model, err := scClient.Locations.GetOperatingSystemOption(ctx, locationID, serverModelID, int64(osID)) + if err != nil { + return err + } + + if model != nil { + formatter := cmdContext.GetOrCreateFormatter(cmd) + return formatter.Format(model) + } + return nil + }, + } + + cmd.Flags().Int64Var(&locationID, "location-id", 0, "Location id (int, required)") + cmd.Flags().Int64Var(&serverModelID, "server-model-id", 0, "Server model id (int, required)") + _ = cmd.MarkFlagRequired("location-id") + _ = cmd.MarkFlagRequired("server-model-id") + + return cmd +} diff --git a/cmd/entities/server_os_options/list.go b/cmd/entities/server_os_options/list.go new file mode 100644 index 0000000..16c3f28 --- /dev/null +++ b/cmd/entities/server_os_options/list.go @@ -0,0 +1,30 @@ +package serverosoptions + +import ( + serverscom "github.com/serverscom/serverscom-go-client/pkg" + "github.com/serverscom/srvctl/cmd/base" + "github.com/spf13/cobra" +) + +func newListCmd(cmdContext *base.CmdContext) *cobra.Command { + var locationID int64 + var serverModelID int64 + + factory := func(verbose bool, args ...string) serverscom.Collection[serverscom.OperatingSystemOption] { + scClient := cmdContext.GetClient().SetVerbose(verbose).GetScClient() + return scClient.Locations.OperatingSystemOptions(locationID, serverModelID) + } + + opts := base.NewListOptions( + &base.BaseListOptions[serverscom.OperatingSystemOption]{}, + ) + + cmd := base.NewListCmd("list", "server-os-options", factory, cmdContext, opts...) + + cmd.Flags().Int64Var(&locationID, "location-id", 0, "Location id (int, required)") + cmd.Flags().Int64Var(&serverModelID, "server-model-id", 0, "Server model id (int, required)") + _ = cmd.MarkFlagRequired("location-id") + _ = cmd.MarkFlagRequired("server-model-id") + + return cmd +} diff --git a/cmd/entities/server_os_options/serverosoptions.go b/cmd/entities/server_os_options/serverosoptions.go new file mode 100644 index 0000000..7b8a0ed --- /dev/null +++ b/cmd/entities/server_os_options/serverosoptions.go @@ -0,0 +1,38 @@ +package serverosoptions + +import ( + "log" + + serverscom "github.com/serverscom/serverscom-go-client/pkg" + "github.com/serverscom/srvctl/cmd/base" + "github.com/serverscom/srvctl/internal/output/entities" + "github.com/spf13/cobra" +) + +func NewCmd(cmdContext *base.CmdContext) *cobra.Command { + osOptionEntity, err := entities.Registry.GetEntityFromValue(serverscom.OperatingSystemOption{}) + if err != nil { + log.Fatal(err) + } + entitiesMap := make(map[string]entities.EntityInterface) + entitiesMap["server-os-options"] = osOptionEntity + cmd := &cobra.Command{ + Use: "server-os-options", + Short: "Manage server os options", + PersistentPreRunE: base.CombinePreRunE( + base.CheckFormatterFlags(cmdContext, entitiesMap), + base.CheckEmptyContexts(cmdContext), + ), + Args: base.NoArgs, + Run: base.UsageRun, + } + + cmd.AddCommand( + newListCmd(cmdContext), + newGetCmd(cmdContext), + ) + + base.AddFormatFlags(cmd) + + return cmd +} diff --git a/cmd/entities/server_os_options/serverosoptions_test.go b/cmd/entities/server_os_options/serverosoptions_test.go new file mode 100644 index 0000000..c7f96c0 --- /dev/null +++ b/cmd/entities/server_os_options/serverosoptions_test.go @@ -0,0 +1,227 @@ +package serverosoptions + +import ( + "errors" + "fmt" + "path/filepath" + "testing" + + . "github.com/onsi/gomega" + serverscom "github.com/serverscom/serverscom-go-client/pkg" + "github.com/serverscom/srvctl/cmd/testutils" + "github.com/serverscom/srvctl/internal/mocks" + "go.uber.org/mock/gomock" +) + +var ( + fixtureBasePath = filepath.Join("..", "..", "..", "testdata", "entities", "os-options") + testOSOptionID = int64(10) + testLocationID = int64(1) + testServerModelID = int64(100) + testOSOption = serverscom.OperatingSystemOption{ + ID: testOSOptionID, + FullName: "Ubuntu 18.04-server x86_64", + Name: "Ubuntu", + Version: "18.04-server", + Arch: "x86_64", + Filesystems: []string{"ext2", "ext4", "swap", "xfs", "reiser"}, + } +) + +func TestGetServerOperatingSystemOptionCmd(t *testing.T) { + testCases := []struct { + name string + id int64 + output string + flags []string + expectedOutput []byte + expectError bool + }{ + { + name: "get os option in default format", + id: testOSOptionID, + flags: []string{"--location-id", fmt.Sprint(testLocationID), "--server-model-id", fmt.Sprint(testServerModelID)}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "get.txt")), + }, + { + name: "get os option in JSON format", + id: testOSOptionID, + output: "json", + flags: []string{"--location-id", fmt.Sprint(testLocationID), "--server-model-id", fmt.Sprint(testServerModelID)}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "get.json")), + }, + { + name: "get os option in YAML format", + id: testOSOptionID, + output: "yaml", + flags: []string{"--location-id", fmt.Sprint(testLocationID), "--server-model-id", fmt.Sprint(testServerModelID)}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "get.yaml")), + }, + { + name: "get os option with service error", + id: testOSOptionID, + flags: []string{"--location-id", fmt.Sprint(testLocationID), "--server-model-id", fmt.Sprint(testServerModelID)}, + expectError: true, + }, + { + name: "get os option missing required flags", + id: testOSOptionID, + expectError: true, + }, + } + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + locationsServiceHandler := mocks.NewMockLocationsService(mockCtrl) + scClient := serverscom.NewClientWithEndpoint("", "") + scClient.Locations = locationsServiceHandler + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + g := NewWithT(t) + var err error + if tc.expectError && len(tc.flags) > 0 { + err = errors.New("some error") + } + if len(tc.flags) > 0 { + locationsServiceHandler.EXPECT(). + GetOperatingSystemOption(gomock.Any(), testLocationID, testServerModelID, tc.id). + Return(&testOSOption, err) + } + testCmdContext := testutils.NewTestCmdContext(scClient) + osCmd := NewCmd(testCmdContext) + args := []string{"server-os-options", "get", fmt.Sprint(tc.id)} + if len(tc.flags) > 0 { + args = append(args, tc.flags...) + } + if tc.output != "" { + args = append(args, "--output", tc.output) + } + builder := testutils.NewTestCommandBuilder(). + WithCommand(osCmd). + WithArgs(args) + cmd := builder.Build() + execErr := cmd.Execute() + if tc.expectError { + g.Expect(execErr).To(HaveOccurred()) + } else { + g.Expect(execErr).To(BeNil()) + g.Expect(builder.GetOutput()).To(BeEquivalentTo(string(tc.expectedOutput))) + } + }) + } +} + +func TestListServerOperatingSystemOptionsCmd(t *testing.T) { + o1 := testOSOption + o2 := testOSOption + o2.ID = testOSOptionID + 1 + o2.FullName = "CentOS 7 x86_64" + o2.Name = "CentOS" + o2.Version = "7" + testCases := []struct { + name string + output string + args []string + expectedOutput []byte + expectError bool + configureMock func(*mocks.MockCollection[serverscom.OperatingSystemOption]) + }{ + { + name: "list all os options", + output: "json", + args: []string{"-A", "--location-id", "1", "--server-model-id", "100"}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "list_all.json")), + configureMock: func(mock *mocks.MockCollection[serverscom.OperatingSystemOption]) { + mock.EXPECT(). + Collect(gomock.Any()). + Return([]serverscom.OperatingSystemOption{o1, o2}, nil) + }, + }, + { + name: "list os options", + output: "json", + args: []string{"--location-id", "1", "--server-model-id", "100"}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "list.json")), + configureMock: func(mock *mocks.MockCollection[serverscom.OperatingSystemOption]) { + mock.EXPECT(). + List(gomock.Any()). + Return([]serverscom.OperatingSystemOption{o1}, nil) + }, + }, + { + name: "list os options with template", + args: []string{"--template", "{{range .}}ID: {{.ID}} Arch: {{.Arch}}\\n{{end}}", "--location-id", "1", "--server-model-id", "100"}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "list_template.txt")), + configureMock: func(mock *mocks.MockCollection[serverscom.OperatingSystemOption]) { + mock.EXPECT(). + List(gomock.Any()). + Return([]serverscom.OperatingSystemOption{o1, o2}, nil) + }, + }, + { + name: "list os options with pageView", + args: []string{"--page-view", "--location-id", "1", "--server-model-id", "100"}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "list_pageview.txt")), + configureMock: func(mock *mocks.MockCollection[serverscom.OperatingSystemOption]) { + mock.EXPECT(). + List(gomock.Any()). + Return([]serverscom.OperatingSystemOption{o1, o2}, nil) + }, + }, + { + name: "list os options with error", + args: []string{"--location-id", "1", "--server-model-id", "100"}, + expectError: true, + configureMock: func(mock *mocks.MockCollection[serverscom.OperatingSystemOption]) { + mock.EXPECT(). + List(gomock.Any()). + Return(nil, errors.New("some error")) + }, + }, + { + name: "list os options missing required flags", + expectError: true, + }, + } + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + locationsServiceHandler := mocks.NewMockLocationsService(mockCtrl) + collectionHandler := mocks.NewMockCollection[serverscom.OperatingSystemOption](mockCtrl) + locationsServiceHandler.EXPECT(). + OperatingSystemOptions(gomock.Any(), gomock.Any()). + Return(collectionHandler). + AnyTimes() + collectionHandler.EXPECT(). + SetParam(gomock.Any(), gomock.Any()). + Return(collectionHandler). + AnyTimes() + scClient := serverscom.NewClientWithEndpoint("", "") + scClient.Locations = locationsServiceHandler + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + g := NewWithT(t) + if tc.configureMock != nil { + tc.configureMock(collectionHandler) + } + testCmdContext := testutils.NewTestCmdContext(scClient) + osCmd := NewCmd(testCmdContext) + args := []string{"server-os-options", "list"} + if len(tc.args) > 0 { + args = append(args, tc.args...) + } + if tc.output != "" { + args = append(args, "--output", tc.output) + } + builder := testutils.NewTestCommandBuilder(). + WithCommand(osCmd). + WithArgs(args) + cmd := builder.Build() + err := cmd.Execute() + if tc.expectError { + g.Expect(err).To(HaveOccurred()) + } else { + g.Expect(err).To(BeNil()) + g.Expect(builder.GetOutput()).To(BeEquivalentTo(string(tc.expectedOutput))) + } + }) + } +} diff --git a/cmd/entities/server_ram_options/list.go b/cmd/entities/server_ram_options/list.go new file mode 100644 index 0000000..970d070 --- /dev/null +++ b/cmd/entities/server_ram_options/list.go @@ -0,0 +1,30 @@ +package serverramoptions + +import ( + serverscom "github.com/serverscom/serverscom-go-client/pkg" + "github.com/serverscom/srvctl/cmd/base" + "github.com/spf13/cobra" +) + +func newListCmd(cmdContext *base.CmdContext) *cobra.Command { + var locationID int64 + var serverModelID int64 + + factory := func(verbose bool, args ...string) serverscom.Collection[serverscom.RAMOption] { + scClient := cmdContext.GetClient().SetVerbose(verbose).GetScClient() + return scClient.Locations.RAMOptions(locationID, serverModelID) + } + + opts := base.NewListOptions( + &base.BaseListOptions[serverscom.RAMOption]{}, + ) + + cmd := base.NewListCmd("list", "server-ram-options", factory, cmdContext, opts...) + + cmd.Flags().Int64Var(&locationID, "location-id", 0, "Location ID (required)") + cmd.Flags().Int64Var(&serverModelID, "server-model-id", 0, "Server model ID (required)") + _ = cmd.MarkFlagRequired("location-id") + _ = cmd.MarkFlagRequired("server-model-id") + + return cmd +} diff --git a/cmd/entities/server_ram_options/serverramoptions.go b/cmd/entities/server_ram_options/serverramoptions.go new file mode 100644 index 0000000..dcd074e --- /dev/null +++ b/cmd/entities/server_ram_options/serverramoptions.go @@ -0,0 +1,37 @@ +package serverramoptions + +import ( + "log" + + serverscom "github.com/serverscom/serverscom-go-client/pkg" + "github.com/serverscom/srvctl/cmd/base" + "github.com/serverscom/srvctl/internal/output/entities" + "github.com/spf13/cobra" +) + +func NewCmd(cmdContext *base.CmdContext) *cobra.Command { + ramOptionEntity, err := entities.Registry.GetEntityFromValue(serverscom.RAMOption{}) + if err != nil { + log.Fatal(err) + } + entitiesMap := make(map[string]entities.EntityInterface) + entitiesMap["server-ram-options"] = ramOptionEntity + cmd := &cobra.Command{ + Use: "server-ram-options", + Short: "List RAM options for a server model", + PersistentPreRunE: base.CombinePreRunE( + base.CheckFormatterFlags(cmdContext, entitiesMap), + base.CheckEmptyContexts(cmdContext), + ), + Args: base.NoArgs, + Run: base.UsageRun, + } + + cmd.AddCommand( + newListCmd(cmdContext), + ) + + base.AddFormatFlags(cmd) + + return cmd +} diff --git a/cmd/entities/server_ram_options/serverramoptions_test.go b/cmd/entities/server_ram_options/serverramoptions_test.go new file mode 100644 index 0000000..4e1c02e --- /dev/null +++ b/cmd/entities/server_ram_options/serverramoptions_test.go @@ -0,0 +1,140 @@ +package serverramoptions + +import ( + "errors" + "fmt" + "path/filepath" + "testing" + + . "github.com/onsi/gomega" + serverscom "github.com/serverscom/serverscom-go-client/pkg" + "github.com/serverscom/srvctl/cmd/testutils" + "github.com/serverscom/srvctl/internal/mocks" + "go.uber.org/mock/gomock" +) + +var ( + fixtureBasePath = filepath.Join("..", "..", "..", "testdata", "entities", "server-ram-options") + testRAMOption1 = serverscom.RAMOption{ + RAM: 16384, + Type: "DDR4", + } + testRAMOption2 = serverscom.RAMOption{ + RAM: 32768, + Type: "DDR4", + } + testLocationID = int64(1) + testServerModelID = int64(100) +) + +func TestListRAMOptionsCmd(t *testing.T) { + r1 := testRAMOption1 + r2 := testRAMOption2 + testCases := []struct { + name string + output string + args []string + expectedOutput []byte + expectError bool + configureMock func(*mocks.MockCollection[serverscom.RAMOption]) + }{ + { + name: "list all ram options", + output: "json", + args: []string{"-A", "--location-id", fmt.Sprint(testLocationID), "--server-model-id", fmt.Sprint(testServerModelID)}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "list_all.json")), + configureMock: func(mock *mocks.MockCollection[serverscom.RAMOption]) { + mock.EXPECT(). + Collect(gomock.Any()). + Return([]serverscom.RAMOption{r1, r2}, nil) + }, + }, + { + name: "list ram options", + output: "json", + args: []string{"--location-id", fmt.Sprint(testLocationID), "--server-model-id", fmt.Sprint(testServerModelID)}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "list.json")), + configureMock: func(mock *mocks.MockCollection[serverscom.RAMOption]) { + mock.EXPECT(). + List(gomock.Any()). + Return([]serverscom.RAMOption{r1}, nil) + }, + }, + { + name: "list ram options with template", + args: []string{"--template", "{{range .}}RAM: {{.RAM}} Type: {{.Type}}\\n{{end}}", "--location-id", fmt.Sprint(testLocationID), "--server-model-id", fmt.Sprint(testServerModelID)}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "list_template.txt")), + configureMock: func(mock *mocks.MockCollection[serverscom.RAMOption]) { + mock.EXPECT(). + List(gomock.Any()). + Return([]serverscom.RAMOption{r1, r2}, nil) + }, + }, + { + name: "list ram options with pageView", + args: []string{"--page-view", "--location-id", fmt.Sprint(testLocationID), "--server-model-id", fmt.Sprint(testServerModelID)}, + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "list_pageview.txt")), + configureMock: func(mock *mocks.MockCollection[serverscom.RAMOption]) { + mock.EXPECT(). + List(gomock.Any()). + Return([]serverscom.RAMOption{r1, r2}, nil) + }, + }, + { + name: "list ram options with error", + args: []string{"--location-id", fmt.Sprint(testLocationID), "--server-model-id", fmt.Sprint(testServerModelID)}, + expectError: true, + configureMock: func(mock *mocks.MockCollection[serverscom.RAMOption]) { + mock.EXPECT(). + List(gomock.Any()). + Return(nil, errors.New("some error")) + }, + }, + { + name: "list ram options missing required flags", + expectError: true, + }, + } + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + locationsServiceHandler := mocks.NewMockLocationsService(mockCtrl) + collectionHandler := mocks.NewMockCollection[serverscom.RAMOption](mockCtrl) + locationsServiceHandler.EXPECT(). + RAMOptions(gomock.Any(), gomock.Any()). + Return(collectionHandler). + AnyTimes() + collectionHandler.EXPECT(). + SetParam(gomock.Any(), gomock.Any()). + Return(collectionHandler). + AnyTimes() + scClient := serverscom.NewClientWithEndpoint("", "") + scClient.Locations = locationsServiceHandler + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + g := NewWithT(t) + if tc.configureMock != nil { + tc.configureMock(collectionHandler) + } + testCmdContext := testutils.NewTestCmdContext(scClient) + ramCmd := NewCmd(testCmdContext) + args := []string{"server-ram-options", "list"} + if len(tc.args) > 0 { + args = append(args, tc.args...) + } + if tc.output != "" { + args = append(args, "--output", tc.output) + } + builder := testutils.NewTestCommandBuilder(). + WithCommand(ramCmd). + WithArgs(args) + cmd := builder.Build() + err := cmd.Execute() + if tc.expectError { + g.Expect(err).To(HaveOccurred()) + } else { + g.Expect(err).To(BeNil()) + g.Expect(builder.GetOutput()).To(BeEquivalentTo(string(tc.expectedOutput))) + } + }) + } +} diff --git a/internal/output/entities/init.go b/internal/output/entities/init.go index e0e29a3..cb0495f 100644 --- a/internal/output/entities/init.go +++ b/internal/output/entities/init.go @@ -26,4 +26,7 @@ func init() { RegisterBandwidthOptionDefinition() RegisterDriveModelOptionDefinition() RegisterServerModelOptionDefinitions() + RegisterSBMModelOptionDefinition() + RegisterOperatingSystemOptionDefinition() + RegisterRAMOptionDefinition() } diff --git a/internal/output/entities/os_options.go b/internal/output/entities/os_options.go new file mode 100644 index 0000000..a26485b --- /dev/null +++ b/internal/output/entities/os_options.go @@ -0,0 +1,30 @@ +package entities + +import ( + "log" + "reflect" + + serverscom "github.com/serverscom/serverscom-go-client/pkg" +) + +var ( + OperatingSystemOptionType = reflect.TypeOf(serverscom.OperatingSystemOption{}) +) + +func RegisterOperatingSystemOptionDefinition() { + osOptionEntity := &Entity{ + fields: []Field{ + {ID: "ID", Name: "ID", Path: "ID", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true}, + {ID: "FullName", Name: "Full Name", Path: "FullName", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true}, + {ID: "Name", Name: "Name", Path: "Name", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true}, + {ID: "Version", Name: "Version", Path: "Version", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true}, + {ID: "Arch", Name: "Arch", Path: "Arch", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true}, + {ID: "Filesystems", Name: "Filesystems", Path: "Filesystems", PageViewHandlerFunc: slicePvHandler}, + }, + + eType: OperatingSystemOptionType, + } + if err := Registry.Register(osOptionEntity); err != nil { + log.Fatal(err) + } +} diff --git a/internal/output/entities/ram_options.go b/internal/output/entities/ram_options.go new file mode 100644 index 0000000..0d2e1db --- /dev/null +++ b/internal/output/entities/ram_options.go @@ -0,0 +1,26 @@ +package entities + +import ( + "log" + "reflect" + + serverscom "github.com/serverscom/serverscom-go-client/pkg" +) + +var ( + RAMOptionType = reflect.TypeOf(serverscom.RAMOption{}) +) + +func RegisterRAMOptionDefinition() { + ramOptionEntity := &Entity{ + fields: []Field{ + {ID: "RAM", Name: "RAM", Path: "RAM", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true}, + {ID: "Type", Name: "Type", Path: "Type", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true}, + }, + + eType: RAMOptionType, + } + if err := Registry.Register(ramOptionEntity); err != nil { + log.Fatal(err) + } +} diff --git a/internal/output/entities/sbm_models.go b/internal/output/entities/sbm_models.go new file mode 100644 index 0000000..2d31978 --- /dev/null +++ b/internal/output/entities/sbm_models.go @@ -0,0 +1,38 @@ +package entities + +import ( + "log" + "reflect" + + serverscom "github.com/serverscom/serverscom-go-client/pkg" +) + +var ( + SBMFlavorType = reflect.TypeOf(serverscom.SBMFlavor{}) +) + +func RegisterSBMModelOptionDefinition() { + sbmFlavorEntity := &Entity{ + fields: []Field{ + {ID: "ID", Name: "ID", Path: "ID", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true}, + {ID: "Name", Name: "Name", Path: "Name", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true}, + {ID: "CPUName", Name: "CPU Name", Path: "CPUName", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, + {ID: "CPUCount", Name: "CPU Count", Path: "CPUCount", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true}, + {ID: "CPUCoresCount", Name: "CPU Cores Count", Path: "CPUCoresCount", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true}, + {ID: "CPUFrequency", Name: "CPU Frequency", Path: "CPUFrequency", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, + {ID: "RAMSize", Name: "RAM Size", Path: "RAMSize", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true}, + {ID: "DrivesConfiguration", Name: "Drives Configuration", Path: "DrivesConfiguration", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, + {ID: "PublicUplinkModelID", Name: "Public Uplink Model ID", Path: "PublicUplinkModelID", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, + {ID: "PublicUplinkModelName", Name: "Public Uplink Model Name", Path: "PublicUplinkModelName", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, + {ID: "PrivateUplinkModelID", Name: "Private Uplink Model ID", Path: "PrivateUplinkModelID", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, + {ID: "PrivateUplinkModelName", Name: "Private Uplink Model Name", Path: "PrivateUplinkModelName", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, + {ID: "BandwidthID", Name: "Bandwidth ID", Path: "BandwidthID", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler}, + {ID: "BandwidthName", Name: "Bandwidth Name", Path: "BandwidthName", ListHandlerFunc: stringHandler, PageViewHandlerFunc: stringHandler, Default: true}, + }, + + eType: SBMFlavorType, + } + if err := Registry.Register(sbmFlavorEntity); err != nil { + log.Fatal(err) + } +} diff --git a/testdata/entities/os-options/get.json b/testdata/entities/os-options/get.json new file mode 100644 index 0000000..6ec9858 --- /dev/null +++ b/testdata/entities/os-options/get.json @@ -0,0 +1,14 @@ +{ + "id": 10, + "full_name": "Ubuntu 18.04-server x86_64", + "name": "Ubuntu", + "version": "18.04-server", + "arch": "x86_64", + "filesystems": [ + "ext2", + "ext4", + "swap", + "xfs", + "reiser" + ] +} \ No newline at end of file diff --git a/testdata/entities/os-options/get.txt b/testdata/entities/os-options/get.txt new file mode 100644 index 0000000..dfbfd44 --- /dev/null +++ b/testdata/entities/os-options/get.txt @@ -0,0 +1,2 @@ +ID Full Name Name Version Arch +10 Ubuntu 18.04-server x86_64 Ubuntu 18.04-server x86_64 diff --git a/testdata/entities/os-options/get.yaml b/testdata/entities/os-options/get.yaml new file mode 100644 index 0000000..766e506 --- /dev/null +++ b/testdata/entities/os-options/get.yaml @@ -0,0 +1,11 @@ +id: 10 +fullname: Ubuntu 18.04-server x86_64 +name: Ubuntu +version: 18.04-server +arch: x86_64 +filesystems: + - ext2 + - ext4 + - swap + - xfs + - reiser diff --git a/testdata/entities/os-options/list.json b/testdata/entities/os-options/list.json new file mode 100644 index 0000000..df659c6 --- /dev/null +++ b/testdata/entities/os-options/list.json @@ -0,0 +1,16 @@ +[ + { + "id": 10, + "full_name": "Ubuntu 18.04-server x86_64", + "name": "Ubuntu", + "version": "18.04-server", + "arch": "x86_64", + "filesystems": [ + "ext2", + "ext4", + "swap", + "xfs", + "reiser" + ] + } +] \ No newline at end of file diff --git a/testdata/entities/os-options/list_all.json b/testdata/entities/os-options/list_all.json new file mode 100644 index 0000000..9388410 --- /dev/null +++ b/testdata/entities/os-options/list_all.json @@ -0,0 +1,30 @@ +[ + { + "id": 10, + "full_name": "Ubuntu 18.04-server x86_64", + "name": "Ubuntu", + "version": "18.04-server", + "arch": "x86_64", + "filesystems": [ + "ext2", + "ext4", + "swap", + "xfs", + "reiser" + ] + }, + { + "id": 11, + "full_name": "CentOS 7 x86_64", + "name": "CentOS", + "version": "7", + "arch": "x86_64", + "filesystems": [ + "ext2", + "ext4", + "swap", + "xfs", + "reiser" + ] + } +] \ No newline at end of file diff --git a/testdata/entities/os-options/list_pageview.txt b/testdata/entities/os-options/list_pageview.txt new file mode 100644 index 0000000..be33d5c --- /dev/null +++ b/testdata/entities/os-options/list_pageview.txt @@ -0,0 +1,21 @@ +ID: 10 +Full Name: Ubuntu 18.04-server x86_64 +Name: Ubuntu +Version: 18.04-server +Arch: x86_64 +Filesystems: ext2 + ext4 + swap + xfs + reiser +--- +ID: 11 +Full Name: CentOS 7 x86_64 +Name: CentOS +Version: 7 +Arch: x86_64 +Filesystems: ext2 + ext4 + swap + xfs + reiser diff --git a/testdata/entities/os-options/list_template.txt b/testdata/entities/os-options/list_template.txt new file mode 100644 index 0000000..406d300 --- /dev/null +++ b/testdata/entities/os-options/list_template.txt @@ -0,0 +1,2 @@ +ID: 10 Arch: x86_64 +ID: 11 Arch: x86_64 diff --git a/testdata/entities/sbm-models/get.json b/testdata/entities/sbm-models/get.json new file mode 100644 index 0000000..45b223c --- /dev/null +++ b/testdata/entities/sbm-models/get.json @@ -0,0 +1,16 @@ +{ + "id": 10, + "name": "P-101", + "cpu_name": "cpu_name", + "cpu_count": 1, + "cpu_cores_count": 2, + "cpu_frequency": "3.8", + "ram_size": 4096, + "drives_configuration": "", + "public_uplink_model_id": 843, + "public_uplink_model_name": "uplink-model-name-30", + "private_uplink_model_id": 842, + "private_uplink_model_name": "uplink-model-name-29", + "bandwidth_id": 844, + "bandwidth_name": "public-bandwidth-model-35" +} \ No newline at end of file diff --git a/testdata/entities/sbm-models/get.txt b/testdata/entities/sbm-models/get.txt new file mode 100644 index 0000000..7c9c9a1 --- /dev/null +++ b/testdata/entities/sbm-models/get.txt @@ -0,0 +1,2 @@ +ID Name CPU Count CPU Cores Count RAM Size Bandwidth Name +10 P-101 1 2 4096 public-bandwidth-model-35 diff --git a/testdata/entities/sbm-models/get.yaml b/testdata/entities/sbm-models/get.yaml new file mode 100644 index 0000000..479b6e3 --- /dev/null +++ b/testdata/entities/sbm-models/get.yaml @@ -0,0 +1,14 @@ +id: 10 +name: P-101 +cpuname: cpu_name +cpucount: 1 +cpucorescount: 2 +cpufrequency: "3.8" +ramsize: 4096 +drivesconfiguration: "" +publicuplinkmodelid: 843 +publicuplinkmodelname: uplink-model-name-30 +privateuplinkmodelid: 842 +privateuplinkmodelname: uplink-model-name-29 +bandwidthid: 844 +bandwidthname: public-bandwidth-model-35 diff --git a/testdata/entities/sbm-models/list.json b/testdata/entities/sbm-models/list.json new file mode 100644 index 0000000..fc19dce --- /dev/null +++ b/testdata/entities/sbm-models/list.json @@ -0,0 +1,18 @@ +[ + { + "id": 10, + "name": "P-101", + "cpu_name": "cpu_name", + "cpu_count": 1, + "cpu_cores_count": 2, + "cpu_frequency": "3.8", + "ram_size": 4096, + "drives_configuration": "", + "public_uplink_model_id": 843, + "public_uplink_model_name": "uplink-model-name-30", + "private_uplink_model_id": 842, + "private_uplink_model_name": "uplink-model-name-29", + "bandwidth_id": 844, + "bandwidth_name": "public-bandwidth-model-35" + } +] \ No newline at end of file diff --git a/testdata/entities/sbm-models/list_all.json b/testdata/entities/sbm-models/list_all.json new file mode 100644 index 0000000..24f7af0 --- /dev/null +++ b/testdata/entities/sbm-models/list_all.json @@ -0,0 +1,34 @@ +[ + { + "id": 10, + "name": "P-101", + "cpu_name": "cpu_name", + "cpu_count": 1, + "cpu_cores_count": 2, + "cpu_frequency": "3.8", + "ram_size": 4096, + "drives_configuration": "", + "public_uplink_model_id": 843, + "public_uplink_model_name": "uplink-model-name-30", + "private_uplink_model_id": 842, + "private_uplink_model_name": "uplink-model-name-29", + "bandwidth_id": 844, + "bandwidth_name": "public-bandwidth-model-35" + }, + { + "id": 11, + "name": "P-102", + "cpu_name": "cpu_name", + "cpu_count": 1, + "cpu_cores_count": 2, + "cpu_frequency": "3.8", + "ram_size": 4096, + "drives_configuration": "", + "public_uplink_model_id": 843, + "public_uplink_model_name": "uplink-model-name-30", + "private_uplink_model_id": 842, + "private_uplink_model_name": "uplink-model-name-29", + "bandwidth_id": 844, + "bandwidth_name": "public-bandwidth-model-35" + } +] \ No newline at end of file diff --git a/testdata/entities/sbm-models/list_pageview.txt b/testdata/entities/sbm-models/list_pageview.txt new file mode 100644 index 0000000..35c8dfc --- /dev/null +++ b/testdata/entities/sbm-models/list_pageview.txt @@ -0,0 +1,29 @@ +ID: 10 +Name: P-101 +CPU Name: cpu_name +CPU Count: 1 +CPU Cores Count: 2 +CPU Frequency: 3.8 +RAM Size: 4096 +Drives Configuration: +Public Uplink Model ID: 843 +Public Uplink Model Name: uplink-model-name-30 +Private Uplink Model ID: 842 +Private Uplink Model Name: uplink-model-name-29 +Bandwidth ID: 844 +Bandwidth Name: public-bandwidth-model-35 +--- +ID: 11 +Name: P-102 +CPU Name: cpu_name +CPU Count: 1 +CPU Cores Count: 2 +CPU Frequency: 3.8 +RAM Size: 4096 +Drives Configuration: +Public Uplink Model ID: 843 +Public Uplink Model Name: uplink-model-name-30 +Private Uplink Model ID: 842 +Private Uplink Model Name: uplink-model-name-29 +Bandwidth ID: 844 +Bandwidth Name: public-bandwidth-model-35 diff --git a/testdata/entities/sbm-models/list_template.txt b/testdata/entities/sbm-models/list_template.txt new file mode 100644 index 0000000..6708331 --- /dev/null +++ b/testdata/entities/sbm-models/list_template.txt @@ -0,0 +1,2 @@ +ID: 10 Name: P-101 +ID: 11 Name: P-102 diff --git a/testdata/entities/server-ram-options/list.json b/testdata/entities/server-ram-options/list.json new file mode 100644 index 0000000..a02d637 --- /dev/null +++ b/testdata/entities/server-ram-options/list.json @@ -0,0 +1,6 @@ +[ + { + "ram": 16384, + "type": "DDR4" + } +] \ No newline at end of file diff --git a/testdata/entities/server-ram-options/list_all.json b/testdata/entities/server-ram-options/list_all.json new file mode 100644 index 0000000..20690ae --- /dev/null +++ b/testdata/entities/server-ram-options/list_all.json @@ -0,0 +1,10 @@ +[ + { + "ram": 16384, + "type": "DDR4" + }, + { + "ram": 32768, + "type": "DDR4" + } +] \ No newline at end of file diff --git a/testdata/entities/server-ram-options/list_pageview.txt b/testdata/entities/server-ram-options/list_pageview.txt new file mode 100644 index 0000000..65904da --- /dev/null +++ b/testdata/entities/server-ram-options/list_pageview.txt @@ -0,0 +1,5 @@ +RAM: 16384 +Type: DDR4 +--- +RAM: 32768 +Type: DDR4 diff --git a/testdata/entities/server-ram-options/list_template.txt b/testdata/entities/server-ram-options/list_template.txt new file mode 100644 index 0000000..d961294 --- /dev/null +++ b/testdata/entities/server-ram-options/list_template.txt @@ -0,0 +1,2 @@ +RAM: 16384 Type: DDR4 +RAM: 32768 Type: DDR4