github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/resource/resourcev1/controller/resource_service_instance_test.go (about)

     1  package controller
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  
     7  	"github.com/IBM-Cloud/bluemix-go"
     8  
     9  	"github.com/IBM-Cloud/bluemix-go/client"
    10  	"github.com/IBM-Cloud/bluemix-go/session"
    11  	"github.com/onsi/gomega/ghttp"
    12  
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("ServiceInstances", func() {
    18  	targetCRN := "crn:v1:d_att288:dedicated::us-south::::d_att288-us-south"
    19  	var server *ghttp.Server
    20  	AfterEach(func() {
    21  		server.Close()
    22  	})
    23  
    24  	Describe("ListInstances()", func() {
    25  		Context("When there is no service instance", func() {
    26  			BeforeEach(func() {
    27  				server = ghttp.NewServer()
    28  				server.AppendHandlers(
    29  					ghttp.CombineHandlers(
    30  						ghttp.VerifyRequest(http.MethodGet, "/v1/resource_instances"),
    31  						ghttp.RespondWith(http.StatusOK, `{"resources":[]}`),
    32  					),
    33  				)
    34  			})
    35  			It("should return zero service instance", func() {
    36  				repo := newTestServiceInstanceRepo(server.URL())
    37  				instances, err := repo.ListInstances(ServiceInstanceQuery{
    38  					ResourceGroupID: "resource_group_id",
    39  				})
    40  
    41  				Expect(err).ShouldNot(HaveOccurred())
    42  				Expect(instances).Should(BeEmpty())
    43  			})
    44  		})
    45  		Context("When there is one service instance", func() {
    46  			BeforeEach(func() {
    47  				server = ghttp.NewServer()
    48  				server.AppendHandlers(
    49  					ghttp.CombineHandlers(
    50  						ghttp.VerifyRequest(http.MethodGet, "/v1/resource_instances"),
    51  						ghttp.RespondWith(http.StatusOK, `{
    52  																	"rows_count":1,
    53  																	"resources":[{
    54  																		"id":"foo",
    55  																		"guid":"a83261db-5cd1-46ae-8cfb-8ebbcc7c0184",
    56  																		"url":"/v1/resource_instances/a83261db-5cd1-46ae-8cfb-8ebbcc7c0184",
    57  																		"created_at":"2017-07-31T06:19:45.16112535Z",
    58  																		"updated_at":null,
    59  																		"deleted_at":null,
    60  																		"name":"test-instance",
    61  																		"target_crn":"crn:v1:d_att288:dedicated::us-south::::d_att288-us-south",
    62  																		"account_id":"560df2058b1e7c402303cc598b3e5540",
    63  																		"resource_plan_id":"rc-pb-28c24fccc-4ca6-4ddd-a3be-7746cdce9912",
    64  																		"resource_group_id":"resource_group_id",
    65  																		"create_time":0,"crn":"",
    66  																		"state":"inactive",
    67  																		"type":"service_instance",
    68  																		"resource_id":"fake-resource-id",
    69  																		"dashboard_url":null,
    70  																		"last_operation":null,
    71  																		"account_url":"/v1/accounts/560df2058b1e7c402303cc598b3e5540",
    72  																		"resource_plan_url":"/v1/catalog/regions/ibm:ys1:us-south/plans/rc-pb-28c24fccc-4ca6-4ddd-a3be-7746cdce9912",
    73  																		"resource_bindings_url":"/v1/resource_instances/a83261db-5cd1-46ae-8cfb-8ebbcc7c0184/resource_bindings",
    74  																		"resource_aliases_url":"/v1/resource_instances/a83261db-5cd1-46ae-8cfb-8ebbcc7c0184/resource_aliases",
    75  																		"siblings_url":"/v1/resource_instances/a83261db-5cd1-46ae-8cfb-8ebbcc7c0184/siblings"}]}`),
    76  					),
    77  				)
    78  			})
    79  			It("should return one service instance", func() {
    80  				repo := newTestServiceInstanceRepo(server.URL())
    81  				instances, err := repo.ListInstances(ServiceInstanceQuery{
    82  					ResourceGroupID: "resource_group_id",
    83  				})
    84  
    85  				Expect(err).ShouldNot(HaveOccurred())
    86  
    87  				Expect(instances).Should(HaveLen(1))
    88  				instance := instances[0]
    89  				Expect(instance.ID).Should(Equal("foo"))
    90  				Expect(instance.ServiceID).Should(Equal("fake-resource-id"))
    91  				Expect(instance.Name).Should(Equal("test-instance"))
    92  				Expect(instance.State).Should(Equal("inactive"))
    93  				Expect(instance.Type).Should(Equal("service_instance"))
    94  			})
    95  		})
    96  
    97  		Context("When there are multiple service instances", func() {
    98  			BeforeEach(func() {
    99  				server = ghttp.NewServer()
   100  				server.AppendHandlers(
   101  					ghttp.CombineHandlers(
   102  						ghttp.VerifyRequest(http.MethodGet, "/v1/resource_instances"),
   103  						ghttp.RespondWith(http.StatusOK, `{
   104  						"rows_count":3,
   105  						"resources":[{
   106  							"id":"foo",
   107  							"guid":"a83261db-5cd1-46ae-8cfb-8ebbcc7c0184",
   108  							"url":"/v1/resource_instances/a83261db-5cd1-46ae-8cfb-8ebbcc7c0184",
   109  							"created_at":"2017-07-31T06:19:45.16112535Z",
   110  							"updated_at":null,
   111  							"deleted_at":null,
   112  							"name":"test-instance",
   113  							"target_crn":"crn:v1:d_att288:dedicated::us-south::::d_att288-us-south",
   114  							"account_id":"560df2058b1e7c402303cc598b3e5540",
   115  							"resource_plan_id":"rc-pb-28c24fccc-4ca6-4ddd-a3be-7746cdce9912",
   116  							"resource_group_id":"",
   117  							"create_time":0,"crn":"",
   118  							"state":"active",
   119  							"type":"service_instance",
   120  							"resource_id":"fake-resource-id",
   121  							"dashboard_url":null,
   122  							"last_operation":null,
   123  							"account_url":"/v1/accounts/560df2058b1e7c402303cc598b3e5540",
   124  							"resource_plan_url":"/v1/catalog/regions/ibm:ys1:us-south/plans/rc-pb-28c24fccc-4ca6-4ddd-a3be-7746cdce9912",
   125  							"resource_bindings_url":"/v1/resource_instances/a83261db-5cd1-46ae-8cfb-8ebbcc7c0184/resource_bindings",
   126  							"resource_aliases_url":"/v1/resource_instances/a83261db-5cd1-46ae-8cfb-8ebbcc7c0184/resource_aliases",
   127  							"siblings_url":"/v1/resource_instances/a83261db-5cd1-46ae-8cfb-8ebbcc7c0184/siblings"
   128  						},
   129  						{
   130  							"id":"foo1",
   131  							"guid":"dea23694-1a2c-45e4-bfa8-7be5226d998c",
   132  							"url":"/v1/resource_instances/dea23694-1a2c-45e4-bfa8-7be5226d998c",
   133  							"created_at":"2017-07-31T06:20:14.592704474Z",
   134  							"updated_at":null,
   135  							"deleted_at":null,
   136  							"name":"test-instance1",
   137  							"target_crn":"crn:v1:d_att288:dedicated::us-south::::d_att288-us-south",
   138  							"account_id":"560df2058b1e7c402303cc598b3e5540",
   139  							"resource_plan_id":"rc-pb-28c24fccc-4ca6-4ddd-a3be-7746cdce9912",
   140  							"resource_group_id":"",
   141  							"create_time":0,
   142  							"crn":"",
   143  							"state":"inactive",
   144  							"type":"service_instance",
   145  							"resource_id":"fake-resource-id1",
   146  							"dashboard_url":null,
   147  							"last_operation":null,
   148  							"account_url":"/v1/accounts/560df2058b1e7c402303cc598b3e5540",
   149  							"resource_plan_url":"/v1/catalog/regions/ibm:ys1:us-south/plans/rc-pb-28c24fccc-4ca6-4ddd-a3be-7746cdce9912",
   150  							"resource_bindings_url":"/v1/resource_instances/dea23694-1a2c-45e4-bfa8-7be5226d998c/resource_bindings",
   151  							"resource_aliases_url":"/v1/resource_instances/dea23694-1a2c-45e4-bfa8-7be5226d998c/resource_aliases",
   152  							"siblings_url":"/v1/resource_instances/dea23694-1a2c-45e4-bfa8-7be5226d998c/siblings"
   153  						},
   154  						{
   155  							"id":"foo2",
   156  							"guid":"50312f63-f43b-4a67-aa32-8626da609adb",
   157  							"url":"/v1/resource_instances/50312f63-f43b-4a67-aa32-8626da609adb",
   158  							"created_at":"2017-07-31T06:27:46.215093281Z",
   159  							"updated_at":"2017-07-31T07:34:07.740506169Z",
   160  							"deleted_at":null,
   161  							"name":"test-instance2",
   162  							"target_crn":"crn:v1:d_att288:dedicated::us-south::::d_att288-us-south",
   163  							"account_id":"560df2058b1e7c402303cc598b3e5540",
   164  							"resource_plan_id":"rc-pb-28c24fccc-4ca6-4ddd-a3be-7746cdce9912",
   165  							"resource_group_id":"",
   166  							"create_time":0,
   167  							"crn":"",
   168  							"state":"active",
   169  							"type":"service_instance",
   170  							"resource_id":"fake-resource-id2",
   171  							"dashboard_url":null,
   172  							"last_operation":null,
   173  							"account_url":"/v1/accounts/560df2058b1e7c402303cc598b3e5540",
   174  							"resource_plan_url":"/v1/catalog/regions/ibm:ys1:us-south/plans/rc-pb-28c24fccc-4ca6-4ddd-a3be-7746cdce9912",
   175  							"resource_bindings_url":"/v1/resource_instances/50312f63-f43b-4a67-aa32-8626da609adb/resource_bindings",
   176  							"resource_aliases_url":"/v1/resource_instances/50312f63-f43b-4a67-aa32-8626da609adb/resource_aliases",
   177  							"siblings_url":"/v1/resource_instances/50312f63-f43b-4a67-aa32-8626da609adb/siblings"
   178  						}
   179  						]}`),
   180  					),
   181  				)
   182  			})
   183  			It("should return all of them", func() {
   184  				repo := newTestServiceInstanceRepo(server.URL())
   185  				instances, err := repo.ListInstances(ServiceInstanceQuery{
   186  					ResourceGroupID: "resource_group_id",
   187  				})
   188  
   189  				Expect(err).ShouldNot(HaveOccurred())
   190  
   191  				Expect(instances).Should(HaveLen(3))
   192  				instance := instances[0]
   193  				Expect(instance.ID).Should(Equal("foo"))
   194  				Expect(instance.ServiceID).Should(Equal("fake-resource-id"))
   195  				Expect(instance.Name).Should(Equal("test-instance"))
   196  				Expect(instance.State).Should(Equal("active"))
   197  				Expect(instance.Type).Should(Equal("service_instance"))
   198  
   199  				instance = instances[1]
   200  				Expect(instance.ID).Should(Equal("foo1"))
   201  				Expect(instance.ServiceID).Should(Equal("fake-resource-id1"))
   202  				Expect(instance.Name).Should(Equal("test-instance1"))
   203  				Expect(instance.State).Should(Equal("inactive"))
   204  				Expect(instance.Type).Should(Equal("service_instance"))
   205  
   206  				instance = instances[2]
   207  				Expect(instance.ID).Should(Equal("foo2"))
   208  				Expect(instance.ServiceID).Should(Equal("fake-resource-id2"))
   209  				Expect(instance.Name).Should(Equal("test-instance2"))
   210  				Expect(instance.State).Should(Equal("active"))
   211  				Expect(instance.Type).Should(Equal("service_instance"))
   212  			})
   213  		})
   214  
   215  	})
   216  
   217  	Describe("CreateInstance()", func() {
   218  		Context("when creation is successful", func() {
   219  			BeforeEach(func() {
   220  				server = ghttp.NewServer()
   221  				server.AppendHandlers(
   222  					ghttp.CombineHandlers(
   223  						ghttp.VerifyRequest(http.MethodPost, "/v1/resource_instances"),
   224  						ghttp.VerifyJSONRepresenting(CreateServiceInstanceRequest{
   225  							Name:            "test-instance-name",
   226  							TargetCrn:       targetCRN,
   227  							ServicePlanID:   "test-resource-plan-id",
   228  							ResourceGroupID: "test-resource-group-id",
   229  							Tags:            []string{},
   230  						}),
   231  						ghttp.RespondWith(http.StatusOK,
   232  							`{
   233  								"id":"crn:v1:staging:public:automation-test:us-south:a/560df2058b1e7c402303cc598b3e5540:ec8b9112-331a-4883-be22-6a023542f4da::",
   234  								"guid":"",
   235  								"url":"/v1/resource_instances/3f50ed1d-070a-4114-baef-bccdc93b48c0",
   236  								"created_at":"2017-08-01T02:32:16.12857946Z",
   237  								"updated_at":null,
   238  								"deleted_at":null,
   239  								"name":"test-instance-name",
   240  								"target_crn":"crn:v1:d_att288:dedicated::us-south::::d_att288-us-south",
   241  								"account_id":"test-account-id",
   242  								"resource_plan_id":"test-resource-plan-id",
   243  								"resource_group_id":"test-resource-group-id",
   244  								"create_time":0,
   245  								"crn":"crn:v1:staging:public:automation-test:us-south:a/560df2058b1e7c402303cc598b3e5540:ec8b9112-331a-4883-be22-6a023542f4da::",
   246  								"state":"active",
   247  								"type":"service_instance",
   248  								"resource_id":"rcdemo21bff2d3d-0872-4dd7-affd-80aed1bb46a5",
   249  								"dashboard_url":"http://rc-performance-test-blue.stage1.ng.bluemix.net/cfs/dashboard/3f50ed1d-070a-4114-baef-bccdc93b48c0",
   250  								"last_operation":null,"account_url":"/v1/accounts/560df2058b1e7c402303cc598b3e5540",
   251  								"resource_plan_url":"/v1/catalog/regions/ibm:ys1:us-south/plans/rcdemo29c8c9fb5-ea26-400e-8ef0-12cd49fd2240",
   252  								"resource_bindings_url":"/v1/resource_instances/3f50ed1d-070a-4114-baef-bccdc93b48c0/resource_bindings",
   253  								"resource_aliases_url":"/v1/resource_instances/3f50ed1d-070a-4114-baef-bccdc93b48c0/resource_aliases",
   254  								"siblings_url":"/v1/resource_instances/3f50ed1d-070a-4114-baef-bccdc93b48c0/siblings"}`),
   255  					),
   256  				)
   257  			})
   258  			It("should return the new service instance", func() {
   259  				instance, err := newTestServiceInstanceRepo(server.URL()).CreateInstance(
   260  					CreateServiceInstanceRequest{
   261  						Name:            "test-instance-name",
   262  						TargetCrn:       targetCRN,
   263  						ServicePlanID:   "test-resource-plan-id",
   264  						ResourceGroupID: "test-resource-group-id",
   265  						Tags:            []string{},
   266  					})
   267  
   268  				Expect(err).ShouldNot(HaveOccurred())
   269  				Expect(instance).ShouldNot(BeNil())
   270  				Expect(instance.ID).Should(Equal("crn:v1:staging:public:automation-test:us-south:a/560df2058b1e7c402303cc598b3e5540:ec8b9112-331a-4883-be22-6a023542f4da::"))
   271  			})
   272  		})
   273  
   274  		Context("when creation failed", func() {
   275  			BeforeEach(func() {
   276  				server = ghttp.NewServer()
   277  				server.AppendHandlers(
   278  					ghttp.CombineHandlers(
   279  						ghttp.VerifyRequest(http.MethodPost, "/v1/resource_instances"),
   280  						ghttp.VerifyJSONRepresenting(CreateServiceInstanceRequest{
   281  							Name:            "test-instance-name",
   282  							TargetCrn:       targetCRN,
   283  							ServicePlanID:   "test-resource-plan-id",
   284  							ResourceGroupID: "test-resource-group-id",
   285  							Tags:            []string{},
   286  						}),
   287  						ghttp.RespondWith(http.StatusBadRequest, `400 Failed to get the region information for the given region_guid`),
   288  					),
   289  				)
   290  			})
   291  			It("should return error", func() {
   292  				_, err := newTestServiceInstanceRepo(server.URL()).CreateInstance(CreateServiceInstanceRequest{
   293  					Name:            "test-instance-name",
   294  					TargetCrn:       targetCRN,
   295  					ServicePlanID:   "test-resource-plan-id",
   296  					ResourceGroupID: "test-resource-group-id",
   297  					Tags:            []string{},
   298  				})
   299  				Expect(err).To(HaveOccurred())
   300  
   301  			})
   302  		})
   303  	})
   304  
   305  	Describe("UpdateInstance()", func() {
   306  		Context("when update is successful", func() {
   307  			BeforeEach(func() {
   308  				isDefault := new(bool)
   309  				*isDefault = false
   310  				server = ghttp.NewServer()
   311  				server.AppendHandlers(
   312  					ghttp.CombineHandlers(
   313  						ghttp.VerifyRequest(http.MethodPatch, "/v1/resource_instances/3f50ed1d-070a-4114-baef-bccdc93b48c0"),
   314  						ghttp.VerifyJSONRepresenting(UpdateServiceInstanceRequest{
   315  							Name:          "new-test-name",
   316  							ServicePlanID: "test-resource-id2",
   317  						}),
   318  						ghttp.RespondWith(http.StatusOK, `
   319  						{"id":"crn:v1:staging:public:automation-test:us-south:a/560df2058b1e7c402303cc598b3e5540:ec8b9112-331a-4883-be22-6a023542f4da::",
   320  						"guid":"",
   321  						"url":"/v1/resource_instances/281ef5c5-bdb2-4fc4-b05a-7126bf1d2923",
   322  						"created_at":"2017-07-31T07:02:16.308084742Z",
   323  						"updated_at":"2017-08-01T02:54:52.188612634Z",
   324  						"deleted_at":null,
   325  						"name":"new-test-name",
   326  						"target_crn":"crn:v1:d_att288:dedicated::us-south::::d_att288-us-south",
   327  						"account_id":"560df2058b1e7c402303cc598b3e5540",
   328  						"resource_plan_id":"test-resource-id2",
   329  						"resource_group_id":"",
   330  						"create_time":0,
   331  						"crn":"crn:v1:staging:public:automation-test:us-south:a/560df2058b1e7c402303cc598b3e5540:ec8b9112-331a-4883-be22-6a023542f4da::",
   332  						"state":"active",
   333  						"type":"service_instance",
   334  						"resource_id":"rcdemo21bff2d3d-0872-4dd7-affd-80aed1bb46a5",
   335  						"dashboard_url":"http://rc-performance-test-blue.stage1.ng.bluemix.net/cfs/dashboard/281ef5c5-bdb2-4fc4-b05a-7126bf1d2923",
   336  						"last_operation":null,
   337  						"account_url":"/v1/accounts/560df2058b1e7c402303cc598b3e5540",
   338  						"resource_plan_url":"/v1/catalog/regions/ibm:ys1:us-south/plans/rcdemo29c8c9fb5-ea26-400e-8ef0-12cd49fd2240",
   339  						"resource_bindings_url":"/v1/resource_instances/281ef5c5-bdb2-4fc4-b05a-7126bf1d2923/resource_bindings",
   340  						"resource_aliases_url":"/v1/resource_instances/281ef5c5-bdb2-4fc4-b05a-7126bf1d2923/resource_aliases",
   341  						"siblings_url":"/v1/resource_instances/281ef5c5-bdb2-4fc4-b05a-7126bf1d2923/siblings"}`),
   342  					),
   343  				)
   344  			})
   345  			It("should return the updated service instance", func() {
   346  				isDefault := new(bool)
   347  				*isDefault = false
   348  				instance, err := newTestServiceInstanceRepo(server.URL()).UpdateInstance("3f50ed1d-070a-4114-baef-bccdc93b48c0", UpdateServiceInstanceRequest{
   349  					Name:          "new-test-name",
   350  					ServicePlanID: "test-resource-id2",
   351  				})
   352  
   353  				Expect(err).ShouldNot(HaveOccurred())
   354  				Expect(instance).ShouldNot(BeNil())
   355  				Expect(instance.Name).Should(Equal("new-test-name"))
   356  				Expect(instance.Crn.String()).Should(Equal("crn:v1:staging:public:automation-test:us-south:a/560df2058b1e7c402303cc598b3e5540:ec8b9112-331a-4883-be22-6a023542f4da::"))
   357  				Expect(instance.ServicePlanID).Should(Equal("test-resource-id2"))
   358  			})
   359  		})
   360  	})
   361  	Describe("Delete()", func() {
   362  		Context("When deletion is successful", func() {
   363  			BeforeEach(func() {
   364  				server = ghttp.NewServer()
   365  				server.AppendHandlers(
   366  					ghttp.CombineHandlers(
   367  						ghttp.VerifyRequest(http.MethodDelete, "/v1/resource_instances/8cbdc8aa-fc59-4e15-9020-c509e7726346"),
   368  						ghttp.RespondWith(http.StatusNoContent, ``),
   369  					),
   370  				)
   371  			})
   372  			It("should return success", func() {
   373  				err := newTestServiceInstanceRepo(server.URL()).DeleteInstance("8cbdc8aa-fc59-4e15-9020-c509e7726346", false)
   374  				Expect(err).ShouldNot(HaveOccurred())
   375  			})
   376  		})
   377  
   378  		Context("When deletion failed", func() {
   379  			BeforeEach(func() {
   380  				server = ghttp.NewServer()
   381  				server.AppendHandlers(
   382  					ghttp.CombineHandlers(
   383  						ghttp.VerifyRequest(http.MethodDelete, "/v1/resource_instances/abc"),
   384  						ghttp.RespondWith(http.StatusNotFound, `{"message":"Not found"}`),
   385  					),
   386  				)
   387  			})
   388  			It("should return error", func() {
   389  				err := newTestServiceInstanceRepo(server.URL()).DeleteInstance("abc", false)
   390  				Expect(err).Should(HaveOccurred())
   391  			})
   392  		})
   393  	})
   394  })
   395  
   396  func newTestServiceInstanceRepo(url string) ResourceServiceInstanceRepository {
   397  
   398  	sess, err := session.New()
   399  	if err != nil {
   400  		log.Fatal(err)
   401  	}
   402  	conf := sess.Config.Copy()
   403  	conf.Endpoint = &url
   404  
   405  	client := client.Client{
   406  		Config:      conf,
   407  		ServiceName: bluemix.ResourceManagementService,
   408  	}
   409  
   410  	return newResourceServiceInstanceAPI(&client)
   411  }