github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/icd/icdv4/whitelist_test.go (about)

     1  package icdv4
     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  
    12  	"github.com/onsi/gomega/ghttp"
    13  
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("Whitelists", func() {
    19  	var server *ghttp.Server
    20  	AfterEach(func() {
    21  		server.Close()
    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, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/whitelists/ip_addresses"),
    30  						ghttp.RespondWith(http.StatusCreated, `
    31                             {
    32                                "task": {
    33                                  "id": "5abb6a7d11a1a5001479a0ad",
    34                                  "description": "Adding whitelist entry for database",
    35                                  "status": "running",
    36                                  "deployment_id": "59b14b19874a1c0018009483",
    37                                  "progress_percent": 10,
    38                                  "created_at": "2018-03-28T10:21:30Z"
    39                                }
    40                              }
    41                          `),
    42  					),
    43  				)
    44  			})
    45  
    46  			It("should return whitelist created", func() {
    47  				whitelistEntry := WhitelistEntry{
    48  					Address:     "172.168.0.1",
    49  					Description: "Address description",
    50  				}
    51  				params := WhitelistReq{
    52  					WhitelistEntry: whitelistEntry,
    53  				}
    54  				target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
    55  				myTask, err := newWhitelist(server.URL()).CreateWhitelist(target, params)
    56  				Expect(err).NotTo(HaveOccurred())
    57  				Expect(myTask).ShouldNot(BeNil())
    58  				Expect(myTask.Id).Should(Equal("5abb6a7d11a1a5001479a0ad"))
    59  				Expect(myTask.Description).Should(Equal("Adding whitelist entry for database"))
    60  				Expect(myTask.Status).Should(Equal("running"))
    61  				Expect(myTask.DeploymentId).Should(Equal("59b14b19874a1c0018009483"))
    62  				Expect(myTask.ProgressPercent).Should(Equal(10))
    63  				Expect(myTask.CreatedAt).Should(Equal("2018-03-28T10:21:30Z"))
    64  			})
    65  		})
    66  		Context("When creation is unsuccessful", func() {
    67  			BeforeEach(func() {
    68  				server = ghttp.NewServer()
    69  				server.SetAllowUnhandledRequests(true)
    70  				server.AppendHandlers(
    71  					ghttp.CombineHandlers(
    72  						ghttp.VerifyRequest(http.MethodPost, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/whitelists/ip_addresses"),
    73  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to create Whitelist`),
    74  					),
    75  				)
    76  			})
    77  
    78  			It("should return error during Whitelist creation", func() {
    79  				whitelistEntry := WhitelistEntry{
    80  					Address:     "172.168.0.1",
    81  					Description: "Address description",
    82  				}
    83  				params := WhitelistReq{
    84  					WhitelistEntry: whitelistEntry,
    85  				}
    86  				target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
    87  				myTask, err := newWhitelist(server.URL()).CreateWhitelist(target, params)
    88  				Expect(err).To(HaveOccurred())
    89  				Expect(myTask.Id).Should(Equal(""))
    90  			})
    91  		})
    92  	})
    93  	Describe("Delete", func() {
    94  		Context("When deletion is successful", func() {
    95  			BeforeEach(func() {
    96  				server = ghttp.NewServer()
    97  				server.AppendHandlers(
    98  					ghttp.CombineHandlers(
    99  						ghttp.VerifyRequest(http.MethodDelete, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/whitelists/ip_addresses/172.168.0.1"),
   100  						ghttp.RespondWith(http.StatusOK, `
   101                             {
   102                                "task": {
   103                                  "id": "5abb6a7d11a1a5001479a0af",
   104                                  "description": "Deleting whitelist entry for database",
   105                                  "status": "running",
   106                                  "deployment_id": "59b14b19874a1c0018009483",
   107                                  "progress_percent": 15,
   108                                  "created_at": "2018-03-28T10:25:30Z"
   109                                }
   110                              }
   111                          `),
   112  					),
   113  				)
   114  			})
   115  
   116  			It("should return whitelist deleted", func() {
   117  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   118  				target2 := "172.168.0.1"
   119  				myTask, err := newWhitelist(server.URL()).DeleteWhitelist(target1, target2)
   120  				Expect(err).NotTo(HaveOccurred())
   121  				Expect(myTask).ShouldNot(BeNil())
   122  				Expect(myTask.Id).Should(Equal("5abb6a7d11a1a5001479a0af"))
   123  				Expect(myTask.Description).Should(Equal("Deleting whitelist entry for database"))
   124  				Expect(myTask.Status).Should(Equal("running"))
   125  				Expect(myTask.DeploymentId).Should(Equal("59b14b19874a1c0018009483"))
   126  				Expect(myTask.ProgressPercent).Should(Equal(15))
   127  				Expect(myTask.CreatedAt).Should(Equal("2018-03-28T10:25:30Z"))
   128  			})
   129  		})
   130  		Context("When deletion is unsuccessful", func() {
   131  			BeforeEach(func() {
   132  				server = ghttp.NewServer()
   133  				server.SetAllowUnhandledRequests(true)
   134  				server.AppendHandlers(
   135  					ghttp.CombineHandlers(
   136  						ghttp.VerifyRequest(http.MethodDelete, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/whitelists/ip_addresses/172.168.0.1"),
   137  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to delete Whitelist`),
   138  					),
   139  				)
   140  			})
   141  
   142  			It("should return error during Whitelist deletion", func() {
   143  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   144  				target2 := "172.168.0.1"
   145  				myTask, err := newWhitelist(server.URL()).DeleteWhitelist(target1, target2)
   146  				Expect(err).To(HaveOccurred())
   147  				Expect(myTask.Id).Should(Equal(""))
   148  			})
   149  		})
   150  	})
   151  	Describe("Get", func() {
   152  		Context("When get is successful", func() {
   153  			BeforeEach(func() {
   154  				server = ghttp.NewServer()
   155  				server.AppendHandlers(
   156  					ghttp.CombineHandlers(
   157  						ghttp.VerifyRequest(http.MethodGet, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/whitelists/ip_addresses"),
   158  						ghttp.RespondWith(http.StatusOK, `
   159                             {
   160                                  "ip_addresses": [
   161                                      {
   162                                          "address": "172.168.0.1"
   163                                      },
   164                                      {
   165                                          "address": "172.168.0.2"
   166                                      }
   167                                  ]
   168                              }
   169                          `),
   170  					),
   171  				)
   172  			})
   173  
   174  			It("should return whitelist", func() {
   175  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   176  				whitelist, err := newWhitelist(server.URL()).GetWhitelist(target1)
   177  				Expect(err).NotTo(HaveOccurred())
   178  				Expect(whitelist).ShouldNot(BeNil())
   179  				Expect(whitelist.WhitelistEntrys[0].Address).Should(Equal("172.168.0.1"))
   180  				Expect(whitelist.WhitelistEntrys[1].Address).Should(Equal("172.168.0.2"))
   181  			})
   182  		})
   183  		Context("When get is unsuccessful", func() {
   184  			BeforeEach(func() {
   185  				server = ghttp.NewServer()
   186  				server.SetAllowUnhandledRequests(true)
   187  				server.AppendHandlers(
   188  					ghttp.CombineHandlers(
   189  						ghttp.VerifyRequest(http.MethodGet, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/whitelists/ip_addresses"),
   190  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to get Whitelist`),
   191  					),
   192  				)
   193  			})
   194  
   195  			It("should return error during Whitelist gete", func() {
   196  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   197  				_, err := newWhitelist(server.URL()).GetWhitelist(target1)
   198  				Expect(err).To(HaveOccurred())
   199  			})
   200  		})
   201  	})
   202  })
   203  
   204  func newWhitelist(url string) Whitelists {
   205  
   206  	sess, err := session.New()
   207  	if err != nil {
   208  		log.Fatal(err)
   209  	}
   210  	conf := sess.Config.Copy()
   211  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   212  	conf.Endpoint = &url
   213  
   214  	client := client.Client{
   215  		Config:      conf,
   216  		ServiceName: bluemix.ICDService,
   217  	}
   218  	return newWhitelistAPI(&client)
   219  }