github.com/sleungcy-sap/cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/target.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
     9  	"github.com/tedsuo/rata"
    10  )
    11  
    12  // TargetSettings represents configuration for establishing a connection to the
    13  // Cloud Controller server.
    14  type TargetSettings struct {
    15  	// DialTimeout is the DNS timeout used to make all requests to the Cloud
    16  	// Controller.
    17  	DialTimeout time.Duration
    18  
    19  	// SkipSSLValidation controls whether a client verifies the server's
    20  	// certificate chain and host name. If SkipSSLValidation is true, TLS accepts
    21  	// any certificate presented by the server and any host name in that
    22  	// certificate for *all* client requests going forward.
    23  	//
    24  	// In this mode, TLS is susceptible to man-in-the-middle attacks. This should
    25  	// be used only for testing.
    26  	SkipSSLValidation bool
    27  
    28  	// URL is a fully qualified URL to the Cloud Controller API.
    29  	URL string
    30  }
    31  
    32  // TargetCF sets the client to use the Cloud Controller specified in the
    33  // configuration. Any other configuration is also applied to the client.
    34  func (client *Client) TargetCF(settings TargetSettings) (Warnings, error) {
    35  	client.cloudControllerURL = settings.URL
    36  	client.router = rata.NewRequestGenerator(settings.URL, internal.APIRoutes)
    37  
    38  	client.connection = cloudcontroller.NewConnection(cloudcontroller.Config{
    39  		DialTimeout:       settings.DialTimeout,
    40  		SkipSSLValidation: settings.SkipSSLValidation,
    41  	})
    42  
    43  	for _, wrapper := range client.wrappers {
    44  		client.connection = wrapper.Wrap(client.connection)
    45  	}
    46  
    47  	info, warnings, err := client.Info()
    48  	if err != nil {
    49  		return warnings, err
    50  	}
    51  
    52  	rootInfo, _, err := client.RootResponse()
    53  	if err != nil {
    54  		return warnings, err
    55  	}
    56  
    57  	client.authorizationEndpoint = info.AuthorizationEndpoint
    58  	client.cloudControllerAPIVersion = info.APIVersion
    59  	client.dopplerEndpoint = info.DopplerEndpoint
    60  	//TODO Remove this condition when earliest supportest CAPI is 1.87.0
    61  	//We have to do this because the current legacy supported CAPI version as of 2020 does not display the log cache url, this will break if a foundation on legacy CAPI have non-standard logcache urls
    62  	if rootInfo.Links.LogCache.HREF != "" {
    63  		client.logCacheEndpoint = rootInfo.Links.LogCache.HREF
    64  	} else {
    65  		client.logCacheEndpoint = strings.Replace(settings.URL, "api", "log-cache", 1)
    66  	}
    67  	client.minCLIVersion = info.MinCLIVersion
    68  	client.routingEndpoint = info.RoutingEndpoint
    69  
    70  	return warnings, nil
    71  }