github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+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  )
     9  
    10  // APIInfo represents a GET response from the '/' endpoint of the cloud
    11  // controller API.
    12  type APIInfo struct {
    13  	// Links is a list of top level Cloud Controller APIs.
    14  	Links struct {
    15  		// CCV3 is the link to the Cloud Controller V3 API
    16  		CCV3 APILink `json:"cloud_controller_v3"`
    17  
    18  		// UAA is the link to the UAA API
    19  		UAA APILink `json:"uaa"`
    20  	} `json:"links"`
    21  }
    22  
    23  // UAA return the HREF for the UAA.
    24  func (info APIInfo) UAA() string {
    25  	return info.Links.UAA.HREF
    26  }
    27  
    28  // CloudControllerAPIVersion return the version for the CloudController.
    29  func (info APIInfo) CloudControllerAPIVersion() string {
    30  	return info.Links.CCV3.Meta.Version
    31  }
    32  
    33  func (info APIInfo) ccV3Link() string {
    34  	return info.Links.CCV3.HREF
    35  }
    36  
    37  // ResourceLinks represents the information returned back from /v3.
    38  type ResourceLinks map[string]APILink
    39  
    40  // UnmarshalJSON helps unmarshal a Cloud Controller /v3 response.
    41  func (resources ResourceLinks) UnmarshalJSON(data []byte) error {
    42  	var ccResourceLinks struct {
    43  		Links map[string]APILink `json:"links"`
    44  	}
    45  	if err := json.Unmarshal(data, &ccResourceLinks); err != nil {
    46  		return err
    47  	}
    48  
    49  	for key, val := range ccResourceLinks.Links {
    50  		resources[key] = val
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  // Info returns back endpoint and API information from /v3.
    57  func (client *Client) Info() (APIInfo, ResourceLinks, Warnings, error) {
    58  	rootResponse, warnings, err := client.rootResponse()
    59  	if err != nil {
    60  		return APIInfo{}, ResourceLinks{}, warnings, err
    61  	}
    62  
    63  	request, err := client.newHTTPRequest(requestOptions{
    64  		Method: http.MethodGet,
    65  		URL:    rootResponse.ccV3Link(),
    66  	})
    67  	if err != nil {
    68  		return APIInfo{}, ResourceLinks{}, warnings, err
    69  	}
    70  
    71  	info := ResourceLinks{} // Explicitly initializing
    72  	response := cloudcontroller.Response{
    73  		Result: &info,
    74  	}
    75  
    76  	err = client.connection.Make(request, &response)
    77  	warnings = append(warnings, response.Warnings...)
    78  
    79  	if err != nil {
    80  		return APIInfo{}, ResourceLinks{}, warnings, err
    81  	}
    82  
    83  	return rootResponse, info, warnings, nil
    84  }
    85  
    86  // rootResponse return the CC API root document.
    87  func (client *Client) rootResponse() (APIInfo, Warnings, error) {
    88  	request, err := client.newHTTPRequest(requestOptions{
    89  		Method: http.MethodGet,
    90  		URL:    client.cloudControllerURL,
    91  	})
    92  	if err != nil {
    93  		return APIInfo{}, nil, err
    94  	}
    95  
    96  	var rootResponse APIInfo
    97  	response := cloudcontroller.Response{
    98  		Result: &rootResponse,
    99  	}
   100  
   101  	err = client.connection.Make(request, &response)
   102  	if err != nil {
   103  		if _, ok := err.(cloudcontroller.NotFoundError); ok {
   104  			return APIInfo{}, nil, cloudcontroller.APINotFoundError{URL: client.cloudControllerURL}
   105  		}
   106  		return APIInfo{}, response.Warnings, err
   107  	}
   108  
   109  	return rootResponse, response.Warnings, nil
   110  }