github.com/jfrog/jfrog-cli-core/v2@v2.51.0/missioncontrol/commands/licenseacquire.go (about)

     1  package commands
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"net/http"
     7  
     8  	"github.com/jfrog/jfrog-cli-core/v2/missioncontrol/utils"
     9  	"github.com/jfrog/jfrog-cli-core/v2/utils/config"
    10  	"github.com/jfrog/jfrog-client-go/http/httpclient"
    11  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    12  	"github.com/jfrog/jfrog-client-go/utils/log"
    13  )
    14  
    15  func LicenseAcquire(bucketId string, name string, serverDetails *config.ServerDetails) error {
    16  	postContent := LicenseAcquireRequestContent{
    17  		Name:         name,
    18  		LicenseCount: 1,
    19  	}
    20  	requestContent, err := json.Marshal(postContent)
    21  	if err != nil {
    22  		return errorutils.CheckErrorf("Failed to marshal json: " + err.Error())
    23  	}
    24  	missionControlUrl := serverDetails.MissionControlUrl + "api/v1/buckets/" + bucketId + "/acquire"
    25  	httpClientDetails := utils.GetMissionControlHttpClientDetails(serverDetails)
    26  	client, err := httpclient.ClientBuilder().SetRetries(3).Build()
    27  	if err != nil {
    28  		return err
    29  	}
    30  	resp, body, err := client.SendPost(missionControlUrl, requestContent, httpClientDetails, "")
    31  	if err != nil {
    32  		return err
    33  	}
    34  	if err = errorutils.CheckResponseStatus(resp, http.StatusOK); err != nil {
    35  		return errors.New(resp.Status + ". " + utils.ReadMissionControlHttpMessage(body))
    36  	}
    37  	log.Debug("Mission Control response: " + resp.Status)
    38  
    39  	// Extract license from response
    40  	var licenseKeys licenseKeys
    41  	err = json.Unmarshal(body, &licenseKeys)
    42  	if err != nil {
    43  		return errorutils.CheckError(err)
    44  	}
    45  	if len(licenseKeys.LicenseKeys) < 1 {
    46  		return errorutils.CheckErrorf("failed to acquire license key from Mission Control: received 0 license keys")
    47  	}
    48  	// Print license to log
    49  	log.Output(licenseKeys.LicenseKeys[0])
    50  	return nil
    51  }
    52  
    53  type LicenseAcquireRequestContent struct {
    54  	Name         string `json:"name,omitempty"`
    55  	LicenseCount int    `json:"license_count,omitempty"`
    56  }
    57  
    58  type licenseKeys struct {
    59  	LicenseKeys []string `json:"license_keys,omitempty"`
    60  }