github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+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  
    34  // Info represents a GET response from the '/' endpoint of the cloud
    35  // controller API.
    36  type Info struct {
    37  	// Links is a list of top level Cloud Controller APIs.
    38  	Links InfoLinks `json:"links"`
    39  }
    40  
    41  // AppSSHEndpoint returns the HREF for SSHing into an app container.
    42  func (info Info) AppSSHEndpoint() string {
    43  	return info.Links.AppSSH.HREF
    44  }
    45  
    46  // AppSSHHostKeyFingerprint returns the SSH key fingerprint of the SSH proxy
    47  // that brokers connections to application instances.
    48  func (info Info) AppSSHHostKeyFingerprint() string {
    49  	return info.Links.AppSSH.Meta.HostKeyFingerprint
    50  }
    51  
    52  // CloudControllerAPIVersion returns the version of the CloudController.
    53  func (info Info) CloudControllerAPIVersion() string {
    54  	return info.Links.CCV3.Meta.Version
    55  }
    56  
    57  // LogCache returns the HREF of the Loggregator Traffic Controller.
    58  func (info Info) LogCache() string {
    59  	return info.Links.LogCache.HREF
    60  }
    61  
    62  // Logging returns the HREF of the Loggregator Traffic Controller.
    63  func (info Info) Logging() string {
    64  	return info.Links.Logging.HREF
    65  }
    66  
    67  // NetworkPolicyV1 returns the HREF of the Container Networking v1 Policy API
    68  func (info Info) NetworkPolicyV1() string {
    69  	return info.Links.NetworkPolicyV1.HREF
    70  }
    71  
    72  // OAuthClient returns the oauth client ID of the SSH proxy that brokers
    73  // connections to application instances.
    74  func (info Info) OAuthClient() string {
    75  	return info.Links.AppSSH.Meta.OAuthClient
    76  }
    77  
    78  func (info Info) Routing() string {
    79  	return info.Links.Routing.HREF
    80  }
    81  
    82  // UAA returns the HREF of the UAA server.
    83  func (info Info) UAA() string {
    84  	return info.Links.UAA.HREF
    85  }
    86  
    87  // ccv3Link returns the HREF of the CloudController v3 API.
    88  func (info Info) ccV3Link() string {
    89  	return info.Links.CCV3.HREF
    90  }
    91  
    92  // ResourceLinks represents the information returned back from /v3.
    93  type ResourceLinks map[string]APILink
    94  
    95  // UnmarshalJSON helps unmarshal a Cloud Controller /v3 response.
    96  func (resources ResourceLinks) UnmarshalJSON(data []byte) error {
    97  	var ccResourceLinks struct {
    98  		Links map[string]APILink `json:"links"`
    99  	}
   100  	err := cloudcontroller.DecodeJSON(data, &ccResourceLinks)
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	for key, val := range ccResourceLinks.Links {
   106  		resources[key] = val
   107  	}
   108  
   109  	return nil
   110  }
   111  
   112  // GetInfo returns endpoint and API information from /v3.
   113  func (client *Client) GetInfo() (Info, ResourceLinks, Warnings, error) {
   114  	rootResponse, warnings, err := client.rootResponse()
   115  	if err != nil {
   116  		return Info{}, ResourceLinks{}, warnings, err
   117  	}
   118  
   119  	request, err := client.newHTTPRequest(requestOptions{
   120  		Method: http.MethodGet,
   121  		URL:    rootResponse.ccV3Link(),
   122  	})
   123  	if err != nil {
   124  		return Info{}, ResourceLinks{}, warnings, err
   125  	}
   126  
   127  	info := ResourceLinks{} // Explicitly initializing
   128  	response := cloudcontroller.Response{
   129  		DecodeJSONResponseInto: &info,
   130  	}
   131  
   132  	err = client.connection.Make(request, &response)
   133  	warnings = append(warnings, response.Warnings...)
   134  
   135  	if err != nil {
   136  		return Info{}, ResourceLinks{}, warnings, err
   137  	}
   138  
   139  	return rootResponse, info, warnings, nil
   140  }
   141  
   142  // rootResponse returns the CC API root document.
   143  func (client *Client) rootResponse() (Info, Warnings, error) {
   144  	request, err := client.newHTTPRequest(requestOptions{
   145  		Method: http.MethodGet,
   146  		URL:    client.cloudControllerURL,
   147  	})
   148  	if err != nil {
   149  		return Info{}, nil, err
   150  	}
   151  
   152  	var rootResponse Info
   153  	response := cloudcontroller.Response{
   154  		DecodeJSONResponseInto: &rootResponse,
   155  	}
   156  
   157  	err = client.connection.Make(request, &response)
   158  	if unknownSourceErr, ok := err.(ccerror.UnknownHTTPSourceError); ok && unknownSourceErr.StatusCode == http.StatusNotFound {
   159  		return Info{}, nil, ccerror.APINotFoundError{URL: client.cloudControllerURL}
   160  	}
   161  
   162  	return rootResponse, response.Warnings, err
   163  }