github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/mccp/mccpv2/spaces_test.go (about)

     1  package mccpv2
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  
     7  	bluemix "github.com/IBM-Cloud/bluemix-go"
     8  	"github.com/IBM-Cloud/bluemix-go/client"
     9  	"github.com/IBM-Cloud/bluemix-go/helpers"
    10  	"github.com/IBM-Cloud/bluemix-go/session"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	"github.com/onsi/gomega/ghttp"
    15  )
    16  
    17  var _ = Describe("Spaces", func() {
    18  	var server *ghttp.Server
    19  	AfterEach(func() {
    20  		server.Close()
    21  	})
    22  
    23  	Describe("Create", func() {
    24  		Context("When creation is successful", func() {
    25  			BeforeEach(func() {
    26  				server = ghttp.NewServer()
    27  				server.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest(http.MethodPost, "/v2/spaces"),
    30  						ghttp.VerifyBody([]byte(`{"name":"testspace","organization_guid":"3c1b6f9d-ffe5-43b5-ab91-7be2331dc546"}`)),
    31  						ghttp.RespondWith(http.StatusCreated, `{
    32  							 "metadata": {
    33  									"guid": "64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b",
    34  									"url": "/v2/space_quota_definitions/64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b",
    35  									"created_at": "2017-05-03T08:52:07Z",
    36  									"updated_at": null
    37  
    38  							  },
    39  							  "entity": {
    40  							    "name": "testspace",
    41  								"organization_guid": "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546",
    42  								"space_quota_definition_guid": null,
    43  								"allow_ssh": true
    44  							  }							
    45  						}`),
    46  					),
    47  				)
    48  			})
    49  
    50  			It("should return Spaces created", func() {
    51  				payload := SpaceCreateRequest{
    52  					Name:    "testspace",
    53  					OrgGUID: "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546",
    54  				}
    55  				myspace, err := newSpaces(server.URL()).Create(payload)
    56  				Expect(err).NotTo(HaveOccurred())
    57  				Expect(myspace).ShouldNot(BeNil())
    58  				Expect(myspace.Metadata.GUID).Should(Equal("64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b"))
    59  				Expect(myspace.Entity.Name).Should(Equal("testspace"))
    60  				Expect(myspace.Entity.OrgGUID).Should(Equal("3c1b6f9d-ffe5-43b5-ab91-7be2331dc546"))
    61  				Expect(myspace.Entity.SpaceQuotaGUID).Should(Equal(""))
    62  			})
    63  		})
    64  		Context("When creation is failed", func() {
    65  			BeforeEach(func() {
    66  				server = ghttp.NewServer()
    67  				server.SetAllowUnhandledRequests(true)
    68  				server.AppendHandlers(
    69  					ghttp.CombineHandlers(
    70  						ghttp.VerifyRequest(http.MethodPost, "/v2/spaces"),
    71  						ghttp.VerifyBody([]byte(`{"name":"testspace","organization_guid":"3c1b6f9d-ffe5-43b5-ab91-7be2331dc546"}`)),
    72  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to create space`),
    73  					),
    74  				)
    75  			})
    76  
    77  			It("should return error when space is created", func() {
    78  				payload := SpaceCreateRequest{
    79  					Name:    "testspace",
    80  					OrgGUID: "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546",
    81  				}
    82  				myspace, err := newSpaces(server.URL()).Create(payload)
    83  				Expect(err).To(HaveOccurred())
    84  				Expect(myspace).Should(BeNil())
    85  			})
    86  		})
    87  	})
    88  
    89  	Describe("Get", func() {
    90  		Context("When read of space is successful", func() {
    91  			BeforeEach(func() {
    92  				server = ghttp.NewServer()
    93  				server.AppendHandlers(
    94  					ghttp.CombineHandlers(
    95  						ghttp.VerifyRequest(http.MethodGet, "/v2/spaces/64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b"),
    96  						ghttp.RespondWith(http.StatusOK, `{
    97  							 "metadata": {
    98  									"guid": "64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b",
    99  									"url": "/v2/space_quota_definitions/64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b",
   100  									"created_at": "2017-05-03T08:52:07Z",
   101  									"updated_at": null
   102  
   103  							  },
   104  							  "entity": {
   105  							    "name": "testspace",
   106  								"organization_guid": "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546",
   107  								"space_quota_definition_guid": null,
   108  								"allow_ssh": true
   109  							  }							
   110  						}`),
   111  					),
   112  				)
   113  			})
   114  
   115  			It("should return Space", func() {
   116  				myspace, err := newSpaces(server.URL()).Get("64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b")
   117  				Expect(err).NotTo(HaveOccurred())
   118  				Expect(myspace).ShouldNot(BeNil())
   119  				Expect(myspace.Metadata.GUID).Should(Equal("64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b"))
   120  				Expect(myspace.Entity.Name).Should(Equal("testspace"))
   121  				Expect(myspace.Entity.OrgGUID).Should(Equal("3c1b6f9d-ffe5-43b5-ab91-7be2331dc546"))
   122  				Expect(myspace.Entity.SpaceQuotaGUID).Should(Equal(""))
   123  			})
   124  		})
   125  		Context("When space retrieve is failed", func() {
   126  			BeforeEach(func() {
   127  				server = ghttp.NewServer()
   128  				server.SetAllowUnhandledRequests(true)
   129  				server.AppendHandlers(
   130  					ghttp.CombineHandlers(
   131  						ghttp.VerifyRequest(http.MethodGet, "/v2/spaces/64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b"),
   132  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve space`),
   133  					),
   134  				)
   135  			})
   136  
   137  			It("should return error when space is retrieved", func() {
   138  				myspace, err := newSpaces(server.URL()).Get("64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b")
   139  				Expect(err).To(HaveOccurred())
   140  				Expect(myspace).Should(BeNil())
   141  			})
   142  		})
   143  	})
   144  
   145  	Describe("Update", func() {
   146  		Context("When update of space is successful", func() {
   147  			BeforeEach(func() {
   148  				server = ghttp.NewServer()
   149  				server.AppendHandlers(
   150  					ghttp.CombineHandlers(
   151  						ghttp.VerifyRequest(http.MethodPut, "/v2/spaces/64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b"),
   152  						ghttp.VerifyBody([]byte(`{"name":"testspaceupdate"}`)),
   153  						ghttp.RespondWith(http.StatusCreated, `{
   154  							 "metadata": {
   155  									"guid": "64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b",
   156  									"url": "/v2/space_quota_definitions/64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b",
   157  									"created_at": "2017-05-03T08:52:07Z",
   158  									"updated_at": "2017-05-03T11:02:23Z"
   159  
   160  							  },
   161  							  "entity": {
   162  							    "name": "testspaceupdate",
   163  								"organization_guid": "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546",
   164  								"space_quota_definition_guid": null,
   165  								"allow_ssh": true
   166  							  }							
   167  						}`),
   168  					),
   169  				)
   170  			})
   171  
   172  			It("should return space update", func() {
   173  				payload := SpaceUpdateRequest{
   174  					Name: helpers.String("testspaceupdate"),
   175  				}
   176  				myspace, err := newSpaces(server.URL()).Update("64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b", payload)
   177  				Expect(err).NotTo(HaveOccurred())
   178  				Expect(myspace).ShouldNot(BeNil())
   179  				Expect(myspace.Metadata.GUID).Should(Equal("64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b"))
   180  				Expect(myspace.Entity.Name).Should(Equal("testspaceupdate"))
   181  				Expect(myspace.Entity.OrgGUID).Should(Equal("3c1b6f9d-ffe5-43b5-ab91-7be2331dc546"))
   182  				Expect(myspace.Entity.SpaceQuotaGUID).Should(Equal(""))
   183  			})
   184  		})
   185  		Context("When space update is failed", func() {
   186  			BeforeEach(func() {
   187  				server = ghttp.NewServer()
   188  				server.SetAllowUnhandledRequests(true)
   189  				server.AppendHandlers(
   190  					ghttp.CombineHandlers(
   191  						ghttp.VerifyRequest(http.MethodPut, "/v2/spaces/64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b"),
   192  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve space`),
   193  					),
   194  				)
   195  			})
   196  
   197  			It("should return error when space updated", func() {
   198  				payload := SpaceUpdateRequest{
   199  					Name: helpers.String("testspaceupdate"),
   200  				}
   201  				myspace, err := newSpaces(server.URL()).Update("64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b", payload)
   202  				Expect(err).To(HaveOccurred())
   203  				Expect(myspace).Should(BeNil())
   204  			})
   205  		})
   206  	})
   207  	Describe("Delete", func() {
   208  		Context("When delete of space is successful", func() {
   209  			BeforeEach(func() {
   210  				server = ghttp.NewServer()
   211  				server.AppendHandlers(
   212  					ghttp.CombineHandlers(
   213  						ghttp.VerifyRequest(http.MethodDelete, "/v2/spaces/64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b"),
   214  						ghttp.RespondWith(http.StatusOK, `{							
   215  						}`),
   216  					),
   217  				)
   218  			})
   219  
   220  			It("should delete Space", func() {
   221  				err := newSpaces(server.URL()).Delete("64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b")
   222  				Expect(err).NotTo(HaveOccurred())
   223  			})
   224  		})
   225  		Context("When space update is failed", func() {
   226  			BeforeEach(func() {
   227  				server = ghttp.NewServer()
   228  				server.SetAllowUnhandledRequests(true)
   229  				server.AppendHandlers(
   230  					ghttp.CombineHandlers(
   231  						ghttp.VerifyRequest(http.MethodDelete, "/v2/spaces/64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b"),
   232  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve space`),
   233  					),
   234  				)
   235  			})
   236  
   237  			It("should return error when space  deleted", func() {
   238  				err := newSpaces(server.URL()).Delete("64ff2b7d-b6d9-48c6-94a2-7f4ba670d67b")
   239  				Expect(err).To(HaveOccurred())
   240  			})
   241  		})
   242  	})
   243  
   244  })
   245  
   246  var _ = Describe("Space Repository", func() {
   247  	var server *ghttp.Server
   248  	AfterEach(func() {
   249  		server.Close()
   250  	})
   251  	Describe("ListSpacesInOrg()", func() {
   252  		Context("Server return one space", func() {
   253  			BeforeEach(func() {
   254  				server = ghttp.NewServer()
   255  				server.AppendHandlers(
   256  					ghttp.CombineHandlers(
   257  						ghttp.VerifyRequest(http.MethodGet, "/v2/organizations/3c1b6f9d-ffe5-43b5-ab91-7be2331dc546/spaces"),
   258  						ghttp.RespondWith(http.StatusOK, `{
   259  							"total_results": 1,
   260  							"total_pages": 1,
   261  							"prev_url": null,
   262  							"next_url": null,
   263  							"resources": [
   264  							{
   265  								"metadata": {
   266  									"guid": "9fd6aed4-b36b-438e-832f-9f29a68ad61c",
   267  									"url": "/v2/spaces/9fd6aed4-b36b-438e-832f-9f29a68ad61c",
   268  									"created_at": "2015-08-18T09:45:19Z",
   269  									"updated_at": "2016-03-07T13:41:50Z"
   270  								},
   271  								"entity": {
   272  									"name": "prod",
   273  									"organization_guid": "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546",
   274  									"space_quota_definition_guid": null,
   275  									"allow_ssh": true
   276  								}
   277  										
   278  							}]
   279  							
   280  								
   281  						}`),
   282  					),
   283  				)
   284  			})
   285  
   286  			It("should return one space", func() {
   287  				myspaces, err := newSpaces(server.URL()).ListSpacesInOrg("3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", "region")
   288  				Expect(err).To(Succeed())
   289  				Expect(len(myspaces)).To(Equal(1))
   290  
   291  				space := myspaces[0]
   292  				Expect(space.GUID).To(Equal("9fd6aed4-b36b-438e-832f-9f29a68ad61c"))
   293  				Expect(space.Name).To(Equal("prod"))
   294  
   295  			})
   296  
   297  		})
   298  		Context("Server return multiple space", func() {
   299  			BeforeEach(func() {
   300  				server = ghttp.NewServer()
   301  				server.AppendHandlers(
   302  					ghttp.CombineHandlers(
   303  						ghttp.VerifyRequest(http.MethodGet, "/v2/organizations/3c1b6f9d-ffe5-43b5-ab91-7be2331dc546/spaces"),
   304  						ghttp.RespondWith(http.StatusOK, `{
   305  							"total_results": 2,
   306  							"total_pages": 1,
   307  							"prev_url": null,
   308  							"next_url": null,
   309  							"resources": [
   310  							{
   311  								"metadata": {
   312  									"guid": "9fd6aed4-b36b-438e-832f-9f29a68ad61c",
   313  									"url": "/v2/spaces/9fd6aed4-b36b-438e-832f-9f29a68ad61c",
   314  									"created_at": "2015-08-18T09:45:19Z",
   315  									"updated_at": "2016-03-07T13:41:50Z"
   316  								},
   317  								"entity": {
   318  									"name": "prod",
   319  									"organization_guid": "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546",
   320  									"space_quota_definition_guid": null,
   321  									"allow_ssh": true
   322  								}
   323  										
   324  							},
   325  							{
   326  								"metadata": {
   327  									"guid": "af759fe9-613d-44c7-81df-65d06d13d723",
   328  									"url": "/v2/spaces/af759fe9-613d-44c7-81df-65d06d13d723",
   329  									"created_at": "2017-05-03T16:55:41Z",
   330  									"updated_at": null
   331  								},
   332  								"entity": {
   333  									"name": "test",
   334  									"organization_guid": "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546",
   335  									"space_quota_definition_guid": null,
   336  									"allow_ssh": true
   337         
   338  								}
   339  							}
   340  							]
   341  															
   342  						}`),
   343  					),
   344  				)
   345  			})
   346  
   347  			It("should return multiple spaces", func() {
   348  				myspaces, err := newSpaces(server.URL()).ListSpacesInOrg("3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", "region")
   349  				Expect(err).To(Succeed())
   350  				Expect(len(myspaces)).To(Equal(2))
   351  
   352  				space := myspaces[0]
   353  				Expect(space.GUID).To(Equal("9fd6aed4-b36b-438e-832f-9f29a68ad61c"))
   354  				Expect(space.Name).To(Equal("prod"))
   355  
   356  				space = myspaces[1]
   357  				Expect(space.GUID).To(Equal("af759fe9-613d-44c7-81df-65d06d13d723"))
   358  				Expect(space.Name).To(Equal("test"))
   359  
   360  			})
   361  
   362  		})
   363  		Context("Server return no spaces", func() {
   364  			BeforeEach(func() {
   365  				server = ghttp.NewServer()
   366  				server.AppendHandlers(
   367  					ghttp.CombineHandlers(
   368  						ghttp.VerifyRequest(http.MethodGet, "/v2/organizations/3c1b6f9d-ffe5-43b5-ab91-7be2331dc546/spaces"),
   369  						ghttp.RespondWith(http.StatusOK, `{
   370  							"total_results": 0,
   371  							"resources": [
   372  							]
   373  															
   374  						}`),
   375  					),
   376  				)
   377  			})
   378  
   379  			It("should return no spaces", func() {
   380  				myspaces, err := newSpaces(server.URL()).ListSpacesInOrg("3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", "region")
   381  				Expect(err).To(Succeed())
   382  				Expect(len(myspaces)).To(Equal(0))
   383  			})
   384  
   385  		})
   386  		Context("Server return error", func() {
   387  			BeforeEach(func() {
   388  				server = ghttp.NewServer()
   389  				server.SetAllowUnhandledRequests(true)
   390  				server.AppendHandlers(
   391  					ghttp.CombineHandlers(
   392  						ghttp.VerifyRequest(http.MethodGet, "/v2/organizations/3c1b6f9d-ffe5-43b5-ab91-7be2331dc546/spaces"),
   393  						ghttp.RespondWith(http.StatusInternalServerError, `{
   394  															
   395  						}`),
   396  					),
   397  				)
   398  			})
   399  
   400  			It("should return error", func() {
   401  				myspaces, err := newSpaces(server.URL()).ListSpacesInOrg("3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", "region")
   402  				Expect(err).To(HaveOccurred())
   403  				Expect(myspaces).To(BeNil())
   404  			})
   405  
   406  		})
   407  
   408  	})
   409  })
   410  
   411  var _ = Describe("Space by Name Repository", func() {
   412  	var server *ghttp.Server
   413  	AfterEach(func() {
   414  		server.Close()
   415  	})
   416  	Describe("FindByNameInOrg()", func() {
   417  		Context("Server return space by name", func() {
   418  			BeforeEach(func() {
   419  				server = ghttp.NewServer()
   420  				server.AppendHandlers(
   421  					ghttp.CombineHandlers(
   422  						ghttp.VerifyRequest(http.MethodGet, "/v2/organizations/3c1b6f9d-ffe5-43b5-ab91-7be2331dc546/spaces"),
   423  						ghttp.RespondWith(http.StatusOK, `{
   424  							"total_results": 1,
   425  							"total_pages": 1,
   426  							"prev_url": null,
   427  							"next_url": null,
   428  							"resources": [
   429  							{
   430  								"metadata": {
   431  									"guid": "9fd6aed4-b36b-438e-832f-9f29a68ad61c",
   432  									"url": "/v2/spaces/9fd6aed4-b36b-438e-832f-9f29a68ad61c",
   433  									"created_at": "2015-08-18T09:45:19Z",
   434  									"updated_at": "2016-03-07T13:41:50Z"
   435  								},
   436  								"entity": {
   437  									"name": "prod",
   438  									"organization_guid": "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546",
   439  									"space_quota_definition_guid": null,
   440  									"allow_ssh": true
   441  								}
   442  										
   443  							}]
   444  							
   445  								
   446  						}`),
   447  					),
   448  				)
   449  			})
   450  
   451  			It("should return one space", func() {
   452  				myspaces, err := newSpaces(server.URL()).FindByNameInOrg("3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", "prod", "region")
   453  				Expect(err).To(Succeed())
   454  				Expect(myspaces.GUID).To(Equal("9fd6aed4-b36b-438e-832f-9f29a68ad61c"))
   455  				Expect(myspaces.Name).To(Equal("prod"))
   456  
   457  			})
   458  
   459  		})
   460  
   461  		Context("Server return no space by name", func() {
   462  			BeforeEach(func() {
   463  				server = ghttp.NewServer()
   464  				server.AppendHandlers(
   465  					ghttp.CombineHandlers(
   466  						ghttp.VerifyRequest(http.MethodGet, "/v2/organizations/3c1b6f9d-ffe5-43b5-ab91-7be2331dc546/spaces"),
   467  						ghttp.RespondWith(http.StatusOK, `{
   468  							"total_results": 0,
   469  							"resources": [
   470  							]
   471  															
   472  						}`),
   473  					),
   474  				)
   475  			})
   476  
   477  			It("should return no spaces", func() {
   478  				myspaces, err := newSpaces(server.URL()).FindByNameInOrg("3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", "xyz", "region")
   479  				Expect(err).To(HaveOccurred())
   480  				Expect(myspaces).To(BeNil())
   481  			})
   482  
   483  		})
   484  		Context("Server return error", func() {
   485  			BeforeEach(func() {
   486  				server = ghttp.NewServer()
   487  				server.SetAllowUnhandledRequests(true)
   488  				server.AppendHandlers(
   489  					ghttp.CombineHandlers(
   490  						ghttp.VerifyRequest(http.MethodGet, "/v2/organizations/3c1b6f9d-ffe5-43b5-ab91-7be2331dc546/spaces"),
   491  						ghttp.RespondWith(http.StatusInternalServerError, `{
   492  															
   493  						}`),
   494  					),
   495  				)
   496  			})
   497  
   498  			It("should return error", func() {
   499  				myspaces, err := newSpaces(server.URL()).FindByNameInOrg("3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", "prod", "region")
   500  				Expect(err).To(HaveOccurred())
   501  				Expect(myspaces).To(BeNil())
   502  			})
   503  
   504  		})
   505  
   506  	})
   507  })
   508  
   509  func newSpaces(url string) Spaces {
   510  
   511  	sess, err := session.New()
   512  	if err != nil {
   513  		log.Fatal(err)
   514  	}
   515  	conf := sess.Config.Copy()
   516  	conf.Endpoint = &url
   517  	client := client.Client{
   518  		Config:      conf,
   519  		ServiceName: bluemix.MccpService,
   520  	}
   521  	return newSpacesAPI(&client)
   522  }