PageRenderTime 171ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/github.com/gophercloud/gophercloud/acceptance/openstack/dns/v2/dns.go

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 164 lines | 115 code | 31 blank | 18 comment | 30 complexity | 82d801e424dc9ed3ebfa2e7a6c645bb2 MD5 | raw file
  1. package v2
  2. import (
  3. "testing"
  4. "github.com/gophercloud/gophercloud"
  5. "github.com/gophercloud/gophercloud/acceptance/tools"
  6. "github.com/gophercloud/gophercloud/openstack/dns/v2/recordsets"
  7. "github.com/gophercloud/gophercloud/openstack/dns/v2/zones"
  8. )
  9. // CreateRecordSet will create a RecordSet with a random name. An error will
  10. // be returned if the zone was unable to be created.
  11. func CreateRecordSet(t *testing.T, client *gophercloud.ServiceClient, zone *zones.Zone) (*recordsets.RecordSet, error) {
  12. t.Logf("Attempting to create recordset: %s", zone.Name)
  13. createOpts := recordsets.CreateOpts{
  14. Name: zone.Name,
  15. Type: "A",
  16. TTL: 3600,
  17. Description: "Test recordset",
  18. Records: []string{"10.1.0.2"},
  19. }
  20. rs, err := recordsets.Create(client, zone.ID, createOpts).Extract()
  21. if err != nil {
  22. return rs, err
  23. }
  24. if err := WaitForRecordSetStatus(client, rs, "ACTIVE"); err != nil {
  25. return rs, err
  26. }
  27. newRS, err := recordsets.Get(client, rs.ZoneID, rs.ID).Extract()
  28. if err != nil {
  29. return newRS, err
  30. }
  31. t.Logf("Created record set: %s", newRS.Name)
  32. return rs, nil
  33. }
  34. // CreateZone will create a Zone with a random name. An error will
  35. // be returned if the zone was unable to be created.
  36. func CreateZone(t *testing.T, client *gophercloud.ServiceClient) (*zones.Zone, error) {
  37. zoneName := tools.RandomString("ACPTTEST", 8) + ".com."
  38. t.Logf("Attempting to create zone: %s", zoneName)
  39. createOpts := zones.CreateOpts{
  40. Name: zoneName,
  41. Email: "root@example.com",
  42. Type: "PRIMARY",
  43. TTL: 7200,
  44. Description: "Test zone",
  45. }
  46. zone, err := zones.Create(client, createOpts).Extract()
  47. if err != nil {
  48. return zone, err
  49. }
  50. if err := WaitForZoneStatus(client, zone, "ACTIVE"); err != nil {
  51. return zone, err
  52. }
  53. newZone, err := zones.Get(client, zone.ID).Extract()
  54. if err != nil {
  55. return zone, err
  56. }
  57. t.Logf("Created Zone: %s", zoneName)
  58. return newZone, nil
  59. }
  60. // CreateSecondaryZone will create a Zone with a random name. An error will
  61. // be returned if the zone was unable to be created.
  62. //
  63. // This is only for example purposes as it will try to do a zone transfer.
  64. func CreateSecondaryZone(t *testing.T, client *gophercloud.ServiceClient) (*zones.Zone, error) {
  65. zoneName := tools.RandomString("ACPTTEST", 8) + ".com."
  66. t.Logf("Attempting to create zone: %s", zoneName)
  67. createOpts := zones.CreateOpts{
  68. Name: zoneName,
  69. Type: "SECONDARY",
  70. Masters: []string{"10.0.0.1"},
  71. }
  72. zone, err := zones.Create(client, createOpts).Extract()
  73. if err != nil {
  74. return zone, err
  75. }
  76. if err := WaitForZoneStatus(client, zone, "ACTIVE"); err != nil {
  77. return zone, err
  78. }
  79. newZone, err := zones.Get(client, zone.ID).Extract()
  80. if err != nil {
  81. return zone, err
  82. }
  83. t.Logf("Created Zone: %s", zoneName)
  84. return newZone, nil
  85. }
  86. // DeleteRecordSet will delete a specified record set. A fatal error will occur if
  87. // the record set failed to be deleted. This works best when used as a deferred
  88. // function.
  89. func DeleteRecordSet(t *testing.T, client *gophercloud.ServiceClient, rs *recordsets.RecordSet) {
  90. err := recordsets.Delete(client, rs.ZoneID, rs.ID).ExtractErr()
  91. if err != nil {
  92. t.Fatalf("Unable to delete record set %s: %v", rs.ID, err)
  93. }
  94. t.Logf("Deleted record set: %s", rs.ID)
  95. }
  96. // DeleteZone will delete a specified zone. A fatal error will occur if
  97. // the zone failed to be deleted. This works best when used as a deferred
  98. // function.
  99. func DeleteZone(t *testing.T, client *gophercloud.ServiceClient, zone *zones.Zone) {
  100. _, err := zones.Delete(client, zone.ID).Extract()
  101. if err != nil {
  102. t.Fatalf("Unable to delete zone %s: %v", zone.ID, err)
  103. }
  104. t.Logf("Deleted zone: %s", zone.ID)
  105. }
  106. // WaitForRecordSetStatus will poll a record set's status until it either matches
  107. // the specified status or the status becomes ERROR.
  108. func WaitForRecordSetStatus(client *gophercloud.ServiceClient, rs *recordsets.RecordSet, status string) error {
  109. return gophercloud.WaitFor(60, func() (bool, error) {
  110. current, err := recordsets.Get(client, rs.ZoneID, rs.ID).Extract()
  111. if err != nil {
  112. return false, err
  113. }
  114. if current.Status == status {
  115. return true, nil
  116. }
  117. return false, nil
  118. })
  119. }
  120. // WaitForZoneStatus will poll a zone's status until it either matches
  121. // the specified status or the status becomes ERROR.
  122. func WaitForZoneStatus(client *gophercloud.ServiceClient, zone *zones.Zone, status string) error {
  123. return gophercloud.WaitFor(60, func() (bool, error) {
  124. current, err := zones.Get(client, zone.ID).Extract()
  125. if err != nil {
  126. return false, err
  127. }
  128. if current.Status == status {
  129. return true, nil
  130. }
  131. return false, nil
  132. })
  133. }