github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/api/cloudcontroller/ccv2/space.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccerror"
     7  	"github.com/liamawhite/cli-with-i18n/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/github.com/liamawhite/cli-with-i18n/util/codegen/generate.go Space codetemplates/delete_async_by_guid.go.template delete_space.go
    43  //go:generate go run $GOPATH/src/github.com/liamawhite/cli-with-i18n/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  	params := FormatQueryParameters(queries)
    48  	params.Add("order-by", "name")
    49  	request, err := client.newHTTPRequest(requestOptions{
    50  		RequestName: internal.GetSpacesRequest,
    51  		Query:       params,
    52  	})
    53  	if err != nil {
    54  		return nil, nil, err
    55  	}
    56  
    57  	var fullSpacesList []Space
    58  	warnings, err := client.paginate(request, Space{}, func(item interface{}) error {
    59  		if space, ok := item.(Space); ok {
    60  			fullSpacesList = append(fullSpacesList, space)
    61  		} else {
    62  			return ccerror.UnknownObjectInListError{
    63  				Expected:   Space{},
    64  				Unexpected: item,
    65  			}
    66  		}
    67  		return nil
    68  	})
    69  
    70  	return fullSpacesList, warnings, err
    71  }
    72  
    73  // GetStagingSpacesBySecurityGroup returns a list of Spaces based on the provided
    74  // SecurityGroup GUID.
    75  func (client *Client) GetStagingSpacesBySecurityGroup(securityGroupGUID string) ([]Space, Warnings, error) {
    76  	request, err := client.newHTTPRequest(requestOptions{
    77  		RequestName: internal.GetSecurityGroupStagingSpacesRequest,
    78  		URIParams:   map[string]string{"security_group_guid": securityGroupGUID},
    79  	})
    80  	if err != nil {
    81  		return nil, nil, err
    82  	}
    83  
    84  	var fullSpacesList []Space
    85  	warnings, err := client.paginate(request, Space{}, func(item interface{}) error {
    86  		if space, ok := item.(Space); ok {
    87  			fullSpacesList = append(fullSpacesList, space)
    88  		} else {
    89  			return ccerror.UnknownObjectInListError{
    90  				Expected:   Space{},
    91  				Unexpected: item,
    92  			}
    93  		}
    94  		return nil
    95  	})
    96  
    97  	return fullSpacesList, warnings, err
    98  }
    99  
   100  // GetRunningSpacesBySecurityGroup returns a list of Spaces based on the provided
   101  // SecurityGroup GUID.
   102  func (client *Client) GetRunningSpacesBySecurityGroup(securityGroupGUID string) ([]Space, Warnings, error) {
   103  	request, err := client.newHTTPRequest(requestOptions{
   104  		RequestName: internal.GetSecurityGroupRunningSpacesRequest,
   105  		URIParams:   map[string]string{"security_group_guid": securityGroupGUID},
   106  	})
   107  	if err != nil {
   108  		return nil, nil, err
   109  	}
   110  
   111  	var fullSpacesList []Space
   112  	warnings, err := client.paginate(request, Space{}, func(item interface{}) error {
   113  		if space, ok := item.(Space); ok {
   114  			fullSpacesList = append(fullSpacesList, space)
   115  		} else {
   116  			return ccerror.UnknownObjectInListError{
   117  				Expected:   Space{},
   118  				Unexpected: item,
   119  			}
   120  		}
   121  		return nil
   122  	})
   123  
   124  	return fullSpacesList, warnings, err
   125  }