Refactoring
This commit is contained in:
@@ -8,12 +8,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
decort "github.com/rudecs/decort-sdk"
|
"github.com/rudecs/decort-sdk/config"
|
||||||
"github.com/rudecs/decort-sdk/kvmx86"
|
"github.com/rudecs/decort-sdk/pkg/cloudapi/kvmx86"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cfg := decort.Config{
|
cfg := config.Config{
|
||||||
AppID: "<APPID>",
|
AppID: "<APPID>",
|
||||||
AppSecret: "<APPSECRET>",
|
AppSecret: "<APPSECRET>",
|
||||||
SSOURL: "https://sso.digitalenergy.online",
|
SSOURL: "https://sso.digitalenergy.online",
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package client
|
package decortsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/rudecs/decort-sdk/account"
|
"github.com/rudecs/decort-sdk/pkg/cloudapi/account"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (dc *decortClient) Account() *account.Account {
|
func (dc *decortClient) Account() *account.Account {
|
||||||
|
|||||||
14
client.go
14
client.go
@@ -1,13 +1,15 @@
|
|||||||
package client
|
package decortsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/google/go-querystring/query"
|
"github.com/google/go-querystring/query"
|
||||||
|
"github.com/rudecs/decort-sdk/config"
|
||||||
|
"github.com/rudecs/decort-sdk/internal/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
type decortClient struct {
|
type decortClient struct {
|
||||||
@@ -15,10 +17,10 @@ type decortClient struct {
|
|||||||
client *http.Client
|
client *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(config Config) *decortClient {
|
func New(cfg config.Config) *decortClient {
|
||||||
return &decortClient{
|
return &decortClient{
|
||||||
decortUrl: config.DecortUrl,
|
decortUrl: cfg.DecortURL,
|
||||||
client: newHttpClient(config),
|
client: client.NewHttpClient(cfg),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,7 +39,7 @@ func (dc *decortClient) DecortApiCall(ctx context.Context, method, url string, p
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
respBytes, err := ioutil.ReadAll(resp.Body)
|
respBytes, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package client
|
package decortsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/rudecs/decort-sdk/compute"
|
"github.com/rudecs/decort-sdk/pkg/cloudapi/compute"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (dc *decortClient) Compute() *compute.Compute {
|
func (dc *decortClient) Compute() *compute.Compute {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package client
|
package decortsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/rudecs/decort-sdk/computeci"
|
"github.com/rudecs/decort-sdk/pkg/cloudapi/computeci"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (dc *decortClient) ComputeCI() *computeci.ComputeCI {
|
func (dc *decortClient) ComputeCI() *computeci.ComputeCI {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package client
|
package config
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
AppID string
|
AppID string
|
||||||
4
disks.go
4
disks.go
@@ -1,7 +1,7 @@
|
|||||||
package client
|
package decortsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/rudecs/decort-sdk/disks"
|
"github.com/rudecs/decort-sdk/pkg/cloudapi/disks"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (dc *decortClient) Disks() *disks.Disks {
|
func (dc *decortClient) Disks() *disks.Disks {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package client
|
package decortsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/rudecs/decort-sdk/extnet"
|
"github.com/rudecs/decort-sdk/pkg/cloudapi/extnet"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (dc *decortClient) Extnet() *extnet.Extnet {
|
func (dc *decortClient) Extnet() *extnet.Extnet {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package client
|
package decortsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/rudecs/decort-sdk/flipgroup"
|
"github.com/rudecs/decort-sdk/pkg/cloudapi/flipgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (dc *decortClient) FlipGroup() *flipgroup.FlipGroup {
|
func (dc *decortClient) FlipGroup() *flipgroup.FlipGroup {
|
||||||
|
|||||||
4
image.go
4
image.go
@@ -1,7 +1,7 @@
|
|||||||
package client
|
package decortsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/rudecs/decort-sdk/image"
|
"github.com/rudecs/decort-sdk/pkg/cloudapi/image"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (dc *decortClient) Image() *image.Image {
|
func (dc *decortClient) Image() *image.Image {
|
||||||
|
|||||||
@@ -4,19 +4,21 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rudecs/decort-sdk/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newHttpClient(config Config) *http.Client {
|
func NewHttpClient(cfg config.Config) *http.Client {
|
||||||
|
|
||||||
transCfg := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: config.SSLSkipVerify}}
|
transCfg := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: cfg.SSLSkipVerify}}
|
||||||
|
|
||||||
return &http.Client{
|
return &http.Client{
|
||||||
Transport: &transport{
|
Transport: &transport{
|
||||||
base: transCfg,
|
base: transCfg,
|
||||||
retries: config.Retries,
|
retries: cfg.Retries,
|
||||||
clientId: config.AppId,
|
clientId: cfg.AppID,
|
||||||
clientSecret: config.AppSecret,
|
clientSecret: cfg.AppSecret,
|
||||||
ssoUrl: config.SSOUrl,
|
ssoUrl: cfg.SSOURL,
|
||||||
//TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
//TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -32,12 +31,13 @@ func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|||||||
return nil, fmt.Errorf("cannot get token: %v", err)
|
return nil, fmt.Errorf("cannot get token: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != 200 {
|
|
||||||
return nil, fmt.Errorf("cannot get token: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
tokenBytes, _ := ioutil.ReadAll(resp.Body)
|
tokenBytes, _ := ioutil.ReadAll(resp.Body)
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
return nil, fmt.Errorf("cannot get token: %s", tokenBytes)
|
||||||
|
}
|
||||||
|
|
||||||
token := string(tokenBytes)
|
token := string(tokenBytes)
|
||||||
|
|
||||||
t.token = token
|
t.token = token
|
||||||
@@ -48,8 +48,10 @@ func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|||||||
req.Header.Add("Authorization", "bearer "+t.token)
|
req.Header.Add("Authorization", "bearer "+t.token)
|
||||||
req.Header.Set("Accept", "application/json")
|
req.Header.Set("Accept", "application/json")
|
||||||
|
|
||||||
|
var resp *http.Response
|
||||||
|
var err error
|
||||||
for i := uint64(0); i < t.retries; i++ {
|
for i := uint64(0); i < t.retries; i++ {
|
||||||
resp, err := t.base.RoundTrip(req)
|
resp, err = t.base.RoundTrip(req)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if resp.StatusCode == 200 {
|
if resp.StatusCode == 200 {
|
||||||
return resp, nil
|
return resp, nil
|
||||||
@@ -58,8 +60,8 @@ func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|||||||
err = fmt.Errorf("%s", respBytes)
|
err = fmt.Errorf("%s", respBytes)
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
}
|
}
|
||||||
fmt.Println(err)
|
//logrus.Errorf("Could not execute request: %v. Retrying %d/%d", err, i+1, t.retries)
|
||||||
time.Sleep(time.Second * 5)
|
time.Sleep(time.Second * 5)
|
||||||
}
|
}
|
||||||
return nil, errors.New("number of retries exceeded")
|
return nil, fmt.Errorf("could not execute request: %v", err)
|
||||||
}
|
}
|
||||||
4
k8ci.go
4
k8ci.go
@@ -1,7 +1,7 @@
|
|||||||
package client
|
package decortsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/rudecs/decort-sdk/k8ci"
|
"github.com/rudecs/decort-sdk/pkg/cloudapi/k8ci"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (dc *decortClient) K8CI() *k8ci.K8CI {
|
func (dc *decortClient) K8CI() *k8ci.K8CI {
|
||||||
|
|||||||
4
k8s.go
4
k8s.go
@@ -1,7 +1,7 @@
|
|||||||
package client
|
package decortsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/rudecs/decort-sdk/k8s"
|
"github.com/rudecs/decort-sdk/pkg/cloudapi/k8s"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (dc *decortClient) K8S() *k8s.K8S {
|
func (dc *decortClient) K8S() *k8s.K8S {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package client
|
package decortsdk
|
||||||
|
|
||||||
import "github.com/rudecs/decort-sdk/kvmppc"
|
import "github.com/rudecs/decort-sdk/pkg/cloudapi/kvmppc"
|
||||||
|
|
||||||
func (dc *decortClient) KVMPPC() *kvmppc.KVMPPC {
|
func (dc *decortClient) KVMPPC() *kvmppc.KVMPPC {
|
||||||
return kvmppc.New(dc)
|
return kvmppc.New(dc)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package client
|
package decortsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/rudecs/decort-sdk/kvmx86"
|
"github.com/rudecs/decort-sdk/pkg/cloudapi/kvmx86"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (dc *decortClient) KVMX86() *kvmx86.KVMX86 {
|
func (dc *decortClient) KVMX86() *kvmx86.KVMX86 {
|
||||||
|
|||||||
4
lb.go
4
lb.go
@@ -1,6 +1,6 @@
|
|||||||
package client
|
package decortsdk
|
||||||
|
|
||||||
import "github.com/rudecs/decort-sdk/lb"
|
import "github.com/rudecs/decort-sdk/pkg/cloudapi/lb"
|
||||||
|
|
||||||
func (dc *decortClient) LB() *lb.LB {
|
func (dc *decortClient) LB() *lb.LB {
|
||||||
return lb.New(dc)
|
return lb.New(dc)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package client
|
package decortsdk
|
||||||
|
|
||||||
import "github.com/rudecs/decort-sdk/locations"
|
import "github.com/rudecs/decort-sdk/pkg/cloudapi/locations"
|
||||||
|
|
||||||
func (dc *decortClient) Locations() *locations.Locations {
|
func (dc *decortClient) Locations() *locations.Locations {
|
||||||
return locations.New(dc)
|
return locations.New(dc)
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user