github.com/IBM-Cloud/bluemix-go@v0.0.0-20240314082800-4e02a69b84b2/api/cis/cisv1/dns_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("Dns", 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 Dns 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/dns_records"), 30 ghttp.RespondWith(http.StatusOK, ` 31 { 32 "result": [ 33 { 34 "id": "0f4740fc36065f8a9343c7ed9445f2a4", 35 "type": "A", 36 "name": "example.com", 37 "content": "192.168.127.127", 38 "proxiable": true, 39 "proxied": false, 40 "ttl": 3600, 41 "locked": false, 42 "zone_id": "3fefc35e7decadb111dcf85d723a4f20", 43 "zone_name": "example.com", 44 "modified_on": "2018-10-11T13:34:45.189800Z", 45 "created_on": "2018-10-11T13:34:45.189800Z" 46 }, 47 { 48 "id": "0f4740fc36065f8a9343c7ed9445f2a4", 49 "type": "A", 50 "name": "www.example.com", 51 "content": "192.168.127.127", 52 "proxiable": false, 53 "proxied": false, 54 "ttl": 1, 55 "locked": false, 56 "zone_id": "3fefc35e7decadb111dcf85d723a4f20", 57 "zone_name": "example.com", 58 "modified_on": "2018-10-12T06:04:36.533540Z", 59 "created_on": "2018-10-12T06:04:36.533540Z" 60 } 61 ], 62 "success": true, 63 "errors": [], 64 "messages": [] 65 } 66 `), 67 ), 68 ) 69 }) 70 71 It("should return Dns list", func() { 72 target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 73 target2 := "3fefc35e7decadb111dcf85d723a4f20" 74 myDns, err := newDns(server.URL()).ListDns(target1, target2) 75 Expect(myDns).ShouldNot(BeNil()) 76 for _, Dns := range myDns { 77 Expect(err).NotTo(HaveOccurred()) 78 Expect(Dns.Id).Should(Equal("0f4740fc36065f8a9343c7ed9445f2a4")) 79 Expect(Dns.Content).Should(Equal("192.168.127.127")) 80 } 81 }) 82 }) 83 Context("When read of Dns is unsuccessful", func() { 84 BeforeEach(func() { 85 server = ghttp.NewServer() 86 server.SetAllowUnhandledRequests(true) 87 server.AppendHandlers( 88 ghttp.CombineHandlers( 89 ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/dns_records"), 90 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve Dns`), 91 ), 92 ) 93 }) 94 95 It("should return error when Dns are retrieved", func() { 96 target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 97 target2 := "3fefc35e7decadb111dcf85d723a4f20" 98 myDnsPtr, err := newDns(server.URL()).ListDns(target1, target2) 99 myDns := myDnsPtr 100 Expect(err).To(HaveOccurred()) 101 Expect(myDns).Should(BeNil()) 102 }) 103 }) 104 }) 105 106 //Delete 107 Describe("Delete", func() { 108 Context("When delete of Dns is successful", func() { 109 BeforeEach(func() { 110 server = ghttp.NewServer() 111 server.AppendHandlers( 112 ghttp.CombineHandlers( 113 ghttp.VerifyRequest(http.MethodDelete, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/dns_records/58e96913cff39f73d8901a6a4ea07e16"), 114 ghttp.RespondWith(http.StatusOK, `{ 115 }`), 116 ), 117 ) 118 }) 119 120 It("should delete Dns", func() { 121 target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 122 target2 := "3fefc35e7decadb111dcf85d723a4f20" 123 params := "58e96913cff39f73d8901a6a4ea07e16" 124 err := newDns(server.URL()).DeleteDns(target1, target2, params) 125 Expect(err).NotTo(HaveOccurred()) 126 }) 127 }) 128 Context("When Dns delete has failed", func() { 129 BeforeEach(func() { 130 server = ghttp.NewServer() 131 server.SetAllowUnhandledRequests(true) 132 server.AppendHandlers( 133 ghttp.CombineHandlers( 134 ghttp.VerifyRequest(http.MethodDelete, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/dns_records/58e96913cff39f73d8901a6a4ea07e16"), 135 ghttp.RespondWith(http.StatusInternalServerError, `Failed to delete service key`), 136 ), 137 ) 138 }) 139 140 It("should return error zone delete", func() { 141 target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 142 target2 := "3fefc35e7decadb111dcf85d723a4f20" 143 params := "58e96913cff39f73d8901a6a4ea07e16" 144 err := newDns(server.URL()).DeleteDns(target1, target2, params) 145 Expect(err).To(HaveOccurred()) 146 }) 147 }) 148 }) 149 //Find 150 Describe("Get", func() { 151 Context("When read of Dns is successful", func() { 152 BeforeEach(func() { 153 server = ghttp.NewServer() 154 server.AppendHandlers( 155 ghttp.CombineHandlers( 156 ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/dns_records/0f4740fc36065f8a9343c7ed9445f2a4"), 157 ghttp.RespondWith(http.StatusOK, ` 158 { 159 "result": { 160 "id": "0f4740fc36065f8a9343c7ed9445f2a4", 161 "type": "A", 162 "name": "www.example.com", 163 "content": "192.168.127.127", 164 "proxiable": false, 165 "proxied": false, 166 "ttl": 1, 167 "locked": false, 168 "zone_id": "3fefc35e7decadb111dcf85d723a4f20", 169 "zone_name": "example.com", 170 "modified_on": "2018-10-12T06:04:36.533540Z", 171 "created_on": "2018-10-12T06:04:36.533540Z" 172 }, 173 "success": true, 174 "errors": [], 175 "messages": [] 176 } 177 `), 178 ), 179 ) 180 }) 181 182 It("should return Dns", func() { 183 target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 184 target2 := "3fefc35e7decadb111dcf85d723a4f20" 185 params := "0f4740fc36065f8a9343c7ed9445f2a4" 186 myDnsPtr, err := newDns(server.URL()).GetDns(target1, target2, params) 187 myDns := *myDnsPtr 188 Expect(err).NotTo(HaveOccurred()) 189 Expect(myDns).ShouldNot(BeNil()) 190 Expect(myDns.Id).Should(Equal("0f4740fc36065f8a9343c7ed9445f2a4")) 191 }) 192 }) 193 Context("When Dns get has failed", func() { 194 BeforeEach(func() { 195 server = ghttp.NewServer() 196 server.SetAllowUnhandledRequests(true) 197 server.AppendHandlers( 198 ghttp.CombineHandlers( 199 ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/dns_records/0f4740fc36065f8a9343c7ed9445f2a4"), 200 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve Dns`), 201 ), 202 ) 203 }) 204 205 It("should return error when Dns is retrieved", func() { 206 target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 207 target2 := "3fefc35e7decadb111dcf85d723a4f20" 208 params := "0f4740fc36065f8a9343c7ed9445f2a4" 209 myDnsPtr, err := newDns(server.URL()).GetDns(target1, target2, params) 210 myDns := myDnsPtr 211 Expect(err).To(HaveOccurred()) 212 Expect(myDns).Should(BeNil()) 213 }) 214 }) 215 }) 216 Describe("Create", func() { 217 Context("When creation is successful", func() { 218 BeforeEach(func() { 219 server = ghttp.NewServer() 220 server.AppendHandlers( 221 ghttp.CombineHandlers( 222 ghttp.VerifyRequest(http.MethodPost, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/dns_records"), 223 ghttp.RespondWith(http.StatusCreated, ` 224 { 225 "result": { 226 "id": "0f4740fc36065f8a9343c7ed9445f2a4", 227 "type": "A", 228 "name": "www.example.com", 229 "content": "192.168.127.127", 230 "proxiable": false, 231 "proxied": false, 232 "ttl": 1, 233 "locked": false, 234 "zone_id": "3fefc35e7decadb111dcf85d723a4f20", 235 "zone_name": "example.com", 236 "modified_on": "2018-10-12T06:04:36.533540Z", 237 "created_on": "2018-10-12T06:04:36.533540Z" 238 }, 239 "success": true, 240 "errors": [], 241 "messages": [] 242 } 243 `), 244 ), 245 ) 246 }) 247 248 It("should return dns created", func() { 249 params := DnsBody{ 250 Name: "www.example.com", 251 DnsType: "A", 252 Content: "192.168.127.127", 253 } 254 target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 255 target2 := "3fefc35e7decadb111dcf85d723a4f20" 256 myDnsPt, err := newDns(server.URL()).CreateDns(target1, target2, params) 257 myDns := *myDnsPt 258 Expect(err).NotTo(HaveOccurred()) 259 Expect(myDns).ShouldNot(BeNil()) 260 Expect(myDns.Id).Should(Equal("0f4740fc36065f8a9343c7ed9445f2a4")) 261 Expect(myDns.Name).Should(Equal("www.example.com")) 262 }) 263 }) 264 Context("When creation is unsuccessful", func() { 265 BeforeEach(func() { 266 server = ghttp.NewServer() 267 server.SetAllowUnhandledRequests(true) 268 server.AppendHandlers( 269 ghttp.CombineHandlers( 270 ghttp.VerifyRequest(http.MethodPost, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/0f4740fc36065f8a9343c7ed9445f2a4/dns_records"), 271 ghttp.RespondWith(http.StatusInternalServerError, `Failed to create Dns`), 272 ), 273 ) 274 }) 275 It("should return error during Dns creation", func() { 276 params := DnsBody{ 277 Name: "www.example.com", 278 DnsType: "A", 279 Content: "192.168.127.127", 280 } 281 target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 282 target2 := "0f4740fc36065f8a9343c7ed9445f2a4" 283 myDnsPtr, err := newDns(server.URL()).CreateDns(target1, target2, params) 284 myDns := myDnsPtr 285 Expect(err).To(HaveOccurred()) 286 Expect(myDns).Should(BeNil()) 287 }) 288 }) 289 }) 290 291 Describe("Update", func() { 292 Context("When update is successful", func() { 293 BeforeEach(func() { 294 server = ghttp.NewServer() 295 server.AppendHandlers( 296 ghttp.CombineHandlers( 297 ghttp.VerifyRequest(http.MethodPost, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/dns_records"), 298 ghttp.RespondWith(http.StatusCreated, ` 299 { 300 "result": { 301 "id": "0f4740fc36065f8a9343c7ed9445f2a4", 302 "type": "A", 303 "name": "www.example.com", 304 "content": "192.168.127.127", 305 "proxiable": false, 306 "proxied": false, 307 "ttl": 1, 308 "locked": false, 309 "zone_id": "3fefc35e7decadb111dcf85d723a4f20", 310 "zone_name": "example.com", 311 "modified_on": "2018-10-12T06:04:36.533540Z", 312 "created_on": "2018-10-12T06:04:36.533540Z" 313 }, 314 "success": true, 315 "errors": [], 316 "messages": [] 317 } 318 `), 319 ), 320 ghttp.CombineHandlers( 321 ghttp.VerifyRequest(http.MethodPut, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/3fefc35e7decadb111dcf85d723a4f20/dns_records/0f4740fc36065f8a9343c7ed9445f2a4"), 322 ghttp.RespondWith(http.StatusCreated, ` 323 { 324 "result": { 325 "id": "0f4740fc36065f8a9343c7ed9445f2a4", 326 "type": "A", 327 "name": "www.example.com", 328 "content": "192.168.127.111", 329 "proxiable": false, 330 "proxied": false, 331 "ttl": 1, 332 "locked": false, 333 "zone_id": "3fefc35e7decadb111dcf85d723a4f20", 334 "zone_name": "example.com", 335 "modified_on": "2018-10-12T06:04:36.533540Z", 336 "created_on": "2018-10-12T06:04:36.533540Z" 337 }, 338 "success": true, 339 "errors": [], 340 "messages": [] 341 } 342 `), 343 ), 344 ) 345 }) 346 347 It("should return dns created", func() { 348 params := DnsBody{ 349 Name: "www.example.com", 350 DnsType: "A", 351 Content: "192.168.127.127", 352 } 353 target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 354 target2 := "3fefc35e7decadb111dcf85d723a4f20" 355 dnsId := "0f4740fc36065f8a9343c7ed9445f2a4" 356 myDnsPt, err := newDns(server.URL()).CreateDns(target1, target2, params) 357 Expect(err).NotTo(HaveOccurred()) 358 params = DnsBody{ 359 Name: "www.example.com", 360 DnsType: "A", 361 Content: "192.168.127.111", 362 } 363 myDnsPt, err = newDns(server.URL()).UpdateDns(target1, target2, dnsId, params) 364 myDns := *myDnsPt 365 Expect(err).NotTo(HaveOccurred()) 366 Expect(myDns).ShouldNot(BeNil()) 367 Expect(myDns.Id).Should(Equal("0f4740fc36065f8a9343c7ed9445f2a4")) 368 Expect(myDns.Name).Should(Equal("www.example.com")) 369 Expect(myDns.Content).Should(Equal("192.168.127.111")) 370 }) 371 }) 372 Context("When update is unsuccessful", func() { 373 BeforeEach(func() { 374 server = ghttp.NewServer() 375 server.AppendHandlers( 376 ghttp.CombineHandlers( 377 ghttp.VerifyRequest(http.MethodPut, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/zones/0f4740fc36065f8a9343c7ed9445f2a4/dns_records/0f4740fc36065f8a9343c7ed9445f2a4"), 378 ghttp.RespondWith(http.StatusInternalServerError, `Failed to update Dns`), 379 ), 380 ) 381 }) 382 It("should return error during Dns update", func() { 383 params := DnsBody{ 384 Name: "www.example.com", 385 DnsType: "A", 386 Content: "192.168.127.127", 387 } 388 target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 389 target2 := "0f4740fc36065f8a9343c7ed9445f2a4" 390 dnsId := "0f4740fc36065f8a9343c7ed9445f2a4" 391 myDnsPtr, err := newDns(server.URL()).UpdateDns(target1, target2, dnsId, params) 392 myDns := myDnsPtr 393 Expect(err).To(HaveOccurred()) 394 Expect(myDns).Should(BeNil()) 395 }) 396 }) 397 }) 398 }) 399 400 func newDns(url string) Dns { 401 402 sess, err := session.New() 403 if err != nil { 404 log.Fatal(err) 405 } 406 conf := sess.Config.Copy() 407 conf.HTTPClient = bluemixHttp.NewHTTPClient(conf) 408 conf.Endpoint = &url 409 410 client := client.Client{ 411 Config: conf, 412 ServiceName: bluemix.CisService, 413 } 414 return newDnsAPI(&client) 415 }