mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
64a00c540a
* added store file function in cpi common utils Change-Id: Ia429a2792266e082d139025a71799d21c30a7df9 * Added api provider steps Change-Id: Icd2829a91db4c53d0de2330822d2b33933973868 * Update apiProviderDownload.yaml * fixed yaml JLINT issue Change-Id: Iac974abc30fa00e68c0177072b93716b0af5e0c5 * Removed trailing spaces Change-Id: I927e9314fce6e9cab68d6b97577c7c96bb2bddad * Resolved common steps groovy script conflict Change-Id: I3ad144b618e1c77953aaeccaa5bf7309aff77ca9 * Change for conflict resolution Change-Id: Ic955833eca844f090b7983f99f9d3649ebb981c7 * Fixed method name and its corresponding implementation Change-Id: I465c1f1d5306bb978386de9efca3c521e385b89c * Moved re-usable function to commonUtils package Change-Id: Ide06462b01caeb2bf438ad7661e01c15bf8e8e24 * Changed the implementation to use existing writeFile method * Fixed review comments on documentation and test structuring Change-Id: Ifebd2f4b50754b2097b2d564fb3cc37c433ef6c9 * Fixed documentation alignment issues * Fixed spaces issue Change-Id: I834bd94e01bce72e7f81ab49ba32671c91c66ca9 * Documentation removed extra spaces Change-Id: I9a639d76ed9b81c870f18349504044bb70753b52 * Fixed doc build issue Change-Id: I96c3e15e73834b64f8b8e3432ce59f6b037f93fd * Fixed documentation build issues Change-Id: I7fca2ba69bc7b7298ee300ccd1ae16a6238dc96b * Re-generated code for fixing build failure Change-Id: I22b7ee6162f643d9f3b60f6a33eb7858927182a0 * Adopted file utils & mock Change-Id: Ic46462003527f41df64395a5a615c19bf374e8ef * Removed ioutil call in the test & adopted error variable names * Removed commented lines Change-Id: I99a12e39bc04323e9c19f1409d97eeca267e6fdb * Added test for asserting file download and adopted error variables Change-Id: I49463a3b75987bf68f5261d45602d2d7bd960a05 * Added download path assertion positive & negative case Change-Id: Ieee461c3973b9dfa8f395dc936e4241ff9694c7b * Modified tests with DownloadPath variable Change-Id: Iaf14c9ea1a8242b6c8d8e9e4fac8c23d9c1b3a74 * Added testcase to validate file content Change-Id: I21aed481b433450c3b536dbb29d45291f61848d8 * Refactored test for file content check to avoid failures Change-Id: I3b4fe9a0de678f437fd4cc0a8203ae9434d9fa8e * Removed auto-generated comments Change-Id: I86c4ac3e7e4476a75d6cbed58826ec1f3278d7d2 * Fixed documentation review comments Change-Id: I4faf31473b53fc53a5517d418c343bf7320eec55 * Fixed documentation indentation Change-Id: I386f046cf4e10ee6deb5a81fcfc8c430c97086c8 * Fix build issue Change-Id: I61a829cabaf03ffd5e77cddc594486a650118fa3
176 lines
6.4 KiB
Go
176 lines
6.4 KiB
Go
// Code generated by piper's step-generator. DO NOT EDIT.
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/config"
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
"github.com/SAP/jenkins-library/pkg/splunk"
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
|
"github.com/SAP/jenkins-library/pkg/validation"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
type apiProviderDownloadOptions struct {
|
|
APIServiceKey string `json:"apiServiceKey,omitempty"`
|
|
APIProviderName string `json:"apiProviderName,omitempty"`
|
|
DownloadPath string `json:"downloadPath,omitempty"`
|
|
}
|
|
|
|
// ApiProviderDownloadCommand Download a specific API Provider from the API Portal
|
|
func ApiProviderDownloadCommand() *cobra.Command {
|
|
const STEP_NAME = "apiProviderDownload"
|
|
|
|
metadata := apiProviderDownloadMetadata()
|
|
var stepConfig apiProviderDownloadOptions
|
|
var startTime time.Time
|
|
var logCollector *log.CollectorHook
|
|
var splunkClient *splunk.Splunk
|
|
telemetryClient := &telemetry.Telemetry{}
|
|
|
|
var createApiProviderDownloadCmd = &cobra.Command{
|
|
Use: STEP_NAME,
|
|
Short: "Download a specific API Provider from the API Portal",
|
|
Long: `With this step you can download a specific API Provider from the API Portal, which returns a JSON file with the api provider contents in to current workspace using the OData API. Learn more about the SAP API Management API for downloading an api provider artifact [here](https://api.sap.com/api/APIPortal_CF/overview).`,
|
|
PreRunE: func(cmd *cobra.Command, _ []string) error {
|
|
startTime = time.Now()
|
|
log.SetStepName(STEP_NAME)
|
|
log.SetVerbose(GeneralConfig.Verbose)
|
|
|
|
GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
|
|
|
|
path, _ := os.Getwd()
|
|
fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
|
|
log.RegisterHook(fatalHook)
|
|
|
|
err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
|
|
if err != nil {
|
|
log.SetErrorCategory(log.ErrorConfiguration)
|
|
return err
|
|
}
|
|
log.RegisterSecret(stepConfig.APIServiceKey)
|
|
|
|
if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
|
|
sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
|
|
log.RegisterHook(&sentryHook)
|
|
}
|
|
|
|
if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
|
|
splunkClient = &splunk.Splunk{}
|
|
logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
|
|
log.RegisterHook(logCollector)
|
|
}
|
|
|
|
validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = validation.ValidateStruct(stepConfig); err != nil {
|
|
log.SetErrorCategory(log.ErrorConfiguration)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
},
|
|
Run: func(_ *cobra.Command, _ []string) {
|
|
stepTelemetryData := telemetry.CustomData{}
|
|
stepTelemetryData.ErrorCode = "1"
|
|
handler := func() {
|
|
config.RemoveVaultSecretFiles()
|
|
stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
|
|
stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
|
|
stepTelemetryData.PiperCommitHash = GitCommit
|
|
telemetryClient.SetData(&stepTelemetryData)
|
|
telemetryClient.Send()
|
|
if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
|
|
splunkClient.Send(telemetryClient.GetData(), logCollector)
|
|
}
|
|
}
|
|
log.DeferExitHandler(handler)
|
|
defer handler()
|
|
telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
|
|
if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
|
|
splunkClient.Initialize(GeneralConfig.CorrelationID,
|
|
GeneralConfig.HookConfig.SplunkConfig.Dsn,
|
|
GeneralConfig.HookConfig.SplunkConfig.Token,
|
|
GeneralConfig.HookConfig.SplunkConfig.Index,
|
|
GeneralConfig.HookConfig.SplunkConfig.SendLogs)
|
|
}
|
|
apiProviderDownload(stepConfig, &stepTelemetryData)
|
|
stepTelemetryData.ErrorCode = "0"
|
|
log.Entry().Info("SUCCESS")
|
|
},
|
|
}
|
|
|
|
addApiProviderDownloadFlags(createApiProviderDownloadCmd, &stepConfig)
|
|
return createApiProviderDownloadCmd
|
|
}
|
|
|
|
func addApiProviderDownloadFlags(cmd *cobra.Command, stepConfig *apiProviderDownloadOptions) {
|
|
cmd.Flags().StringVar(&stepConfig.APIServiceKey, "apiServiceKey", os.Getenv("PIPER_apiServiceKey"), "Service key JSON string to access the API Management Runtime service instance of plan 'api'")
|
|
cmd.Flags().StringVar(&stepConfig.APIProviderName, "apiProviderName", os.Getenv("PIPER_apiProviderName"), "Specifies the name of the API Provider.")
|
|
cmd.Flags().StringVar(&stepConfig.DownloadPath, "downloadPath", os.Getenv("PIPER_downloadPath"), "Specifies api provider download directory location. The file name must not be included in the path.")
|
|
|
|
cmd.MarkFlagRequired("apiServiceKey")
|
|
cmd.MarkFlagRequired("apiProviderName")
|
|
cmd.MarkFlagRequired("downloadPath")
|
|
}
|
|
|
|
// retrieve step metadata
|
|
func apiProviderDownloadMetadata() config.StepData {
|
|
var theMetaData = config.StepData{
|
|
Metadata: config.StepMetadata{
|
|
Name: "apiProviderDownload",
|
|
Aliases: []config.Alias{},
|
|
Description: "Download a specific API Provider from the API Portal",
|
|
},
|
|
Spec: config.StepSpec{
|
|
Inputs: config.StepInputs{
|
|
Secrets: []config.StepSecrets{
|
|
{Name: "apimApiServiceKeyCredentialsId", Description: "Jenkins secret text credential ID containing the service key to the API Management Runtime service instance of plan 'api'", Type: "jenkins"},
|
|
},
|
|
Parameters: []config.StepParameters{
|
|
{
|
|
Name: "apiServiceKey",
|
|
ResourceRef: []config.ResourceReference{
|
|
{
|
|
Name: "apimApiServiceKeyCredentialsId",
|
|
Param: "apiServiceKey",
|
|
Type: "secret",
|
|
},
|
|
},
|
|
Scope: []string{"PARAMETERS"},
|
|
Type: "string",
|
|
Mandatory: true,
|
|
Aliases: []config.Alias{},
|
|
Default: os.Getenv("PIPER_apiServiceKey"),
|
|
},
|
|
{
|
|
Name: "apiProviderName",
|
|
ResourceRef: []config.ResourceReference{},
|
|
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
|
Type: "string",
|
|
Mandatory: true,
|
|
Aliases: []config.Alias{},
|
|
Default: os.Getenv("PIPER_apiProviderName"),
|
|
},
|
|
{
|
|
Name: "downloadPath",
|
|
ResourceRef: []config.ResourceReference{},
|
|
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
|
Type: "string",
|
|
Mandatory: true,
|
|
Aliases: []config.Alias{},
|
|
Default: os.Getenv("PIPER_downloadPath"),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
return theMetaData
|
|
}
|