|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- package cloudbrain
-
- import (
- "fmt"
-
- "code.gitea.io/gitea/models"
- "code.gitea.io/gitea/modules/setting"
- resty "github.com/go-resty/resty/v2"
- )
-
- var (
- restyClient *resty.Client
- HOST string
- TOKEN string
- )
-
- func getRestyClient() *resty.Client {
- if restyClient == nil {
- restyClient = resty.New()
- }
- return restyClient
- }
-
- func checkSetting() {
- if len(HOST) != 0 && len(TOKEN) != 0 && restyClient != nil {
- return
- }
- _ = loginCloudbrain()
- }
-
- func loginCloudbrain() error {
- conf := setting.GetCloudbrainConfig()
-
- username := conf.Username
- password := conf.Password
- HOST = conf.Host
- var loginResult models.CloudBrainLoginResult
-
- client := getRestyClient()
-
- res, err := client.R().
- SetHeader("Content-Type", "application/json").
- SetBody(map[string]interface{}{"username": username, "password": password, "expiration": "604800"}).
- SetResult(&loginResult).
- Post(HOST + "/rest-server/api/v1/token")
- if err != nil {
- return fmt.Errorf("resty loginCloudbrain: %s", err)
- }
-
- if loginResult.Code != "S000" {
- return fmt.Errorf("%s: %s", loginResult.Msg, res.String())
- }
-
- TOKEN = loginResult.Payload["token"].(string)
- return nil
- }
-
- func CreateJob(jobName string, createJobParams models.CreateJobParams) (*models.CreateJobResult, error) {
- checkSetting()
- client := getRestyClient()
- var jobResult models.CreateJobResult
-
- retry := 0
-
- sendjob:
- res, err := client.R().
- SetHeader("Content-Type", "application/json").
- SetAuthToken(TOKEN).
- SetBody(createJobParams).
- SetResult(&jobResult).
- Put(HOST + "/rest-server/api/v1/jobs/" + jobName)
-
- if err != nil {
- return nil, fmt.Errorf("resty create job: %s", err)
- }
-
- if jobResult.Code == "S401" && retry < 1 {
- retry++
- _ = loginCloudbrain()
- goto sendjob
- }
-
- if jobResult.Code != "S000" {
- return &jobResult, fmt.Errorf("jobResult err: %s", res.String())
- }
-
- return &jobResult, nil
- }
-
- func GetJob(jobID string) (*models.GetJobResult, error) {
- checkSetting()
- // http://192.168.204.24/rest-server/api/v1/jobs/90e26e500c4b3011ea0a251099a987938b96
- client := getRestyClient()
- var getJobResult models.GetJobResult
-
- retry := 0
-
- sendjob:
- res, err := client.R().
- SetHeader("Content-Type", "application/json").
- SetAuthToken(TOKEN).
- SetResult(&getJobResult).
- Get(HOST + "/rest-server/api/v1/jobs/" + jobID)
-
- if err != nil {
- return nil, fmt.Errorf("resty GetJob: %s", err)
- }
-
- if getJobResult.Code == "S401" && retry < 1 {
- retry++
- _ = loginCloudbrain()
- goto sendjob
- }
-
- if getJobResult.Code != "S000" {
- return &getJobResult, fmt.Errorf("jobResult GetJob err: %s", res.String())
- }
-
- return &getJobResult, nil
- }
-
- func GetImages() (*models.GetImagesResult, error) {
- checkSetting()
- client := getRestyClient()
- var getImagesResult models.GetImagesResult
-
- retry := 0
-
- sendjob:
- res, err := client.R().
- SetHeader("Content-Type", "application/json").
- SetAuthToken(TOKEN).
- SetResult(&getImagesResult).
- Get(HOST + "/rest-server/api/v1/image/list/")
-
- if err != nil {
- return nil, fmt.Errorf("resty GetJob: %s", err)
- }
-
- if getImagesResult.Code == "S401" && retry < 1 {
- retry++
- _ = loginCloudbrain()
- goto sendjob
- }
-
- if getImagesResult.Code != "S000" {
- return &getImagesResult, fmt.Errorf("getImgesResult err: %s", res.String())
- }
-
- return &getImagesResult, nil
- }
|