Skip to content
Snippets Groups Projects
Commit 78ecf453 authored by Florent Gluck's avatar Florent Gluck
Browse files

WIP: stateless client

parent 3d2557b4
Branches
No related tags found
No related merge requests found
......@@ -4,8 +4,7 @@ import (
"fmt"
"net/url"
g "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/globals"
libclient "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/vm"
nc "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/nexusclient"
"gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/client/exec"
u "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/client/utils"
......@@ -14,10 +13,9 @@ import (
"gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/common/vm"
)
func AttachToVMs(vms []vm.VMAttachCredentialsSerialized) (int, error) {
func AttachToVMs(nc *NexusClient, vms []vm.VMAttachCredentialsSerialized) (int, error) {
statusCode := 0
client := g.GetInstance().Client
url, err := url.Parse(client.BaseURL)
if err != nil {
u.PrintlnErr(err)
......@@ -25,12 +23,10 @@ func AttachToVMs(vms []vm.VMAttachCredentialsSerialized) (int, error) {
}
hostname := url.Hostname()
cert := g.GetInstance().PubCert
for _, v := range vms {
p := params.VMAttachCreds{Pwd: v.Pwd}
creds, err := libclient.VMGetSpiceCreds(v.ID.String(), p)
creds, err := nc.VMGetSpiceCreds(v.ID.String(), p)
if err != nil {
u.PrintlnErr(err)
return 1, err
......
......@@ -5,7 +5,7 @@ import (
"regexp"
"strings"
libclient "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/vm"
nc "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/nexusclient"
"gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/client/cmd"
u "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/client/utils"
......@@ -128,7 +128,7 @@ func getFilteredVMCredentials(patterns []string) ([]vm.VMAttachCredentialsSerial
}
}
vmAttachCreds, err := libclient.GetVMAttachCredentials()
vmAttachCreds, err := nc.GetVMAttachCredentials()
if err != nil {
return nil, err
}
......
......@@ -8,9 +8,8 @@ import (
"os"
"strings"
g "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/globals"
"gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/utils"
libclient "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/vm"
nc "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/nexusclient"
u "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/client/utils"
......@@ -65,7 +64,7 @@ List of VM access capabilities:
printRegexUsageDetails()
}
func (cmd *AddAccess) Run(args []string) int {
func (cmd *AddAccess) Run(nc *NexusClient, args []string) int {
argc := len(args)
if argc < 1 {
cmd.PrintUsage()
......@@ -127,7 +126,7 @@ func (cmd *AddAccess) Run(args []string) int {
}
}
if err := libclient.VMAddAccess(vmID, vmID, email, vmAccessCaps); err != nil {
if err := nc.VMAddAccess(vmID, vmID, email, vmAccessCaps); err != nil {
u.PrintlnErr(err)
statusCode = 1
} else {
......@@ -143,7 +142,7 @@ func (cmd *AddAccess) Run(args []string) int {
return 1
}
vms, err := getFilteredVMs(libclient.GetEditAccessVMs, patterns)
vms, err := getFilteredVMs(nc.GetEditAccessVMs, patterns)
if err != nil {
u.PrintlnErr(err)
return 1
......@@ -160,7 +159,7 @@ func (cmd *AddAccess) Run(args []string) int {
}
for _, vm := range vms {
if err := libclient.VMAddAccess(vm.ID.String(), vm.Name, email, vmAccessCaps); err != nil {
if err := nc.VMAddAccess(vm.ID.String(), vm.Name, email, vmAccessCaps); err != nil {
u.PrintlnErr(err)
statusCode = 1
} else {
......
package cmdVersion
import (
libclient "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/version"
nc "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/nexusclient"
u "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/client/utils"
"gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/client/version"
......@@ -28,13 +28,13 @@ func (cmd *Version) PrintUsage() {
u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――")
}
func (cmd *Version) Run(args []string) int {
func (cmd *Version) Run(nc *NexusClient, args []string) int {
if len(args) > 0 {
cmd.PrintUsage()
return 1
}
serverVersion, err := libclient.GetServerVersion()
serverVersion, err := nc.GetServerVersion()
if err != nil {
u.PrintlnErr("Error: " + err.Error())
return 1
......@@ -49,7 +49,7 @@ func (cmd *Version) Run(args []string) int {
// Checks the client version is compatible with the server's API.
func CheckServerCompatibility(appname string) bool {
// Checks the client version is compatible with the server's API.
serverVersion, err := libclient.GetServerVersion()
serverVersion, err := nc.GetServerVersion()
if err != nil {
u.PrintlnErr("Error: " + err.Error())
return false
......
......@@ -23,7 +23,7 @@ import (
"strings"
"syscall"
"gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/login"
"gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/nexusclient"
"gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/client/version"
......@@ -152,12 +152,7 @@ func run() int {
return 1
}
client := resty.New()
// To disable the check of the CA signed certificate
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
client.SetBaseURL("https://" + serverEnvVar)
g.Init(certPath, client)
nc := nexusclient.New(serverEnvVar)
// Checks the client version is compatible with the server's API.
if !cmdVersion.CheckServerCompatibility(appname) {
......@@ -175,7 +170,7 @@ func run() int {
pwd := string(bytePwd)
// Authenticate the user (internally obtains a JWT token).
err = login.Authenticate(email, pwd)
err = nc.Authenticate(email, pwd)
if err != nil {
u.PrintlnErr("Error: " + err.Error())
return 1
......
......@@ -9,9 +9,16 @@ type NexusClient struct {
}
func New() *NexusClient {
// host has the following format:
// ip:port, e.g. 127.0.0.1:1077
func New(host string) *NexusClient {
nc := &NexusClient {
client: resty.New()
}
// To disable the check of the CA signed certificate
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
client.SetBaseURL("https://" + host)
return nc
}
......@@ -3,7 +3,6 @@ package nexusclient
import (
"encoding/json"
g "gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/globals"
"gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/libclient/response"
"gitedu.hesge.ch/flg_projects/nexus_vdi/nexus/common/params"
......@@ -13,10 +12,8 @@ import (
// curl --cacert ca-cert.pem -X POST https://localhost:8000/login -H 'Content-Type: application/json' -d '{"email": "johndoe@nexus.org", "pwd":"pipomolo"}'
// Returns true if authentication succeeded or an error if it failed.
func (nc *NexusClient) Authenticate(user, pwd string) error {
client := g.GetInstance().Client
loginArgs := &params.Login{user, pwd}
resp, err := client.R().SetBody(loginArgs).Post("/login")
resp, err := nc.client.R().SetBody(loginArgs).Post("/login")
if err != nil {
return err
}
......@@ -36,9 +33,7 @@ func (nc *NexusClient) Authenticate(user, pwd string) error {
// Obtain a new JWT token.
func (nc *NexusClient) RefreshToken() error {
client := g.GetInstance().Client
resp, err := client.R().Get("/token/refresh")
resp, err := nc.client.R().Get("/token/refresh")
if err != nil {
return err
} else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment