github.com/IBM-Cloud/bluemix-go@v0.0.0-20240314082800-4e02a69b84b2/api/cis/cisv1/glbs_test.go (about)

     1  package cisv1
     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  	bluemixHttp "github.com/IBM-Cloud/bluemix-go/http"
    10  	"github.com/IBM-Cloud/bluemix-go/session"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	"github.com/onsi/gomega/ghttp"
    14  )
    15  
    16  var _ = Describe("Glbs", func() {
    17  	var server *ghttp.Server
    18  	AfterEach(func() {
    19  		server.Close()
    20  	})
    21  
    22  	//List
    23  	Describe("List", func() {
    24  		Context("When read of Glbs is successful", func() {
    25  			BeforeEach(func() {
    26  				server = ghttp.NewServer()
    27  				server.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/load_balancers"),
    30  						ghttp.RespondWith(http.StatusOK, `
    31                             {
    32                                "result": [
    33                                  {
    34                                    "description": "",
    35                                    "created_on": "2018-11-23T11:11:16.984683Z",
    36                                    "modified_on": "2018-11-23T11:11:24.727621Z",
    37                                    "id": "678106b2b5143fa9560e320961500f81",
    38                                    "proxied": true,
    39                                    "enabled": true,
    40                                    "name": "www.example.com",
    41                                    "session_affinity": "none",
    42                                    "fallback_pool": "4112ba6c2974ec43886f90736968e838",
    43                                    "default_pools": [
    44                                      "6563ebae141638f92ebbdc4a821bef8c",
    45                                      "4112ba6c2974ec43886f90736968e838"
    46                                    ],
    47                                    "pop_pools": {},
    48                                    "region_pools": {
    49                                      "EEU": [
    50                                        "4112ba6c2974ec43886f90736968e838"
    51                                      ],
    52                                      "ENAM": [
    53                                        "6563ebae141638f92ebbdc4a821bef8c"
    54                                      ],
    55                                      "WEU": [
    56                                        "4112ba6c2974ec43886f90736968e838"
    57                                      ],
    58                                      "WNAM": [
    59                                        "6563ebae141638f92ebbdc4a821bef8c"
    60                                      ]
    61                                    }
    62                                  }
    63                                ],
    64                                "success": true,
    65                                "errors": [],
    66                                "messages": []
    67                              }
    68                          `),
    69  					),
    70  				)
    71  			})
    72  
    73  			It("should return Glb list", func() {
    74  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
    75  				target2 := "3fefc35e7decadb111dcf85d723a4f20"
    76  				myGlbs, err := newGlb(server.URL()).ListGlbs(target1, target2)
    77  				Expect(myGlbs).ShouldNot(BeNil())
    78  				for _, Glb := range myGlbs {
    79  					Expect(err).NotTo(HaveOccurred())
    80  					Expect(Glb.Id).Should(Equal("678106b2b5143fa9560e320961500f81"))
    81  				}
    82  			})
    83  		})
    84  		Context("When read of Glbs is unsuccessful", func() {
    85  			BeforeEach(func() {
    86  				server = ghttp.NewServer()
    87  				server.SetAllowUnhandledRequests(true)
    88  				server.AppendHandlers(
    89  					ghttp.CombineHandlers(
    90  						ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/load_balancers"),
    91  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve Glbs`),
    92  					),
    93  				)
    94  			})
    95  
    96  			It("should return error when Glb are retrieved", func() {
    97  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
    98  				target2 := "3fefc35e7decadb111dcf85d723a4f20"
    99  				myGlbPtr, err := newGlb(server.URL()).ListGlbs(target1, target2)
   100  				myGlb := myGlbPtr
   101  				Expect(err).To(HaveOccurred())
   102  				Expect(myGlb).Should(BeNil())
   103  			})
   104  		})
   105  	})
   106  
   107  	//Delete
   108  	Describe("Delete", func() {
   109  		Context("When delete of Glb is successful", func() {
   110  			BeforeEach(func() {
   111  				server = ghttp.NewServer()
   112  				server.AppendHandlers(
   113  					ghttp.CombineHandlers(
   114  						ghttp.VerifyRequest(http.MethodDelete, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/load_balancers/92859a0f6b4d3e55b953e0e29bb96338"),
   115  						ghttp.RespondWith(http.StatusOK, `{                         
   116                          }`),
   117  					),
   118  				)
   119  			})
   120  
   121  			It("should delete Glb", func() {
   122  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   123  				target2 := "3fefc35e7decadb111dcf85d723a4f20"
   124  				params := "92859a0f6b4d3e55b953e0e29bb96338"
   125  				err := newGlb(server.URL()).DeleteGlb(target1, target2, params)
   126  				Expect(err).NotTo(HaveOccurred())
   127  			})
   128  		})
   129  		Context("When Glb delete has failed", func() {
   130  			BeforeEach(func() {
   131  				server = ghttp.NewServer()
   132  				server.SetAllowUnhandledRequests(true)
   133  				server.AppendHandlers(
   134  					ghttp.CombineHandlers(
   135  						ghttp.VerifyRequest(http.MethodDelete, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/load_balancers/92859a0f6b4d3e55b953e0e29bb96338"),
   136  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to delete service key`),
   137  					),
   138  				)
   139  			})
   140  
   141  			It("should return error zone delete", func() {
   142  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   143  				target2 := "3fefc35e7decadb111dcf85d723a4f20"
   144  				params := "92859a0f6b4d3e55b953e0e29bb96338"
   145  				err := newGlb(server.URL()).DeleteGlb(target1, target2, params)
   146  				Expect(err).To(HaveOccurred())
   147  			})
   148  		})
   149  	})
   150  	//Find
   151  	Describe("Get", func() {
   152  		Context("When read of Glb is successful", func() {
   153  			BeforeEach(func() {
   154  				server = ghttp.NewServer()
   155  				server.AppendHandlers(
   156  					ghttp.CombineHandlers(
   157  						ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/load_balancers/92859a0f6b4d3e55b953e0e29bb96338"),
   158  						ghttp.RespondWith(http.StatusOK, `
   159                                                          {
   160                                "result": {
   161                                  "description": "",
   162                                  "created_on": "2018-11-23T11:11:16.984683Z",
   163                                  "modified_on": "2018-11-23T11:11:24.727621Z",
   164                                  "id": "678106b2b5143fa9560e320961500f81",
   165                                  "proxied": true,
   166                                  "enabled": true,
   167                                  "name": "www.example.com",
   168                                  "session_affinity": "none",
   169                                  "fallback_pool": "4112ba6c2974ec43886f90736968e838",
   170                                  "default_pools": [
   171                                    "6563ebae141638f92ebbdc4a821bef8c",
   172                                    "4112ba6c2974ec43886f90736968e838"
   173                                  ],
   174                                  "pop_pools": {},
   175                                  "region_pools": {
   176                                    "EEU": [
   177                                      "4112ba6c2974ec43886f90736968e838"
   178                                    ],
   179                                    "ENAM": [
   180                                      "6563ebae141638f92ebbdc4a821bef8c"
   181                                    ],
   182                                    "WEU": [
   183                                      "4112ba6c2974ec43886f90736968e838"
   184                                    ],
   185                                    "WNAM": [
   186                                      "6563ebae141638f92ebbdc4a821bef8c"
   187                                    ]
   188                                  }
   189                                },
   190                                "success": true,
   191                                "errors": [],
   192                                "messages": []
   193                              }
   194                          `),
   195  					),
   196  				)
   197  			})
   198  
   199  			It("should return Glb", func() {
   200  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   201  				target2 := "3fefc35e7decadb111dcf85d723a4f20"
   202  				params := "92859a0f6b4d3e55b953e0e29bb96338"
   203  				myGlbPtr, err := newGlb(server.URL()).GetGlb(target1, target2, params)
   204  				myGlb := *myGlbPtr
   205  				Expect(err).NotTo(HaveOccurred())
   206  				Expect(myGlb).ShouldNot(BeNil())
   207  				Expect(myGlb.Id).Should(Equal("678106b2b5143fa9560e320961500f81"))
   208  			})
   209  		})
   210  		Context("When Glb get has failed", func() {
   211  			BeforeEach(func() {
   212  				server = ghttp.NewServer()
   213  				server.SetAllowUnhandledRequests(true)
   214  				server.AppendHandlers(
   215  					ghttp.CombineHandlers(
   216  						ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/load_balancers/92859a0f6b4d3e55b953e0e29bb96338"),
   217  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve Glb`),
   218  					),
   219  				)
   220  			})
   221  
   222  			It("should return error when Glb is retrieved", func() {
   223  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   224  				target2 := "3fefc35e7decadb111dcf85d723a4f20"
   225  				params := "92859a0f6b4d3e55b953e0e29bb96338"
   226  				myGlbPtr, err := newGlb(server.URL()).GetGlb(target1, target2, params)
   227  				myGlb := myGlbPtr
   228  				Expect(err).To(HaveOccurred())
   229  				Expect(myGlb).Should(BeNil())
   230  			})
   231  		})
   232  	})
   233  	Describe("Create", func() {
   234  		Context("When creation is successful", func() {
   235  			BeforeEach(func() {
   236  				server = ghttp.NewServer()
   237  				server.AppendHandlers(
   238  					ghttp.CombineHandlers(
   239  						ghttp.VerifyRequest(http.MethodPost, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/load_balancers"),
   240  						ghttp.VerifyJSON(`{"proxied": true, "name": "www.example.com", "session_affinity": "none", "fallback_pool": "4112ba6c2974ec43886f90736968e838", "default_pools": ["6563ebae141638f92ebbdc4a821bef8c", "4112ba6c2974ec43886f90736968e838"]}`),
   241  						ghttp.RespondWith(http.StatusCreated, `
   242                             {
   243                                "result": {
   244                                  "description": "",
   245                                  "created_on": "2018-11-26T06:53:23.749062Z",
   246                                  "modified_on": "2018-11-26T06:53:23.749062Z",
   247                                  "id": "07085e0ea3c40225dcab6aff04cf64d9",
   248                                  "ttl": 30,
   249                                  "proxied": true,
   250                                  "enabled": true,
   251                                  "name": "www.example.com",
   252                                  "session_affinity": "none",
   253                                  "fallback_pool": "4112ba6c2974ec43886f90736968e838",
   254                                  "default_pools": [
   255                                    "6563ebae141638f92ebbdc4a821bef8c",
   256                                    "4112ba6c2974ec43886f90736968e838"
   257                                  ]
   258                                },
   259                                "success": true,
   260                                "errors": [],
   261                                "messages": []
   262                              }
   263                          `),
   264  					),
   265  				)
   266  			})
   267  
   268  			It("should return glb created", func() {
   269  				params := GlbBody{
   270  					Name:            "www.example.com",
   271  					SessionAffinity: "none",
   272  					DefaultPools: []string{
   273  						"6563ebae141638f92ebbdc4a821bef8c",
   274  						"4112ba6c2974ec43886f90736968e838",
   275  					},
   276  					FallbackPool: "4112ba6c2974ec43886f90736968e838",
   277  					Proxied:      true,
   278  				}
   279  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   280  				target2 := "3fefc35e7decadb111dcf85d723a4f20"
   281  				myGlbPt, err := newGlb(server.URL()).CreateGlb(target1, target2, params)
   282  				myGlb := *myGlbPt
   283  				Expect(err).NotTo(HaveOccurred())
   284  				Expect(myGlb).ShouldNot(BeNil())
   285  				Expect(myGlb.Id).Should(Equal("07085e0ea3c40225dcab6aff04cf64d9"))
   286  				Expect(myGlb.Name).Should(Equal("www.example.com"))
   287  			})
   288  		})
   289  		Context("When creation is unsuccessful", func() {
   290  			BeforeEach(func() {
   291  				server = ghttp.NewServer()
   292  				server.SetAllowUnhandledRequests(true)
   293  				server.AppendHandlers(
   294  					ghttp.CombineHandlers(
   295  						ghttp.VerifyRequest(http.MethodPost, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/load_balancers"),
   296  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to create Glb`),
   297  					),
   298  				)
   299  			})
   300  			It("should return error during Glb creation", func() {
   301  				params := GlbBody{
   302  					Name:            "www.example.com",
   303  					SessionAffinity: "none",
   304  					DefaultPools: []string{
   305  						"6563ebae141638f92ebbdc4a821bef8c",
   306  						"4112ba6c2974ec43886f90736968e838",
   307  					},
   308  					FallbackPool: "4112ba6c2974ec43886f90736968e838",
   309  					Proxied:      true,
   310  				}
   311  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   312  				target2 := "3fefc35e7decadb111dcf85d723a4f20"
   313  				myGlbPtr, err := newGlb(server.URL()).CreateGlb(target1, target2, params)
   314  				myGlb := myGlbPtr
   315  				Expect(err).To(HaveOccurred())
   316  				Expect(myGlb).Should(BeNil())
   317  			})
   318  		})
   319  	})
   320  
   321  	Describe("Update", func() {
   322  		Context("When update is successful", func() {
   323  			BeforeEach(func() {
   324  				server = ghttp.NewServer()
   325  				server.AppendHandlers(
   326  					ghttp.CombineHandlers(
   327  						ghttp.VerifyRequest(http.MethodPost, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/load_balancers"),
   328  						ghttp.VerifyJSON(`{"proxied": true, "name": "www.example.com", "session_affinity": "none", "fallback_pool": "4112ba6c2974ec43886f90736968e838", "default_pools": ["6563ebae141638f92ebbdc4a821bef8c", "4112ba6c2974ec43886f90736968e838"]}`),
   329  						ghttp.RespondWith(http.StatusCreated, `
   330                             {
   331                                "result": {
   332                                  "description": "",
   333                                  "created_on": "2018-11-26T06:53:23.749062Z",
   334                                  "modified_on": "2018-11-26T06:53:23.749062Z",
   335                                  "id": "07085e0ea3c40225dcab6aff04cf64d9",
   336                                  "ttl": 30,
   337                                  "proxied": true,
   338                                  "enabled": true,
   339                                  "name": "www.example.com",
   340                                  "session_affinity": "none",
   341                                  "fallback_pool": "4112ba6c2974ec43886f90736968e838",
   342                                  "default_pools": [
   343                                    "6563ebae141638f92ebbdc4a821bef8c",
   344                                    "4112ba6c2974ec43886f90736968e838"
   345                                  ]
   346                                },
   347                                "success": true,
   348                                "errors": [],
   349                                "messages": []
   350                              }
   351                          `),
   352  					),
   353  					ghttp.CombineHandlers(
   354  						ghttp.VerifyRequest(http.MethodPut, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/load_balancers/07085e0ea3c40225dcab6aff04cf64d9"),
   355  						ghttp.VerifyJSON(`{"proxied": true, "name": "www.example.com", "session_affinity": "none", "fallback_pool": "4112ba6c2974ec43886f90736968e888", "default_pools": ["6563ebae141638f92ebbdc4a821bef8c", "4112ba6c2974ec43886f90736968e838"]}`),
   356  						ghttp.RespondWith(http.StatusCreated, `
   357                             {
   358                                "result": {
   359                                  "description": "",
   360                                  "created_on": "2018-11-26T06:53:23.749062Z",
   361                                  "modified_on": "2018-11-26T06:53:23.749062Z",
   362                                  "id": "07085e0ea3c40225dcab6aff04cf64d9",
   363                                  "ttl": 30,
   364                                  "proxied": true,
   365                                  "enabled": true,
   366                                  "name": "www.example.com",
   367                                  "session_affinity": "none",
   368                                  "fallback_pool": "4112ba6c2974ec43886f90736968e888",
   369                                  "default_pools": [
   370                                    "6563ebae141638f92ebbdc4a821bef8c",
   371                                    "4112ba6c2974ec43886f90736968e838"
   372                                  ]
   373                                },
   374                                "success": true,
   375                                "errors": [],
   376                                "messages": []
   377                              }
   378                          `),
   379  					),
   380  				)
   381  			})
   382  
   383  			It("should return glb updated", func() {
   384  				params := GlbBody{
   385  					Name:            "www.example.com",
   386  					SessionAffinity: "none",
   387  					DefaultPools: []string{
   388  						"6563ebae141638f92ebbdc4a821bef8c",
   389  						"4112ba6c2974ec43886f90736968e838",
   390  					},
   391  					FallbackPool: "4112ba6c2974ec43886f90736968e838",
   392  					Proxied:      true,
   393  				}
   394  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   395  				target2 := "3fefc35e7decadb111dcf85d723a4f20"
   396  				myGlbPt, err := newGlb(server.URL()).CreateGlb(target1, target2, params)
   397  				myGlb := *myGlbPt
   398  				Expect(err).NotTo(HaveOccurred())
   399  				Expect(myGlb).ShouldNot(BeNil())
   400  				Expect(myGlb.Id).Should(Equal("07085e0ea3c40225dcab6aff04cf64d9"))
   401  				Expect(myGlb.Name).Should(Equal("www.example.com"))
   402  				params = GlbBody{
   403  					Name:            "www.example.com",
   404  					SessionAffinity: "none",
   405  					DefaultPools: []string{
   406  						"6563ebae141638f92ebbdc4a821bef8c",
   407  						"4112ba6c2974ec43886f90736968e838",
   408  					},
   409  					FallbackPool: "4112ba6c2974ec43886f90736968e888",
   410  					Proxied:      true,
   411  				}
   412  				target1 = "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   413  				target2 = "3fefc35e7decadb111dcf85d723a4f20"
   414  				target3 := "07085e0ea3c40225dcab6aff04cf64d9"
   415  				myGlbPt, err = newGlb(server.URL()).UpdateGlb(target1, target2, target3, params)
   416  				myGlb = *myGlbPt
   417  				Expect(err).NotTo(HaveOccurred())
   418  				Expect(myGlb).ShouldNot(BeNil())
   419  				Expect(myGlb.Id).Should(Equal("07085e0ea3c40225dcab6aff04cf64d9"))
   420  				Expect(myGlb.Name).Should(Equal("www.example.com"))
   421  				Expect(myGlb.FallbackPool).Should(Equal("4112ba6c2974ec43886f90736968e888"))
   422  			})
   423  		})
   424  		Context("When creation is unsuccessful", func() {
   425  			BeforeEach(func() {
   426  				server = ghttp.NewServer()
   427  				server.AppendHandlers(
   428  					ghttp.CombineHandlers(
   429  						ghttp.VerifyRequest(http.MethodPut, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/load_balancers/07085e0ea3c40225dcab6aff04cf64d9"),
   430  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to create Glb`),
   431  					),
   432  				)
   433  			})
   434  			It("should return error during Glb creation", func() {
   435  				params := GlbBody{
   436  					Name:            "www.example.com",
   437  					SessionAffinity: "none",
   438  					DefaultPools: []string{
   439  						"6563ebae141638f92ebbdc4a821bef8c",
   440  						"4112ba6c2974ec43886f90736968e838",
   441  					},
   442  					FallbackPool: "4112ba6c2974ec43886f90736968e838",
   443  					Proxied:      true,
   444  				}
   445  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   446  				target2 := "3fefc35e7decadb111dcf85d723a4f20"
   447  				target3 := "07085e0ea3c40225dcab6aff04cf64d9"
   448  				myGlbPtr, err := newGlb(server.URL()).UpdateGlb(target1, target2, target3, params)
   449  				myGlb := myGlbPtr
   450  				Expect(err).To(HaveOccurred())
   451  				Expect(myGlb).Should(BeNil())
   452  			})
   453  		})
   454  	})
   455  })
   456  
   457  func newGlb(url string) Glbs {
   458  
   459  	sess, err := session.New()
   460  	if err != nil {
   461  		log.Fatal(err)
   462  	}
   463  	conf := sess.Config.Copy()
   464  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   465  	conf.Endpoint = &url
   466  
   467  	client := client.Client{
   468  		Config:      conf,
   469  		ServiceName: bluemix.CisService,
   470  	}
   471  	return newGlbAPI(&client)
   472  }