github.com/SAP/jenkins-library@v1.362.0/cmd/gcpPublishEvent_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 gcpPublishEventOptions struct { 19 VaultNamespace string `json:"vaultNamespace,omitempty"` 20 VaultServerURL string `json:"vaultServerUrl,omitempty"` 21 OIDCToken string `json:"OIDCToken,omitempty"` 22 GcpProjectNumber string `json:"gcpProjectNumber,omitempty"` 23 GcpWorkloadIDentityPool string `json:"gcpWorkloadIdentityPool,omitempty"` 24 GcpWorkloadIDentityPoolProvider string `json:"gcpWorkloadIdentityPoolProvider,omitempty"` 25 Topic string `json:"topic,omitempty"` 26 EventSource string `json:"eventSource,omitempty"` 27 EventType string `json:"eventType,omitempty"` 28 EventData string `json:"eventData,omitempty"` 29 AdditionalEventData string `json:"additionalEventData,omitempty"` 30 } 31 32 // GcpPublishEventCommand Publishes an event to GCP using OIDC authentication (beta) 33 func GcpPublishEventCommand() *cobra.Command { 34 const STEP_NAME = "gcpPublishEvent" 35 36 metadata := gcpPublishEventMetadata() 37 var stepConfig gcpPublishEventOptions 38 var startTime time.Time 39 var logCollector *log.CollectorHook 40 var splunkClient *splunk.Splunk 41 telemetryClient := &telemetry.Telemetry{} 42 43 var createGcpPublishEventCmd = &cobra.Command{ 44 Use: STEP_NAME, 45 Short: "Publishes an event to GCP using OIDC authentication (beta)", 46 Long: `This step is in beta. 47 Authentication to GCP is handled by an OIDC token received from, for example, Vault.`, 48 PreRunE: func(cmd *cobra.Command, _ []string) error { 49 startTime = time.Now() 50 log.SetStepName(STEP_NAME) 51 log.SetVerbose(GeneralConfig.Verbose) 52 53 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 54 55 path, _ := os.Getwd() 56 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 57 log.RegisterHook(fatalHook) 58 59 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 60 if err != nil { 61 log.SetErrorCategory(log.ErrorConfiguration) 62 return err 63 } 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 gcpPublishEvent(stepConfig, &stepTelemetryData) 122 stepTelemetryData.ErrorCode = "0" 123 log.Entry().Info("SUCCESS") 124 }, 125 } 126 127 addGcpPublishEventFlags(createGcpPublishEventCmd, &stepConfig) 128 return createGcpPublishEventCmd 129 } 130 131 func addGcpPublishEventFlags(cmd *cobra.Command, stepConfig *gcpPublishEventOptions) { 132 cmd.Flags().StringVar(&stepConfig.VaultNamespace, "vaultNamespace", os.Getenv("PIPER_vaultNamespace"), "") 133 cmd.Flags().StringVar(&stepConfig.VaultServerURL, "vaultServerUrl", os.Getenv("PIPER_vaultServerUrl"), "") 134 cmd.Flags().StringVar(&stepConfig.OIDCToken, "OIDCToken", os.Getenv("PIPER_OIDCToken"), "") 135 cmd.Flags().StringVar(&stepConfig.GcpProjectNumber, "gcpProjectNumber", os.Getenv("PIPER_gcpProjectNumber"), "") 136 cmd.Flags().StringVar(&stepConfig.GcpWorkloadIDentityPool, "gcpWorkloadIdentityPool", os.Getenv("PIPER_gcpWorkloadIdentityPool"), "A workload identity pool is an entity that lets you manage external identities.") 137 cmd.Flags().StringVar(&stepConfig.GcpWorkloadIDentityPoolProvider, "gcpWorkloadIdentityPoolProvider", os.Getenv("PIPER_gcpWorkloadIdentityPoolProvider"), "A workload identity pool provider is an entity that describes a relationship between Google Cloud and your IdP.") 138 cmd.Flags().StringVar(&stepConfig.Topic, "topic", os.Getenv("PIPER_topic"), "The pubsub topic to which the message is published.") 139 cmd.Flags().StringVar(&stepConfig.EventSource, "eventSource", os.Getenv("PIPER_eventSource"), "The events source as defined by CDEvents.") 140 cmd.Flags().StringVar(&stepConfig.EventType, "eventType", os.Getenv("PIPER_eventType"), "") 141 cmd.Flags().StringVar(&stepConfig.EventData, "eventData", os.Getenv("PIPER_eventData"), "Data to be merged with the generated data for the cloud event data field (JSON)") 142 cmd.Flags().StringVar(&stepConfig.AdditionalEventData, "additionalEventData", os.Getenv("PIPER_additionalEventData"), "Data (formatted as JSON string) to add to eventData. This can be used to enrich eventData that comes from the pipeline environment.") 143 144 } 145 146 // retrieve step metadata 147 func gcpPublishEventMetadata() config.StepData { 148 var theMetaData = config.StepData{ 149 Metadata: config.StepMetadata{ 150 Name: "gcpPublishEvent", 151 Aliases: []config.Alias{}, 152 Description: "Publishes an event to GCP using OIDC authentication (beta)", 153 }, 154 Spec: config.StepSpec{ 155 Inputs: config.StepInputs{ 156 Parameters: []config.StepParameters{ 157 { 158 Name: "vaultNamespace", 159 ResourceRef: []config.ResourceReference{}, 160 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 161 Type: "string", 162 Mandatory: false, 163 Aliases: []config.Alias{}, 164 Default: os.Getenv("PIPER_vaultNamespace"), 165 }, 166 { 167 Name: "vaultServerUrl", 168 ResourceRef: []config.ResourceReference{}, 169 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 170 Type: "string", 171 Mandatory: false, 172 Aliases: []config.Alias{}, 173 Default: os.Getenv("PIPER_vaultServerUrl"), 174 }, 175 { 176 Name: "OIDCToken", 177 ResourceRef: []config.ResourceReference{}, 178 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 179 Type: "string", 180 Mandatory: false, 181 Aliases: []config.Alias{}, 182 Default: os.Getenv("PIPER_OIDCToken"), 183 }, 184 { 185 Name: "gcpProjectNumber", 186 ResourceRef: []config.ResourceReference{}, 187 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 188 Type: "string", 189 Mandatory: false, 190 Aliases: []config.Alias{}, 191 Default: os.Getenv("PIPER_gcpProjectNumber"), 192 }, 193 { 194 Name: "gcpWorkloadIdentityPool", 195 ResourceRef: []config.ResourceReference{}, 196 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 197 Type: "string", 198 Mandatory: false, 199 Aliases: []config.Alias{}, 200 Default: os.Getenv("PIPER_gcpWorkloadIdentityPool"), 201 }, 202 { 203 Name: "gcpWorkloadIdentityPoolProvider", 204 ResourceRef: []config.ResourceReference{}, 205 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 206 Type: "string", 207 Mandatory: false, 208 Aliases: []config.Alias{}, 209 Default: os.Getenv("PIPER_gcpWorkloadIdentityPoolProvider"), 210 }, 211 { 212 Name: "topic", 213 ResourceRef: []config.ResourceReference{}, 214 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 215 Type: "string", 216 Mandatory: false, 217 Aliases: []config.Alias{}, 218 Default: os.Getenv("PIPER_topic"), 219 }, 220 { 221 Name: "eventSource", 222 ResourceRef: []config.ResourceReference{}, 223 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 224 Type: "string", 225 Mandatory: false, 226 Aliases: []config.Alias{}, 227 Default: os.Getenv("PIPER_eventSource"), 228 }, 229 { 230 Name: "eventType", 231 ResourceRef: []config.ResourceReference{}, 232 Scope: []string{"PARAMETERS"}, 233 Type: "string", 234 Mandatory: false, 235 Aliases: []config.Alias{}, 236 Default: os.Getenv("PIPER_eventType"), 237 }, 238 { 239 Name: "eventData", 240 ResourceRef: []config.ResourceReference{ 241 { 242 Name: "commonPipelineEnvironment", 243 Param: "custom/eventData", 244 }, 245 }, 246 Scope: []string{}, 247 Type: "string", 248 Mandatory: false, 249 Aliases: []config.Alias{}, 250 Default: os.Getenv("PIPER_eventData"), 251 }, 252 { 253 Name: "additionalEventData", 254 ResourceRef: []config.ResourceReference{}, 255 Scope: []string{"PARAMETERS"}, 256 Type: "string", 257 Mandatory: false, 258 Aliases: []config.Alias{}, 259 Default: os.Getenv("PIPER_additionalEventData"), 260 }, 261 }, 262 }, 263 }, 264 } 265 return theMetaData 266 }