github.com/jaylevin/jenkins-library@v1.230.4/cmd/cloudFoundryCreateService.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/SAP/jenkins-library/pkg/cloudfoundry"
     7  	"github.com/SAP/jenkins-library/pkg/command"
     8  	"github.com/SAP/jenkins-library/pkg/log"
     9  	"github.com/SAP/jenkins-library/pkg/telemetry"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  func cloudFoundryCreateService(config cloudFoundryCreateServiceOptions, telemetryData *telemetry.CustomData) {
    14  
    15  	cf := cloudfoundry.CFUtils{Exec: &command.Command{}}
    16  
    17  	err := runCloudFoundryCreateService(&config, telemetryData, cf)
    18  
    19  	if err != nil {
    20  		log.Entry().WithError(err).Fatal("step execution failed")
    21  	}
    22  
    23  }
    24  
    25  func runCloudFoundryCreateService(config *cloudFoundryCreateServiceOptions, telemetryData *telemetry.CustomData, cf cloudfoundry.CFUtils) (err error) {
    26  	var c = cf.Exec
    27  
    28  	loginOptions := cloudfoundry.LoginOptions{
    29  		CfAPIEndpoint: config.CfAPIEndpoint,
    30  		CfOrg:         config.CfOrg,
    31  		CfSpace:       config.CfSpace,
    32  		Username:      config.Username,
    33  		Password:      config.Password,
    34  	}
    35  
    36  	err = cf.Login(loginOptions)
    37  
    38  	if err != nil {
    39  		return fmt.Errorf("Error while logging in: %w", err)
    40  	}
    41  
    42  	defer func() {
    43  		logoutErr := cf.Logout()
    44  		if logoutErr != nil {
    45  			err = fmt.Errorf("Error while logging out occurred: %w", logoutErr)
    46  		}
    47  	}()
    48  
    49  	err = cloudFoundryCreateServiceRequest(config, telemetryData, c)
    50  
    51  	if err != nil {
    52  		return fmt.Errorf("Service creation failed: %w", err)
    53  	}
    54  
    55  	log.Entry().Info("Service creation completed successfully")
    56  
    57  	return err
    58  
    59  }
    60  
    61  func cloudFoundryCreateServiceRequest(config *cloudFoundryCreateServiceOptions, telemetryData *telemetry.CustomData, c command.ExecRunner) error {
    62  	var err error
    63  	log.Entry().Info("Creating Cloud Foundry Service")
    64  
    65  	cfCreateServiceScript := []string{"create-service", config.CfService, config.CfServicePlan, config.CfServiceInstanceName}
    66  
    67  	if config.CfServiceBroker != "" {
    68  		cfCreateServiceScript = append(cfCreateServiceScript, "-b", config.CfServiceBroker)
    69  	}
    70  	if config.CfCreateServiceConfig != "" {
    71  		cfCreateServiceScript = append(cfCreateServiceScript, "-c", config.CfCreateServiceConfig)
    72  	}
    73  	if config.CfServiceTags != "" {
    74  		cfCreateServiceScript = append(cfCreateServiceScript, "-t", config.CfServiceTags)
    75  	}
    76  	if config.ServiceManifest != "" && fileExists(config.ServiceManifest) {
    77  
    78  		cfCreateServiceScript = []string{"create-service-push", "--no-push", "--service-manifest", config.ServiceManifest}
    79  
    80  		if len(config.ManifestVariablesFiles) >= 0 {
    81  			varFileOpts, err := cloudfoundry.GetVarsFileOptions(config.ManifestVariablesFiles)
    82  			if err != nil {
    83  				return errors.Wrapf(err, "Cannot prepare var-file-options: '%v'", config.ManifestVariablesFiles)
    84  			}
    85  			cfCreateServiceScript = append(cfCreateServiceScript, varFileOpts...)
    86  		}
    87  		if len(config.ManifestVariables) >= 0 {
    88  			varOptions, err := cloudfoundry.GetVarsOptions(config.ManifestVariables)
    89  			if err != nil {
    90  				return errors.Wrapf(err, "Cannot prepare var-options: '%v'", config.ManifestVariables)
    91  			}
    92  			cfCreateServiceScript = append(cfCreateServiceScript, varOptions...)
    93  		}
    94  	}
    95  	err = c.RunExecutable("cf", cfCreateServiceScript...)
    96  
    97  	if err != nil {
    98  		return fmt.Errorf("Failed to Create Service: %w", err)
    99  	}
   100  	return nil
   101  }