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