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