github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/api/cloudcontroller/ccv2/space.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"net/url"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
     9  )
    10  
    11  // Space represents a Cloud Controller Space.
    12  type Space struct {
    13  	// GUID is the unique space identifier.
    14  	GUID string
    15  
    16  	// OrganizationGUID is the unique identifier of the organization this space
    17  	// belongs to.
    18  	OrganizationGUID string
    19  
    20  	// Name is the name given to the space.
    21  	Name string
    22  
    23  	// AllowSSH specifies whether SSH is enabled for this space.
    24  	AllowSSH bool
    25  
    26  	// SpaceQuotaDefinitionGUID is the unique identifier of the space quota
    27  	// defined for this space.
    28  	SpaceQuotaDefinitionGUID string
    29  }
    30  
    31  // UnmarshalJSON helps unmarshal a Cloud Controller Space response.
    32  func (space *Space) UnmarshalJSON(data []byte) error {
    33  	var ccSpace struct {
    34  		Metadata internal.Metadata `json:"metadata"`
    35  		Entity   struct {
    36  			Name                     string `json:"name"`
    37  			AllowSSH                 bool   `json:"allow_ssh"`
    38  			SpaceQuotaDefinitionGUID string `json:"space_quota_definition_guid"`
    39  			OrganizationGUID         string `json:"organization_guid"`
    40  		} `json:"entity"`
    41  	}
    42  	err := cloudcontroller.DecodeJSON(data, &ccSpace)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	space.GUID = ccSpace.Metadata.GUID
    48  	space.Name = ccSpace.Entity.Name
    49  	space.AllowSSH = ccSpace.Entity.AllowSSH
    50  	space.SpaceQuotaDefinitionGUID = ccSpace.Entity.SpaceQuotaDefinitionGUID
    51  	space.OrganizationGUID = ccSpace.Entity.OrganizationGUID
    52  	return nil
    53  }
    54  
    55  // DeleteSpace deletes the Space associated with the provided
    56  // GUID. It will return the Cloud Controller job that is assigned to the
    57  // Space deletion.
    58  func (client *Client) DeleteSpace(guid string) (Job, Warnings, error) {
    59  	request, err := client.newHTTPRequest(requestOptions{
    60  		RequestName: internal.DeleteSpaceRequest,
    61  		URIParams:   Params{"space_guid": guid},
    62  		Query: url.Values{
    63  			"recursive": {"true"},
    64  			"async":     {"true"},
    65  		},
    66  	})
    67  	if err != nil {
    68  		return Job{}, nil, err
    69  	}
    70  
    71  	var job Job
    72  	response := cloudcontroller.Response{
    73  		Result: &job,
    74  	}
    75  
    76  	err = client.connection.Make(request, &response)
    77  	return job, response.Warnings, err
    78  }
    79  
    80  // GetSecurityGroupSpaces returns a list of Spaces based on the provided
    81  // SecurityGroup GUID.
    82  func (client *Client) GetSecurityGroupSpaces(securityGroupGUID string) ([]Space, Warnings, error) {
    83  	request, err := client.newHTTPRequest(requestOptions{
    84  		RequestName: internal.GetSecurityGroupSpacesRequest,
    85  		URIParams:   map[string]string{"security_group_guid": securityGroupGUID},
    86  	})
    87  	if err != nil {
    88  		return nil, nil, err
    89  	}
    90  
    91  	var fullSpacesList []Space
    92  	warnings, err := client.paginate(request, Space{}, func(item interface{}) error {
    93  		if space, ok := item.(Space); ok {
    94  			fullSpacesList = append(fullSpacesList, space)
    95  		} else {
    96  			return ccerror.UnknownObjectInListError{
    97  				Expected:   Space{},
    98  				Unexpected: item,
    99  			}
   100  		}
   101  		return nil
   102  	})
   103  
   104  	return fullSpacesList, warnings, err
   105  }
   106  
   107  // GetSecurityGroupStagingSpaces returns a list of Spaces based on the provided
   108  // SecurityGroup GUID.
   109  func (client *Client) GetSecurityGroupStagingSpaces(securityGroupGUID string) ([]Space, Warnings, error) {
   110  	request, err := client.newHTTPRequest(requestOptions{
   111  		RequestName: internal.GetSecurityGroupStagingSpacesRequest,
   112  		URIParams:   map[string]string{"security_group_guid": securityGroupGUID},
   113  	})
   114  	if err != nil {
   115  		return nil, nil, err
   116  	}
   117  
   118  	var fullSpacesList []Space
   119  	warnings, err := client.paginate(request, Space{}, func(item interface{}) error {
   120  		if space, ok := item.(Space); ok {
   121  			fullSpacesList = append(fullSpacesList, space)
   122  		} else {
   123  			return ccerror.UnknownObjectInListError{
   124  				Expected:   Space{},
   125  				Unexpected: item,
   126  			}
   127  		}
   128  		return nil
   129  	})
   130  
   131  	return fullSpacesList, warnings, err
   132  }
   133  
   134  // GetSpaces returns a list of Spaces based off of the provided filters.
   135  func (client *Client) GetSpaces(filters ...Filter) ([]Space, Warnings, error) {
   136  	params := ConvertFilterParameters(filters)
   137  	params.Add("order-by", "name")
   138  	request, err := client.newHTTPRequest(requestOptions{
   139  		RequestName: internal.GetSpacesRequest,
   140  		Query:       params,
   141  	})
   142  	if err != nil {
   143  		return nil, nil, err
   144  	}
   145  
   146  	var fullSpacesList []Space
   147  	warnings, err := client.paginate(request, Space{}, func(item interface{}) error {
   148  		if space, ok := item.(Space); ok {
   149  			fullSpacesList = append(fullSpacesList, space)
   150  		} else {
   151  			return ccerror.UnknownObjectInListError{
   152  				Expected:   Space{},
   153  				Unexpected: item,
   154  			}
   155  		}
   156  		return nil
   157  	})
   158  
   159  	return fullSpacesList, warnings, err
   160  }