summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKiran Kamineni <kiran.k.kamineni@intel.com>2018-10-08 15:32:09 -0700
committerKiran Kamineni <kiran.k.kamineni@intel.com>2018-10-08 15:32:13 -0700
commit90144a5ec60d7941cf8bbd81dc3e525df981c0d3 (patch)
tree690a7fe54388489f4e0b1da6e89753c12ded2899
parente3e7be41330b3cdb15aef2d0c69ec09377ded69f (diff)
Add readiness check into the cmd line3.0.0
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 <kiran.k.kamineni@intel.com>
-rw-r--r--sms-service/src/preload/preload.go72
1 files changed, 69 insertions, 3 deletions
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 {