github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/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  // ResourceLinks represents the information returned back from /v3.
    99  type ResourceLinks map[string]resources.APILink
   100  
   101  // UnmarshalJSON helps unmarshal a Cloud Controller /v3 response.
   102  func (links ResourceLinks) UnmarshalJSON(data []byte) error {
   103  	var ccResourceLinks struct {
   104  		Links map[string]resources.APILink `json:"links"`
   105  	}
   106  	err := cloudcontroller.DecodeJSON(data, &ccResourceLinks)
   107  	if err != nil {
   108  		return err
   109  	}
   110  
   111  	for key, val := range ccResourceLinks.Links {
   112  		links[key] = val
   113  	}
   114  
   115  	return nil
   116  }
   117  
   118  // GetInfo returns endpoint and API information from /v3.
   119  func (client *Client) GetInfo() (Info, Warnings, error) {
   120  	rootResponse, warnings, err := client.RootResponse()
   121  	if err != nil {
   122  		return Info{}, warnings, err
   123  	}
   124  
   125  	return rootResponse, warnings, err
   126  }
   127  
   128  // rootResponse returns the CC API root document.
   129  func (client *Client) RootResponse() (Info, Warnings, error) {
   130  	var responseBody Info
   131  
   132  	_, warnings, err := client.MakeRequest(RequestParams{
   133  		URL:          client.CloudControllerURL,
   134  		ResponseBody: &responseBody,
   135  	})
   136  
   137  	unknownSourceErr, ok := err.(ccerror.UnknownHTTPSourceError)
   138  	if ok && unknownSourceErr.StatusCode == http.StatusNotFound {
   139  		return Info{}, nil, ccerror.APINotFoundError{URL: client.CloudControllerURL}
   140  	}
   141  
   142  	return responseBody, warnings, err
   143  }