ApplySpec takes a spec object and applies it
(token string, account *model.Account, specObj *model.SpecObject)
| 126 | |
| 127 | // ApplySpec takes a spec object and applies it |
| 128 | func ApplySpec(token string, account *model.Account, specObj *model.SpecObject) error { |
| 129 | requestBody, err := json.Marshal(specObj.Spec) |
| 130 | if err != nil { |
| 131 | _ = utils.LogError(fmt.Sprintf("error while applying service unable to marshal spec - %s", err.Error()), nil) |
| 132 | return err |
| 133 | } |
| 134 | url, err := adjustPath(fmt.Sprintf("%s%s", account.ServerURL, specObj.API), specObj.Meta) |
| 135 | if err != nil { |
| 136 | return err |
| 137 | } |
| 138 | |
| 139 | req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(requestBody)) |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) |
| 144 | resp, err := http.DefaultClient.Do(req) |
| 145 | if err != nil { |
| 146 | _ = utils.LogError(fmt.Sprintf("error while applying service unable to send http request - %s", err.Error()), nil) |
| 147 | return err |
| 148 | } |
| 149 | |
| 150 | v := map[string]interface{}{} |
| 151 | _ = json.NewDecoder(resp.Body).Decode(&v) |
| 152 | utils.CloseTheCloser(req.Body) |
| 153 | |
| 154 | if resp.StatusCode == http.StatusAccepted { |
| 155 | // Make checker send this status |
| 156 | utils.LogInfo(fmt.Sprintf("Successfully queued %s", specObj.Type)) |
| 157 | } else if resp.StatusCode == http.StatusOK { |
| 158 | utils.LogInfo(fmt.Sprintf("Successfully applied %s", specObj.Type)) |
| 159 | } else { |
| 160 | _ = utils.LogError(fmt.Sprintf("error while applying service got http status code %s - %s", resp.Status, v["error"]), nil) |
| 161 | return fmt.Errorf("%v", v["error"]) |
| 162 | } |
| 163 | return nil |
| 164 | } |
| 165 | |
| 166 | func adjustPath(path string, meta map[string]string) (string, error) { |
| 167 | newPath := path |
no test coverage detected