github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/cloudcontroller/ccv3/info.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     8  	"code.cloudfoundry.org/cli/resources"
     9  )
    10  
    11  type InfoLinks struct {
    12  	// AppSSH is the link for application ssh info.
    13  	AppSSH resources.APILink `json:"app_ssh"`
    14  
    15  	// CCV3 is the link to the Cloud Controller V3 API.
    16  	CCV3 resources.APILink `json:"cloud_controller_v3"`
    17  
    18  	// Logging is the link to the Logging API.
    19  	Logging resources.APILink `json:"logging"`
    20  
    21  	// Logging is the link to the Logging API.
    22  	LogCache resources.APILink `json:"log_cache"`
    23  
    24  	// NetworkPolicyV1 is the link to the Container to Container Networking
    25  	// API.
    26  	NetworkPolicyV1 resources.APILink `json:"network_policy_v1"`
    27  
    28  	// Routing is the link to the routing API
    29  	Routing resources.APILink `json:"routing"`
    30  
    31  	// UAA is the link to the UAA API.
    32  	UAA resources.APILink `json:"uaa"`
    33  
    34  	// Login is the link to the Login API.
    35  	Login resources.APILink `json:"login"`
    36  }
    37  
    38  // Info represents a GET response from the '/' endpoint of the cloud
    39  // controller API.
    40  type Info struct {
    41  	// Links is a list of top level Cloud Controller APIs.
    42  	Links   InfoLinks `json:"links"`
    43  	CFOnK8s bool      `json:"cf_on_k8s"`
    44  }
    45  
    46  // AppSSHEndpoint returns the HREF for SSHing into an app container.
    47  func (info Info) AppSSHEndpoint() string {
    48  	return info.Links.AppSSH.HREF
    49  }
    50  
    51  // AppSSHHostKeyFingerprint returns the SSH key fingerprint of the SSH proxy
    52  // that brokers connections to application instances.
    53  func (info Info) AppSSHHostKeyFingerprint() string {
    54  	return info.Links.AppSSH.Meta.HostKeyFingerprint
    55  }
    56  
    57  // CloudControllerAPIVersion returns the version of the CloudController.
    58  func (info Info) CloudControllerAPIVersion() string {
    59  	return info.Links.CCV3.Meta.Version
    60  }
    61  
    62  // LogCache returns the HREF of the Loggregator Traffic Controller.
    63  func (info Info) LogCache() string {
    64  	return info.Links.LogCache.HREF
    65  }
    66  
    67  // Logging returns the HREF of the Loggregator Traffic Controller.
    68  func (info Info) Logging() string {
    69  	return info.Links.Logging.HREF
    70  }
    71  
    72  // NetworkPolicyV1 returns the HREF of the Container Networking v1 Policy API
    73  func (info Info) NetworkPolicyV1() string {
    74  	return info.Links.NetworkPolicyV1.HREF
    75  }
    76  
    77  // OAuthClient returns the oauth client ID of the SSH proxy that brokers
    78  // connections to application instances.
    79  func (info Info) OAuthClient() string {
    80  	return info.Links.AppSSH.Meta.OAuthClient
    81  }
    82  
    83  // Routing returns the HREF of the routing API.
    84  func (info Info) Routing() string {
    85  	return info.Links.Routing.HREF
    86  }
    87  
    88  // UAA returns the HREF of the UAA server.
    89  func (info Info) UAA() string {
    90  	return info.Links.UAA.HREF
    91  }
    92  
    93  // Login returns the HREF of the login server.
    94  func (info Info) Login() string {
    95  	return info.Links.Login.HREF
    96  }
    97  
    98  // ccv3Link returns the HREF of the CloudController v3 API.
    99  func (info Info) ccV3Link() string {
   100  	return info.Links.CCV3.HREF
   101  }
   102  
   103  // ResourceLinks represents the information returned back from /v3.
   104  type ResourceLinks map[string]resources.APILink
   105  
   106  // UnmarshalJSON helps unmarshal a Cloud Controller /v3 response.
   107  func (links ResourceLinks) UnmarshalJSON(data []byte) error {
   108  	var ccResourceLinks struct {
   109  		Links map[string]resources.APILink `json:"links"`
   110  	}
   111  	err := cloudcontroller.DecodeJSON(data, &ccResourceLinks)
   112  	if err != nil {
   113  		return err
   114  	}
   115  
   116  	for key, val := range ccResourceLinks.Links {
   117  		links[key] = val
   118  	}
   119  
   120  	return nil
   121  }
   122  
   123  // GetInfo returns endpoint and API information from /v3.
   124  func (client *Client) GetInfo() (Info, Warnings, error) {
   125  	rootResponse, warnings, err := client.RootResponse()
   126  	if err != nil {
   127  		return Info{}, warnings, err
   128  	}
   129  
   130  	return rootResponse, warnings, err
   131  }
   132  
   133  // rootResponse returns the CC API root document.
   134  func (client *Client) RootResponse() (Info, Warnings, error) {
   135  	var responseBody Info
   136  
   137  	_, warnings, err := client.MakeRequest(RequestParams{
   138  		URL:          client.CloudControllerURL,
   139  		ResponseBody: &responseBody,
   140  	})
   141  
   142  	unknownSourceErr, ok := err.(ccerror.UnknownHTTPSourceError)
   143  	if ok && unknownSourceErr.StatusCode == http.StatusNotFound {
   144  		return Info{}, nil, ccerror.APINotFoundError{URL: client.CloudControllerURL}
   145  	}
   146  
   147  	return responseBody, warnings, err
   148  }