2016-07-13 22:03:41 +02:00
|
|
|
package scaleway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/scaleway/scaleway-cli/pkg/api"
|
|
|
|
"github.com/scaleway/scaleway-cli/pkg/scwversion"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains scaleway configuration values
|
|
|
|
type Config struct {
|
|
|
|
Organization string
|
|
|
|
APIKey string
|
2016-10-10 20:02:32 +02:00
|
|
|
Region string
|
2016-07-13 22:03:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client contains scaleway api clients
|
|
|
|
type Client struct {
|
|
|
|
scaleway *api.ScalewayAPI
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client configures and returns a fully initialized Scaleway client
|
|
|
|
func (c *Config) Client() (*Client, error) {
|
|
|
|
api, err := api.NewScalewayAPI(
|
|
|
|
c.Organization,
|
|
|
|
c.APIKey,
|
|
|
|
scwversion.UserAgent(),
|
2016-10-10 20:02:32 +02:00
|
|
|
c.Region,
|
2016-07-13 22:03:41 +02:00
|
|
|
func(s *api.ScalewayAPI) {
|
|
|
|
s.Logger = newTerraformLogger()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Client{api}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTerraformLogger() api.Logger {
|
|
|
|
return &terraformLogger{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type terraformLogger struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *terraformLogger) LogHTTP(r *http.Request) {
|
|
|
|
log.Printf("[DEBUG] %s %s\n", r.Method, r.URL.Path)
|
|
|
|
}
|
|
|
|
func (l *terraformLogger) Fatalf(format string, v ...interface{}) {
|
|
|
|
log.Printf("[FATAL] %s\n", fmt.Sprintf(format, v))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
func (l *terraformLogger) Debugf(format string, v ...interface{}) {
|
|
|
|
log.Printf("[DEBUG] %s\n", fmt.Sprintf(format, v))
|
|
|
|
}
|
|
|
|
func (l *terraformLogger) Infof(format string, v ...interface{}) {
|
|
|
|
log.Printf("[INFO ] %s\n", fmt.Sprintf(format, v))
|
|
|
|
}
|
|
|
|
func (l *terraformLogger) Warnf(format string, v ...interface{}) {
|
|
|
|
log.Printf("[WARN ] %s\n", fmt.Sprintf(format, v))
|
|
|
|
}
|