github.com/SAP/jenkins-library@v1.362.0/cmd/cloudFoundryCreateSpace_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 cloudFoundryCreateSpaceOptions struct {
    19  	CfAPIEndpoint string `json:"cfApiEndpoint,omitempty"`
    20  	Username      string `json:"username,omitempty"`
    21  	Password      string `json:"password,omitempty"`
    22  	CfOrg         string `json:"cfOrg,omitempty"`
    23  	CfSpace       string `json:"cfSpace,omitempty"`
    24  }
    25  
    26  // CloudFoundryCreateSpaceCommand Creates a user defined space in Cloud Foundry
    27  func CloudFoundryCreateSpaceCommand() *cobra.Command {
    28  	const STEP_NAME = "cloudFoundryCreateSpace"
    29  
    30  	metadata := cloudFoundryCreateSpaceMetadata()
    31  	var stepConfig cloudFoundryCreateSpaceOptions
    32  	var startTime time.Time
    33  	var logCollector *log.CollectorHook
    34  	var splunkClient *splunk.Splunk
    35  	telemetryClient := &telemetry.Telemetry{}
    36  
    37  	var createCloudFoundryCreateSpaceCmd = &cobra.Command{
    38  		Use:   STEP_NAME,
    39  		Short: "Creates a user defined space in Cloud Foundry",
    40  		Long: `Creates a cf space in Cloud Foundry
    41  Mandatory:
    42  * Cloud Foundry API endpoint, Organization, name of the Cf space to be created`,
    43  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    44  			startTime = time.Now()
    45  			log.SetStepName(STEP_NAME)
    46  			log.SetVerbose(GeneralConfig.Verbose)
    47  
    48  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    49  
    50  			path, _ := os.Getwd()
    51  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    52  			log.RegisterHook(fatalHook)
    53  
    54  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    55  			if err != nil {
    56  				log.SetErrorCategory(log.ErrorConfiguration)
    57  				return err
    58  			}
    59  			log.RegisterSecret(stepConfig.Username)
    60  			log.RegisterSecret(stepConfig.Password)
    61  
    62  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    63  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    64  				log.RegisterHook(&sentryHook)
    65  			}
    66  
    67  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 || len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
    68  				splunkClient = &splunk.Splunk{}
    69  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    70  				log.RegisterHook(logCollector)
    71  			}
    72  
    73  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
    74  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
    75  			}
    76  
    77  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    78  			if err != nil {
    79  				return err
    80  			}
    81  			if err = validation.ValidateStruct(stepConfig); err != nil {
    82  				log.SetErrorCategory(log.ErrorConfiguration)
    83  				return err
    84  			}
    85  
    86  			return nil
    87  		},
    88  		Run: func(_ *cobra.Command, _ []string) {
    89  			stepTelemetryData := telemetry.CustomData{}
    90  			stepTelemetryData.ErrorCode = "1"
    91  			handler := func() {
    92  				config.RemoveVaultSecretFiles()
    93  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    94  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    95  				stepTelemetryData.PiperCommitHash = GitCommit
    96  				telemetryClient.SetData(&stepTelemetryData)
    97  				telemetryClient.Send()
    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  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   105  				}
   106  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   107  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   108  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   109  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   110  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   111  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   112  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   113  				}
   114  			}
   115  			log.DeferExitHandler(handler)
   116  			defer handler()
   117  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME, GeneralConfig.HookConfig.PendoConfig.Token)
   118  			cloudFoundryCreateSpace(stepConfig, &stepTelemetryData)
   119  			stepTelemetryData.ErrorCode = "0"
   120  			log.Entry().Info("SUCCESS")
   121  		},
   122  	}
   123  
   124  	addCloudFoundryCreateSpaceFlags(createCloudFoundryCreateSpaceCmd, &stepConfig)
   125  	return createCloudFoundryCreateSpaceCmd
   126  }
   127  
   128  func addCloudFoundryCreateSpaceFlags(cmd *cobra.Command, stepConfig *cloudFoundryCreateSpaceOptions) {
   129  	cmd.Flags().StringVar(&stepConfig.CfAPIEndpoint, "cfApiEndpoint", `https://api.cf.eu10.hana.ondemand.com`, "Cloud Foundry API endpoint")
   130  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User or E-Mail for CF")
   131  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for Cloud Foundry User")
   132  	cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "Cloud Foundry org")
   133  	cmd.Flags().StringVar(&stepConfig.CfSpace, "cfSpace", os.Getenv("PIPER_cfSpace"), "The name of the Cloud Foundry Space to be created")
   134  
   135  	cmd.MarkFlagRequired("cfApiEndpoint")
   136  	cmd.MarkFlagRequired("username")
   137  	cmd.MarkFlagRequired("password")
   138  	cmd.MarkFlagRequired("cfOrg")
   139  	cmd.MarkFlagRequired("cfSpace")
   140  }
   141  
   142  // retrieve step metadata
   143  func cloudFoundryCreateSpaceMetadata() config.StepData {
   144  	var theMetaData = config.StepData{
   145  		Metadata: config.StepMetadata{
   146  			Name:        "cloudFoundryCreateSpace",
   147  			Aliases:     []config.Alias{},
   148  			Description: "Creates a user defined space in Cloud Foundry",
   149  		},
   150  		Spec: config.StepSpec{
   151  			Inputs: config.StepInputs{
   152  				Secrets: []config.StepSecrets{
   153  					{Name: "cfCredentialsId", Description: "Jenkins credentials ID containing user and password to authenticate to the Cloud Foundry API", Type: "jenkins", Aliases: []config.Alias{{Name: "cloudFoundry/credentialsId", Deprecated: false}}},
   154  				},
   155  				Resources: []config.StepResources{
   156  					{Name: "deployDescriptor", Type: "stash"},
   157  				},
   158  				Parameters: []config.StepParameters{
   159  					{
   160  						Name:        "cfApiEndpoint",
   161  						ResourceRef: []config.ResourceReference{},
   162  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   163  						Type:        "string",
   164  						Mandatory:   true,
   165  						Aliases:     []config.Alias{{Name: "cloudFoundry/apiEndpoint"}},
   166  						Default:     `https://api.cf.eu10.hana.ondemand.com`,
   167  					},
   168  					{
   169  						Name: "username",
   170  						ResourceRef: []config.ResourceReference{
   171  							{
   172  								Name:  "cfCredentialsId",
   173  								Param: "username",
   174  								Type:  "secret",
   175  							},
   176  						},
   177  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   178  						Type:      "string",
   179  						Mandatory: true,
   180  						Aliases:   []config.Alias{},
   181  						Default:   os.Getenv("PIPER_username"),
   182  					},
   183  					{
   184  						Name: "password",
   185  						ResourceRef: []config.ResourceReference{
   186  							{
   187  								Name:  "cfCredentialsId",
   188  								Param: "password",
   189  								Type:  "secret",
   190  							},
   191  						},
   192  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   193  						Type:      "string",
   194  						Mandatory: true,
   195  						Aliases:   []config.Alias{},
   196  						Default:   os.Getenv("PIPER_password"),
   197  					},
   198  					{
   199  						Name:        "cfOrg",
   200  						ResourceRef: []config.ResourceReference{},
   201  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   202  						Type:        "string",
   203  						Mandatory:   true,
   204  						Aliases:     []config.Alias{{Name: "cloudFoundry/org"}},
   205  						Default:     os.Getenv("PIPER_cfOrg"),
   206  					},
   207  					{
   208  						Name:        "cfSpace",
   209  						ResourceRef: []config.ResourceReference{},
   210  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   211  						Type:        "string",
   212  						Mandatory:   true,
   213  						Aliases:     []config.Alias{{Name: "cloudFoundry/space"}},
   214  						Default:     os.Getenv("PIPER_cfSpace"),
   215  					},
   216  				},
   217  			},
   218  			Containers: []config.Container{
   219  				{Name: "cf", Image: "ppiper/cf-cli:latest"},
   220  			},
   221  		},
   222  	}
   223  	return theMetaData
   224  }