github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/api/cloudcontroller/ccv2/space_test.go (about)

     1  package ccv2_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  	. "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  var _ = Describe("Space", func() {
    15  	var client *Client
    16  
    17  	BeforeEach(func() {
    18  		client = NewTestClient()
    19  	})
    20  
    21  	Describe("DeleteSpace", func() {
    22  		Context("when no errors are encountered", func() {
    23  			BeforeEach(func() {
    24  				jsonResponse := `{
    25  				"metadata": {
    26  					"guid": "job-guid",
    27  					"created_at": "2016-06-08T16:41:27Z",
    28  					"url": "/v2/jobs/job-guid"
    29  				},
    30  				"entity": {
    31  					"guid": "job-guid",
    32  					"status": "queued"
    33  				}
    34  			}`
    35  
    36  				server.AppendHandlers(
    37  					CombineHandlers(
    38  						VerifyRequest(http.MethodDelete, "/v2/spaces/some-space-guid", "recursive=true&async=true"),
    39  						RespondWith(http.StatusAccepted, jsonResponse, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
    40  					))
    41  			})
    42  
    43  			It("deletes the Space and returns all warnings", func() {
    44  				job, warnings, err := client.DeleteSpace("some-space-guid")
    45  
    46  				Expect(err).NotTo(HaveOccurred())
    47  				Expect(warnings).To(ConsistOf(Warnings{"warning-1", "warning-2"}))
    48  				Expect(job.GUID).To(Equal("job-guid"))
    49  				Expect(job.Status).To(Equal(constant.JobStatusQueued))
    50  			})
    51  		})
    52  
    53  		Context("when an error is encountered", func() {
    54  			BeforeEach(func() {
    55  				response := `{
    56  "code": 30003,
    57  "description": "The Space could not be found: some-space-guid",
    58  "error_code": "CF-SpaceNotFound"
    59  }`
    60  				server.AppendHandlers(
    61  					CombineHandlers(
    62  						VerifyRequest(http.MethodDelete, "/v2/spaces/some-space-guid", "recursive=true&async=true"),
    63  						RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
    64  					))
    65  			})
    66  
    67  			It("returns an error and all warnings", func() {
    68  				_, warnings, err := client.DeleteSpace("some-space-guid")
    69  
    70  				Expect(err).To(MatchError(ccerror.ResourceNotFoundError{
    71  					Message: "The Space could not be found: some-space-guid",
    72  				}))
    73  				Expect(warnings).To(ConsistOf(Warnings{"warning-1", "warning-2"}))
    74  			})
    75  		})
    76  	})
    77  
    78  	Describe("GetSpaces", func() {
    79  		Context("when no errors are encountered", func() {
    80  			Context("when results are paginated", func() {
    81  				BeforeEach(func() {
    82  					response1 := `{
    83  						"next_url": "/v2/spaces?q=some-query:some-value&page=2&order-by=name",
    84  						"resources": [
    85  							{
    86  								"metadata": {
    87  									"guid": "space-guid-1"
    88  								},
    89  								"entity": {
    90  									"name": "space-1",
    91  									"allow_ssh": false,
    92  									"space_quota_definition_guid": "some-space-quota-guid-1",
    93  									"organization_guid": "org-guid-1"
    94  								}
    95  							},
    96  							{
    97  								"metadata": {
    98  									"guid": "space-guid-2"
    99  								},
   100  								"entity": {
   101  									"name": "space-2",
   102  									"allow_ssh": true,
   103  									"space_quota_definition_guid": "some-space-quota-guid-2",
   104  									"organization_guid": "org-guid-2"
   105  								}
   106  							}
   107  						]
   108  					}`
   109  					response2 := `{
   110  						"next_url": null,
   111  						"resources": [
   112  							{
   113  								"metadata": {
   114  									"guid": "space-guid-3"
   115  								},
   116  								"entity": {
   117  									"name": "space-3",
   118  									"allow_ssh": false,
   119  									"space_quota_definition_guid": "some-space-quota-guid-3",
   120  									"organization_guid": "org-guid-3"
   121  								}
   122  							},
   123  							{
   124  								"metadata": {
   125  									"guid": "space-guid-4"
   126  								},
   127  								"entity": {
   128  									"name": "space-4",
   129  									"allow_ssh": true,
   130  									"space_quota_definition_guid": "some-space-quota-guid-4",
   131  									"organization_guid": "org-guid-4"
   132  								}
   133  							}
   134  						]
   135  					}`
   136  					server.AppendHandlers(
   137  						CombineHandlers(
   138  							VerifyRequest(http.MethodGet, "/v2/spaces", "q=some-query:some-value&order-by=name"),
   139  							RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   140  						))
   141  					server.AppendHandlers(
   142  						CombineHandlers(
   143  							VerifyRequest(http.MethodGet, "/v2/spaces", "q=some-query:some-value&page=2&order-by=name"),
   144  							RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"warning-2"}}),
   145  						))
   146  				})
   147  
   148  				It("returns paginated results and all warnings", func() {
   149  					spaces, warnings, err := client.GetSpaces(Filter{
   150  						Type:     "some-query",
   151  						Operator: constant.EqualOperator,
   152  						Values:   []string{"some-value"},
   153  					})
   154  
   155  					Expect(err).NotTo(HaveOccurred())
   156  					Expect(spaces).To(Equal([]Space{
   157  						{
   158  							GUID:                     "space-guid-1",
   159  							OrganizationGUID:         "org-guid-1",
   160  							Name:                     "space-1",
   161  							AllowSSH:                 false,
   162  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-1",
   163  						},
   164  						{
   165  							GUID:                     "space-guid-2",
   166  							OrganizationGUID:         "org-guid-2",
   167  							Name:                     "space-2",
   168  							AllowSSH:                 true,
   169  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-2",
   170  						},
   171  						{
   172  							GUID:                     "space-guid-3",
   173  							OrganizationGUID:         "org-guid-3",
   174  							Name:                     "space-3",
   175  							AllowSSH:                 false,
   176  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-3",
   177  						},
   178  						{
   179  							GUID:                     "space-guid-4",
   180  							OrganizationGUID:         "org-guid-4",
   181  							Name:                     "space-4",
   182  							AllowSSH:                 true,
   183  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-4",
   184  						},
   185  					}))
   186  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   187  				})
   188  			})
   189  		})
   190  
   191  		Context("when an error is encountered", func() {
   192  			BeforeEach(func() {
   193  				response := `{
   194    "code": 10001,
   195    "description": "Some Error",
   196    "error_code": "CF-SomeError"
   197  }`
   198  				server.AppendHandlers(
   199  					CombineHandlers(
   200  						VerifyRequest(http.MethodGet, "/v2/spaces", "order-by=name"),
   201  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   202  					))
   203  			})
   204  
   205  			It("returns an error and all warnings", func() {
   206  				_, warnings, err := client.GetSpaces()
   207  
   208  				Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{
   209  					ResponseCode: http.StatusTeapot,
   210  					V2ErrorResponse: ccerror.V2ErrorResponse{
   211  						Code:        10001,
   212  						Description: "Some Error",
   213  						ErrorCode:   "CF-SomeError",
   214  					},
   215  				}))
   216  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   217  			})
   218  		})
   219  	})
   220  
   221  	Describe("GetSecurityGroupSpaces", func() {
   222  		Context("when no errors are encountered", func() {
   223  			Context("when results are paginated", func() {
   224  				BeforeEach(func() {
   225  					response1 := `{
   226  						"next_url": "/v2/security_groups/security-group-guid/spaces?page=2",
   227  						"resources": [
   228  							{
   229  								"metadata": {
   230  									"guid": "space-guid-1"
   231  								},
   232  								"entity": {
   233  									"name": "space-1",
   234  									"allow_ssh": false,
   235  									"space_quota_definition_guid": "some-space-quota-guid-1",
   236  									"organization_guid": "org-guid-1"
   237  								}
   238  							},
   239  							{
   240  								"metadata": {
   241  									"guid": "space-guid-2"
   242  								},
   243  								"entity": {
   244  									"name": "space-2",
   245  									"allow_ssh": true,
   246  									"space_quota_definition_guid": "some-space-quota-guid-2",
   247  									"organization_guid": "org-guid-2"
   248  								}
   249  							}
   250  						]
   251  					}`
   252  					response2 := `{
   253  						"next_url": null,
   254  						"resources": [
   255  							{
   256  								"metadata": {
   257  									"guid": "space-guid-3"
   258  								},
   259  								"entity": {
   260  									"name": "space-3",
   261  									"allow_ssh": false,
   262  									"space_quota_definition_guid": "some-space-quota-guid-3",
   263  									"organization_guid": "org-guid-3"
   264  								}
   265  							},
   266  							{
   267  								"metadata": {
   268  									"guid": "space-guid-4"
   269  								},
   270  								"entity": {
   271  									"name": "space-4",
   272  									"allow_ssh": true,
   273  									"space_quota_definition_guid": "some-space-quota-guid-4",
   274  									"organization_guid": "org-guid-4"
   275  								}
   276  							}
   277  						]
   278  					}`
   279  					server.AppendHandlers(
   280  						CombineHandlers(
   281  							VerifyRequest(http.MethodGet, "/v2/security_groups/security-group-guid/spaces", ""),
   282  							RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   283  						))
   284  					server.AppendHandlers(
   285  						CombineHandlers(
   286  							VerifyRequest(http.MethodGet, "/v2/security_groups/security-group-guid/spaces", "page=2"),
   287  							RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"warning-2"}}),
   288  						))
   289  				})
   290  
   291  				It("returns paginated results and all warnings", func() {
   292  					spaces, warnings, err := client.GetSecurityGroupSpaces("security-group-guid")
   293  
   294  					Expect(err).NotTo(HaveOccurred())
   295  					Expect(spaces).To(Equal([]Space{
   296  						{
   297  							GUID:                     "space-guid-1",
   298  							OrganizationGUID:         "org-guid-1",
   299  							Name:                     "space-1",
   300  							AllowSSH:                 false,
   301  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-1",
   302  						},
   303  						{
   304  							GUID:                     "space-guid-2",
   305  							OrganizationGUID:         "org-guid-2",
   306  							Name:                     "space-2",
   307  							AllowSSH:                 true,
   308  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-2",
   309  						},
   310  						{
   311  							GUID:                     "space-guid-3",
   312  							OrganizationGUID:         "org-guid-3",
   313  							Name:                     "space-3",
   314  							AllowSSH:                 false,
   315  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-3",
   316  						},
   317  						{
   318  							GUID:                     "space-guid-4",
   319  							OrganizationGUID:         "org-guid-4",
   320  							Name:                     "space-4",
   321  							AllowSSH:                 true,
   322  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-4",
   323  						},
   324  					}))
   325  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   326  				})
   327  			})
   328  		})
   329  
   330  		Context("when an error is encountered", func() {
   331  			BeforeEach(func() {
   332  				response := `{
   333    "code": 10001,
   334    "description": "Some Error",
   335    "error_code": "CF-SomeError"
   336  }`
   337  				server.AppendHandlers(
   338  					CombineHandlers(
   339  						VerifyRequest(http.MethodGet, "/v2/security_groups/security-group-guid/spaces"),
   340  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   341  					))
   342  			})
   343  
   344  			It("returns an error and all warnings", func() {
   345  				_, warnings, err := client.GetSecurityGroupSpaces("security-group-guid")
   346  
   347  				Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{
   348  					ResponseCode: http.StatusTeapot,
   349  					V2ErrorResponse: ccerror.V2ErrorResponse{
   350  						Code:        10001,
   351  						Description: "Some Error",
   352  						ErrorCode:   "CF-SomeError",
   353  					},
   354  				}))
   355  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   356  			})
   357  		})
   358  	})
   359  
   360  	Describe("GetSecurityGroupStagingSpaces", func() {
   361  		Context("when no errors are encountered", func() {
   362  			Context("when results are paginated", func() {
   363  				BeforeEach(func() {
   364  					response1 := `{
   365  						"next_url": "/v2/security_groups/security-group-guid/staging_spaces?page=2",
   366  						"resources": [
   367  							{
   368  								"metadata": {
   369  									"guid": "space-guid-1"
   370  								},
   371  								"entity": {
   372  									"name": "space-1",
   373  									"allow_ssh": false,
   374  									"space_quota_definition_guid": "some-space-quota-guid-1",
   375  									"organization_guid": "org-guid-1"
   376  								}
   377  							},
   378  							{
   379  								"metadata": {
   380  									"guid": "space-guid-2"
   381  								},
   382  								"entity": {
   383  									"name": "space-2",
   384  									"allow_ssh": true,
   385  									"space_quota_definition_guid": "some-space-quota-guid-2",
   386  									"organization_guid": "org-guid-2"
   387  								}
   388  							}
   389  						]
   390  					}`
   391  					response2 := `{
   392  						"next_url": null,
   393  						"resources": [
   394  							{
   395  								"metadata": {
   396  									"guid": "space-guid-3"
   397  								},
   398  								"entity": {
   399  									"name": "space-3",
   400  									"allow_ssh": false,
   401  									"space_quota_definition_guid": "some-space-quota-guid-3",
   402  									"organization_guid": "org-guid-3"
   403  								}
   404  							},
   405  							{
   406  								"metadata": {
   407  									"guid": "space-guid-4"
   408  								},
   409  								"entity": {
   410  									"name": "space-4",
   411  									"allow_ssh": true,
   412  									"space_quota_definition_guid": "some-space-quota-guid-4",
   413  									"organization_guid": "org-guid-4"
   414  								}
   415  							}
   416  						]
   417  					}`
   418  					server.AppendHandlers(
   419  						CombineHandlers(
   420  							VerifyRequest(http.MethodGet, "/v2/security_groups/security-group-guid/staging_spaces", ""),
   421  							RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"warning-1"}}),
   422  						))
   423  					server.AppendHandlers(
   424  						CombineHandlers(
   425  							VerifyRequest(http.MethodGet, "/v2/security_groups/security-group-guid/staging_spaces", "page=2"),
   426  							RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"warning-2"}}),
   427  						))
   428  				})
   429  
   430  				It("returns paginated results and all warnings", func() {
   431  					spaces, warnings, err := client.GetSecurityGroupStagingSpaces("security-group-guid")
   432  
   433  					Expect(err).NotTo(HaveOccurred())
   434  					Expect(spaces).To(Equal([]Space{
   435  						{
   436  							GUID:                     "space-guid-1",
   437  							OrganizationGUID:         "org-guid-1",
   438  							Name:                     "space-1",
   439  							AllowSSH:                 false,
   440  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-1",
   441  						},
   442  						{
   443  							GUID:                     "space-guid-2",
   444  							OrganizationGUID:         "org-guid-2",
   445  							Name:                     "space-2",
   446  							AllowSSH:                 true,
   447  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-2",
   448  						},
   449  						{
   450  							GUID:                     "space-guid-3",
   451  							OrganizationGUID:         "org-guid-3",
   452  							Name:                     "space-3",
   453  							AllowSSH:                 false,
   454  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-3",
   455  						},
   456  						{
   457  							GUID:                     "space-guid-4",
   458  							OrganizationGUID:         "org-guid-4",
   459  							Name:                     "space-4",
   460  							AllowSSH:                 true,
   461  							SpaceQuotaDefinitionGUID: "some-space-quota-guid-4",
   462  						},
   463  					}))
   464  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   465  				})
   466  			})
   467  		})
   468  
   469  		Context("when an error is encountered", func() {
   470  			BeforeEach(func() {
   471  				response := `{
   472    "code": 10001,
   473    "description": "Some Error",
   474    "error_code": "CF-SomeError"
   475  }`
   476  				server.AppendHandlers(
   477  					CombineHandlers(
   478  						VerifyRequest(http.MethodGet, "/v2/security_groups/security-group-guid/staging_spaces"),
   479  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   480  					))
   481  			})
   482  
   483  			It("returns an error and all warnings", func() {
   484  				_, warnings, err := client.GetSecurityGroupStagingSpaces("security-group-guid")
   485  
   486  				Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{
   487  					ResponseCode: http.StatusTeapot,
   488  					V2ErrorResponse: ccerror.V2ErrorResponse{
   489  						Code:        10001,
   490  						Description: "Some Error",
   491  						ErrorCode:   "CF-SomeError",
   492  					},
   493  				}))
   494  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   495  			})
   496  		})
   497  	})
   498  })