Skip to content

Commit 0e0da9b

Browse files
committed
Added resolveIdentifier and getIdentifier api helpers
1 parent c2021a9 commit 0e0da9b

File tree

1 file changed

+63
-23
lines changed

1 file changed

+63
-23
lines changed

api_helpers.go

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"os"
56
"strings"
67
"sync"
78
"time"
@@ -40,6 +41,66 @@ type ScalewayResolvedIdentifier struct {
4041
Needle string
4142
}
4243

44+
// fillIdentifierCache fills the cache by fetching fro the API
45+
func fillIdentifierCache(api *ScalewayAPI) {
46+
log.Debugf("Filling the cache")
47+
var wg sync.WaitGroup
48+
wg.Add(5)
49+
go func() {
50+
api.GetServers(true, 0)
51+
wg.Done()
52+
}()
53+
go func() {
54+
api.GetImages()
55+
wg.Done()
56+
}()
57+
go func() {
58+
api.GetSnapshots()
59+
wg.Done()
60+
}()
61+
go func() {
62+
api.GetVolumes()
63+
wg.Done()
64+
}()
65+
go func() {
66+
api.GetBootscripts()
67+
wg.Done()
68+
}()
69+
wg.Wait()
70+
}
71+
72+
// getIdentifier returns a an identifier if the resolved needles only match one element, else, it exists the program
73+
func getIdentifier(api *ScalewayAPI, needle string) *ScalewayIdentifier {
74+
idents := resolveIdentifier(api, needle)
75+
76+
if len(idents) == 1 {
77+
return &idents[0]
78+
}
79+
if len(idents) == 0 {
80+
log.Fatalf("No such identifier: %s", needle)
81+
}
82+
log.Errorf("Too many candidates for %s (%d)", needle, len(idents))
83+
for _, identifier := range idents {
84+
// FIXME: also print the name
85+
log.Infof("- %s", identifier.Identifier)
86+
}
87+
os.Exit(1)
88+
return nil
89+
}
90+
91+
// resolveIdentifier resolves needle provided by the user
92+
func resolveIdentifier(api *ScalewayAPI, needle string) []ScalewayIdentifier {
93+
idents := api.Cache.LookUpIdentifiers(needle)
94+
if len(idents) > 0 {
95+
return idents
96+
}
97+
98+
fillIdentifierCache(api)
99+
100+
idents = api.Cache.LookUpIdentifiers(needle)
101+
return idents
102+
}
103+
43104
// resolveIdentifiers resolves needles provided by the user
44105
func resolveIdentifiers(api *ScalewayAPI, needles []string, out chan ScalewayResolvedIdentifier) {
45106
// first attempt, only lookup from the cache
@@ -57,29 +118,8 @@ func resolveIdentifiers(api *ScalewayAPI, needles []string, out chan ScalewayRes
57118
}
58119
// fill the cache by fetching from the API and resolve missing identifiers
59120
if len(unresolved) > 0 {
60-
var wg sync.WaitGroup
61-
wg.Add(5)
62-
go func() {
63-
api.GetServers(true, 0)
64-
wg.Done()
65-
}()
66-
go func() {
67-
api.GetImages()
68-
wg.Done()
69-
}()
70-
go func() {
71-
api.GetSnapshots()
72-
wg.Done()
73-
}()
74-
go func() {
75-
api.GetVolumes()
76-
wg.Done()
77-
}()
78-
go func() {
79-
api.GetBootscripts()
80-
wg.Done()
81-
}()
82-
wg.Wait()
121+
fillIdentifierCache(api)
122+
83123
for _, needle := range unresolved {
84124
idents := api.Cache.LookUpIdentifiers(needle)
85125
out <- ScalewayResolvedIdentifier{

0 commit comments

Comments
 (0)