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,7 @@ 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+ func CreateServer (api * ScalewayAPI , imageName string , name string , bootscript string , env string , additionalVolumes string ) (string , error ) {
191203 if name == "" {
192204 name = strings .Replace (namesgenerator .GetRandomName (0 ), "_" , "-" , - 1 )
193205 }
@@ -268,33 +280,22 @@ func WaitForServerReady(api *ScalewayAPI, serverID string) (*ScalewayServer, err
268280
269281 dest := fmt .Sprintf ("%s:22" , server .PublicAddress .IP )
270282
271- err = WaitForTCPPortOpen (dest )
283+ err = utils . WaitForTCPPortOpen (dest )
272284 if err != nil {
273285 return nil , err
274286 }
275287
276288 return server , nil
277289}
278290
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-
290291// ByCreationDate sorts images by CreationDate field
291292type ByCreationDate []ScalewayImageInterface
292293
293294func (a ByCreationDate ) Len () int { return len (a ) }
294295func (a ByCreationDate ) Swap (i , j int ) { a [i ], a [j ] = a [j ], a [i ] }
295296func (a ByCreationDate ) Less (i , j int ) bool { return a [j ].CreationDate .Before (a [i ].CreationDate ) }
296297
297- func startServer (api * ScalewayAPI , needle string , wait bool ) error {
298+ func StartServer (api * ScalewayAPI , needle string , wait bool ) error {
298299 server := api .GetServerID (needle )
299300
300301 err := api .PostServerAction (server , "poweron" )
@@ -313,8 +314,8 @@ func startServer(api *ScalewayAPI, needle string, wait bool) error {
313314 return nil
314315}
315316
316- func startServerOnce (api * ScalewayAPI , needle string , wait bool , successChan chan bool , errChan chan error ) {
317- err := startServer (api , needle , wait )
317+ func StartServerOnce (api * ScalewayAPI , needle string , wait bool , successChan chan bool , errChan chan error ) {
318+ err := StartServer (api , needle , wait )
318319
319320 if err != nil {
320321 errChan <- err
0 commit comments