github.com/xgoffin/jenkins-library@v1.154.0/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  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    70  			if err != nil {
    71  				return err
    72  			}
    73  			if err = validation.ValidateStruct(stepConfig); err != nil {
    74  				log.SetErrorCategory(log.ErrorConfiguration)
    75  				return err
    76  			}
    77  
    78  			return nil
    79  		},
    80  		Run: func(_ *cobra.Command, _ []string) {
    81  			stepTelemetryData := telemetry.CustomData{}
    82  			stepTelemetryData.ErrorCode = "1"
    83  			handler := func() {
    84  				config.RemoveVaultSecretFiles()
    85  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    86  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    87  				stepTelemetryData.PiperCommitHash = GitCommit
    88  				telemetryClient.SetData(&stepTelemetryData)
    89  				telemetryClient.Send()
    90  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    91  					splunkClient.Send(telemetryClient.GetData(), logCollector)
    92  				}
    93  			}
    94  			log.DeferExitHandler(handler)
    95  			defer handler()
    96  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
    97  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    98  				splunkClient.Initialize(GeneralConfig.CorrelationID,
    99  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   100  					GeneralConfig.HookConfig.SplunkConfig.Token,
   101  					GeneralConfig.HookConfig.SplunkConfig.Index,
   102  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   103  			}
   104  			apiKeyValueMapDownload(stepConfig, &stepTelemetryData)
   105  			stepTelemetryData.ErrorCode = "0"
   106  			log.Entry().Info("SUCCESS")
   107  		},
   108  	}
   109  
   110  	addApiKeyValueMapDownloadFlags(createApiKeyValueMapDownloadCmd, &stepConfig)
   111  	return createApiKeyValueMapDownloadCmd
   112  }
   113  
   114  func addApiKeyValueMapDownloadFlags(cmd *cobra.Command, stepConfig *apiKeyValueMapDownloadOptions) {
   115  	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'")
   116  	cmd.Flags().StringVar(&stepConfig.KeyValueMapName, "keyValueMapName", os.Getenv("PIPER_keyValueMapName"), "Specifies the name of the Key Value Map.")
   117  	cmd.Flags().StringVar(&stepConfig.DownloadPath, "downloadPath", os.Getenv("PIPER_downloadPath"), "Specifies Key Value Map download CSV file location.")
   118  
   119  	cmd.MarkFlagRequired("apiServiceKey")
   120  	cmd.MarkFlagRequired("keyValueMapName")
   121  	cmd.MarkFlagRequired("downloadPath")
   122  }
   123  
   124  // retrieve step metadata
   125  func apiKeyValueMapDownloadMetadata() config.StepData {
   126  	var theMetaData = config.StepData{
   127  		Metadata: config.StepMetadata{
   128  			Name:        "apiKeyValueMapDownload",
   129  			Aliases:     []config.Alias{},
   130  			Description: "Download a specific Key Value Map from the API Portal",
   131  		},
   132  		Spec: config.StepSpec{
   133  			Inputs: config.StepInputs{
   134  				Secrets: []config.StepSecrets{
   135  					{Name: "apimApiServiceKeyCredentialsId", Description: "Jenkins secret text credential ID containing the service key to the API Management Runtime service instance of plan 'api'", Type: "jenkins"},
   136  				},
   137  				Parameters: []config.StepParameters{
   138  					{
   139  						Name: "apiServiceKey",
   140  						ResourceRef: []config.ResourceReference{
   141  							{
   142  								Name:  "apimApiServiceKeyCredentialsId",
   143  								Param: "apiServiceKey",
   144  								Type:  "secret",
   145  							},
   146  						},
   147  						Scope:     []string{"PARAMETERS"},
   148  						Type:      "string",
   149  						Mandatory: true,
   150  						Aliases:   []config.Alias{},
   151  						Default:   os.Getenv("PIPER_apiServiceKey"),
   152  					},
   153  					{
   154  						Name:        "keyValueMapName",
   155  						ResourceRef: []config.ResourceReference{},
   156  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   157  						Type:        "string",
   158  						Mandatory:   true,
   159  						Aliases:     []config.Alias{},
   160  						Default:     os.Getenv("PIPER_keyValueMapName"),
   161  					},
   162  					{
   163  						Name:        "downloadPath",
   164  						ResourceRef: []config.ResourceReference{},
   165  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   166  						Type:        "string",
   167  						Mandatory:   true,
   168  						Aliases:     []config.Alias{},
   169  						Default:     os.Getenv("PIPER_downloadPath"),
   170  					},
   171  				},
   172  			},
   173  		},
   174  	}
   175  	return theMetaData
   176  }