1- package main
1+ package api
22
33import (
44 "fmt"
@@ -10,8 +10,29 @@ import (
1010 log "github.com/Sirupsen/logrus"
1111 "github.com/docker/docker/pkg/namesgenerator"
1212 "github.com/dustin/go-humanize"
13+ "github.com/scaleway/scaleway-cli/utils"
1314)
1415
16+ // ScalewayResolvedIdentifier represents a list of matching identifier for a specifier pattern
17+ type ScalewayResolvedIdentifier struct {
18+ // Identifiers holds matching identifiers
19+ Identifiers []ScalewayIdentifier
20+
21+ // Needle is the criteria used to lookup identifiers
22+ Needle string
23+ }
24+
25+ // ScalewayImageInterface is an interface to multiple Scaleway items
26+ type ScalewayImageInterface struct {
27+ CreationDate time.Time
28+ Identifier string
29+ Name string
30+ Tag string
31+ VirtualSize float64
32+ Public bool
33+ Type string
34+ }
35+
1536// CreateVolumeFromHumanSize creates a volume on the API with a human readable size
1637func CreateVolumeFromHumanSize (api * ScalewayAPI , size string ) (* string , error ) {
1738 bytes , err := humanize .ParseBytes (size )
@@ -32,15 +53,6 @@ func CreateVolumeFromHumanSize(api *ScalewayAPI, size string) (*string, error) {
3253 return & volumeID , nil
3354}
3455
35- // ScalewayResolvedIdentifier represents a list of matching identifier for a specifier pattern
36- type ScalewayResolvedIdentifier struct {
37- // Identifiers holds matching identifiers
38- Identifiers []ScalewayIdentifier
39-
40- // Needle is the criteria used to lookup identifiers
41- Needle string
42- }
43-
4456// fillIdentifierCache fills the cache by fetching fro the API
4557func fillIdentifierCache (api * ScalewayAPI ) {
4658 log .Debugf ("Filling the cache" )
@@ -69,9 +81,9 @@ func fillIdentifierCache(api *ScalewayAPI) {
6981 wg .Wait ()
7082}
7183
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 )
84+ // GetIdentifier returns a an identifier if the resolved needles only match one element, else, it exists the program
85+ func GetIdentifier (api * ScalewayAPI , needle string ) * ScalewayIdentifier {
86+ idents := ResolveIdentifier (api , needle )
7587
7688 if len (idents ) == 1 {
7789 return & idents [0 ]
@@ -88,8 +100,8 @@ func getIdentifier(api *ScalewayAPI, needle string) *ScalewayIdentifier {
88100 return nil
89101}
90102
91- // resolveIdentifier resolves needle provided by the user
92- func resolveIdentifier (api * ScalewayAPI , needle string ) []ScalewayIdentifier {
103+ // ResolveIdentifier resolves needle provided by the user
104+ func ResolveIdentifier (api * ScalewayAPI , needle string ) []ScalewayIdentifier {
93105 idents := api .Cache .LookUpIdentifiers (needle )
94106 if len (idents ) > 0 {
95107 return idents
@@ -101,8 +113,8 @@ func resolveIdentifier(api *ScalewayAPI, needle string) []ScalewayIdentifier {
101113 return idents
102114}
103115
104- // resolveIdentifiers resolves needles provided by the user
105- func resolveIdentifiers (api * ScalewayAPI , needles []string , out chan ScalewayResolvedIdentifier ) {
116+ // ResolveIdentifiers resolves needles provided by the user
117+ func ResolveIdentifiers (api * ScalewayAPI , needles []string , out chan ScalewayResolvedIdentifier ) {
106118 // first attempt, only lookup from the cache
107119 var unresolved []string
108120 for _ , needle := range needles {
@@ -131,8 +143,8 @@ func resolveIdentifiers(api *ScalewayAPI, needles []string, out chan ScalewayRes
131143 close (out )
132144}
133145
134- // inspectIdentifiers inspects identifiers concurrently
135- func inspectIdentifiers (api * ScalewayAPI , ci chan ScalewayResolvedIdentifier , cj chan interface {}) {
146+ // InspectIdentifiers inspects identifiers concurrently
147+ func InspectIdentifiers (api * ScalewayAPI , ci chan ScalewayResolvedIdentifier , cj chan interface {}) {
136148 var wg sync.WaitGroup
137149 for {
138150 idents , ok := <- ci
@@ -187,7 +199,8 @@ func inspectIdentifiers(api *ScalewayAPI, ci chan ScalewayResolvedIdentifier, cj
187199 close (cj )
188200}
189201
190- func createServer (api * ScalewayAPI , imageName string , name string , bootscript string , env string , additionalVolumes string ) (string , error ) {
202+ // CreateServer creates a server using API based on typical server fields
203+ func CreateServer (api * ScalewayAPI , imageName string , name string , bootscript string , env string , additionalVolumes string ) (string , error ) {
191204 if name == "" {
192205 name = strings .Replace (namesgenerator .GetRandomName (0 ), "_" , "-" , - 1 )
193206 }
@@ -268,33 +281,23 @@ func WaitForServerReady(api *ScalewayAPI, serverID string) (*ScalewayServer, err
268281
269282 dest := fmt .Sprintf ("%s:22" , server .PublicAddress .IP )
270283
271- err = WaitForTCPPortOpen (dest )
284+ err = utils . WaitForTCPPortOpen (dest )
272285 if err != nil {
273286 return nil , err
274287 }
275288
276289 return server , nil
277290}
278291
279- // ScalewayImageInterface is an interface to multiple Scaleway items
280- type ScalewayImageInterface struct {
281- CreationDate time.Time
282- Identifier string
283- Name string
284- Tag string
285- VirtualSize float64
286- Public bool
287- Type string
288- }
289-
290292// ByCreationDate sorts images by CreationDate field
291293type ByCreationDate []ScalewayImageInterface
292294
293295func (a ByCreationDate ) Len () int { return len (a ) }
294296func (a ByCreationDate ) Swap (i , j int ) { a [i ], a [j ] = a [j ], a [i ] }
295297func (a ByCreationDate ) Less (i , j int ) bool { return a [j ].CreationDate .Before (a [i ].CreationDate ) }
296298
297- func startServer (api * ScalewayAPI , needle string , wait bool ) error {
299+ // StartServer start a server based on its needle, can optionaly block while server is booting
300+ func StartServer (api * ScalewayAPI , needle string , wait bool ) error {
298301 server := api .GetServerID (needle )
299302
300303 err := api .PostServerAction (server , "poweron" )
@@ -313,8 +316,9 @@ func startServer(api *ScalewayAPI, needle string, wait bool) error {
313316 return nil
314317}
315318
316- func startServerOnce (api * ScalewayAPI , needle string , wait bool , successChan chan bool , errChan chan error ) {
317- err := startServer (api , needle , wait )
319+ // StartServerOnce wraps StartServer for golang channel
320+ func StartServerOnce (api * ScalewayAPI , needle string , wait bool , successChan chan bool , errChan chan error ) {
321+ err := StartServer (api , needle , wait )
318322
319323 if err != nil {
320324 errChan <- err
0 commit comments