Skip to content

Add methods to use the BTP CLI #5305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
120 changes: 120 additions & 0 deletions pkg/btp/Authentication.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package btp

import (
"errors"
"fmt"

"github.com/SAP/jenkins-library/pkg/log"
)

type BTPUtils struct {
Exec ExecRunner
}

type LoginOptions struct {
Url string
Subdomain string
User string
Password string
Tenant string
}

type ConfigOptions struct {
Format string
Verbose bool
}

func NewBTPUtils(exec ExecRunner) *BTPUtils {
b := new(BTPUtils)
b.Exec = exec

configOptions := ConfigOptions{
Format: "json",
}
b.SetConfig(configOptions)
return b
}

func (btp *BTPUtils) Login(options LoginOptions) error {
if btp.Exec == nil {
btp.Exec = &Executor{}
}

if options.Url == "" || options.Subdomain == "" || options.User == "" || options.Password == "" {
return fmt.Errorf("Failed to login to BTP: %w", errors.New("Parameters missing. Please provide the CLI URL, Subdomain, Space, User and Password"))
}

log.Entry().Info("Logging in to BTP")

builder := NewBTPCommandBuilder().
WithAction("login").
WithURL(options.Url).
WithSubdomain(options.Subdomain).
WithUser(options.User).
WithPassword(options.Password)

if options.Tenant != "" {
builder = builder.WithTenant(options.Tenant)
}

btpLoginScript, _ := builder.Build()

log.Entry().WithField("CLI URL:", options.Url).WithField("Subdomain", options.Subdomain).WithField("User", options.User).WithField("Password", options.Password).WithField("Tenant", options.Tenant)

err := btp.Exec.Run(btpLoginScript)

if err != nil {
return fmt.Errorf("Failed to login to BTP: %w", err)
}
log.Entry().Info("Logged in successfully to BTP.")
return nil
}

func (btp *BTPUtils) Logout() error {
if btp.Exec == nil {
btp.Exec = &Executor{}
}

log.Entry().Info("Logout of BTP")

builder := NewBTPCommandBuilder().
WithAction("logout")

btpLogoutScript, _ := builder.Build()

err := btp.Exec.Run(btpLogoutScript)

if err != nil {
return fmt.Errorf("Failed to Logout of BTP: %w", err)
}
log.Entry().Info("Logged out successfully")
return nil
}

func (btp *BTPUtils) SetConfig(options ConfigOptions) error {
if btp.Exec == nil {
btp.Exec = &Executor{}
}

if options.Format == "" {
return fmt.Errorf("Failed to set the configuration of the BTP CLI: %w", errors.New("Parameters missing. Please provide the Format"))
}

builder := NewBTPCommandBuilder().
WithAction("set config").
WithFormat(options.Format).
WithVerbose()

btpConfigScript, _ := builder.Build()

log.Entry().WithField("Format:", options.Format)

err := btp.Exec.Run(btpConfigScript)

if err != nil {
log.SetErrorCategory(log.ErrorConfiguration)
return fmt.Errorf("Failed to define the configuration of the BTP CLI: %w", err)
}
log.Entry().Info("Configuration successfully defined.")
return nil
}
83 changes: 83 additions & 0 deletions pkg/btp/Authentication_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package btp

import (
"bytes"
"fmt"
"testing"

"github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
)

func loginMockCleanup(m *mock.BtpExecutorMock) {
m.ShouldFailOnCommand = map[string]error{}
m.StdoutReturn = map[string]string{}
m.Calls = []mock.BtpExecCall{}
}

func TestBTPLogin(t *testing.T) {

m := &mock.BtpExecutorMock{
StdoutReturn: map[string]string{},
}

m.Stdout(new(bytes.Buffer))

t.Run("BTP Login: missing parameter", func(t *testing.T) {

defer loginMockCleanup(m)

btpConfig := LoginOptions{}
btp := BTPUtils{Exec: m}
err := btp.Login(btpConfig)
assert.EqualError(t, err, "Failed to login to BTP: Parameters missing. Please provide the CLI URL, Subdomain, Space, User and Password")
})
t.Run("BTP Login: failure", func(t *testing.T) {

defer loginMockCleanup(m)

m.ShouldFailOnCommand = map[string]error{"btp login .*": fmt.Errorf("wrong password or account does not exist")}

btpConfig := LoginOptions{
Url: "https://api.endpoint.com",
Subdomain: "xxx",
User: "[email protected]",
Password: "xxx",
}

btp := BTPUtils{Exec: m}
err := btp.Login(btpConfig)
assert.EqualError(t, err, "Failed to login to BTP: wrong password or account does not exist")
})

t.Run("BTP Login: success", func(t *testing.T) {

defer loginMockCleanup(m)

m.StdoutReturn = map[string]string{"btp login .*": "Authentication successful"}

btpConfig := LoginOptions{
Url: "https://api.endpoint.com",
Subdomain: "xxx",
User: "[email protected]",
Password: "xxx",
}

btp := BTPUtils{Exec: m}
err := btp.Login(btpConfig)

assert.NoError(t, err)
})
}

func TestBTPLogout(t *testing.T) {

m := &mock.BtpExecutorMock{}

t.Run("BTP Logout", func(t *testing.T) {
btp := BTPUtils{Exec: m}
err := btp.Logout()

assert.NoError(t, err)
})
}
Loading