1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/abap/build/connector.go
Christian Luttenberger 7a028c4149
Refactor build framework steps (#2068)
* adding my steps

* messy step

* Update abapEnvironmentAssembly.go

* clean up

* change yaml

* corrections

* Update cloudFoundryDeploy.go

* update

* delete simulation step

* remove simulate

* Update PiperGoUtils.groovy

* Update PiperGoUtils.groovy

* Update CommonStepsTest.groovy

* add docu

* Update abapEnvironmentAssembly.md

* changes due to PR

* Update .gitignore

* b

* CV list

* Update abapEnvironmentAssembly.go

* testing with simulation

* Update abapEnvironmentAssembly.go

* remove simulation

* renaming

* Update mkdocs.yml

* moving service key to yaml and fixing code climate

* Update abapEnvironmentAssemblePackages.go

* Update abapEnvironmentAssemblePackages.go

* Update abapEnvironmentAssemblePackages.go

* Update abapEnvironmentAssemblePackages.go

* change input

* Update abapEnvironmentAssemblePackages.go

* change json tag

* fixed error handling

* documentation

* Update abapEnvironmentAssemblePackages.md

* Update abapEnvironmentAssemblePackages.md

* fixing code climate issues

* fixing code climate issues

* Update abapEnvironmentAssemblePackages.yaml

* fixing code climate issues

* Update abapEnvironmentAssemblePackages.yaml

* adding unittests

* adding unittests and improved logging

* yaml -> json

* change scope of cfServiceKeyName

* correct indentation

* Update CommonStepsTest.groovy

* maintain correct step order

* Move Connector to connector.go

* Refactor bfw with unit tests

* remove spaces

* CodeClimate Fix for unexported type

* ABAP BF - Adding Error Handling Unmarshal

* Revert Unmarshal

Co-authored-by: rosemarieB <45030247+rosemarieB@users.noreply.github.com>
Co-authored-by: Daniel Mieg <56156797+DanielMieg@users.noreply.github.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
Co-authored-by: Koerner <tilo.koerner@sap.com>
Co-authored-by: tiloKo <70266685+tiloKo@users.noreply.github.com>
2020-09-30 16:40:36 +02:00

177 lines
5.7 KiB
Go

package build
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"time"
"github.com/SAP/jenkins-library/pkg/abaputils"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/pkg/errors"
)
// Connector : Connector Utility Wrapping http client
type Connector struct {
Client piperhttp.Sender
DownloadClient piperhttp.Downloader
Header map[string][]string
Baseurl string
}
// ConnectorConfiguration : Handover Structure for Connector Creation
type ConnectorConfiguration struct {
CfAPIEndpoint string
CfOrg string
CfSpace string
CfServiceInstance string
CfServiceKeyName string
Host string
Username string
Password string
AddonDescriptor string
MaxRuntimeInMinutes int
}
// ******** technical communication calls ********
// GetToken : Get the X-CRSF Token from ABAP Backend for later post
func (conn *Connector) GetToken(appendum string) error {
url := conn.Baseurl + appendum
conn.Header["X-CSRF-Token"] = []string{"Fetch"}
response, err := conn.Client.SendRequest("HEAD", url, nil, conn.Header, nil)
if err != nil {
if response == nil {
return errors.Wrap(err, "Fetching X-CSRF-Token failed")
}
defer response.Body.Close()
errorbody, _ := ioutil.ReadAll(response.Body)
return errors.Wrapf(err, "Fetching X-CSRF-Token failed: %v", string(errorbody))
}
defer response.Body.Close()
token := response.Header.Get("X-CSRF-Token")
conn.Header["X-CSRF-Token"] = []string{token}
return nil
}
// Get : http get request
func (conn Connector) Get(appendum string) ([]byte, error) {
url := conn.Baseurl + appendum
response, err := conn.Client.SendRequest("GET", url, nil, conn.Header, nil)
if err != nil {
if response == nil || response.Body == nil {
return nil, errors.Wrap(err, "Get failed")
}
defer response.Body.Close()
errorbody, _ := ioutil.ReadAll(response.Body)
return errorbody, errors.Wrapf(err, "Get failed: %v", string(errorbody))
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
return body, err
}
// Post : http post request
func (conn Connector) Post(appendum string, importBody string) ([]byte, error) {
url := conn.Baseurl + appendum
var response *http.Response
var err error
if importBody == "" {
response, err = conn.Client.SendRequest("POST", url, nil, conn.Header, nil)
} else {
response, err = conn.Client.SendRequest("POST", url, bytes.NewBuffer([]byte(importBody)), conn.Header, nil)
}
if err != nil {
if response == nil {
return nil, errors.Wrap(err, "Post failed")
}
defer response.Body.Close()
errorbody, _ := ioutil.ReadAll(response.Body)
return errorbody, errors.Wrapf(err, "Post failed: %v", string(errorbody))
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
return body, err
}
// Download : download a file via http
func (conn Connector) Download(appendum string, downloadPath string) error {
url := conn.Baseurl + appendum
err := conn.DownloadClient.DownloadFile(url, downloadPath, nil, nil)
return err
}
// InitAAKaaS : initialize Connector for communication with AAKaaS backend
func (conn *Connector) InitAAKaaS(aAKaaSEndpoint string, username string, password string, inputclient piperhttp.Sender) {
conn.Client = inputclient
conn.Header = make(map[string][]string)
conn.Header["Accept"] = []string{"application/json"}
conn.Header["Content-Type"] = []string{"application/json"}
cookieJar, _ := cookiejar.New(nil)
conn.Client.SetOptions(piperhttp.ClientOptions{
Username: username,
Password: password,
CookieJar: cookieJar,
})
conn.Baseurl = aAKaaSEndpoint
}
// InitBuildFramework : initialize Connector for communication with ABAP SCP instance
func (conn *Connector) InitBuildFramework(config ConnectorConfiguration, com abaputils.Communication, inputclient piperhttp.Sender) error {
conn.Client = inputclient
conn.Header = make(map[string][]string)
conn.Header["Accept"] = []string{"application/json"}
conn.Header["Content-Type"] = []string{"application/json"}
conn.DownloadClient = &piperhttp.Client{}
conn.DownloadClient.SetOptions(piperhttp.ClientOptions{TransportTimeout: 20 * time.Second})
// Mapping for options
subOptions := abaputils.AbapEnvironmentOptions{}
subOptions.CfAPIEndpoint = config.CfAPIEndpoint
subOptions.CfServiceInstance = config.CfServiceInstance
subOptions.CfServiceKeyName = config.CfServiceKeyName
subOptions.CfOrg = config.CfOrg
subOptions.CfSpace = config.CfSpace
subOptions.Host = config.Host
subOptions.Password = config.Password
subOptions.Username = config.Username
// Determine the host, user and password, either via the input parameters or via a cloud foundry service key
connectionDetails, err := com.GetAbapCommunicationArrangementInfo(subOptions, "/sap/opu/odata/BUILD/CORE_SRV")
if err != nil {
return errors.Wrap(err, "Parameters for the ABAP Connection not available")
}
conn.DownloadClient.SetOptions(piperhttp.ClientOptions{
Username: connectionDetails.User,
Password: connectionDetails.Password,
})
cookieJar, _ := cookiejar.New(nil)
conn.Client.SetOptions(piperhttp.ClientOptions{
Username: connectionDetails.User,
Password: connectionDetails.Password,
CookieJar: cookieJar,
})
conn.Baseurl = connectionDetails.URL
return nil
}
// UploadSarFile : upload *.sar file
func (conn Connector) UploadSarFile(appendum string, sarFile []byte) error {
url := conn.Baseurl + appendum
response, err := conn.Client.SendRequest("PUT", url, bytes.NewBuffer(sarFile), conn.Header, nil)
if err != nil {
defer response.Body.Close()
errorbody, _ := ioutil.ReadAll(response.Body)
return errors.Wrapf(err, "Upload of SAR file failed: %v", string(errorbody))
}
defer response.Body.Close()
return nil
}