github.com/IBM-Cloud/bluemix-go@v0.0.0-20240314082800-4e02a69b84b2/api/cis/cisv1/zones_test.go (about) 1 package cisv1 2 3 import ( 4 "log" 5 "net/http" 6 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 "github.com/onsi/gomega/ghttp" 10 11 bluemix "github.com/IBM-Cloud/bluemix-go" 12 "github.com/IBM-Cloud/bluemix-go/client" 13 bluemixHttp "github.com/IBM-Cloud/bluemix-go/http" 14 "github.com/IBM-Cloud/bluemix-go/session" 15 ) 16 17 var _ = Describe("Zones", func() { 18 var server *ghttp.Server 19 AfterEach(func() { 20 server.Close() 21 }) 22 Describe("Create", func() { 23 Context("When creation is successful", func() { 24 BeforeEach(func() { 25 server = ghttp.NewServer() 26 server.AppendHandlers( 27 ghttp.CombineHandlers( 28 ghttp.VerifyRequest(http.MethodPost, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones"), 29 ghttp.RespondWith(http.StatusCreated, ` 30 { 31 "result": { 32 "id": "3fefc35e7decadb111dcf85d723a4f20", 33 "name": "example.com", 34 "status": "pending", 35 "paused": false, 36 "name_servers": [ 37 "ns002.name.cloud.ibm.com", 38 "ns007.name.cloud.ibm.com" 39 ], 40 "original_name_servers": [ 41 "ns005.name.cloud.ibm.com", 42 "ns016.name.cloud.ibm.com" 43 ], 44 "original_registrar": null, 45 "original_dnshost": null, 46 "modified_on": "2018-05-04T14:16:28.369359Z", 47 "created_on": "2018-05-04T14:16:28.369359Z", 48 "vanity_name_servers": [], 49 "account": { 50 "id": "796b4ef449812595ea9fe92d1e910756", 51 "name": "b424c068-c944-4565-b0bf-b278e5ec98ed" 52 } 53 }, 54 "success": true, 55 "errors": [], 56 "messages": [] 57 } 58 `), 59 ), 60 ) 61 }) 62 63 It("should return zone created", func() { 64 params := ZoneBody{Name: "wcpcloudnl.com"} 65 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 66 myZonePtr, err := newZone(server.URL()).CreateZone(target, params) 67 myZone := *myZonePtr 68 Expect(err).NotTo(HaveOccurred()) 69 Expect(myZone).ShouldNot(BeNil()) 70 Expect(myZone.Id).Should(Equal("3fefc35e7decadb111dcf85d723a4f20")) 71 Expect(myZone.Name).Should(Equal("example.com")) 72 }) 73 }) 74 Context("When creation is unsuccessful", func() { 75 BeforeEach(func() { 76 server = ghttp.NewServer() 77 server.SetAllowUnhandledRequests(true) 78 server.AppendHandlers( 79 ghttp.CombineHandlers( 80 ghttp.VerifyRequest(http.MethodPost, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones"), 81 ghttp.RespondWith(http.StatusInternalServerError, `Failed to create Zone`), 82 ), 83 ) 84 }) 85 86 It("should return error during Zone creation", func() { 87 params := ZoneBody{Name: "wcpcloudnl.com"} 88 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 89 myZonePtr, err := newZone(server.URL()).CreateZone(target, params) 90 myZone := myZonePtr 91 Expect(err).To(HaveOccurred()) 92 Expect(myZone).Should(BeNil()) 93 }) 94 }) 95 }) 96 //List 97 Describe("List", func() { 98 Context("When read of Zones is successful", func() { 99 BeforeEach(func() { 100 server = ghttp.NewServer() 101 server.AppendHandlers( 102 ghttp.CombineHandlers( 103 ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones"), 104 ghttp.RespondWith(http.StatusOK, ` 105 { 106 "result": [ 107 { 108 "id": "3fefc35e7decadb111dcf85d723a4f20", 109 "name": "example.com", 110 "status": "active", 111 "paused": false, 112 "name_servers": [ 113 "ns002.name.cloud.ibm.com", 114 "ns007.name.cloud.ibm.com" 115 ], 116 "original_name_servers": [ 117 "ns005.name.cloud.ibm.com", 118 "ns016.name.cloud.ibm.com" 119 ], 120 "original_registrar": null, 121 "original_dnshost": null, 122 "modified_on": "2018-10-12T06:34:35.992900Z", 123 "created_on": "2018-05-04T14:16:28.369359Z", 124 "vanity_name_servers": [], 125 "account": { 126 "id": "796b4ef449812595ea9fe92d1e910756", 127 "name": "b424c068-c944-4565-b0bf-b278e5ec98ed" 128 } 129 } 130 ], 131 "result_info": { 132 "page": 1, 133 "per_page": 20, 134 "total_pages": 1, 135 "count": 1, 136 "total_count": 1 137 }, 138 "success": true, 139 "errors": [], 140 "messages": [] 141 } 142 `), 143 ), 144 ) 145 }) 146 147 It("should return Zone list", func() { 148 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 149 myZones, err := newZone(server.URL()).ListZones(target) 150 for _, Zone := range myZones { 151 Expect(err).NotTo(HaveOccurred()) 152 Expect(Zone.Id).Should(Equal("3fefc35e7decadb111dcf85d723a4f20")) 153 Expect(Zone.Name).Should(Equal("example.com")) 154 } 155 }) 156 }) 157 Context("When read of Zones is unsuccessful", func() { 158 BeforeEach(func() { 159 server = ghttp.NewServer() 160 server.SetAllowUnhandledRequests(true) 161 server.AppendHandlers( 162 ghttp.CombineHandlers( 163 ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones"), 164 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve Zones`), 165 ), 166 ) 167 }) 168 169 It("should return error when Zone are retrieved", func() { 170 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 171 myZonePtr, err := newZone(server.URL()).ListZones(target) 172 myZone := myZonePtr 173 Expect(err).To(HaveOccurred()) 174 Expect(myZone).Should(BeNil()) 175 }) 176 }) 177 }) 178 //Delete 179 Describe("Delete", func() { 180 Context("When delete of Zone is successful", func() { 181 BeforeEach(func() { 182 server = ghttp.NewServer() 183 server.AppendHandlers( 184 ghttp.CombineHandlers( 185 ghttp.VerifyRequest(http.MethodDelete, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20"), 186 ghttp.RespondWith(http.StatusOK, `{ 187 }`), 188 ), 189 ) 190 }) 191 192 It("should delete Zone", func() { 193 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 194 params := "3fefc35e7decadb111dcf85d723a4f20" 195 err := newZone(server.URL()).DeleteZone(target, params) 196 Expect(err).NotTo(HaveOccurred()) 197 }) 198 }) 199 Context("When Zone delete has failed", func() { 200 BeforeEach(func() { 201 server = ghttp.NewServer() 202 server.SetAllowUnhandledRequests(true) 203 server.AppendHandlers( 204 ghttp.CombineHandlers( 205 ghttp.VerifyRequest(http.MethodDelete, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20"), 206 ghttp.RespondWith(http.StatusInternalServerError, `Failed to delete service key`), 207 ), 208 ) 209 }) 210 211 It("should return error zone delete", func() { 212 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 213 params := "3fefc35e7decadb111dcf85d723a4f20" 214 err := newZone(server.URL()).DeleteZone(target, params) 215 Expect(err).To(HaveOccurred()) 216 }) 217 }) 218 }) 219 //Find 220 Describe("Get", func() { 221 Context("When read of Zone is successful", func() { 222 BeforeEach(func() { 223 server = ghttp.NewServer() 224 server.AppendHandlers( 225 ghttp.CombineHandlers( 226 ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20"), 227 ghttp.RespondWith(http.StatusOK, ` 228 { 229 "result": { 230 "id": "3fefc35e7decadb111dcf85d723a4f20", 231 "name": "example.com", 232 "status": "active", 233 "paused": false, 234 "name_servers": [ 235 "ns002.name.cloud.ibm.com", 236 "ns007.name.cloud.ibm.com" 237 ], 238 "original_name_servers": [ 239 "ns005.name.cloud.ibm.com", 240 "ns016.name.cloud.ibm.com" 241 ], 242 "original_registrar": null, 243 "original_dnshost": null, 244 "modified_on": "2018-10-12T06:34:35.992900Z", 245 "created_on": "2018-05-04T14:16:28.369359Z", 246 "vanity_name_servers": [], 247 "account": { 248 "id": "796b4ef449812595ea9fe92d1e910756", 249 "name": "b424c068-c944-4565-b0bf-b278e5ec98ed" 250 } 251 }, 252 "success": true, 253 "errors": [], 254 "messages": [] 255 } 256 `), 257 ), 258 ) 259 }) 260 261 It("should return Zone", func() { 262 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 263 params := "3fefc35e7decadb111dcf85d723a4f20" 264 myZonePtr, err := newZone(server.URL()).GetZone(target, params) 265 myZone := *myZonePtr 266 Expect(err).NotTo(HaveOccurred()) 267 Expect(myZone).ShouldNot(BeNil()) 268 Expect(myZone.Id).Should(Equal("3fefc35e7decadb111dcf85d723a4f20")) 269 Expect(myZone.Name).Should(Equal("example.com")) 270 }) 271 }) 272 Context("When Zone get has failed", func() { 273 BeforeEach(func() { 274 server = ghttp.NewServer() 275 server.SetAllowUnhandledRequests(true) 276 server.AppendHandlers( 277 ghttp.CombineHandlers( 278 ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20"), 279 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve Zone`), 280 ), 281 ) 282 }) 283 284 It("should return error when Zone is retrieved", func() { 285 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 286 params := "3fefc35e7decadb111dcf85d723a4f20" 287 myZonePtr, err := newZone(server.URL()).GetZone(target, params) 288 myZone := myZonePtr 289 Expect(err).To(HaveOccurred()) 290 Expect(myZone).Should(BeNil()) 291 }) 292 }) 293 }) 294 295 }) 296 297 func newZone(url string) Zones { 298 299 sess, err := session.New() 300 if err != nil { 301 log.Fatal(err) 302 } 303 conf := sess.Config.Copy() 304 conf.HTTPClient = bluemixHttp.NewHTTPClient(conf) 305 conf.Endpoint = &url 306 307 client := client.Client{ 308 Config: conf, 309 ServiceName: bluemix.CisService, 310 } 311 return newZoneAPI(&client) 312 }