1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/abaputils/addonDescriptor.go

108 lines
3.1 KiB
Go
Raw Normal View History

AAKaaS check steps to run without prior PV input (#2091) * 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 * AAKaaS Checks as First Step * remove old coding 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: Chris <42861202+bluesbrother84@users.noreply.github.com>
2020-09-30 10:30:53 +02:00
package abaputils
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
)
// AddonDescriptor contains fields about the addonProduct
type AddonDescriptor struct {
AddonProduct string `json:"addonProduct"`
AddonVersionYAML string `json:"addonVersion"`
AddonVersion string `json:"addonVersionAAK"`
AddonUniqueID string `json:"addonUniqueID"`
CustomerID interface{} `json:"customerID"`
AddonSpsLevel string
AddonPatchLevel string
TargetVectorID string
Repositories []Repository `json:"repositories"`
}
// Repository contains fields for the repository/component version
type Repository struct {
Name string `json:"name"`
Tag string `json:"tag"`
Branch string `json:"branch"`
VersionYAML string `json:"version"`
Version string `json:"versionAAK"`
PackageName string
PackageType string
SpLevel string
PatchLevel string
PredecessorCommitID string
Status string
Namespace string
SarXMLFilePath string
}
// ReadAddonDescriptorType is the type for ReadAddonDescriptor for mocking
type ReadAddonDescriptorType func(FileName string) (AddonDescriptor, error)
// ReadAddonDescriptor parses AddonDescriptor YAML file
func ReadAddonDescriptor(FileName string) (AddonDescriptor, error) {
var addonDescriptor AddonDescriptor
err := addonDescriptor.initFromYmlFile(FileName)
return addonDescriptor, err
}
// ConstructAddonDescriptorFromJSON : Create new AddonDescriptor filled with data from JSON
func ConstructAddonDescriptorFromJSON(JSON []byte) (AddonDescriptor, error) {
var addonDescriptor AddonDescriptor
err := addonDescriptor.initFromJSON(JSON)
return addonDescriptor, err
}
// initFromYmlFile : Reads from file
func (me *AddonDescriptor) initFromYmlFile(FileName string) error {
filelocation, err := filepath.Glob(FileName)
if err != nil || len(filelocation) != 1 {
return errors.New(fmt.Sprintf("Could not find %v", FileName))
}
filename, err := filepath.Abs(filelocation[0])
if err != nil {
return errors.New(fmt.Sprintf("Could not get path of %v", FileName))
}
var yamlBytes []byte
yamlBytes, err = ioutil.ReadFile(filename)
if err != nil {
return errors.New(fmt.Sprintf("Could not read %v", FileName))
}
var jsonBytes []byte
jsonBytes, err = yaml.YAMLToJSON(yamlBytes)
if err != nil {
return errors.New(fmt.Sprintf("Could not parse %v", FileName))
}
err = me.initFromJSON(jsonBytes)
if err != nil {
return errors.New(fmt.Sprintf("Could not unmarshal %v", FileName))
}
return nil
}
// initFromJSON : Init from json
func (me *AddonDescriptor) initFromJSON(JSON []byte) error {
return json.Unmarshal(JSON, me)
}
// AsJSON : dito
func (me *AddonDescriptor) AsJSON() []byte {
//hopefully no errors should happen here or they are covered by the users unit tests
jsonBytes, _ := json.Marshal(me)
return jsonBytes
}
// SetRepositories : dito
func (me *AddonDescriptor) SetRepositories(Repositories []Repository) {
me.Repositories = Repositories
}