Guest User

Untitled

a guest
Mar 24th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. package api
  2.  
  3. import (
  4. "errors"
  5. consulapi "github.com/hashicorp/consul/api"
  6. "os"
  7. )
  8.  
  9. // Interface to have all signature methods.
  10. type DataStoreRequester interface {
  11. InitializeDataStoreClient() error
  12. CheckDataStoreHealth() error
  13. RequestPUT(string, string) error
  14. RequestGET(string) (string, error)
  15. RequestGETS() ([]string, error)
  16. RequestDELETE(string) error
  17. }
  18.  
  19. // Consul Struct
  20. type ConsulStruct struct {
  21. consulClient *consulapi.Client
  22. }
  23.  
  24. func (c *ConsulStruct) InitializeDataStoreClient() error {
  25. if os.Getenv("DATASTORE_IP") == "" {
  26. return errors.New("DATASTORE_IP (consul/cassandra) environment variable not set.")
  27. }
  28. config := consulapi.DefaultConfig()
  29. config.Address = os.Getenv("DATASTORE_IP") + ":8500"
  30.  
  31. client, err := consulapi.NewClient(config)
  32. if err != nil {
  33. return err
  34. }
  35. c.consulClient = client
  36.  
  37. return nil
  38. }
  39.  
  40. func (c *ConsulStruct) CheckDataStoreHealth() error {
  41. kv := c.consulClient.KV()
  42. _, _, err := kv.Get("test", nil)
  43. if err != nil {
  44. return errors.New("[ERROR] Cannot talk to Consul. Check if it is running/reachable.")
  45. }
  46. return nil
  47. }
  48.  
  49. func (c *ConsulStruct) RequestPUT(key string, value string) error {
  50. kv := c.consulClient.KV()
  51. p := &consulapi.KVPair{Key: key, Value: []byte(value)}
  52. _, err := kv.Put(p, nil)
  53. if err != nil {
  54. return err
  55. }
  56. return nil
  57. }
  58.  
  59. func (c *ConsulStruct) RequestGET(key string) (string, error) {
  60. kv := c.consulClient.KV()
  61. pair, _, err := kv.Get(key, nil)
  62. if pair == nil {
  63. return string("No value found for key."), err
  64. }
  65. return string(pair.Value), err
  66.  
  67. }
  68.  
  69. func (c *ConsulStruct) RequestGETS() ([]string, error) {
  70. kv := c.consulClient.KV()
  71. pairs, _, err := kv.List("", nil)
  72. if len(pairs) == 0 {
  73. return []string{"No keys found."}, err
  74. }
  75.  
  76. var res []string
  77.  
  78. for _, keypair := range pairs {
  79. res = append(res, keypair.Key)
  80. }
  81. return res, err
  82. }
  83.  
  84. func (c *ConsulStruct) RequestDELETE(key string) error {
  85. kv := c.consulClient.KV()
  86. _, err := kv.Delete(key, nil)
  87. if err != nil {
  88. return err
  89. }
  90. return nil
  91. }
  92.  
  93. // TODO(sshank): Place holder to add connection to MUSIC's Cassandra.
  94. type CassandraStruct struct {
  95. // Add necesary Cassandra client.
  96. }
  97.  
  98. func (cs *CassandraStruct) InitializeDataStoreClient() error {
  99.  
  100. return nil
  101. }
  102.  
  103. func (cs *CassandraStruct) CheckDataStoreHealth() error {
  104.  
  105. return nil
  106. }
  107.  
  108. func (cs *CassandraStruct) RequestPUT(string, string) error {
  109.  
  110. return nil
  111. }
  112.  
  113. func (cs *CassandraStruct) RequestGET(string) error {
  114. return nil
  115. }
  116.  
  117. func (cs *CassandraStruct) RequestGETS() ([]string, error) {
  118. return []string{""}, nil
  119. }
  120.  
  121. func (cs *CassandraStruct) RequestDELETE(string) error {
  122. return nil
  123. }
Add Comment
Please, Sign In to add comment