From 1d66e64e54c59ea9184bd614790498451d025e25 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Mon, 19 Jun 2017 13:46:51 -0400 Subject: Add POST and DELETE /license endpoints for v4 (#6665) * Add POST and DELETE /license endpoints for v4 * Fix comment text --- model/client4.go | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'model/client4.go') diff --git a/model/client4.go b/model/client4.go index c692a8415..cfecd7b25 100644 --- a/model/client4.go +++ b/model/client4.go @@ -1835,7 +1835,7 @@ func (c *Client4) InvalidateCaches() (bool, *Response) { } } -// UpdateConfig will update the server configuration +// UpdateConfig will update the server configuration. func (c *Client4) UpdateConfig(config *Config) (*Config, *Response) { if r, err := c.DoApiPut(c.GetConfigRoute(), config.ToJson()); err != nil { return nil, &Response{StatusCode: r.StatusCode, Error: err} @@ -1845,6 +1845,50 @@ func (c *Client4) UpdateConfig(config *Config) (*Config, *Response) { } } +// UploadLicenseFile will add a license file to the system. +func (c *Client4) UploadLicenseFile(data []byte) (bool, *Response) { + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + + if part, err := writer.CreateFormFile("license", "test-license.mattermost-license"); err != nil { + return false, &Response{Error: NewAppError("UploadLicenseFile", "model.client.set_profile_user.no_file.app_error", nil, err.Error(), http.StatusBadRequest)} + } else if _, err = io.Copy(part, bytes.NewBuffer(data)); err != nil { + return false, &Response{Error: NewAppError("UploadLicenseFile", "model.client.set_profile_user.no_file.app_error", nil, err.Error(), http.StatusBadRequest)} + } + + if err := writer.Close(); err != nil { + return false, &Response{Error: NewAppError("UploadLicenseFile", "model.client.set_profile_user.writer.app_error", nil, err.Error(), http.StatusBadRequest)} + } + + rq, _ := http.NewRequest("POST", c.ApiUrl+c.GetLicenseRoute(), bytes.NewReader(body.Bytes())) + rq.Header.Set("Content-Type", writer.FormDataContentType()) + rq.Close = true + + if len(c.AuthToken) > 0 { + rq.Header.Set(HEADER_AUTH, c.AuthType+" "+c.AuthToken) + } + + if rp, err := c.HttpClient.Do(rq); err != nil { + return false, &Response{StatusCode: http.StatusForbidden, Error: NewAppError(c.GetLicenseRoute(), "model.client.connecting.app_error", nil, err.Error(), http.StatusForbidden)} + } else if rp.StatusCode >= 300 { + return false, &Response{StatusCode: rp.StatusCode, Error: AppErrorFromJson(rp.Body)} + } else { + defer closeBody(rp) + return CheckStatusOK(rp), BuildResponse(rp) + } +} + +// RemoveLicenseFile will remove the server license it exists. Note that this will +// disable all enterprise features. +func (c *Client4) RemoveLicenseFile() (bool, *Response) { + if r, err := c.DoApiDelete(c.GetLicenseRoute()); err != nil { + return false, &Response{StatusCode: r.StatusCode, Error: err} + } else { + defer closeBody(r) + return CheckStatusOK(r), BuildResponse(r) + } +} + // Webhooks Section // CreateIncomingWebhook creates an incoming webhook for a channel. -- cgit v1.2.3-1-g7c22