github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/api/cloudcontroller/ccv2/space.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     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  	OrganizationGUID         string
    14  	Name                     string
    15  	AllowSSH                 bool
    16  	SpaceQuotaDefinitionGUID string
    17  }
    18  
    19  // UnmarshalJSON helps unmarshal a Cloud Controller Space response.
    20  func (space *Space) UnmarshalJSON(data []byte) error {
    21  	var ccSpace struct {
    22  		Metadata internal.Metadata `json:"metadata"`
    23  		Entity   struct {
    24  			Name                     string `json:"name"`
    25  			AllowSSH                 bool   `json:"allow_ssh"`
    26  			SpaceQuotaDefinitionGUID string `json:"space_quota_definition_guid"`
    27  			OrganizationGUID         string `json:"organization_guid"`
    28  		} `json:"entity"`
    29  	}
    30  	if err := json.Unmarshal(data, &ccSpace); err != nil {
    31  		return err
    32  	}
    33  
    34  	space.GUID = ccSpace.Metadata.GUID
    35  	space.Name = ccSpace.Entity.Name
    36  	space.AllowSSH = ccSpace.Entity.AllowSSH
    37  	space.SpaceQuotaDefinitionGUID = ccSpace.Entity.SpaceQuotaDefinitionGUID
    38  	space.OrganizationGUID = ccSpace.Entity.OrganizationGUID
    39  	return nil
    40  }
    41  
    42  //go:generate go run $GOPATH/src/code.cloudfoundry.org/cli/util/codegen/generate.go Space codetemplates/delete_async_by_guid.go.template delete_space.go
    43  //go:generate go run $GOPATH/src/code.cloudfoundry.org/cli/util/codegen/generate.go Space codetemplates/delete_async_by_guid_test.go.template delete_space_test.go
    44  
    45  // GetSpaces returns a list of Spaces based off of the provided queries.
    46  func (client *Client) GetSpaces(queries []Query) ([]Space, Warnings, error) {
    47  	request, err := client.newHTTPRequest(requestOptions{
    48  		RequestName: internal.GetSpacesRequest,
    49  		Query:       FormatQueryParameters(queries),
    50  	})
    51  	if err != nil {
    52  		return nil, nil, err
    53  	}
    54  
    55  	var fullSpacesList []Space
    56  	warnings, err := client.paginate(request, Space{}, func(item interface{}) error {
    57  		if space, ok := item.(Space); ok {
    58  			fullSpacesList = append(fullSpacesList, space)
    59  		} else {
    60  			return ccerror.UnknownObjectInListError{
    61  				Expected:   Space{},
    62  				Unexpected: item,
    63  			}
    64  		}
    65  		return nil
    66  	})
    67  
    68  	return fullSpacesList, warnings, err
    69  }
    70  
    71  // GetStagingSpacesBySecurityGroup returns a list of Spaces based on the provided
    72  // SecurityGroup GUID.
    73  func (client *Client) GetStagingSpacesBySecurityGroup(securityGroupGUID string) ([]Space, Warnings, error) {
    74  	request, err := client.newHTTPRequest(requestOptions{
    75  		RequestName: internal.GetSecurityGroupStagingSpacesRequest,
    76  		URIParams:   map[string]string{"security_group_guid": securityGroupGUID},
    77  	})
    78  	if err != nil {
    79  		return nil, nil, err
    80  	}
    81  
    82  	var fullSpacesList []Space
    83  	warnings, err := client.paginate(request, Space{}, func(item interface{}) error {
    84  		if space, ok := item.(Space); ok {
    85  			fullSpacesList = append(fullSpacesList, space)
    86  		} else {
    87  			return ccerror.UnknownObjectInListError{
    88  				Expected:   Space{},
    89  				Unexpected: item,
    90  			}
    91  		}
    92  		return nil
    93  	})
    94  
    95  	return fullSpacesList, warnings, err
    96  }
    97  
    98  // GetRunningSpacesBySecurityGroup returns a list of Spaces based on the provided
    99  // SecurityGroup GUID.
   100  func (client *Client) GetRunningSpacesBySecurityGroup(securityGroupGUID string) ([]Space, Warnings, error) {
   101  	request, err := client.newHTTPRequest(requestOptions{
   102  		RequestName: internal.GetSecurityGroupRunningSpacesRequest,
   103  		URIParams:   map[string]string{"security_group_guid": securityGroupGUID},
   104  	})
   105  	if err != nil {
   106  		return nil, nil, err
   107  	}
   108  
   109  	var fullSpacesList []Space
   110  	warnings, err := client.paginate(request, Space{}, func(item interface{}) error {
   111  		if space, ok := item.(Space); ok {
   112  			fullSpacesList = append(fullSpacesList, space)
   113  		} else {
   114  			return ccerror.UnknownObjectInListError{
   115  				Expected:   Space{},
   116  				Unexpected: item,
   117  			}
   118  		}
   119  		return nil
   120  	})
   121  
   122  	return fullSpacesList, warnings, err
   123  }