Skip to content

Commit

Permalink
AAKaaS reserve SAP#2
Browse files Browse the repository at this point in the history
  • Loading branch information
tiloKo committed Sep 18, 2020
1 parent 8a81de2 commit 64e771e
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 162 deletions.
141 changes: 28 additions & 113 deletions cmd/abapAddonAssemblyKitReserveNextPackages.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"time"

"github.com/SAP/jenkins-library/pkg/abap/aakaas"
abapbuild "github.com/SAP/jenkins-library/pkg/abap/build"
"github.com/SAP/jenkins-library/pkg/abaputils"
"github.com/SAP/jenkins-library/pkg/command"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
Expand Down Expand Up @@ -32,8 +34,8 @@ func abapAddonAssemblyKitReserveNextPackages(config abapAddonAssemblyKitReserveN

func runAbapAddonAssemblyKitReserveNextPackages(config *abapAddonAssemblyKitReserveNextPackagesOptions, telemetryData *telemetry.CustomData, client piperhttp.Sender,
cpe *abapAddonAssemblyKitReserveNextPackagesCommonPipelineEnvironment, maxRuntimeInMinutes time.Duration, pollIntervalsInSeconds time.Duration) error {
conn := new(connector)
conn.initAAK(config.AbapAddonAssemblyKitEndpoint, config.Username, config.Password, client)
conn := new(abapbuild.Connector)
conn.InitAAKaaS(config.AbapAddonAssemblyKitEndpoint, config.Username, config.Password, client)

var addonDescriptor abaputils.AddonDescriptor
json.Unmarshal([]byte(config.AddonDescriptor), &addonDescriptor)
Expand All @@ -54,16 +56,16 @@ func runAbapAddonAssemblyKitReserveNextPackages(config *abapAddonAssemblyKitRese
return nil
}

func copyFieldsToRepositories(pckgWR []packageWithRepository) []abaputils.Repository {
func copyFieldsToRepositories(pckgWR []aakaas.PackageWithRepository) []abaputils.Repository {
var repos []abaputils.Repository
for i := range pckgWR {
pckgWR[i].p.copyFieldsToRepo(&pckgWR[i].repo)
repos = append(repos, pckgWR[i].repo)
pckgWR[i].Package.CopyFieldsToRepo(&pckgWR[i].Repo)
repos = append(repos, pckgWR[i].Repo)
}
return repos
}

func pollReserveNextPackages(pckgWR []packageWithRepository, maxRuntimeInMinutes time.Duration, pollIntervalsInSeconds time.Duration) error {
func pollReserveNextPackages(pckgWR []aakaas.PackageWithRepository, maxRuntimeInMinutes time.Duration, pollIntervalsInSeconds time.Duration) error {
timeout := time.After(maxRuntimeInMinutes)
ticker := time.Tick(pollIntervalsInSeconds)
for {
Expand All @@ -73,24 +75,24 @@ func pollReserveNextPackages(pckgWR []packageWithRepository, maxRuntimeInMinutes
case <-ticker:
var allFinished bool = true
for i := range pckgWR {
err := pckgWR[i].p.get()
err := pckgWR[i].Package.GetPackageAndNamespace()
// if there is an error, reservation is not yet finished
if err != nil {
log.Entry().Infof("Reservation of %s is not yet finished, check again in %s", pckgWR[i].p.PackageName, pollIntervalsInSeconds)
log.Entry().Infof("Reservation of %s is not yet finished, check again in %s", pckgWR[i].Package.PackageName, pollIntervalsInSeconds)
allFinished = false
} else {
switch pckgWR[i].p.Status {
case locked:
return fmt.Errorf("Package %s has invalid status 'locked'", pckgWR[i].p.PackageName)
case creationTriggered:
log.Entry().Infof("Reservation of %s is still running with status 'creation triggered', check again in %s", pckgWR[i].p.PackageName, pollIntervalsInSeconds)
switch pckgWR[i].Package.Status {
case aakaas.PackageStatusLocked:
return fmt.Errorf("Package %s has invalid status 'locked'", pckgWR[i].Package.PackageName)
case aakaas.PackageStatusCreationTriggered:
log.Entry().Infof("Reservation of %s is still running with status 'creation triggered', check again in %s", pckgWR[i].Package.PackageName, pollIntervalsInSeconds)
allFinished = false
case planned:
log.Entry().Infof("Reservation of %s was succesful with status 'planned'", pckgWR[i].p.PackageName)
case released:
log.Entry().Infof("Reservation of %s not needed, package is already in status 'released'", pckgWR[i].p.PackageName)
case aakaas.PackageStatusPlanned:
log.Entry().Infof("Reservation of %s was succesful with status 'planned'", pckgWR[i].Package.PackageName)
case aakaas.PackageStatusReleased:
log.Entry().Infof("Reservation of %s not needed, package is already in status 'released'", pckgWR[i].Package.PackageName)
default:
return fmt.Errorf("Package %s has unknown status '%s'", pckgWR[i].p.PackageName, pckgWR[i].p.Status)
return fmt.Errorf("Package %s has unknown status '%s'", pckgWR[i].Package.PackageName, pckgWR[i].Package.Status)
}
}
}
Expand All @@ -102,107 +104,20 @@ func pollReserveNextPackages(pckgWR []packageWithRepository, maxRuntimeInMinutes
}
}

func reservePackages(repositories []abaputils.Repository, conn connector) ([]packageWithRepository, error) {
var packagesWithRepos []packageWithRepository
func reservePackages(repositories []abaputils.Repository, conn abapbuild.Connector) ([]aakaas.PackageWithRepository, error) {
var packagesWithRepos []aakaas.PackageWithRepository
for i := range repositories {
var p pckg
p.init(repositories[i], conn)
err := p.reserveNext()
var p aakaas.Package
p.InitPackage(repositories[i], conn)
err := p.ReserveNext()
if err != nil {
return packagesWithRepos, err
}
pWR := packageWithRepository{
p: p,
repo: repositories[i],
pWR := aakaas.PackageWithRepository{
Package: p,
Repo: repositories[i],
}
packagesWithRepos = append(packagesWithRepos, pWR)
}
return packagesWithRepos, nil
}

func (p *pckg) init(repo abaputils.Repository, conn connector) {
p.connector = conn
p.ComponentName = repo.Name
p.VersionYAML = repo.VersionYAML
p.PackageName = repo.PackageName
p.Status = packageStatus(repo.Status)
}

func (p *pckg) copyFieldsToRepo(initialRepo *abaputils.Repository) {
initialRepo.PackageName = p.PackageName
initialRepo.PackageType = p.Type
initialRepo.PredecessorCommitID = p.PredecessorCommitID
initialRepo.Status = string(p.Status)
initialRepo.Namespace = p.Namespace
log.Entry().Infof("Package name %s, type %s, status %s, namespace %s, predecessorCommitID %s", p.PackageName, p.Type, p.Status, p.Namespace, p.PredecessorCommitID)
}

func (p *pckg) reserveNext() error {
if p.ComponentName == "" || p.VersionYAML == "" {
return errors.New("Parameters missing. Please provide the name and version of the component")
}
log.Entry().Infof("Reserve package for %s version %s", p.ComponentName, p.VersionYAML)
p.connector.getToken("/odata/aas_ocs_package")
appendum := "/odata/aas_ocs_package/DeterminePackageForScv?Name='" + p.ComponentName + "'&Version='" + p.VersionYAML + "'"
body, err := p.connector.post(appendum, "")
if err != nil {
return err
}
var jPck jsonPackageDeterminePackageForScv
json.Unmarshal(body, &jPck)
p.PackageName = jPck.DeterminePackage.Package.PackageName
p.Type = jPck.DeterminePackage.Package.Type
p.PredecessorCommitID = jPck.DeterminePackage.Package.PredecessorCommitID
p.Status = jPck.DeterminePackage.Package.Status
p.Namespace = jPck.DeterminePackage.Package.Namespace
log.Entry().Infof("Reservation of package %s started", p.PackageName)
return nil
}

func (p *pckg) get() error {
appendum := "/odata/aas_ocs_package/OcsPackageSet('" + p.PackageName + "')"
body, err := p.connector.get(appendum)
if err != nil {
return err
}
var jPck jsonPackage
json.Unmarshal(body, &jPck)
p.Status = jPck.Package.Status
p.Namespace = jPck.Package.Namespace
return nil
}

type packageStatus string

const (
planned packageStatus = "P"
locked packageStatus = "L"
released packageStatus = "R"
creationTriggered packageStatus = "C"
)

type jsonPackageDeterminePackageForScv struct {
DeterminePackage struct {
Package *pckg `json:"DeterminePackageForScv"`
} `json:"d"`
}

type jsonPackage struct {
Package *pckg `json:"d"`
}

type pckg struct {
connector
ComponentName string
PackageName string `json:"Name"`
VersionYAML string
Type string `json:"Type"`
PredecessorCommitID string `json:"PredecessorCommitId"`
Status packageStatus `json:"Status"`
Namespace string `json:"Namespace"`
}

type packageWithRepository struct {
p pckg
repo abaputils.Repository
}
27 changes: 16 additions & 11 deletions cmd/abapAddonAssemblyKitReserveNextPackages_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestAbapAddonAssemblyKitReserveNextPackagesCommand(t *testing.T) {

testCmd := AbapAddonAssemblyKitReserveNextPackagesCommand()

// only high level testing performed - details are tested in step generation procudure
// only high level testing performed - details are tested in step generation procedure
assert.Equal(t, "abapAddonAssemblyKitReserveNextPackages", testCmd.Use, "command name incorrect")

}
Loading

0 comments on commit 64e771e

Please sign in to comment.