Skip to content
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
37 changes: 21 additions & 16 deletions internal/namespaces/baremetal/v1/custom_offer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"github.com/scaleway/scaleway-cli/v2/core"
"github.com/scaleway/scaleway-cli/v2/core/human"
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
product_catalog "github.com/scaleway/scaleway-sdk-go/api/product_catalog/v2alpha1"
productcatalog "github.com/scaleway/scaleway-sdk-go/api/product_catalog/v2alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

var offerAvailabilityMarshalSpecs = human.EnumMarshalSpecs{
Expand Down Expand Up @@ -84,30 +85,28 @@ func serverOfferListBuilder(c *core.Command) *core.Command {

c.Interceptor = func(ctx context.Context, argsI any, runner core.CommandRunner) (any, error) {
req := argsI.(*baremetal.ListOffersRequest)
rawResp, err := runner(ctx, argsI)
if err != nil {
return nil, err
}

offers, _ := rawResp.([]*baremetal.Offer)
client := core.ExtractClient(ctx)
baremetalAPI := baremetal.NewAPI(client)
offers, _ := baremetalAPI.ListOffers(req, scw.WithAllPages())

productAPI := product_catalog.NewPublicCatalogAPI(client)
productAPI := productcatalog.NewPublicCatalogAPI(client)
environmentalImpact, _ := productAPI.ListPublicCatalogProducts(
&product_catalog.PublicCatalogAPIListPublicCatalogProductsRequest{
ProductTypes: []product_catalog.ListPublicCatalogProductsRequestProductType{
product_catalog.ListPublicCatalogProductsRequestProductTypeElasticMetal,
&productcatalog.PublicCatalogAPIListPublicCatalogProductsRequest{
ProductTypes: []productcatalog.ListPublicCatalogProductsRequestProductType{
productcatalog.ListPublicCatalogProductsRequestProductTypeElasticMetal,
},
Zone: &req.Zone,
},
scw.WithAllPages(),
)

unitOfMeasure := product_catalog.PublicCatalogProductUnitOfMeasureCountableUnitMonth
if req.SubscriptionPeriod == "hour" {
unitOfMeasure = product_catalog.PublicCatalogProductUnitOfMeasureCountableUnitHour
unitOfMeasure := productcatalog.PublicCatalogProductUnitOfMeasureCountableUnitHour
if req.SubscriptionPeriod == "month" {
unitOfMeasure = productcatalog.PublicCatalogProductUnitOfMeasureCountableUnitMonth
}

impactMap := make(map[string]*product_catalog.PublicCatalogProduct)
impactMap := make(map[string]*productcatalog.PublicCatalogProduct)
for _, impact := range environmentalImpact.Products {
if impact != nil && impact.UnitOfMeasure.Unit == unitOfMeasure {
key := strings.TrimSpace(strings.TrimPrefix(impact.Product, "Elastic Metal "))
Expand All @@ -116,9 +115,15 @@ func serverOfferListBuilder(c *core.Command) *core.Command {
}

var customOfferRes []customOffer
for _, offer := range offers {
for _, offer := range offers.Offers {
impact, ok := impactMap[offer.Name]
if !ok || impact == nil {
if !ok {
customOfferRes = append(customOfferRes, customOffer{
Offer: offer,
KgCo2Equivalent: nil,
M3WaterUsage: nil,
})

continue
}
customOfferRes = append(customOfferRes, customOffer{
Expand Down
2 changes: 1 addition & 1 deletion internal/namespaces/baremetal/v1/custom_offer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Test_baremetalGetOffer(t *testing.T) {
}

func Test_baremetalListOffer(t *testing.T) {
t.Run("Monthly", core.Test(&core.TestConfig{
t.Run("Simple", core.Test(&core.TestConfig{
Commands: baremetal.GetCommands(),
Cmd: "scw baremetal offer list",
Check: core.TestCheckGolden(),
Expand Down
31 changes: 19 additions & 12 deletions internal/namespaces/baremetal/v1/custom_server_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ import (
"github.com/scaleway/scaleway-cli/v2/core"
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/baremetal/v1"
baremetalSDK "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/stretchr/testify/assert"
)

var (
offerNameNVME = getenv("OFFER_NAME_NVME", "EM-I215E-NVME")
offerNameSATA = getenv("OFFER_NAME_SATA", "EM-B111X-SATA")
zone = getenv("zone", "fr-par-2")
)

// All test below should succeed to create an instance.
func Test_CreateServer(t *testing.T) {
// Simple use cases
Expand All @@ -19,22 +26,22 @@ func Test_CreateServer(t *testing.T) {
BeforeFunc: func(ctx *core.BeforeFuncCtx) error {
api := baremetalSDK.NewAPI(ctx.Client)
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
OfferName: offerName,
Zone: region,
OfferName: offerNameNVME,
Zone: scw.Zone(zone),
})
if server.Stock != baremetalSDK.OfferStockAvailable {
return errors.New("offer out of stock")
}

return nil
},
Cmd: "scw baremetal server create zone=" + region + " type=" + offerName + " -w",
Cmd: "scw baremetal server create zone=" + zone + " type=" + offerNameNVME + " -w",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
AfterFunc: core.ExecAfterCmd(
"scw baremetal server delete {{ .CmdResult.ID }} zone=" + region,
"scw baremetal server delete {{ .CmdResult.ID }} zone=" + zone,
),
},
))
Expand All @@ -44,16 +51,16 @@ func Test_CreateServer(t *testing.T) {
BeforeFunc: func(ctx *core.BeforeFuncCtx) error {
api := baremetalSDK.NewAPI(ctx.Client)
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
OfferName: offerName,
Zone: region,
OfferName: offerNameNVME,
Zone: scw.Zone(zone),
})
if server.Stock != baremetalSDK.OfferStockAvailable {
return errors.New("offer out of stock")
}

return nil
},
Cmd: "scw baremetal server create name=test-create-server-with-name zone=" + region + " type=" + offerName + " -w",
Cmd: "scw baremetal server create name=test-create-server-with-name zone=" + zone + " type=" + offerNameNVME + " -w",
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
Expand All @@ -66,7 +73,7 @@ func Test_CreateServer(t *testing.T) {
core.TestCheckExitCode(0),
),
AfterFunc: core.ExecAfterCmd(
"scw baremetal server delete {{ .CmdResult.ID }} zone=" + region,
"scw baremetal server delete {{ .CmdResult.ID }} zone=" + zone,
),
}))

Expand All @@ -75,16 +82,16 @@ func Test_CreateServer(t *testing.T) {
BeforeFunc: func(ctx *core.BeforeFuncCtx) error {
api := baremetalSDK.NewAPI(ctx.Client)
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
OfferName: offerName,
Zone: region,
OfferName: offerNameNVME,
Zone: scw.Zone(zone),
})
if server.Stock != baremetalSDK.OfferStockAvailable {
return errors.New("offer out of stock")
}

return nil
},
Cmd: "scw baremetal server create tags.0=prod tags.1=blue zone=" + region + " type=" + offerName + " -w",
Cmd: "scw baremetal server create tags.0=prod tags.1=blue zone=" + zone + " type=" + offerNameNVME + " -w",
Check: core.TestCheckCombine(
func(t *testing.T, ctx *core.CheckFuncCtx) {
t.Helper()
Expand All @@ -94,7 +101,7 @@ func Test_CreateServer(t *testing.T) {
core.TestCheckExitCode(0),
),
AfterFunc: core.ExecAfterCmd(
"scw baremetal server delete {{ .CmdResult.ID }} zone=" + region,
"scw baremetal server delete {{ .CmdResult.ID }} zone=" + zone,
),
}))
})
Expand Down
25 changes: 13 additions & 12 deletions internal/namespaces/baremetal/v1/custom_server_fip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/baremetal/v1"
flexibleip "github.com/scaleway/scaleway-cli/v2/internal/namespaces/flexibleip/v1alpha1"
baremetalSDK "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func Test_CreateFlexibleIPInteractive(t *testing.T) {
Expand All @@ -24,24 +25,24 @@ func Test_CreateFlexibleIPInteractive(t *testing.T) {
func(ctx *core.BeforeFuncCtx) error {
api := baremetalSDK.NewAPI(ctx.Client)
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
OfferName: offerName,
Zone: region,
OfferName: offerNameNVME,
Zone: scw.Zone(zone),
})
if server.Stock != baremetalSDK.OfferStockAvailable {
return errors.New("offer out of stock")
}

return nil
},
createServerAndWaitDefault("Server"),
createServerAndWait(),
),
Cmd: "scw baremetal server add-flexible-ip {{ .Server.ID }}",
Cmd: "scw baremetal server add-flexible-ip {{ .Server.ID }} zone=" + zone,
Check: core.TestCheckCombine(
core.TestCheckGolden(),
),
AfterFunc: core.AfterFuncCombine(
deleteServerDefault("Server"),
core.ExecAfterCmd("scw fip ip delete {{ .CmdResult.ID }}"),
deleteServer("Server"),
core.ExecAfterCmd("scw fip ip delete {{ .CmdResult.ID }} zone="+zone),
),
PromptResponseMocks: promptResponse,
}))
Expand All @@ -57,24 +58,24 @@ func Test_CreateFlexibleIP(t *testing.T) {
func(ctx *core.BeforeFuncCtx) error {
api := baremetalSDK.NewAPI(ctx.Client)
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
OfferName: offerName,
Zone: region,
OfferName: offerNameNVME,
Zone: scw.Zone(zone),
})
if server.Stock != baremetalSDK.OfferStockAvailable {
return errors.New("offer out of stock")
}

return nil
},
createServerAndWaitDefault("Server"),
createServerAndWait(),
),
Cmd: "scw baremetal server add-flexible-ip {{ .Server.ID }} ip-type=IPv4",
Cmd: "scw baremetal server add-flexible-ip {{ .Server.ID }} ip-type=IPv4 zone=" + zone,
Check: core.TestCheckCombine(
core.TestCheckGolden(),
),
AfterFunc: core.AfterFuncCombine(
deleteServerDefault("Server"),
core.ExecAfterCmd("scw fip ip delete {{ .CmdResult.ID }}"),
deleteServer("Server"),
core.ExecAfterCmd("scw fip ip delete {{ .CmdResult.ID }} zone="+zone),
),
}))
}
17 changes: 9 additions & 8 deletions internal/namespaces/baremetal/v1/custom_server_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/baremetal/v1"
iam "github.com/scaleway/scaleway-cli/v2/internal/namespaces/iam/v1alpha1"
baremetalSDK "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func Test_InstallServer(t *testing.T) {
Expand All @@ -24,8 +25,8 @@ func Test_InstallServer(t *testing.T) {
func(ctx *core.BeforeFuncCtx) error {
api := baremetalSDK.NewAPI(ctx.Client)
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
OfferName: offerName,
Zone: region,
OfferName: offerNameNVME,
Zone: scw.Zone(zone),
})
if server.Stock != baremetalSDK.OfferStockAvailable {
err := errors.New("offer out of stock")
Expand All @@ -36,10 +37,10 @@ func Test_InstallServer(t *testing.T) {
return nil
},
addSSH("key", sshKey),
createServerAndWait("Server"),
createServerAndWait(),
),
Commands: cmds,
Cmd: "scw baremetal server install {{ .Server.ID }} zone=" + region + " hostname=test-install-server ssh-key-ids.0={{ .key.ID }} os-id=" + osID + " -w",
Cmd: "scw baremetal server install {{ .Server.ID }} zone=" + zone + " hostname=test-install-server ssh-key-ids.0={{ .key.ID }} os-id=" + osID + " -w",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
Expand All @@ -56,8 +57,8 @@ func Test_InstallServer(t *testing.T) {
func(ctx *core.BeforeFuncCtx) error {
api := baremetalSDK.NewAPI(ctx.Client)
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
OfferName: offerName,
Zone: region,
OfferName: offerNameNVME,
Zone: scw.Zone(zone),
})
if server.Stock != baremetalSDK.OfferStockAvailable {
return errors.New("offer out of stock")
Expand All @@ -66,9 +67,9 @@ func Test_InstallServer(t *testing.T) {
return nil
},
addSSH("key", sshKey),
createServerAndWait("Server"),
createServerAndWait(),
),
Cmd: "scw baremetal server install {{ .Server.ID }} zone=" + region + " hostname=test-install-server all-ssh-keys=true os-id=" + osID + " -w",
Cmd: "scw baremetal server install {{ .Server.ID }} zone=" + zone + " hostname=test-install-server all-ssh-keys=true os-id=" + osID + " -w",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
Expand Down
29 changes: 12 additions & 17 deletions internal/namespaces/baremetal/v1/custom_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,25 @@ import (
"github.com/scaleway/scaleway-sdk-go/scw"
)

const (
offerName = "EM-B220E-NVME"
region = "fr-par-1"
)

func Test_StartServerErrors(t *testing.T) {
t.Run("Error: cannot be started while not delivered", core.Test(&core.TestConfig{
BeforeFunc: core.BeforeFuncCombine(
func(ctx *core.BeforeFuncCtx) error {
api := baremetalSDK.NewAPI(ctx.Client)
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
OfferName: "EM-B111X-SATA",
Zone: region,
OfferName: offerNameSATA,
Zone: scw.Zone(zone),
})
if server.Stock != baremetalSDK.OfferStockAvailable {
return errors.New("offer out of stock")
}

return nil
},
createServer("Server", "EM-B111X-SATA"),
createServer("Server", offerNameSATA),
),
Commands: baremetal.GetCommands(),
Cmd: "scw baremetal server start zone=" + region + " {{ .Server.ID }}",
Cmd: "scw baremetal server start zone=" + zone + " {{ .Server.ID }}",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
Expand Down Expand Up @@ -62,19 +57,19 @@ func Test_StopServerErrors(t *testing.T) {
func(ctx *core.BeforeFuncCtx) error {
api := baremetalSDK.NewAPI(ctx.Client)
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
OfferName: "EM-B111X-SATA",
Zone: region,
OfferName: offerNameSATA,
Zone: scw.Zone(zone),
})
if server.Stock != baremetalSDK.OfferStockAvailable {
return errors.New("offer out of stock")
}

return nil
},
createServer("Server", "EM-B111X-SATA"),
createServer("Server", offerNameSATA),
),
Commands: baremetal.GetCommands(),
Cmd: "scw baremetal server stop zone=" + region + " {{ .Server.ID }}",
Cmd: "scw baremetal server stop zone=" + zone + " {{ .Server.ID }}",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
Expand Down Expand Up @@ -103,19 +98,19 @@ func Test_RebootServerErrors(t *testing.T) {
func(ctx *core.BeforeFuncCtx) error {
api := baremetalSDK.NewAPI(ctx.Client)
server, _ := api.GetOfferByName(&baremetalSDK.GetOfferByNameRequest{
OfferName: "EM-B111X-SATA",
Zone: region,
OfferName: offerNameSATA,
Zone: scw.Zone(zone),
})
if server.Stock != baremetalSDK.OfferStockAvailable {
return errors.New("offer out of stock")
}

return nil
},
createServer("Server", "EM-B111X-SATA"),
createServer("Server", offerNameSATA),
),
Commands: baremetal.GetCommands(),
Cmd: "scw baremetal server reboot zone=" + region + " {{ .Server.ID }}",
Cmd: "scw baremetal server reboot zone=" + zone + " {{ .Server.ID }}",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
Expand Down
Loading
Loading