From 90144a5ec60d7941cf8bbd81dc3e525df981c0d3 Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Mon, 8 Oct 2018 15:32:09 -0700 Subject: Add readiness check into the cmd line Preload is not checking if SMS is ready or not before issuing the upload commands. This patch fixes that behavior. Issue-ID: AAF-545 Change-Id: I485aeab4f797cacce1036cd6e24910c65fa4eb69 Signed-off-by: Kiran Kamineni --- sms-service/src/preload/preload.go | 72 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 3 deletions(-) (limited to 'sms-service') diff --git a/sms-service/src/preload/preload.go b/sms-service/src/preload/preload.go index af6e1f6..2e654fa 100644 --- a/sms-service/src/preload/preload.go +++ b/sms-service/src/preload/preload.go @@ -115,13 +115,51 @@ func (c *smsClient) init() error { return nil } -func (c *smsClient) sendPostRequest(relURL string, message map[string]interface{}) error { +func (c *smsClient) resolveURL(relURL string) (*url.URL, error) { rel, err := url.Parse(relURL) + if err != nil { + return nil, pkgerrors.Cause(err) + } + + return c.BaseURL.ResolveReference(rel), nil + +} + +func (c *smsClient) sendGetRequest(relURL string) (map[string]interface{}, error) { + + u, err := c.resolveURL(relURL) + if err != nil { + return nil, pkgerrors.Cause(err) + } + + resp, err := c.httpClient.Get(u.String()) + if err != nil { + return nil, pkgerrors.Cause(err) + } + + if resp.StatusCode >= 400 && resp.StatusCode < 600 { + // Request Failed + errText, _ := ioutil.ReadAll(resp.Body) + return nil, pkgerrors.Errorf("Request Failed with: %s and Error: %s", + resp.Status, string(errText)) + } + + var result map[string]interface{} + err = json.NewDecoder(resp.Body).Decode(&result) + if err != nil { + return nil, pkgerrors.Cause(err) + } + + return result, nil +} + +func (c *smsClient) sendPostRequest(relURL string, message map[string]interface{}) error { + + u, err := c.resolveURL(relURL) if err != nil { return pkgerrors.Cause(err) } - u := c.BaseURL.ResolveReference(rel) body, err := json.Marshal(message) if err != nil { @@ -157,8 +195,8 @@ func (c *smsClient) createDomain(domain string) error { } func (c *smsClient) createSecret(domain string, secret string, - values map[string]interface{}) error { + message := map[string]interface{}{ "name": secret, "values": values, @@ -173,6 +211,22 @@ func (c *smsClient) createSecret(domain string, secret string, return nil } +func (c *smsClient) isReady() bool { + + url := "v1/sms/quorum/status" + res, err := c.sendGetRequest(url) + if err != nil { + fmt.Println(pkgerrors.Cause(err)) + return false + } + + if res["sealstatus"] == true { + return false + } + + return true +} + //uploadToSMS reads through the domain or domains and uploads //their corresponding secrets to SMS service func (c *smsClient) uploadToSMS(data DataJSON) error { @@ -189,6 +243,18 @@ func (c *smsClient) uploadToSMS(data DataJSON) error { return pkgerrors.New("Invalid JSON Data. No domain or domains found") } + isReady := make(chan bool) + go func() { + for c.isReady() == false { + time.Sleep(5 * time.Second) + fmt.Println("Waiting for SMS to accept requests...") + } + isReady <- true + }() + <-isReady + + fmt.Println("Uploading data...") + for _, d := range ldata { err := c.createDomain(d.Name) if err != nil { -- cgit 1.2.3-korg