github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/cloudcontroller/ccv3/info.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  )
    10  
    11  // APIInfo represents a GET response from the '/' endpoint of the cloud
    12  // controller API.
    13  type APIInfo struct {
    14  	// Links is a list of top level Cloud Controller APIs.
    15  	Links struct {
    16  		// AppSSH is the link for application ssh info
    17  		AppSSH APILink `json:"app_ssh"`
    18  
    19  		// CCV3 is the link to the Cloud Controller V3 API
    20  		CCV3 APILink `json:"cloud_controller_v3"`
    21  
    22  		// Logging is the link to the Logging API
    23  		Logging APILink `json:"logging"`
    24  
    25  		NetworkPolicyV1 APILink `json:"network_policy_v1"`
    26  
    27  		// UAA is the link to the UAA API
    28  		UAA APILink `json:"uaa"`
    29  	} `json:"links"`
    30  }
    31  
    32  func (info APIInfo) AppSSHHostKeyFingerprint() string {
    33  	return info.Links.AppSSH.Meta.HostKeyFingerprint
    34  }
    35  
    36  func (info APIInfo) AppSSHEndpoint() string {
    37  	return info.Links.AppSSH.HREF
    38  }
    39  
    40  func (info APIInfo) OAuthClient() string {
    41  	return info.Links.AppSSH.Meta.OAuthClient
    42  }
    43  
    44  // Logging returns the HREF for Logging.
    45  func (info APIInfo) Logging() string {
    46  	return info.Links.Logging.HREF
    47  }
    48  
    49  func (info APIInfo) NetworkPolicyV1() string {
    50  	return info.Links.NetworkPolicyV1.HREF
    51  }
    52  
    53  // UAA returns the HREF for the UAA.
    54  func (info APIInfo) UAA() string {
    55  	return info.Links.UAA.HREF
    56  }
    57  
    58  // CloudControllerAPIVersion returns the version for the CloudController.
    59  func (info APIInfo) CloudControllerAPIVersion() string {
    60  	return info.Links.CCV3.Meta.Version
    61  }
    62  
    63  func (info APIInfo) ccV3Link() string {
    64  	return info.Links.CCV3.HREF
    65  }
    66  
    67  // ResourceLinks represents the information returned back from /v3.
    68  type ResourceLinks map[string]APILink
    69  
    70  // UnmarshalJSON helps unmarshal a Cloud Controller /v3 response.
    71  func (resources ResourceLinks) UnmarshalJSON(data []byte) error {
    72  	var ccResourceLinks struct {
    73  		Links map[string]APILink `json:"links"`
    74  	}
    75  	if err := json.Unmarshal(data, &ccResourceLinks); err != nil {
    76  		return err
    77  	}
    78  
    79  	for key, val := range ccResourceLinks.Links {
    80  		resources[key] = val
    81  	}
    82  
    83  	return nil
    84  }
    85  
    86  // Info returns endpoint and API information from /v3.
    87  func (client *Client) Info() (APIInfo, ResourceLinks, Warnings, error) {
    88  	rootResponse, warnings, err := client.rootResponse()
    89  	if err != nil {
    90  		return APIInfo{}, ResourceLinks{}, warnings, err
    91  	}
    92  
    93  	request, err := client.newHTTPRequest(requestOptions{
    94  		Method: http.MethodGet,
    95  		URL:    rootResponse.ccV3Link(),
    96  	})
    97  	if err != nil {
    98  		return APIInfo{}, ResourceLinks{}, warnings, err
    99  	}
   100  
   101  	info := ResourceLinks{} // Explicitly initializing
   102  	response := cloudcontroller.Response{
   103  		Result: &info,
   104  	}
   105  
   106  	err = client.connection.Make(request, &response)
   107  	warnings = append(warnings, response.Warnings...)
   108  
   109  	if err != nil {
   110  		return APIInfo{}, ResourceLinks{}, warnings, err
   111  	}
   112  
   113  	return rootResponse, info, warnings, nil
   114  }
   115  
   116  // rootResponse returns the CC API root document.
   117  func (client *Client) rootResponse() (APIInfo, Warnings, error) {
   118  	request, err := client.newHTTPRequest(requestOptions{
   119  		Method: http.MethodGet,
   120  		URL:    client.cloudControllerURL,
   121  	})
   122  	if err != nil {
   123  		return APIInfo{}, nil, err
   124  	}
   125  
   126  	var rootResponse APIInfo
   127  	response := cloudcontroller.Response{
   128  		Result: &rootResponse,
   129  	}
   130  
   131  	err = client.connection.Make(request, &response)
   132  	if unknownSourceErr, ok := err.(ccerror.UnknownHTTPSourceError); ok && unknownSourceErr.StatusCode == http.StatusNotFound {
   133  		return APIInfo{}, nil, ccerror.APINotFoundError{URL: client.cloudControllerURL}
   134  	}
   135  
   136  	return rootResponse, response.Warnings, err
   137  }