|
8 | 8 | "context" |
9 | 9 | "encoding/json" |
10 | 10 | "fmt" |
11 | | - "io/ioutil" |
12 | 11 | "net/http" |
13 | 12 | "time" |
14 | 13 |
|
@@ -57,29 +56,48 @@ func generateFuncStr(spec *DeployFunctionSpec) string { |
57 | 56 | return spec.FunctionName |
58 | 57 | } |
59 | 58 |
|
| 59 | +type DeployResponse struct { |
| 60 | + Message string |
| 61 | + RollingUpdate bool |
| 62 | + URL string |
| 63 | +} |
| 64 | + |
60 | 65 | // DeployFunction first tries to deploy a function and if it exists will then attempt |
61 | 66 | // a rolling update. Warnings are suppressed for the second API call (if required.) |
62 | | -func (c *Client) DeployFunction(context context.Context, spec *DeployFunctionSpec) int { |
| 67 | +func (c *Client) DeployFunction(context context.Context, spec *DeployFunctionSpec) (*DeployResponse, *http.Response, error) { |
63 | 68 |
|
64 | 69 | rollingUpdateInfo := fmt.Sprintf("Function %s already exists, attempting rolling-update.", spec.FunctionName) |
65 | | - statusCode, deployOutput := c.deploy(context, spec, spec.Update) |
| 70 | + res, err := c.deploy(context, spec, spec.Update) |
| 71 | + |
| 72 | + if err != nil && IsUnknown(err) { |
| 73 | + return nil, res, err |
| 74 | + } |
| 75 | + |
| 76 | + var deployResponse DeployResponse |
| 77 | + if err == nil { |
| 78 | + deployResponse.Message = fmt.Sprintf("Deployed. %s.", res.Status) |
| 79 | + deployResponse.URL = fmt.Sprintf("%s/function/%s", c.GatewayURL.String(), generateFuncStr(spec)) |
| 80 | + } |
66 | 81 |
|
67 | | - if spec.Update == true && statusCode == http.StatusNotFound { |
| 82 | + if spec.Update == true && IsNotFound(err) { |
68 | 83 | // Re-run the function with update=false |
| 84 | + res, err = c.deploy(context, spec, false) |
| 85 | + if err == nil { |
| 86 | + deployResponse.Message = fmt.Sprintf("Deployed. %s.", res.Status) |
| 87 | + deployResponse.URL = fmt.Sprintf("%s/function/%s", c.GatewayURL.String(), generateFuncStr(spec)) |
| 88 | + } |
| 89 | + |
| 90 | + } else if res.StatusCode == http.StatusOK { |
| 91 | + deployResponse.Message += rollingUpdateInfo |
| 92 | + deployResponse.RollingUpdate = true |
69 | 93 |
|
70 | | - statusCode, deployOutput = c.deploy(context, spec, false) |
71 | | - } else if statusCode == http.StatusOK { |
72 | | - fmt.Println(rollingUpdateInfo) |
73 | 94 | } |
74 | | - fmt.Println() |
75 | | - fmt.Println(deployOutput) |
76 | | - return statusCode |
| 95 | + |
| 96 | + return &deployResponse, res, err |
77 | 97 | } |
78 | 98 |
|
79 | 99 | // deploy a function to an OpenFaaS gateway over REST |
80 | | -func (c *Client) deploy(context context.Context, spec *DeployFunctionSpec, update bool) (int, string) { |
81 | | - |
82 | | - var deployOutput string |
| 100 | +func (c *Client) deploy(context context.Context, spec *DeployFunctionSpec, update bool) (*http.Response, error) { |
83 | 101 | // Need to alter Gateway to allow nil/empty string as fprocess, to avoid this repetition. |
84 | 102 | var fprocessTemplate string |
85 | 103 | if len(spec.FProcess) > 0 { |
@@ -146,37 +164,20 @@ func (c *Client) deploy(context context.Context, spec *DeployFunctionSpec, updat |
146 | 164 | request, err = c.newRequest(method, "/system/functions", reader) |
147 | 165 |
|
148 | 166 | if err != nil { |
149 | | - deployOutput += fmt.Sprintln(err) |
150 | | - return http.StatusInternalServerError, deployOutput |
| 167 | + return nil, err |
151 | 168 | } |
152 | 169 |
|
153 | 170 | res, err := c.doRequest(context, request) |
154 | 171 |
|
155 | 172 | if err != nil { |
156 | | - deployOutput += fmt.Sprintln("Is OpenFaaS deployed? Do you need to specify the --gateway flag?") |
157 | | - deployOutput += fmt.Sprintln(err) |
158 | | - return http.StatusInternalServerError, deployOutput |
159 | | - } |
160 | | - |
161 | | - if res.Body != nil { |
162 | | - defer res.Body.Close() |
| 173 | + errMessage := fmt.Sprintln("Is OpenFaaS deployed? Do you need to specify the --gateway flag?") |
| 174 | + errMessage += fmt.Sprintln(err) |
| 175 | + return res, NewUnknown(errMessage, 0) |
163 | 176 | } |
164 | 177 |
|
165 | | - switch res.StatusCode { |
166 | | - case http.StatusOK, http.StatusCreated, http.StatusAccepted: |
167 | | - deployOutput += fmt.Sprintf("Deployed. %s.\n", res.Status) |
168 | | - |
169 | | - deployedURL := fmt.Sprintf("URL: %s/function/%s", c.GatewayURL.String(), generateFuncStr(spec)) |
170 | | - deployOutput += fmt.Sprintln(deployedURL) |
171 | | - case http.StatusUnauthorized: |
172 | | - deployOutput += fmt.Sprintln("unauthorized access, run \"faas-cli login\" to setup authentication for this server") |
173 | | - |
174 | | - default: |
175 | | - bytesOut, err := ioutil.ReadAll(res.Body) |
176 | | - if err == nil { |
177 | | - deployOutput += fmt.Sprintf("Unexpected status: %d, message: %s\n", res.StatusCode, string(bytesOut)) |
178 | | - } |
| 178 | + err = checkForAPIError(res) |
| 179 | + if err != nil { |
| 180 | + return res, err |
179 | 181 | } |
180 | | - |
181 | | - return res.StatusCode, deployOutput |
| 182 | + return res, nil |
182 | 183 | } |
0 commit comments