Add a method for getting supported resources on a server

This commit is contained in:
Brendan Burns
2015-10-12 14:23:35 -07:00
parent b9c7cf43b2
commit 5afbf578b0
3 changed files with 173 additions and 0 deletions

View File

@@ -52,6 +52,7 @@ type Interface interface {
ComponentStatusesInterface
SwaggerSchemaInterface
Extensions() ExtensionsInterface
ResourcesInterface
}
func (c *Client) ReplicationControllers(namespace string) ReplicationControllerInterface {
@@ -119,6 +120,12 @@ type VersionInterface interface {
ServerAPIVersions() (*unversioned.APIVersions, error)
}
// ResourcesInterface has methods for obtaining supported resources on the API server
type ResourcesInterface interface {
SupportedResourcesForGroupVersion(groupVersion string) (*api.APIResourceList, error)
SupportedResources() (map[string]*api.APIResourceList, error)
}
// APIStatus is exposed by errors that can be converted to an api.Status object
// for finer grained details.
type APIStatus interface {
@@ -145,6 +152,42 @@ func (c *Client) ServerVersion() (*version.Info, error) {
return &info, nil
}
// SupportedResourcesForGroupVersion retrieves the list of resources supported by the API server for a group version.
func (c *Client) SupportedResourcesForGroupVersion(groupVersion string) (*api.APIResourceList, error) {
var prefix string
if groupVersion == "v1" {
prefix = "/api"
} else {
prefix = "/apis"
}
body, err := c.Get().AbsPath(prefix, groupVersion).Do().Raw()
if err != nil {
return nil, err
}
resources := api.APIResourceList{}
if err := json.Unmarshal(body, &resources); err != nil {
return nil, err
}
return &resources, nil
}
// SupportedResources gets all supported resources for all group versions. The key in the map is an API groupVersion.
func (c *Client) SupportedResources() (map[string]*api.APIResourceList, error) {
apis, err := c.ServerAPIVersions()
if err != nil {
return nil, err
}
result := map[string]*api.APIResourceList{}
for _, groupVersion := range apis.Versions {
resources, err := c.SupportedResourcesForGroupVersion(groupVersion)
if err != nil {
return nil, err
}
result[groupVersion] = resources
}
return result, nil
}
// ServerAPIVersions retrieves and parses the list of API versions the server supports.
func (c *Client) ServerAPIVersions() (*unversioned.APIVersions, error) {
body, err := c.Get().UnversionedPath("").Do().Raw()