Merge pull request #10450 from ioito/hotfix/qx-license-upload

fix: add license upload
This commit is contained in:
Zexi Li
2021-03-22 10:41:35 +08:00
committed by GitHub
+40
View File
@@ -15,6 +15,12 @@
package misc
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"os"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/modules"
"yunion.io/x/onecloud/pkg/mcclient/options"
@@ -40,6 +46,40 @@ func init() {
return nil
})
type LicenseUploadOptions struct {
FILE string
}
R(&LicenseUploadOptions{}, "license-upload", "Upload license", func(s *mcclient.ClientSession, args *LicenseUploadOptions) error {
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
fileWriter, err := bodyWriter.CreateFormFile("license", args.FILE)
if err != nil {
return err
}
fh, err := os.Open(args.FILE)
if err != nil {
return err
}
defer fh.Close()
_, err = io.Copy(fileWriter, fh)
if err != nil {
return err
}
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()
header := http.Header{}
header.Set("Content-Type", contentType)
lic, err := modules.License.Upload(s, header, bodyBuf)
if err != nil {
return err
}
printObject(lic)
return nil
})
type LicenseShowOptions struct {
SERVICE string `help:"service name" choices:"compute|service_tree"`
}