github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/api/cloudcontroller/ccv2/space.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
     8  )
     9  
    10  // Space represents a Cloud Controller Space.
    11  type Space struct {
    12  	GUID     string
    13  	Name     string
    14  	AllowSSH bool
    15  }
    16  
    17  // UnmarshalJSON helps unmarshal a Cloud Controller Space response.
    18  func (space *Space) UnmarshalJSON(data []byte) error {
    19  	var ccSpace struct {
    20  		Metadata internal.Metadata `json:"metadata"`
    21  		Entity   struct {
    22  			Name     string `json:"name"`
    23  			AllowSSH bool   `json:"allow_ssh"`
    24  		} `json:"entity"`
    25  	}
    26  	if err := json.Unmarshal(data, &ccSpace); err != nil {
    27  		return err
    28  	}
    29  
    30  	space.GUID = ccSpace.Metadata.GUID
    31  	space.Name = ccSpace.Entity.Name
    32  	space.AllowSSH = ccSpace.Entity.AllowSSH
    33  	return nil
    34  }
    35  
    36  // GetSpaces returns back a list of Spaces based off of the provided queries.
    37  func (client *Client) GetSpaces(queries []Query) ([]Space, Warnings, error) {
    38  	request, err := client.newHTTPRequest(requestOptions{
    39  		RequestName: internal.SpacesRequest,
    40  		Query:       FormatQueryParameters(queries),
    41  	})
    42  	if err != nil {
    43  		return nil, nil, err
    44  	}
    45  
    46  	var fullSpacesList []Space
    47  	warnings, err := client.paginate(request, Space{}, func(item interface{}) error {
    48  		if space, ok := item.(Space); ok {
    49  			fullSpacesList = append(fullSpacesList, space)
    50  		} else {
    51  			return cloudcontroller.UnknownObjectInListError{
    52  				Expected:   Space{},
    53  				Unexpected: item,
    54  			}
    55  		}
    56  		return nil
    57  	})
    58  
    59  	return fullSpacesList, warnings, err
    60  }