Skip to content

Commit bcf357d

Browse files
author
Quentin Perez
committed
Merge pull request #256 from QuentinPerez/fix_UCHAR_MAX
Add region and arch field for images
2 parents 5880469 + cc41e3f commit bcf357d

31 files changed

+471
-380
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,9 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
11521152

11531153
### master (unreleased)
11541154

1155+
* Match bootscript/image with the good architecture ([#255](https://github.com/scaleway/scaleway-cli/issues/255))
1156+
* Support of region/owner/arch in the cache file ([#255](https://github.com/scaleway/scaleway-cli/issues/255))
1157+
* Remove some `fatal` and `Exit`
11551158
* Use rfc4716 (openSSH) to generate the fingerprints ([#151](https://github.com/scaleway/scaleway-cli/issues/151))
11561159
* Switch from `Party` to `Godep`
11571160
* create-image-from-http.sh: Support HTTP proxy ([#249](https://github.com/scaleway/scaleway-cli/issues/249))

pkg/api/api.go

Lines changed: 79 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ type ScalewayImage struct {
233233
// FIXME: extra_volumes
234234
}
235235

236+
// ScalewayImageIdentifier represents a Scaleway Image Identifier
237+
type ScalewayImageIdentifier struct {
238+
Identifier string
239+
Arch string
240+
Region string
241+
Owner string
242+
}
243+
236244
// ScalewayOneImage represents the response of a GET /images/UUID API call
237245
type ScalewayOneImage struct {
238246
Image ScalewayImage `json:"image,omitempty"`
@@ -323,6 +331,12 @@ type ScalewayKernel struct {
323331

324332
// ScalewayBootscript represents a Scaleway Bootscript
325333
type ScalewayBootscript struct {
334+
// Arch is the architecture target of the bootscript
335+
Arch string `json:"architecture,omitempty"`
336+
337+
// Organization is the owner of the bootscript
338+
Organization string `json:"organization,omitempty"`
339+
326340
// Identifier is a unique identifier for the bootscript
327341
Identifier string `json:"id,omitempty"`
328342

@@ -477,6 +491,9 @@ type ScalewayNewSecurityGroup struct {
477491

478492
// ScalewayServer represents a Scaleway C1 server
479493
type ScalewayServer struct {
494+
// Arch is the architecture target of the server
495+
Arch string `json:"arch,omitempty"`
496+
480497
// Identifier is a unique identifier for the server
481498
Identifier string `json:"id,omitempty"`
482499

@@ -539,6 +556,7 @@ type ScalewayServer struct {
539556

540557
// ScalewayServerPatchDefinition represents a Scaleway C1 server with nullable fields (for PATCH)
541558
type ScalewayServerPatchDefinition struct {
559+
Arch *string `json:"arch,omitempty"`
542560
Name *string `json:"name,omitempty"`
543561
CreationDate *string `json:"creation_date,omitempty"`
544562
ModificationDate *string `json:"modification_date,omitempty"`
@@ -1003,7 +1021,8 @@ func (s *ScalewayAPI) GetServers(all bool, limit int) (*[]ScalewayServer, error)
10031021
return nil, err
10041022
}
10051023
for _, server := range servers.Servers {
1006-
s.Cache.InsertServer(server.Identifier, server.Name)
1024+
// FIXME region, arch, owner, title
1025+
s.Cache.InsertServer(server.Identifier, "fr-1", server.Arch, server.Organization, server.Name)
10071026
}
10081027
// FIXME: when API limit is ready, remove the following code
10091028
if limit > 0 && limit < len(servers.Servers) {
@@ -1030,7 +1049,8 @@ func (s *ScalewayAPI) GetServer(serverID string) (*ScalewayServer, error) {
10301049
if err = json.Unmarshal(body, &oneServer); err != nil {
10311050
return nil, err
10321051
}
1033-
s.Cache.InsertServer(oneServer.Server.Identifier, oneServer.Server.Name)
1052+
// FIXME region, arch, owner, title
1053+
s.Cache.InsertServer(oneServer.Server.Identifier, "fr-1", oneServer.Server.Arch, oneServer.Server.Organization, oneServer.Server.Name)
10341054
return &oneServer.Server, nil
10351055
}
10361056

@@ -1083,7 +1103,8 @@ func (s *ScalewayAPI) PostServer(definition ScalewayServerDefinition) (string, e
10831103
if err = json.Unmarshal(body, &server); err != nil {
10841104
return "", err
10851105
}
1086-
s.Cache.InsertServer(server.Server.Identifier, server.Server.Name)
1106+
// FIXME region, arch, owner, title
1107+
s.Cache.InsertServer(server.Server.Identifier, "fr-1", server.Server.Arch, server.Server.Organization, server.Server.Name)
10871108
return server.Server.Identifier, nil
10881109
}
10891110

@@ -1139,7 +1160,8 @@ func (s *ScalewayAPI) PostSnapshot(volumeID string, name string) (string, error)
11391160
if err = json.Unmarshal(body, &snapshot); err != nil {
11401161
return "", err
11411162
}
1142-
s.Cache.InsertSnapshot(snapshot.Snapshot.Identifier, snapshot.Snapshot.Name)
1163+
// FIXME region, arch, owner, title
1164+
s.Cache.InsertSnapshot(snapshot.Snapshot.Identifier, "fr-1", "", snapshot.Snapshot.Organization, snapshot.Snapshot.Name)
11431165
return snapshot.Snapshot.Identifier, nil
11441166
}
11451167

@@ -1170,7 +1192,8 @@ func (s *ScalewayAPI) PostImage(volumeID string, name string, bootscript string,
11701192
if err = json.Unmarshal(body, &image); err != nil {
11711193
return "", err
11721194
}
1173-
s.Cache.InsertImage(image.Image.Identifier, image.Image.Name)
1195+
// FIXME region, arch, owner, title
1196+
s.Cache.InsertImage(image.Image.Identifier, "fr-1", image.Image.Arch, image.Image.Organization, image.Image.Name)
11741197
return image.Image.Identifier, nil
11751198
}
11761199

@@ -1280,7 +1303,8 @@ func (s *ScalewayAPI) GetImages() (*[]ScalewayImage, error) {
12801303
return nil, err
12811304
}
12821305
for _, image := range images.Images {
1283-
s.Cache.InsertImage(image.Identifier, image.Name)
1306+
// FIXME region, arch, owner, title
1307+
s.Cache.InsertImage(image.Identifier, "fr-1", image.Arch, image.Organization, image.Name)
12841308
}
12851309
return &images.Images, nil
12861310
}
@@ -1302,7 +1326,8 @@ func (s *ScalewayAPI) GetImage(imageID string) (*ScalewayImage, error) {
13021326
if err = json.Unmarshal(body, &oneImage); err != nil {
13031327
return nil, err
13041328
}
1305-
s.Cache.InsertImage(oneImage.Image.Identifier, oneImage.Image.Name)
1329+
// FIXME region, arch, owner, title
1330+
s.Cache.InsertImage(oneImage.Image.Identifier, "fr-1", oneImage.Image.Arch, oneImage.Image.Organization, oneImage.Image.Name)
13061331
return &oneImage.Image, nil
13071332
}
13081333

@@ -1343,7 +1368,8 @@ func (s *ScalewayAPI) GetSnapshots() (*[]ScalewaySnapshot, error) {
13431368
return nil, err
13441369
}
13451370
for _, snapshot := range snapshots.Snapshots {
1346-
s.Cache.InsertSnapshot(snapshot.Identifier, snapshot.Name)
1371+
// FIXME region, arch, owner, title
1372+
s.Cache.InsertSnapshot(snapshot.Identifier, "fr-1", "", snapshot.Organization, snapshot.Name)
13471373
}
13481374
return &snapshots.Snapshots, nil
13491375
}
@@ -1365,7 +1391,8 @@ func (s *ScalewayAPI) GetSnapshot(snapshotID string) (*ScalewaySnapshot, error)
13651391
if err = json.Unmarshal(body, &oneSnapshot); err != nil {
13661392
return nil, err
13671393
}
1368-
s.Cache.InsertSnapshot(oneSnapshot.Snapshot.Identifier, oneSnapshot.Snapshot.Name)
1394+
// FIXME region, arch, owner, title
1395+
s.Cache.InsertSnapshot(oneSnapshot.Snapshot.Identifier, "fr-1", "", oneSnapshot.Snapshot.Organization, oneSnapshot.Snapshot.Name)
13691396
return &oneSnapshot.Snapshot, nil
13701397
}
13711398

@@ -1390,7 +1417,8 @@ func (s *ScalewayAPI) GetVolumes() (*[]ScalewayVolume, error) {
13901417
return nil, err
13911418
}
13921419
for _, volume := range volumes.Volumes {
1393-
s.Cache.InsertVolume(volume.Identifier, volume.Name)
1420+
// FIXME region, arch, owner, title
1421+
s.Cache.InsertVolume(volume.Identifier, "fr-1", "", volume.Organization, volume.Name)
13941422
}
13951423
return &volumes.Volumes, nil
13961424
}
@@ -1412,7 +1440,8 @@ func (s *ScalewayAPI) GetVolume(volumeID string) (*ScalewayVolume, error) {
14121440
if err = json.Unmarshal(body, &oneVolume); err != nil {
14131441
return nil, err
14141442
}
1415-
s.Cache.InsertVolume(oneVolume.Volume.Identifier, oneVolume.Volume.Name)
1443+
// FIXME region, arch, owner, title
1444+
s.Cache.InsertVolume(oneVolume.Volume.Identifier, "fr-1", "", oneVolume.Volume.Organization, oneVolume.Volume.Name)
14161445
return &oneVolume.Volume, nil
14171446
}
14181447

@@ -1436,7 +1465,8 @@ func (s *ScalewayAPI) GetBootscripts() (*[]ScalewayBootscript, error) {
14361465
return nil, err
14371466
}
14381467
for _, bootscript := range bootscripts.Bootscripts {
1439-
s.Cache.InsertBootscript(bootscript.Identifier, bootscript.Title)
1468+
// FIXME region, arch, owner, title
1469+
s.Cache.InsertBootscript(bootscript.Identifier, "fr-1", bootscript.Arch, bootscript.Organization, bootscript.Title)
14401470
}
14411471
return &bootscripts.Bootscripts, nil
14421472
}
@@ -1458,7 +1488,8 @@ func (s *ScalewayAPI) GetBootscript(bootscriptID string) (*ScalewayBootscript, e
14581488
if err = json.Unmarshal(body, &oneBootscript); err != nil {
14591489
return nil, err
14601490
}
1461-
s.Cache.InsertBootscript(oneBootscript.Bootscript.Identifier, oneBootscript.Bootscript.Title)
1491+
// FIXME region, arch, owner, title
1492+
s.Cache.InsertBootscript(oneBootscript.Bootscript.Identifier, "fr-1", oneBootscript.Bootscript.Arch, oneBootscript.Bootscript.Organization, oneBootscript.Bootscript.Title)
14621493
return &oneBootscript.Bootscript, nil
14631494
}
14641495

@@ -1736,82 +1767,81 @@ func (s *ScalewayAPI) GetDashboard() (*ScalewayDashboard, error) {
17361767
}
17371768

17381769
// GetServerID returns exactly one server matching or dies
1739-
func (s *ScalewayAPI) GetServerID(needle string) string {
1770+
func (s *ScalewayAPI) GetServerID(needle string) (string, error) {
17401771
// Parses optional type prefix, i.e: "server:name" -> "name"
17411772
_, needle = parseNeedle(needle)
17421773

17431774
servers, err := s.ResolveServer(needle)
17441775
if err != nil {
1745-
log.Fatalf("Unable to resolve server %s: %s", needle, err)
1776+
return "", fmt.Errorf("Unable to resolve server %s: %s", needle, err)
17461777
}
17471778
if len(servers) == 1 {
1748-
return servers[0].Identifier
1779+
return servers[0].Identifier, nil
17491780
}
17501781
if len(servers) == 0 {
1751-
log.Fatalf("No such server: %s", needle)
1782+
return "", fmt.Errorf("No such server: %s", needle)
17521783
}
1753-
1754-
showResolverResults(needle, servers)
1755-
os.Exit(1)
1756-
return ""
1784+
return "", showResolverResults(needle, servers)
17571785
}
17581786

17591787
func showResolverResults(needle string, results ScalewayResolverResults) error {
1760-
log.Errorf("Too many candidates for %s (%d)", needle, len(results))
1761-
1788+
arch := ""
17621789
w := tabwriter.NewWriter(os.Stderr, 20, 1, 3, ' ', 0)
17631790
defer w.Flush()
17641791
sort.Sort(results)
17651792
for _, result := range results {
1766-
fmt.Fprintf(w, "- %s\t%s\t%s\n", result.TruncIdentifier(), result.CodeName(), result.Name)
1793+
arch = result.Arch
1794+
if arch == "" {
1795+
arch = "n/a"
1796+
}
1797+
fmt.Fprintf(w, "- %s\t%s\t%s\t%s\n", result.TruncIdentifier(), result.CodeName(), result.Name, arch)
17671798
}
1768-
return nil
1799+
return fmt.Errorf("Too many candidates for %s (%d)", needle, len(results))
17691800
}
17701801

17711802
// GetSnapshotID returns exactly one snapshot matching or dies
1772-
func (s *ScalewayAPI) GetSnapshotID(needle string) string {
1803+
func (s *ScalewayAPI) GetSnapshotID(needle string) (string, error) {
17731804
// Parses optional type prefix, i.e: "snapshot:name" -> "name"
17741805
_, needle = parseNeedle(needle)
17751806

17761807
snapshots, err := s.ResolveSnapshot(needle)
17771808
if err != nil {
1778-
log.Fatalf("Unable to resolve snapshot %s: %s", needle, err)
1809+
return "", fmt.Errorf("Unable to resolve snapshot %s: %s", needle, err)
17791810
}
17801811
if len(snapshots) == 1 {
1781-
return snapshots[0].Identifier
1812+
return snapshots[0].Identifier, nil
17821813
}
17831814
if len(snapshots) == 0 {
1784-
log.Fatalf("No such snapshot: %s", needle)
1815+
return "", fmt.Errorf("No such snapshot: %s", needle)
17851816
}
1786-
1787-
showResolverResults(needle, snapshots)
1788-
os.Exit(1)
1789-
return ""
1817+
return "", showResolverResults(needle, snapshots)
17901818
}
17911819

17921820
// GetImageID returns exactly one image matching or dies
1793-
func (s *ScalewayAPI) GetImageID(needle string, exitIfMissing bool) string {
1821+
func (s *ScalewayAPI) GetImageID(needle string, exitIfMissing bool) (*ScalewayImageIdentifier, error) {
17941822
// Parses optional type prefix, i.e: "image:name" -> "name"
17951823
_, needle = parseNeedle(needle)
17961824

17971825
images, err := s.ResolveImage(needle)
17981826
if err != nil {
1799-
log.Fatalf("Unable to resolve image %s: %s", needle, err)
1827+
return nil, fmt.Errorf("Unable to resolve image %s: %s", needle, err)
18001828
}
18011829
if len(images) == 1 {
1802-
return images[0].Identifier
1830+
return &ScalewayImageIdentifier{
1831+
Identifier: images[0].Identifier,
1832+
Arch: images[0].Arch,
1833+
// FIXME region, owner hardcoded
1834+
Region: "fr-1",
1835+
Owner: "",
1836+
}, nil
18031837
}
18041838
if len(images) == 0 {
18051839
if exitIfMissing {
1806-
log.Fatalf("No such image: %s", needle)
1807-
} else {
1808-
return ""
1840+
return nil, fmt.Errorf("No such image: %s", needle)
18091841
}
1842+
return nil, nil
18101843
}
1811-
1812-
showResolverResults(needle, images)
1813-
os.Exit(1)
1814-
return ""
1844+
return nil, showResolverResults(needle, images)
18151845
}
18161846

18171847
// GetSecurityGroups returns a ScalewaySecurityGroups
@@ -2133,24 +2163,22 @@ func (s *ScalewayAPI) GetQuotas() (*ScalewayGetQuotas, error) {
21332163
}
21342164

21352165
// GetBootscriptID returns exactly one bootscript matching or dies
2136-
func (s *ScalewayAPI) GetBootscriptID(needle string) string {
2166+
func (s *ScalewayAPI) GetBootscriptID(needle, arch string) (string, error) {
21372167
// Parses optional type prefix, i.e: "bootscript:name" -> "name"
21382168
_, needle = parseNeedle(needle)
21392169

21402170
bootscripts, err := s.ResolveBootscript(needle)
21412171
if err != nil {
2142-
log.Fatalf("Unable to resolve bootscript %s: %s", needle, err)
2172+
return "", fmt.Errorf("Unable to resolve bootscript %s: %s", needle, err)
21432173
}
2174+
bootscripts.FilterByArch(arch)
21442175
if len(bootscripts) == 1 {
2145-
return bootscripts[0].Identifier
2176+
return bootscripts[0].Identifier, nil
21462177
}
21472178
if len(bootscripts) == 0 {
2148-
log.Fatalf("No such bootscript: %s", needle)
2179+
return "", fmt.Errorf("No such bootscript: %s", needle)
21492180
}
2150-
2151-
showResolverResults(needle, bootscripts)
2152-
os.Exit(1)
2153-
return ""
2181+
return "", showResolverResults(needle, bootscripts)
21542182
}
21552183

21562184
// HideAPICredentials removes API credentials from a string

0 commit comments

Comments
 (0)