github.com/xgoffin/jenkins-library@v1.154.0/cmd/cloudFoundryCreateSpace.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  )
    11  
    12  func cloudFoundryCreateSpace(config cloudFoundryCreateSpaceOptions, telemetryData *telemetry.CustomData) {
    13  
    14  	c := command.Command{}
    15  	cf := cloudfoundry.CFUtils{Exec: &command.Command{}}
    16  
    17  	// reroute command output to logging framework
    18  	c.Stdout(log.Writer())
    19  	c.Stderr(log.Writer())
    20  
    21  	err := runCloudFoundryCreateSpace(&config, telemetryData, cf, &c)
    22  
    23  	if err != nil {
    24  		log.Entry().WithError(err).Fatal("step execution failed")
    25  	}
    26  
    27  }
    28  
    29  func runCloudFoundryCreateSpace(config *cloudFoundryCreateSpaceOptions, telemetryData *telemetry.CustomData, cf cloudfoundry.CFUtils, s command.ShellRunner) (err error) {
    30  
    31  	var c = cf.Exec
    32  
    33  	cfLoginError := s.RunShell("/bin/sh", fmt.Sprintf("yes '' | cf login -a %s -u %s -p %s", config.CfAPIEndpoint, config.Username, config.Password))
    34  
    35  	if cfLoginError != nil {
    36  		return fmt.Errorf("Error while logging in occured: %w", cfLoginError)
    37  	}
    38  	log.Entry().Info("Successfully logged into cloud foundry.")
    39  
    40  	defer func() {
    41  		logoutErr := cf.Logout()
    42  		if logoutErr != nil {
    43  			err = fmt.Errorf("Error while logging out occured: %w", logoutErr)
    44  		}
    45  	}()
    46  
    47  	log.Entry().Infof("Creating Cloud Foundry Space: '%s'", config.CfSpace)
    48  
    49  	cfCreateSpaceScript := []string{"create-space", config.CfSpace, "-o", config.CfOrg}
    50  
    51  	err = c.RunExecutable("cf", cfCreateSpaceScript...)
    52  
    53  	if err != nil {
    54  		return fmt.Errorf("Creating a cf space has failed: %w", err)
    55  	}
    56  
    57  	log.Entry().Info("Cloud foundry space has been created successfully")
    58  
    59  	return err
    60  }