2022-06-06 19:14:52 -04:00
|
|
|
// Copyright (c) 2015-2022 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2022-06-27 06:58:25 -04:00
|
|
|
package subnet
|
2022-06-06 19:14:52 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-11-15 16:53:05 -05:00
|
|
|
"mime/multipart"
|
2022-06-06 19:14:52 -04:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
xhttp "github.com/minio/minio/internal/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-06-27 06:58:25 -04:00
|
|
|
respBodyLimit = 1 << 20 // 1 MiB
|
2022-06-06 19:14:52 -04:00
|
|
|
)
|
|
|
|
|
2022-11-15 16:53:05 -05:00
|
|
|
// Upload given file content (payload) to specified URL
|
|
|
|
func (c Config) Upload(reqURL string, filename string, payload []byte) (string, error) {
|
2022-10-27 03:20:01 -04:00
|
|
|
if !c.Registered() {
|
|
|
|
return "", errors.New("Deployment is not registered with SUBNET. Please register the deployment via 'mc license register ALIAS'")
|
2022-06-06 19:14:52 -04:00
|
|
|
}
|
2022-11-15 16:53:05 -05:00
|
|
|
|
|
|
|
var body bytes.Buffer
|
|
|
|
writer := multipart.NewWriter(&body)
|
|
|
|
part, e := writer.CreateFormFile("file", filename)
|
|
|
|
if e != nil {
|
|
|
|
return "", e
|
2022-06-06 19:14:52 -04:00
|
|
|
}
|
2022-11-15 16:53:05 -05:00
|
|
|
|
|
|
|
if _, e = part.Write(payload); e != nil {
|
|
|
|
return "", e
|
|
|
|
}
|
|
|
|
writer.Close()
|
|
|
|
|
|
|
|
r, e := http.NewRequest(http.MethodPost, reqURL, &body)
|
|
|
|
if e != nil {
|
|
|
|
return "", e
|
2022-06-06 19:14:52 -04:00
|
|
|
}
|
2022-11-15 16:53:05 -05:00
|
|
|
r.Header.Add("Content-Type", writer.FormDataContentType())
|
|
|
|
|
|
|
|
return c.submitPost(r)
|
|
|
|
}
|
2022-06-06 19:14:52 -04:00
|
|
|
|
2022-11-15 16:53:05 -05:00
|
|
|
func (c Config) submitPost(r *http.Request) (string, error) {
|
2022-10-27 03:20:01 -04:00
|
|
|
configLock.RLock()
|
2022-11-15 16:53:05 -05:00
|
|
|
r.Header.Set(xhttp.SubnetAPIKey, c.APIKey)
|
2022-10-27 03:20:01 -04:00
|
|
|
configLock.RUnlock()
|
2022-11-15 16:53:05 -05:00
|
|
|
r.Header.Set(xhttp.MinioDeploymentID, xhttp.GlobalDeploymentID)
|
2022-06-27 06:58:25 -04:00
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
Transport: c.transport,
|
2022-06-06 19:14:52 -04:00
|
|
|
}
|
|
|
|
|
2022-06-27 06:58:25 -04:00
|
|
|
resp, err := client.Do(r)
|
2022-06-06 19:14:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-06-27 06:58:25 -04:00
|
|
|
defer xhttp.DrainBody(resp.Body)
|
|
|
|
|
2022-09-19 14:05:16 -04:00
|
|
|
respBytes, err := io.ReadAll(io.LimitReader(resp.Body, respBodyLimit))
|
2022-06-06 19:14:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-06-27 06:58:25 -04:00
|
|
|
respStr := string(respBytes)
|
2022-06-06 19:14:52 -04:00
|
|
|
|
2022-06-27 06:58:25 -04:00
|
|
|
if resp.StatusCode == http.StatusOK {
|
|
|
|
return respStr, nil
|
2022-06-06 19:14:52 -04:00
|
|
|
}
|
|
|
|
|
2022-06-27 06:58:25 -04:00
|
|
|
return respStr, fmt.Errorf("SUBNET request failed with code %d and error: %s", resp.StatusCode, respStr)
|
2022-06-06 19:14:52 -04:00
|
|
|
}
|
2022-11-15 16:53:05 -05:00
|
|
|
|
|
|
|
// Post submit 'payload' to specified URL
|
|
|
|
func (c Config) Post(reqURL string, payload interface{}) (string, error) {
|
|
|
|
if !c.Registered() {
|
|
|
|
return "", errors.New("Deployment is not registered with SUBNET. Please register the deployment via 'mc license register ALIAS'")
|
|
|
|
}
|
|
|
|
body, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
r, err := http.NewRequest(http.MethodPost, reqURL, bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
return c.submitPost(r)
|
|
|
|
}
|