github.com/arunkumar7540/cli@v6.45.0+incompatible/api/cloudcontroller/ccv3/space.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"bytes"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
     8  	"encoding/json"
     9  )
    10  
    11  // Space represents a Cloud Controller V3 Space.
    12  type Space struct {
    13  	// GUID is a unique space identifier.
    14  	GUID string `json:"guid,omitempty"`
    15  	// Name is the name of the space.
    16  	Name string `json:"name"`
    17  	// Relationships list the relationships to the space.
    18  	Relationships Relationships `json:"relationships,omitempty"`
    19  	// Metadata is used for custom tagging of API resources
    20  	Metadata *Metadata `json:"metadata,omitempty"`
    21  }
    22  
    23  // GetSpaces lists spaces with optional filters.
    24  func (client *Client) GetSpaces(query ...Query) ([]Space, Warnings, error) {
    25  	request, err := client.newHTTPRequest(requestOptions{
    26  		RequestName: internal.GetSpacesRequest,
    27  		Query:       query,
    28  	})
    29  	if err != nil {
    30  		return nil, nil, err
    31  	}
    32  
    33  	var fullSpacesList []Space
    34  	warnings, err := client.paginate(request, Space{}, func(item interface{}) error {
    35  		if space, ok := item.(Space); ok {
    36  			fullSpacesList = append(fullSpacesList, space)
    37  		} else {
    38  			return ccerror.UnknownObjectInListError{
    39  				Expected:   Space{},
    40  				Unexpected: item,
    41  			}
    42  		}
    43  		return nil
    44  	})
    45  
    46  	return fullSpacesList, warnings, err
    47  }
    48  
    49  func (client *Client) UpdateSpace(space Space) (Space, Warnings, error) {
    50  	spaceGUID := space.GUID
    51  	space.GUID = ""
    52  	space.Relationships = nil
    53  
    54  	spaceBytes, err := json.Marshal(space)
    55  	if err != nil {
    56  		return Space{}, nil, err
    57  	}
    58  	request, err := client.newHTTPRequest(requestOptions{
    59  		RequestName: internal.PatchSpaceRequest,
    60  		Body:        bytes.NewReader(spaceBytes),
    61  		URIParams:   map[string]string{"space_guid": spaceGUID},
    62  	})
    63  
    64  	if err != nil {
    65  		return Space{}, nil, err
    66  	}
    67  
    68  	var responseSpace Space
    69  	response := cloudcontroller.Response{
    70  		DecodeJSONResponseInto: &responseSpace,
    71  	}
    72  	err = client.connection.Make(request, &response)
    73  
    74  	if err != nil {
    75  		return Space{}, nil, err
    76  	}
    77  	return responseSpace, response.Warnings, err
    78  }