MCPcopy
hub / github.com/spacecloud-io/space-cloud / ApplySpec

Function ApplySpec

space-cli/cmd/modules/operations/apply.go:128–164  ·  view source on GitHub ↗

ApplySpec takes a spec object and applies it

(token string, account *model.Account, specObj *model.SpecObject)

Source from the content-addressed store, hash-verified

126
127// ApplySpec takes a spec object and applies it
128func 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
166func adjustPath(path string, meta map[string]string) (string, error) {
167 newPath := path

Callers 1

ApplyFunction · 0.70

Calls 4

adjustPathFunction · 0.70
ErrorMethod · 0.65
DoMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected