github.com/sleungcy/cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/space.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"net/url"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
    11  )
    12  
    13  // Space represents a Cloud Controller Space.
    14  type Space struct {
    15  	// AllowSSH specifies whether SSH is enabled for this space.
    16  	AllowSSH bool
    17  
    18  	// GUID is the unique space identifier.
    19  	GUID string
    20  
    21  	// Name is the name given to the space.
    22  	Name string
    23  
    24  	// OrganizationGUID is the unique identifier of the organization this space
    25  	// belongs to.
    26  	OrganizationGUID string
    27  
    28  	// SpaceQuotaDefinitionGUID is the unique identifier of the space quota
    29  	// defined for this space.
    30  	SpaceQuotaDefinitionGUID string
    31  }
    32  
    33  // UnmarshalJSON helps unmarshal a Cloud Controller Space response.
    34  func (space *Space) UnmarshalJSON(data []byte) error {
    35  	var ccSpace struct {
    36  		Metadata internal.Metadata `json:"metadata"`
    37  		Entity   struct {
    38  			Name                     string `json:"name"`
    39  			AllowSSH                 bool   `json:"allow_ssh"`
    40  			SpaceQuotaDefinitionGUID string `json:"space_quota_definition_guid"`
    41  			OrganizationGUID         string `json:"organization_guid"`
    42  		} `json:"entity"`
    43  	}
    44  	err := cloudcontroller.DecodeJSON(data, &ccSpace)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	space.GUID = ccSpace.Metadata.GUID
    50  	space.Name = ccSpace.Entity.Name
    51  	space.AllowSSH = ccSpace.Entity.AllowSSH
    52  	space.SpaceQuotaDefinitionGUID = ccSpace.Entity.SpaceQuotaDefinitionGUID
    53  	space.OrganizationGUID = ccSpace.Entity.OrganizationGUID
    54  	return nil
    55  }
    56  
    57  type createSpaceRequestBody struct {
    58  	Name             string `json:"name"`
    59  	OrganizationGUID string `json:"organization_guid"`
    60  }
    61  
    62  // CreateSpace creates a new space with the provided spaceName in the org with
    63  // the provided orgGUID.
    64  func (client *Client) CreateSpace(spaceName string, orgGUID string) (Space, Warnings, error) {
    65  	requestBody := createSpaceRequestBody{
    66  		Name:             spaceName,
    67  		OrganizationGUID: orgGUID,
    68  	}
    69  
    70  	bodyBytes, _ := json.Marshal(requestBody)
    71  
    72  	request, err := client.newHTTPRequest(requestOptions{
    73  		RequestName: internal.PostSpaceRequest,
    74  		Body:        bytes.NewReader(bodyBytes),
    75  	})
    76  
    77  	if err != nil {
    78  		return Space{}, nil, err
    79  	}
    80  
    81  	var space Space
    82  	response := cloudcontroller.Response{
    83  		DecodeJSONResponseInto: &space,
    84  	}
    85  
    86  	err = client.connection.Make(request, &response)
    87  
    88  	return space, response.Warnings, err
    89  }
    90  
    91  // DeleteSpace deletes the Space associated with the provided
    92  // GUID. It will return the Cloud Controller job that is assigned to the
    93  // Space deletion.
    94  func (client *Client) DeleteSpace(guid string) (Job, Warnings, error) {
    95  	request, err := client.newHTTPRequest(requestOptions{
    96  		RequestName: internal.DeleteSpaceRequest,
    97  		URIParams:   Params{"space_guid": guid},
    98  		Query: url.Values{
    99  			"recursive": {"true"},
   100  			"async":     {"true"},
   101  		},
   102  	})
   103  	if err != nil {
   104  		return Job{}, nil, err
   105  	}
   106  
   107  	var job Job
   108  	response := cloudcontroller.Response{
   109  		DecodeJSONResponseInto: &job,
   110  	}
   111  
   112  	err = client.connection.Make(request, &response)
   113  	return job, response.Warnings, err
   114  }
   115  
   116  // GetSecurityGroupSpaces returns a list of Spaces based on the provided
   117  // SecurityGroup GUID.
   118  func (client *Client) GetSecurityGroupSpaces(securityGroupGUID string) ([]Space, Warnings, error) {
   119  	request, err := client.newHTTPRequest(requestOptions{
   120  		RequestName: internal.GetSecurityGroupSpacesRequest,
   121  		URIParams:   map[string]string{"security_group_guid": securityGroupGUID},
   122  	})
   123  	if err != nil {
   124  		return nil, nil, err
   125  	}
   126  
   127  	var fullSpacesList []Space
   128  	warnings, err := client.paginate(request, Space{}, func(item interface{}) error {
   129  		if space, ok := item.(Space); ok {
   130  			fullSpacesList = append(fullSpacesList, space)
   131  		} else {
   132  			return ccerror.UnknownObjectInListError{
   133  				Expected:   Space{},
   134  				Unexpected: item,
   135  			}
   136  		}
   137  		return nil
   138  	})
   139  
   140  	return fullSpacesList, warnings, err
   141  }
   142  
   143  // GetSecurityGroupStagingSpaces returns a list of Spaces based on the provided
   144  // SecurityGroup GUID.
   145  func (client *Client) GetSecurityGroupStagingSpaces(securityGroupGUID string) ([]Space, Warnings, error) {
   146  	request, err := client.newHTTPRequest(requestOptions{
   147  		RequestName: internal.GetSecurityGroupStagingSpacesRequest,
   148  		URIParams:   map[string]string{"security_group_guid": securityGroupGUID},
   149  	})
   150  	if err != nil {
   151  		return nil, nil, err
   152  	}
   153  
   154  	var fullSpacesList []Space
   155  	warnings, err := client.paginate(request, Space{}, func(item interface{}) error {
   156  		if space, ok := item.(Space); ok {
   157  			fullSpacesList = append(fullSpacesList, space)
   158  		} else {
   159  			return ccerror.UnknownObjectInListError{
   160  				Expected:   Space{},
   161  				Unexpected: item,
   162  			}
   163  		}
   164  		return nil
   165  	})
   166  
   167  	return fullSpacesList, warnings, err
   168  }
   169  
   170  // GetSpaces returns a list of Spaces based off of the provided filters.
   171  func (client *Client) GetSpaces(filters ...Filter) ([]Space, Warnings, error) {
   172  	params := ConvertFilterParameters(filters)
   173  	params.Add("order-by", "name")
   174  	request, err := client.newHTTPRequest(requestOptions{
   175  		RequestName: internal.GetSpacesRequest,
   176  		Query:       params,
   177  	})
   178  	if err != nil {
   179  		return nil, nil, err
   180  	}
   181  
   182  	var fullSpacesList []Space
   183  	warnings, err := client.paginate(request, Space{}, func(item interface{}) error {
   184  		if space, ok := item.(Space); ok {
   185  			fullSpacesList = append(fullSpacesList, space)
   186  		} else {
   187  			return ccerror.UnknownObjectInListError{
   188  				Expected:   Space{},
   189  				Unexpected: item,
   190  			}
   191  		}
   192  		return nil
   193  	})
   194  
   195  	return fullSpacesList, warnings, err
   196  }
   197  
   198  // UpdateSpaceDeveloper grants the space developer role to the user or client
   199  // associated with the given UAA ID.
   200  func (client *Client) UpdateSpaceDeveloper(spaceGUID string, uaaID string) (Warnings, error) {
   201  	request, err := client.newHTTPRequest(requestOptions{
   202  		RequestName: internal.PutSpaceDeveloperRequest,
   203  		URIParams: map[string]string{
   204  			"space_guid":     spaceGUID,
   205  			"developer_guid": uaaID,
   206  		},
   207  	})
   208  	if err != nil {
   209  		return Warnings{}, err
   210  	}
   211  
   212  	response := cloudcontroller.Response{}
   213  	err = client.connection.Make(request, &response)
   214  	return response.Warnings, err
   215  }
   216  
   217  type updateRoleRequestBody struct {
   218  	Username string `json:"username"`
   219  }
   220  
   221  // UpdateSpaceDeveloperByUsername grants the given username the space developer role.
   222  func (client *Client) UpdateSpaceDeveloperByUsername(spaceGUID string, username string) (Warnings, error) {
   223  	requestBody := updateRoleRequestBody{
   224  		Username: username,
   225  	}
   226  
   227  	bodyBytes, err := json.Marshal(requestBody)
   228  	if err != nil {
   229  		return Warnings{}, err
   230  	}
   231  
   232  	request, err := client.newHTTPRequest(requestOptions{
   233  		RequestName: internal.PutSpaceDeveloperByUsernameRequest,
   234  		URIParams:   map[string]string{"space_guid": spaceGUID},
   235  		Body:        bytes.NewReader(bodyBytes),
   236  	})
   237  	if err != nil {
   238  		return Warnings{}, err
   239  	}
   240  
   241  	response := cloudcontroller.Response{}
   242  	err = client.connection.Make(request, &response)
   243  
   244  	return Warnings(response.Warnings), err
   245  }
   246  
   247  // UpdateSpaceManager grants the space manager role to the user or client
   248  // associated with the given UAA ID.
   249  func (client *Client) UpdateSpaceManager(spaceGUID string, uaaID string) (Warnings, error) {
   250  	request, err := client.newHTTPRequest(requestOptions{
   251  		RequestName: internal.PutSpaceManagerRequest,
   252  		URIParams: map[string]string{
   253  			"space_guid":   spaceGUID,
   254  			"manager_guid": uaaID,
   255  		},
   256  	})
   257  	if err != nil {
   258  		return Warnings{}, err
   259  	}
   260  
   261  	response := cloudcontroller.Response{}
   262  	err = client.connection.Make(request, &response)
   263  	return response.Warnings, err
   264  }
   265  
   266  // UpdateSpaceManagerByUsername grants the given username the space manager role.
   267  func (client *Client) UpdateSpaceManagerByUsername(spaceGUID string, username string) (Warnings, error) {
   268  	requestBody := updateRoleRequestBody{
   269  		Username: username,
   270  	}
   271  
   272  	bodyBytes, err := json.Marshal(requestBody)
   273  	if err != nil {
   274  		return Warnings{}, err
   275  	}
   276  	request, err := client.newHTTPRequest(requestOptions{
   277  		RequestName: internal.PutSpaceManagerByUsernameRequest,
   278  		URIParams:   map[string]string{"space_guid": spaceGUID},
   279  		Body:        bytes.NewReader(bodyBytes),
   280  	})
   281  
   282  	if err != nil {
   283  		return nil, err
   284  	}
   285  
   286  	response := cloudcontroller.Response{}
   287  
   288  	err = client.connection.Make(request, &response)
   289  
   290  	return response.Warnings, err
   291  }