github.com/IBM-Cloud/bluemix-go@v0.0.0-20240314082800-4e02a69b84b2/api/resource/resourcev2/controllerv2/resource_service_instance_test.go (about)

     1  package controllerv2
     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  	var server *ghttp.Server
    19  	AfterEach(func() {
    20  		server.Close()
    21  	})
    22  
    23  	Describe("ListInstances()", func() {
    24  		Context("When there is no service instance", func() {
    25  			BeforeEach(func() {
    26  				server = ghttp.NewServer()
    27  				server.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest(http.MethodGet, "/v2/resource_instances"),
    30  						ghttp.RespondWith(http.StatusOK, `{"resources":[]}`),
    31  					),
    32  				)
    33  			})
    34  			It("should return zero service instance", func() {
    35  				repo := newTestServiceInstanceRepo(server.URL())
    36  				instances, err := repo.ListInstances(ServiceInstanceQuery{
    37  					ResourceGroupID: "resource_group_id",
    38  				})
    39  
    40  				Expect(err).ShouldNot(HaveOccurred())
    41  				Expect(instances).Should(BeEmpty())
    42  			})
    43  		})
    44  		Context("When there is one service instance", func() {
    45  			BeforeEach(func() {
    46  				server = ghttp.NewServer()
    47  				server.AppendHandlers(
    48  					ghttp.CombineHandlers(
    49  						ghttp.VerifyRequest(http.MethodGet, "/v2/resource_instances"),
    50  						ghttp.RespondWith(http.StatusOK, `{
    51  																	"rows_count":1,
    52  																	"resources":[{
    53  																		"id":"foo",
    54  																		"guid":"a83261db-5cd1-46ae-8cfb-8ebbcc7c0184",
    55  																		"url":"/v1/resource_instances/a83261db-5cd1-46ae-8cfb-8ebbcc7c0184",
    56  																		"created_at":"2017-07-31T06:19:45.16112535Z",
    57  																		"updated_at":null,
    58  																		"deleted_at":null,
    59  																		"name":"test-instance",
    60  																		"target_crn":"crn:v1:d_att288:dedicated::us-south::::d_att288-us-south",
    61  																		"account_id":"560df2058b1e7c402303cc598b3e5540",
    62  																		"resource_plan_id":"rc-pb-28c24fccc-4ca6-4ddd-a3be-7746cdce9912",
    63  																		"resource_group_id":"resource_group_id",
    64  																		"create_time":0,"crn":"",
    65  																		"state":"inactive",
    66  																		"type":"service_instance",
    67  																		"resource_id":"fake-resource-id",
    68  																		"dashboard_url":null,
    69  																		"last_operation":null,
    70  																		"account_url":"/v1/accounts/560df2058b1e7c402303cc598b3e5540",
    71  																		"resource_plan_url":"/v1/catalog/regions/ibm:ys1:us-south/plans/rc-pb-28c24fccc-4ca6-4ddd-a3be-7746cdce9912",
    72  																		"resource_bindings_url":"/v1/resource_instances/a83261db-5cd1-46ae-8cfb-8ebbcc7c0184/resource_bindings",
    73  																		"resource_aliases_url":"/v1/resource_instances/a83261db-5cd1-46ae-8cfb-8ebbcc7c0184/resource_aliases",
    74  																		"siblings_url":"/v1/resource_instances/a83261db-5cd1-46ae-8cfb-8ebbcc7c0184/siblings"}]}`),
    75  					),
    76  				)
    77  			})
    78  			It("should return one service instance", func() {
    79  				repo := newTestServiceInstanceRepo(server.URL())
    80  				instances, err := repo.ListInstances(ServiceInstanceQuery{
    81  					ResourceGroupID: "resource_group_id",
    82  				})
    83  
    84  				Expect(err).ShouldNot(HaveOccurred())
    85  
    86  				Expect(instances).Should(HaveLen(1))
    87  				instance := instances[0]
    88  				Expect(instance.ID).Should(Equal("foo"))
    89  				Expect(instance.ServiceID).Should(Equal("fake-resource-id"))
    90  				Expect(instance.Name).Should(Equal("test-instance"))
    91  				Expect(instance.State).Should(Equal("inactive"))
    92  				Expect(instance.Type).Should(Equal("service_instance"))
    93  			})
    94  		})
    95  
    96  		Context("When there are multiple service instances", func() {
    97  			BeforeEach(func() {
    98  				server = ghttp.NewServer()
    99  				server.AppendHandlers(
   100  					ghttp.CombineHandlers(
   101  						ghttp.VerifyRequest(http.MethodGet, "/v2/resource_instances"),
   102  						ghttp.RespondWith(http.StatusOK, `{
   103  						"rows_count":3,
   104  						"resources":[{
   105  							"id":"foo",
   106  							"guid":"a83261db-5cd1-46ae-8cfb-8ebbcc7c0184",
   107  							"url":"/v1/resource_instances/a83261db-5cd1-46ae-8cfb-8ebbcc7c0184",
   108  							"created_at":"2017-07-31T06:19:45.16112535Z",
   109  							"updated_at":null,
   110  							"deleted_at":null,
   111  							"name":"test-instance",
   112  							"target_crn":"crn:v1:d_att288:dedicated::us-south::::d_att288-us-south",
   113  							"account_id":"560df2058b1e7c402303cc598b3e5540",
   114  							"resource_plan_id":"rc-pb-28c24fccc-4ca6-4ddd-a3be-7746cdce9912",
   115  							"resource_group_id":"",
   116  							"create_time":0,"crn":"",
   117  							"state":"active",
   118  							"type":"service_instance",
   119  							"resource_id":"fake-resource-id",
   120  							"dashboard_url":null,
   121  							"last_operation":null,
   122  							"account_url":"/v1/accounts/560df2058b1e7c402303cc598b3e5540",
   123  							"resource_plan_url":"/v1/catalog/regions/ibm:ys1:us-south/plans/rc-pb-28c24fccc-4ca6-4ddd-a3be-7746cdce9912",
   124  							"resource_bindings_url":"/v1/resource_instances/a83261db-5cd1-46ae-8cfb-8ebbcc7c0184/resource_bindings",
   125  							"resource_aliases_url":"/v1/resource_instances/a83261db-5cd1-46ae-8cfb-8ebbcc7c0184/resource_aliases",
   126  							"siblings_url":"/v1/resource_instances/a83261db-5cd1-46ae-8cfb-8ebbcc7c0184/siblings"
   127  						},
   128  						{
   129  							"id":"foo1",
   130  							"guid":"dea23694-1a2c-45e4-bfa8-7be5226d998c",
   131  							"url":"/v1/resource_instances/dea23694-1a2c-45e4-bfa8-7be5226d998c",
   132  							"created_at":"2017-07-31T06:20:14.592704474Z",
   133  							"updated_at":null,
   134  							"deleted_at":null,
   135  							"name":"test-instance1",
   136  							"target_crn":"crn:v1:d_att288:dedicated::us-south::::d_att288-us-south",
   137  							"account_id":"560df2058b1e7c402303cc598b3e5540",
   138  							"resource_plan_id":"rc-pb-28c24fccc-4ca6-4ddd-a3be-7746cdce9912",
   139  							"resource_group_id":"",
   140  							"create_time":0,
   141  							"crn":"",
   142  							"state":"inactive",
   143  							"type":"service_instance",
   144  							"resource_id":"fake-resource-id1",
   145  							"dashboard_url":null,
   146  							"last_operation":null,
   147  							"account_url":"/v1/accounts/560df2058b1e7c402303cc598b3e5540",
   148  							"resource_plan_url":"/v1/catalog/regions/ibm:ys1:us-south/plans/rc-pb-28c24fccc-4ca6-4ddd-a3be-7746cdce9912",
   149  							"resource_bindings_url":"/v1/resource_instances/dea23694-1a2c-45e4-bfa8-7be5226d998c/resource_bindings",
   150  							"resource_aliases_url":"/v1/resource_instances/dea23694-1a2c-45e4-bfa8-7be5226d998c/resource_aliases",
   151  							"siblings_url":"/v1/resource_instances/dea23694-1a2c-45e4-bfa8-7be5226d998c/siblings"
   152  						},
   153  						{
   154  							"id":"foo2",
   155  							"guid":"50312f63-f43b-4a67-aa32-8626da609adb",
   156  							"url":"/v1/resource_instances/50312f63-f43b-4a67-aa32-8626da609adb",
   157  							"created_at":"2017-07-31T06:27:46.215093281Z",
   158  							"updated_at":"2017-07-31T07:34:07.740506169Z",
   159  							"deleted_at":null,
   160  							"name":"test-instance2",
   161  							"target_crn":"crn:v1:d_att288:dedicated::us-south::::d_att288-us-south",
   162  							"account_id":"560df2058b1e7c402303cc598b3e5540",
   163  							"resource_plan_id":"rc-pb-28c24fccc-4ca6-4ddd-a3be-7746cdce9912",
   164  							"resource_group_id":"",
   165  							"create_time":0,
   166  							"crn":"",
   167  							"state":"active",
   168  							"type":"service_instance",
   169  							"resource_id":"fake-resource-id2",
   170  							"dashboard_url":null,
   171  							"last_operation":null,
   172  							"account_url":"/v1/accounts/560df2058b1e7c402303cc598b3e5540",
   173  							"resource_plan_url":"/v1/catalog/regions/ibm:ys1:us-south/plans/rc-pb-28c24fccc-4ca6-4ddd-a3be-7746cdce9912",
   174  							"resource_bindings_url":"/v1/resource_instances/50312f63-f43b-4a67-aa32-8626da609adb/resource_bindings",
   175  							"resource_aliases_url":"/v1/resource_instances/50312f63-f43b-4a67-aa32-8626da609adb/resource_aliases",
   176  							"siblings_url":"/v1/resource_instances/50312f63-f43b-4a67-aa32-8626da609adb/siblings"
   177  						}
   178  						]}`),
   179  					),
   180  				)
   181  			})
   182  			It("should return all of them", func() {
   183  				repo := newTestServiceInstanceRepo(server.URL())
   184  				instances, err := repo.ListInstances(ServiceInstanceQuery{
   185  					ResourceGroupID: "resource_group_id",
   186  				})
   187  
   188  				Expect(err).ShouldNot(HaveOccurred())
   189  
   190  				Expect(instances).Should(HaveLen(3))
   191  				instance := instances[0]
   192  				Expect(instance.ID).Should(Equal("foo"))
   193  				Expect(instance.ServiceID).Should(Equal("fake-resource-id"))
   194  				Expect(instance.Name).Should(Equal("test-instance"))
   195  				Expect(instance.State).Should(Equal("active"))
   196  				Expect(instance.Type).Should(Equal("service_instance"))
   197  
   198  				instance = instances[1]
   199  				Expect(instance.ID).Should(Equal("foo1"))
   200  				Expect(instance.ServiceID).Should(Equal("fake-resource-id1"))
   201  				Expect(instance.Name).Should(Equal("test-instance1"))
   202  				Expect(instance.State).Should(Equal("inactive"))
   203  				Expect(instance.Type).Should(Equal("service_instance"))
   204  
   205  				instance = instances[2]
   206  				Expect(instance.ID).Should(Equal("foo2"))
   207  				Expect(instance.ServiceID).Should(Equal("fake-resource-id2"))
   208  				Expect(instance.Name).Should(Equal("test-instance2"))
   209  				Expect(instance.State).Should(Equal("active"))
   210  				Expect(instance.Type).Should(Equal("service_instance"))
   211  			})
   212  		})
   213  
   214  	})
   215  
   216  })
   217  
   218  func newTestServiceInstanceRepo(url string) ResourceServiceInstanceRepository {
   219  
   220  	sess, err := session.New()
   221  	if err != nil {
   222  		log.Fatal(err)
   223  	}
   224  	conf := sess.Config.Copy()
   225  	conf.Endpoint = &url
   226  
   227  	client := client.Client{
   228  		Config:      conf,
   229  		ServiceName: bluemix.ResourceManagementServicev2,
   230  	}
   231  
   232  	return newResourceServiceInstanceAPI(&client)
   233  }