github.com/SAP/jenkins-library@v1.362.0/cmd/apiProviderList_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 "path/filepath" 9 "time" 10 11 "github.com/SAP/jenkins-library/pkg/config" 12 "github.com/SAP/jenkins-library/pkg/log" 13 "github.com/SAP/jenkins-library/pkg/piperenv" 14 "github.com/SAP/jenkins-library/pkg/splunk" 15 "github.com/SAP/jenkins-library/pkg/telemetry" 16 "github.com/SAP/jenkins-library/pkg/validation" 17 "github.com/spf13/cobra" 18 ) 19 20 type apiProviderListOptions struct { 21 APIServiceKey string `json:"apiServiceKey,omitempty"` 22 Top int `json:"top,omitempty"` 23 Skip int `json:"skip,omitempty"` 24 Filter string `json:"filter,omitempty"` 25 Count bool `json:"count,omitempty"` 26 Search string `json:"search,omitempty"` 27 Orderby string `json:"orderby,omitempty"` 28 Select string `json:"select,omitempty"` 29 Expand string `json:"expand,omitempty"` 30 } 31 32 type apiProviderListCommonPipelineEnvironment struct { 33 custom struct { 34 APIProviderList string 35 } 36 } 37 38 func (p *apiProviderListCommonPipelineEnvironment) persist(path, resourceName string) { 39 content := []struct { 40 category string 41 name string 42 value interface{} 43 }{ 44 {category: "custom", name: "apiProviderList", value: p.custom.APIProviderList}, 45 } 46 47 errCount := 0 48 for _, param := range content { 49 err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value) 50 if err != nil { 51 log.Entry().WithError(err).Error("Error persisting piper environment.") 52 errCount++ 53 } 54 } 55 if errCount > 0 { 56 log.Entry().Error("failed to persist Piper environment") 57 } 58 } 59 60 // ApiProviderListCommand Get a full List of all API providers from the API Portal 61 func ApiProviderListCommand() *cobra.Command { 62 const STEP_NAME = "apiProviderList" 63 64 metadata := apiProviderListMetadata() 65 var stepConfig apiProviderListOptions 66 var startTime time.Time 67 var commonPipelineEnvironment apiProviderListCommonPipelineEnvironment 68 var logCollector *log.CollectorHook 69 var splunkClient *splunk.Splunk 70 telemetryClient := &telemetry.Telemetry{} 71 72 var createApiProviderListCmd = &cobra.Command{ 73 Use: STEP_NAME, 74 Short: "Get a full List of all API providers from the API Portal", 75 Long: `With this step, you can get a list of all API providers from the API Portal using the OData API. Learn more about the API Management API for getting list of an API Providers [here](https://help.sap.com/viewer/66d066d903c2473f81ec33acfe2ccdb4/Cloud/en-US/e26b3320cd534ae4bc743af8013a8abb.html).`, 76 PreRunE: func(cmd *cobra.Command, _ []string) error { 77 startTime = time.Now() 78 log.SetStepName(STEP_NAME) 79 log.SetVerbose(GeneralConfig.Verbose) 80 81 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 82 83 path, _ := os.Getwd() 84 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 85 log.RegisterHook(fatalHook) 86 87 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 88 if err != nil { 89 log.SetErrorCategory(log.ErrorConfiguration) 90 return err 91 } 92 log.RegisterSecret(stepConfig.APIServiceKey) 93 94 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 95 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 96 log.RegisterHook(&sentryHook) 97 } 98 99 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 || len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 { 100 splunkClient = &splunk.Splunk{} 101 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 102 log.RegisterHook(logCollector) 103 } 104 105 if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil { 106 log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook") 107 } 108 109 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 110 if err != nil { 111 return err 112 } 113 if err = validation.ValidateStruct(stepConfig); err != nil { 114 log.SetErrorCategory(log.ErrorConfiguration) 115 return err 116 } 117 118 return nil 119 }, 120 Run: func(_ *cobra.Command, _ []string) { 121 stepTelemetryData := telemetry.CustomData{} 122 stepTelemetryData.ErrorCode = "1" 123 handler := func() { 124 commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment") 125 config.RemoveVaultSecretFiles() 126 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 127 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 128 stepTelemetryData.PiperCommitHash = GitCommit 129 telemetryClient.SetData(&stepTelemetryData) 130 telemetryClient.Send() 131 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 132 splunkClient.Initialize(GeneralConfig.CorrelationID, 133 GeneralConfig.HookConfig.SplunkConfig.Dsn, 134 GeneralConfig.HookConfig.SplunkConfig.Token, 135 GeneralConfig.HookConfig.SplunkConfig.Index, 136 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 137 splunkClient.Send(telemetryClient.GetData(), logCollector) 138 } 139 if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 { 140 splunkClient.Initialize(GeneralConfig.CorrelationID, 141 GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint, 142 GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken, 143 GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex, 144 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 145 splunkClient.Send(telemetryClient.GetData(), logCollector) 146 } 147 } 148 log.DeferExitHandler(handler) 149 defer handler() 150 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME, GeneralConfig.HookConfig.PendoConfig.Token) 151 apiProviderList(stepConfig, &stepTelemetryData, &commonPipelineEnvironment) 152 stepTelemetryData.ErrorCode = "0" 153 log.Entry().Info("SUCCESS") 154 }, 155 } 156 157 addApiProviderListFlags(createApiProviderListCmd, &stepConfig) 158 return createApiProviderListCmd 159 } 160 161 func addApiProviderListFlags(cmd *cobra.Command, stepConfig *apiProviderListOptions) { 162 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'") 163 cmd.Flags().IntVar(&stepConfig.Top, "top", 0, "Show only the first n items.") 164 cmd.Flags().IntVar(&stepConfig.Skip, "skip", 0, "Skip the first n items.") 165 cmd.Flags().StringVar(&stepConfig.Filter, "filter", os.Getenv("PIPER_filter"), "Filter items by property values.") 166 cmd.Flags().BoolVar(&stepConfig.Count, "count", false, "Include count of items.") 167 cmd.Flags().StringVar(&stepConfig.Search, "search", os.Getenv("PIPER_search"), "Search items by search phrases.") 168 cmd.Flags().StringVar(&stepConfig.Orderby, "orderby", os.Getenv("PIPER_orderby"), "Order by property values.") 169 cmd.Flags().StringVar(&stepConfig.Select, "select", os.Getenv("PIPER_select"), "Select properties to be returned.") 170 cmd.Flags().StringVar(&stepConfig.Expand, "expand", os.Getenv("PIPER_expand"), "Expand related entities.") 171 172 cmd.MarkFlagRequired("apiServiceKey") 173 } 174 175 // retrieve step metadata 176 func apiProviderListMetadata() config.StepData { 177 var theMetaData = config.StepData{ 178 Metadata: config.StepMetadata{ 179 Name: "apiProviderList", 180 Aliases: []config.Alias{}, 181 Description: "Get a full List of all API providers from the API Portal", 182 }, 183 Spec: config.StepSpec{ 184 Inputs: config.StepInputs{ 185 Secrets: []config.StepSecrets{ 186 {Name: "apimApiServiceKeyCredentialsId", Description: "Jenkins secret text credential ID containing the service key to the API Management Runtime service instance of plan 'api'", Type: "jenkins"}, 187 }, 188 Parameters: []config.StepParameters{ 189 { 190 Name: "apiServiceKey", 191 ResourceRef: []config.ResourceReference{ 192 { 193 Name: "apimApiServiceKeyCredentialsId", 194 Param: "apiServiceKey", 195 Type: "secret", 196 }, 197 }, 198 Scope: []string{"PARAMETERS"}, 199 Type: "string", 200 Mandatory: true, 201 Aliases: []config.Alias{}, 202 Default: os.Getenv("PIPER_apiServiceKey"), 203 }, 204 { 205 Name: "top", 206 ResourceRef: []config.ResourceReference{}, 207 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 208 Type: "int", 209 Mandatory: false, 210 Aliases: []config.Alias{}, 211 Default: 0, 212 }, 213 { 214 Name: "skip", 215 ResourceRef: []config.ResourceReference{}, 216 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 217 Type: "int", 218 Mandatory: false, 219 Aliases: []config.Alias{}, 220 Default: 0, 221 }, 222 { 223 Name: "filter", 224 ResourceRef: []config.ResourceReference{}, 225 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 226 Type: "string", 227 Mandatory: false, 228 Aliases: []config.Alias{}, 229 Default: os.Getenv("PIPER_filter"), 230 }, 231 { 232 Name: "count", 233 ResourceRef: []config.ResourceReference{}, 234 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 235 Type: "bool", 236 Mandatory: false, 237 Aliases: []config.Alias{}, 238 Default: false, 239 }, 240 { 241 Name: "search", 242 ResourceRef: []config.ResourceReference{}, 243 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 244 Type: "string", 245 Mandatory: false, 246 Aliases: []config.Alias{}, 247 Default: os.Getenv("PIPER_search"), 248 }, 249 { 250 Name: "orderby", 251 ResourceRef: []config.ResourceReference{}, 252 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 253 Type: "string", 254 Mandatory: false, 255 Aliases: []config.Alias{}, 256 Default: os.Getenv("PIPER_orderby"), 257 }, 258 { 259 Name: "select", 260 ResourceRef: []config.ResourceReference{}, 261 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 262 Type: "string", 263 Mandatory: false, 264 Aliases: []config.Alias{}, 265 Default: os.Getenv("PIPER_select"), 266 }, 267 { 268 Name: "expand", 269 ResourceRef: []config.ResourceReference{}, 270 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 271 Type: "string", 272 Mandatory: false, 273 Aliases: []config.Alias{}, 274 Default: os.Getenv("PIPER_expand"), 275 }, 276 }, 277 }, 278 Outputs: config.StepOutputs{ 279 Resources: []config.StepResources{ 280 { 281 Name: "commonPipelineEnvironment", 282 Type: "piperEnvironment", 283 Parameters: []map[string]interface{}{ 284 {"name": "custom/apiProviderList"}, 285 }, 286 }, 287 }, 288 }, 289 }, 290 } 291 return theMetaData 292 }