github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/apiKeyValueMapDownload_generated.go (about)

     1  // Code generated by piper's step-generator. DO NOT EDIT.
     2  
     3  package cmd
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"time"
     9  
    10  	"github.com/SAP/jenkins-library/pkg/config"
    11  	"github.com/SAP/jenkins-library/pkg/log"
    12  	"github.com/SAP/jenkins-library/pkg/splunk"
    13  	"github.com/SAP/jenkins-library/pkg/telemetry"
    14  	"github.com/SAP/jenkins-library/pkg/validation"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  type apiKeyValueMapDownloadOptions struct {
    19  	APIServiceKey   string `json:"apiServiceKey,omitempty"`
    20  	KeyValueMapName string `json:"keyValueMapName,omitempty"`
    21  	DownloadPath    string `json:"downloadPath,omitempty"`
    22  }
    23  
    24  // ApiKeyValueMapDownloadCommand Download a specific Key Value Map from the API Portal
    25  func ApiKeyValueMapDownloadCommand() *cobra.Command {
    26  	const STEP_NAME = "apiKeyValueMapDownload"
    27  
    28  	metadata := apiKeyValueMapDownloadMetadata()
    29  	var stepConfig apiKeyValueMapDownloadOptions
    30  	var startTime time.Time
    31  	var logCollector *log.CollectorHook
    32  	var splunkClient *splunk.Splunk
    33  	telemetryClient := &telemetry.Telemetry{}
    34  
    35  	var createApiKeyValueMapDownloadCmd = &cobra.Command{
    36  		Use:   STEP_NAME,
    37  		Short: "Download a specific Key Value Map from the API Portal",
    38  		Long: `With this step you can download a specific Key Value Map from the API Portal, which returns a zip file with the Key Value Map contents in to current workspace using the OData API.
    39  Learn more about the SAP API Management API for downloading an Key Value Map artifact [here](https://help.sap.com/viewer/66d066d903c2473f81ec33acfe2ccdb4/Cloud/en-US/e26b3320cd534ae4bc743af8013a8abb.html).`,
    40  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    41  			startTime = time.Now()
    42  			log.SetStepName(STEP_NAME)
    43  			log.SetVerbose(GeneralConfig.Verbose)
    44  
    45  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    46  
    47  			path, _ := os.Getwd()
    48  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    49  			log.RegisterHook(fatalHook)
    50  
    51  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    52  			if err != nil {
    53  				log.SetErrorCategory(log.ErrorConfiguration)
    54  				return err
    55  			}
    56  			log.RegisterSecret(stepConfig.APIServiceKey)
    57  
    58  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    59  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    60  				log.RegisterHook(&sentryHook)
    61  			}
    62  
    63  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    64  				splunkClient = &splunk.Splunk{}
    65  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    66  				log.RegisterHook(logCollector)
    67  			}
    68  
    69  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
    70  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
    71  			}
    72  
    73  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    74  			if err != nil {
    75  				return err
    76  			}
    77  			if err = validation.ValidateStruct(stepConfig); err != nil {
    78  				log.SetErrorCategory(log.ErrorConfiguration)
    79  				return err
    80  			}
    81  
    82  			return nil
    83  		},
    84  		Run: func(_ *cobra.Command, _ []string) {
    85  			stepTelemetryData := telemetry.CustomData{}
    86  			stepTelemetryData.ErrorCode = "1"
    87  			handler := func() {
    88  				config.RemoveVaultSecretFiles()
    89  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    90  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    91  				stepTelemetryData.PiperCommitHash = GitCommit
    92  				telemetryClient.SetData(&stepTelemetryData)
    93  				telemetryClient.Send()
    94  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    95  					splunkClient.Initialize(GeneralConfig.CorrelationID,
    96  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
    97  						GeneralConfig.HookConfig.SplunkConfig.Token,
    98  						GeneralConfig.HookConfig.SplunkConfig.Index,
    99  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   100  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   101  				}
   102  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   103  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   104  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   105  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   106  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   107  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   108  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   109  				}
   110  			}
   111  			log.DeferExitHandler(handler)
   112  			defer handler()
   113  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   114  			apiKeyValueMapDownload(stepConfig, &stepTelemetryData)
   115  			stepTelemetryData.ErrorCode = "0"
   116  			log.Entry().Info("SUCCESS")
   117  		},
   118  	}
   119  
   120  	addApiKeyValueMapDownloadFlags(createApiKeyValueMapDownloadCmd, &stepConfig)
   121  	return createApiKeyValueMapDownloadCmd
   122  }
   123  
   124  func addApiKeyValueMapDownloadFlags(cmd *cobra.Command, stepConfig *apiKeyValueMapDownloadOptions) {
   125  	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'")
   126  	cmd.Flags().StringVar(&stepConfig.KeyValueMapName, "keyValueMapName", os.Getenv("PIPER_keyValueMapName"), "Specifies the name of the Key Value Map.")
   127  	cmd.Flags().StringVar(&stepConfig.DownloadPath, "downloadPath", os.Getenv("PIPER_downloadPath"), "Specifies Key Value Map download CSV file location.")
   128  
   129  	cmd.MarkFlagRequired("apiServiceKey")
   130  	cmd.MarkFlagRequired("keyValueMapName")
   131  	cmd.MarkFlagRequired("downloadPath")
   132  }
   133  
   134  // retrieve step metadata
   135  func apiKeyValueMapDownloadMetadata() config.StepData {
   136  	var theMetaData = config.StepData{
   137  		Metadata: config.StepMetadata{
   138  			Name:        "apiKeyValueMapDownload",
   139  			Aliases:     []config.Alias{},
   140  			Description: "Download a specific Key Value Map from the API Portal",
   141  		},
   142  		Spec: config.StepSpec{
   143  			Inputs: config.StepInputs{
   144  				Secrets: []config.StepSecrets{
   145  					{Name: "apimApiServiceKeyCredentialsId", Description: "Jenkins secret text credential ID containing the service key to the API Management Runtime service instance of plan 'api'", Type: "jenkins"},
   146  				},
   147  				Parameters: []config.StepParameters{
   148  					{
   149  						Name: "apiServiceKey",
   150  						ResourceRef: []config.ResourceReference{
   151  							{
   152  								Name:  "apimApiServiceKeyCredentialsId",
   153  								Param: "apiServiceKey",
   154  								Type:  "secret",
   155  							},
   156  						},
   157  						Scope:     []string{"PARAMETERS"},
   158  						Type:      "string",
   159  						Mandatory: true,
   160  						Aliases:   []config.Alias{},
   161  						Default:   os.Getenv("PIPER_apiServiceKey"),
   162  					},
   163  					{
   164  						Name:        "keyValueMapName",
   165  						ResourceRef: []config.ResourceReference{},
   166  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   167  						Type:        "string",
   168  						Mandatory:   true,
   169  						Aliases:     []config.Alias{},
   170  						Default:     os.Getenv("PIPER_keyValueMapName"),
   171  					},
   172  					{
   173  						Name:        "downloadPath",
   174  						ResourceRef: []config.ResourceReference{},
   175  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   176  						Type:        "string",
   177  						Mandatory:   true,
   178  						Aliases:     []config.Alias{},
   179  						Default:     os.Getenv("PIPER_downloadPath"),
   180  					},
   181  				},
   182  			},
   183  		},
   184  	}
   185  	return theMetaData
   186  }