github.com/SAP/jenkins-library@v1.362.0/cmd/ascAppUpload_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 ascAppUploadOptions struct {
    19  	ServerURL          string `json:"serverUrl,omitempty"`
    20  	AppToken           string `json:"appToken,omitempty"`
    21  	AppID              string `json:"appId,omitempty"`
    22  	FilePath           string `json:"filePath,omitempty"`
    23  	JamfTargetSystem   string `json:"jamfTargetSystem,omitempty"`
    24  	ReleaseAppVersion  string `json:"releaseAppVersion,omitempty"`
    25  	ReleaseDescription string `json:"releaseDescription,omitempty"`
    26  	ReleaseDate        string `json:"releaseDate,omitempty"`
    27  	ReleaseVisible     bool   `json:"releaseVisible,omitempty"`
    28  }
    29  
    30  // AscAppUploadCommand Upload an app to ASC
    31  func AscAppUploadCommand() *cobra.Command {
    32  	const STEP_NAME = "ascAppUpload"
    33  
    34  	metadata := ascAppUploadMetadata()
    35  	var stepConfig ascAppUploadOptions
    36  	var startTime time.Time
    37  	var logCollector *log.CollectorHook
    38  	var splunkClient *splunk.Splunk
    39  	telemetryClient := &telemetry.Telemetry{}
    40  
    41  	var createAscAppUploadCmd = &cobra.Command{
    42  		Use:   STEP_NAME,
    43  		Short: "Upload an app to ASC",
    44  		Long: `With this step you can upload an app to ASC.
    45  It creates a new release note in ASC and uploads the binary to ASC and therewith to Jamf.
    46  For more information about ASC, check out [Application Support Center](https://github.com/SAP/application-support-center).`,
    47  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    48  			startTime = time.Now()
    49  			log.SetStepName(STEP_NAME)
    50  			log.SetVerbose(GeneralConfig.Verbose)
    51  
    52  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    53  
    54  			path, _ := os.Getwd()
    55  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    56  			log.RegisterHook(fatalHook)
    57  
    58  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    59  			if err != nil {
    60  				log.SetErrorCategory(log.ErrorConfiguration)
    61  				return err
    62  			}
    63  			log.RegisterSecret(stepConfig.AppToken)
    64  
    65  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    66  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    67  				log.RegisterHook(&sentryHook)
    68  			}
    69  
    70  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 || len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
    71  				splunkClient = &splunk.Splunk{}
    72  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    73  				log.RegisterHook(logCollector)
    74  			}
    75  
    76  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
    77  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
    78  			}
    79  
    80  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    81  			if err != nil {
    82  				return err
    83  			}
    84  			if err = validation.ValidateStruct(stepConfig); err != nil {
    85  				log.SetErrorCategory(log.ErrorConfiguration)
    86  				return err
    87  			}
    88  
    89  			return nil
    90  		},
    91  		Run: func(_ *cobra.Command, _ []string) {
    92  			stepTelemetryData := telemetry.CustomData{}
    93  			stepTelemetryData.ErrorCode = "1"
    94  			handler := func() {
    95  				config.RemoveVaultSecretFiles()
    96  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    97  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    98  				stepTelemetryData.PiperCommitHash = GitCommit
    99  				telemetryClient.SetData(&stepTelemetryData)
   100  				telemetryClient.Send()
   101  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   102  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   103  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
   104  						GeneralConfig.HookConfig.SplunkConfig.Token,
   105  						GeneralConfig.HookConfig.SplunkConfig.Index,
   106  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   107  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   108  				}
   109  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   110  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   111  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   112  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   113  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   114  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   115  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   116  				}
   117  			}
   118  			log.DeferExitHandler(handler)
   119  			defer handler()
   120  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME, GeneralConfig.HookConfig.PendoConfig.Token)
   121  			ascAppUpload(stepConfig, &stepTelemetryData)
   122  			stepTelemetryData.ErrorCode = "0"
   123  			log.Entry().Info("SUCCESS")
   124  		},
   125  	}
   126  
   127  	addAscAppUploadFlags(createAscAppUploadCmd, &stepConfig)
   128  	return createAscAppUploadCmd
   129  }
   130  
   131  func addAscAppUploadFlags(cmd *cobra.Command, stepConfig *ascAppUploadOptions) {
   132  	cmd.Flags().StringVar(&stepConfig.ServerURL, "serverUrl", os.Getenv("PIPER_serverUrl"), "The URL to the ASC backend")
   133  	cmd.Flags().StringVar(&stepConfig.AppToken, "appToken", os.Getenv("PIPER_appToken"), "App token used to authenticate with the ASC backend")
   134  	cmd.Flags().StringVar(&stepConfig.AppID, "appId", os.Getenv("PIPER_appId"), "The app ID in ASC")
   135  	cmd.Flags().StringVar(&stepConfig.FilePath, "filePath", os.Getenv("PIPER_filePath"), "The path to the app binary")
   136  	cmd.Flags().StringVar(&stepConfig.JamfTargetSystem, "jamfTargetSystem", os.Getenv("PIPER_jamfTargetSystem"), "The jamf target system")
   137  	cmd.Flags().StringVar(&stepConfig.ReleaseAppVersion, "releaseAppVersion", `Pending Release`, "The new app version name to be created in ASC")
   138  	cmd.Flags().StringVar(&stepConfig.ReleaseDescription, "releaseDescription", `<p>TBD</p>`, "The new release description")
   139  	cmd.Flags().StringVar(&stepConfig.ReleaseDate, "releaseDate", os.Getenv("PIPER_releaseDate"), "The new release date (Format: MM/DD/YYYY) Default is the current date")
   140  	cmd.Flags().BoolVar(&stepConfig.ReleaseVisible, "releaseVisible", false, "The new release visible flag")
   141  
   142  	cmd.MarkFlagRequired("serverUrl")
   143  	cmd.MarkFlagRequired("appId")
   144  	cmd.MarkFlagRequired("filePath")
   145  	cmd.MarkFlagRequired("jamfTargetSystem")
   146  }
   147  
   148  // retrieve step metadata
   149  func ascAppUploadMetadata() config.StepData {
   150  	var theMetaData = config.StepData{
   151  		Metadata: config.StepMetadata{
   152  			Name:        "ascAppUpload",
   153  			Aliases:     []config.Alias{},
   154  			Description: "Upload an app to ASC",
   155  		},
   156  		Spec: config.StepSpec{
   157  			Inputs: config.StepInputs{
   158  				Secrets: []config.StepSecrets{
   159  					{Name: "ascAppTokenCredentialsId", Description: "Jenkins secret text credential ID containing the authentication token for the ASC app", Type: "jenkins"},
   160  				},
   161  				Parameters: []config.StepParameters{
   162  					{
   163  						Name:        "serverUrl",
   164  						ResourceRef: []config.ResourceReference{},
   165  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   166  						Type:        "string",
   167  						Mandatory:   true,
   168  						Aliases:     []config.Alias{{Name: "ascServerUrl"}},
   169  						Default:     os.Getenv("PIPER_serverUrl"),
   170  					},
   171  					{
   172  						Name: "appToken",
   173  						ResourceRef: []config.ResourceReference{
   174  							{
   175  								Name:    "ascVaultSecretName",
   176  								Type:    "vaultSecret",
   177  								Default: "asc",
   178  							},
   179  
   180  							{
   181  								Name: "ascAppTokenCredentialsId",
   182  								Type: "secret",
   183  							},
   184  						},
   185  						Scope:     []string{"PARAMETERS"},
   186  						Type:      "string",
   187  						Mandatory: false,
   188  						Aliases:   []config.Alias{{Name: "ascAppToken"}},
   189  						Default:   os.Getenv("PIPER_appToken"),
   190  					},
   191  					{
   192  						Name:        "appId",
   193  						ResourceRef: []config.ResourceReference{},
   194  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   195  						Type:        "string",
   196  						Mandatory:   true,
   197  						Aliases:     []config.Alias{},
   198  						Default:     os.Getenv("PIPER_appId"),
   199  					},
   200  					{
   201  						Name:        "filePath",
   202  						ResourceRef: []config.ResourceReference{},
   203  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   204  						Type:        "string",
   205  						Mandatory:   true,
   206  						Aliases:     []config.Alias{},
   207  						Default:     os.Getenv("PIPER_filePath"),
   208  					},
   209  					{
   210  						Name:        "jamfTargetSystem",
   211  						ResourceRef: []config.ResourceReference{},
   212  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   213  						Type:        "string",
   214  						Mandatory:   true,
   215  						Aliases:     []config.Alias{},
   216  						Default:     os.Getenv("PIPER_jamfTargetSystem"),
   217  					},
   218  					{
   219  						Name:        "releaseAppVersion",
   220  						ResourceRef: []config.ResourceReference{},
   221  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   222  						Type:        "string",
   223  						Mandatory:   false,
   224  						Aliases:     []config.Alias{},
   225  						Default:     `Pending Release`,
   226  					},
   227  					{
   228  						Name:        "releaseDescription",
   229  						ResourceRef: []config.ResourceReference{},
   230  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   231  						Type:        "string",
   232  						Mandatory:   false,
   233  						Aliases:     []config.Alias{},
   234  						Default:     `<p>TBD</p>`,
   235  					},
   236  					{
   237  						Name:        "releaseDate",
   238  						ResourceRef: []config.ResourceReference{},
   239  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   240  						Type:        "string",
   241  						Mandatory:   false,
   242  						Aliases:     []config.Alias{},
   243  						Default:     os.Getenv("PIPER_releaseDate"),
   244  					},
   245  					{
   246  						Name:        "releaseVisible",
   247  						ResourceRef: []config.ResourceReference{},
   248  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   249  						Type:        "bool",
   250  						Mandatory:   false,
   251  						Aliases:     []config.Alias{},
   252  						Default:     false,
   253  					},
   254  				},
   255  			},
   256  		},
   257  	}
   258  	return theMetaData
   259  }