github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/api/cloudcontroller/ccv3/info.go (about)

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