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