1package vps 2 3import ( 4 "fmt" 5 "github.com/transip/gotransip/v6/repository" 6 "github.com/transip/gotransip/v6/rest" 7) 8 9// LicenseRepository allows you to manage all vps license api actions 10// like listing, getting license keys, ordering, updating, deleting licenses 11type LicenseRepository repository.RestRepository 12 13// LicenseType is one of the following strings 14// 'addon', 'operating-system' 15type LicenseType string 16 17// Definition of all of the possible license types 18const ( 19 // Addon licenses can be purchased individually 20 LicenseTypeAddon LicenseType = "addon" 21 // Operating system licenses cannot be directly purchased, or cancelled, 22 // they are attached to your VPS the moment you install an operating system that requires a license. 23 // Operating systems such as Plesk, DirectAdmin, cPanel and etc need a valid license. 24 // An operating system license can only be upgraded or downgraded 25 LicenseTypeOperatingSystem LicenseType = "operating-system" 26) 27 28// Licenses struct contains Active, Available and Cancellable License structs in it 29type Licenses struct { 30 // A list of licenses active on your VPS 31 Active []License `json:"active"` 32 // A list of available licenses that you can order for your VPS 33 Available []LicenseProduct `json:"available"` 34 // A list of licenses active on your VPS that you can cancel 35 Cancellable []License `json:"cancellable"` 36} 37 38// License struct for a vps license 39type License struct { 40 // The License id 41 ID int64 `json:"id"` 42 // License name 43 Name string `json:"name"` 44 // Price in cents 45 Price int `json:"price"` 46 // Recurring price in cents 47 RecurringPrice int `json:"recurringPrice"` 48 // License type: 'operating-system', 'addon' 49 Type LicenseType `json:"type"` 50 // Quantity already purchased 51 Quantity int `json:"quantity"` 52 // Maximum quantity you are allowed to purchase 53 MaxQuantity int `json:"maxQuantity"` 54 // License keys belonging to this License 55 Keys []LicenseKey `json:"keys"` 56} 57 58// LicenseProduct struct for a orderable license 59type LicenseProduct struct { 60 // License name 61 Name string `json:"name"` 62 // Price in cents 63 Price int `json:"price"` 64 // Recurring price in cents 65 RecurringPrice int `json:"recurringPrice"` 66 // License type: 'operating-system', 'addon' 67 Type LicenseType `json:"type"` 68 // Maximum quantity you are allowed to purchase 69 MaxQuantity int `json:"maxQuantity"` 70} 71 72// licensesWrapper struct contains Licenses in it, 73// this is solely used for unmarshalling 74type licensesWrapper struct { 75 Licenses Licenses `json:"licenses"` 76} 77 78// licenseReplaceRequest struct contains a License Name in it, 79// this is solely used for marshalling 80type licenseReplaceRequest struct { 81 NewLicenseName string `json:"newLicenseName"` 82} 83 84// LicenseKey struct contains a license key and name for a specific License 85type LicenseKey struct { 86 // License name 87 Name string `json:"name"` 88 // License key 89 Key string `json:"key"` 90} 91 92// The LicenseOrder struct is used for ordering a new license for a VPS 93type LicenseOrder struct { 94 // Name of the license that you want to order 95 LicenseName string `json:"licenseName"` 96 // Quantity of this license that you want to order 97 Quantity int `json:"quantity"` 98} 99 100// ReplaceLicenseRequest this struct is used for replacing a 'operating-system' type license with a new license 101type ReplaceLicenseRequest struct { 102 // The License id 103 LicenseID int64 104 // NewLicenseName is the name of the license with which you want to replace the current 'operating-system' license 105 NewLicenseName string 106} 107 108// GetAll returns a struct with 'cancellable', 'available' and 'active' licenses in it for the given VPS 109// 110// Operating system licenses cannot be directly purchased, or cancelled, 111// they are attached to your VPS the moment you install an operating system that requires a license. 112// Operating systems such as Plesk, DirectAdmin, cPanel and etc need a valid license. 113// An operating system license can only be upgraded or downgraded by using the Update an operating system license API call. 114// 115// Addon licenses can be purchased individually through the Order an addon license API call. 116func (r *LicenseRepository) GetAll(vpsName string) (Licenses, error) { 117 var response licensesWrapper 118 restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/licenses", vpsName)} 119 err := r.Client.Get(restRequest, &response) 120 121 return response.Licenses, err 122} 123 124// Order allows you to order a new license for a given Vps. 125// In order to purchase an addon license for your VPS, use this API call. 126// The licenses that can be ordered can be requested using the get licenses api call 127func (r *LicenseRepository) Order(vpsName string, order LicenseOrder) error { 128 restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/licenses", vpsName), Body: &order} 129 130 return r.Client.Post(restRequest) 131} 132 133// Replace allows you to switch between operating system licenses 134// 135// Provide your desired license name in the licenseName parameter for either to upgrade or downgrade. 136// Only operating system licenses can be passed through this API call. 137func (r *LicenseRepository) Replace(vpsName string, request ReplaceLicenseRequest) error { 138 requestBody := licenseReplaceRequest{NewLicenseName: request.NewLicenseName} 139 restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/licenses/%d", vpsName, request.LicenseID), Body: &requestBody} 140 141 return r.Client.Put(restRequest) 142} 143 144// Cancel allows you to cancel a license for a given vps by its id and the VPS name. 145// Operating system licenses cannot be cancelled. 146func (r *LicenseRepository) Cancel(vpsName string, licenseID int64) error { 147 restRequest := rest.Request{Endpoint: fmt.Sprintf("/vps/%s/licenses/%d", vpsName, licenseID)} 148 149 return r.Client.Delete(restRequest) 150} 151