diff --git a/README.md b/README.md index 48ab1f3..06910ec 100644 --- a/README.md +++ b/README.md @@ -47,14 +47,15 @@ go get github.com/ionos-cloud/sdk-go/v6@latest ## Environment Variables -| Environment Variable | Description | -|----------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `IONOS_USERNAME` | Specify the username used to login, to authenticate against the IONOS Cloud API | -| `IONOS_PASSWORD` | Specify the password used to login, to authenticate against the IONOS Cloud API | -| `IONOS_TOKEN` | Specify the token used to login, if a token is being used instead of username and password | -| `IONOS_API_URL` | Specify the API URL. It will overwrite the API endpoint default value `api.ionos.com`. Note: the host URL does not contain the `/cloudapi/v6` path, so it should _not_ be included in the `IONOS_API_URL` environment variable | -| `IONOS_LOG_LEVEL` | Specify the Log Level used to log messages. Possible values: Off, Debug, Trace | -| `IONOS_PINNED_CERT` | Specify the SHA-256 public fingerprint here, enables certificate pinning | +| Environment Variable | Description | +|-------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `IONOS_USERNAME` | Specify the username used to login, to authenticate against the IONOS Cloud API | +| `IONOS_PASSWORD` | Specify the password used to login, to authenticate against the IONOS Cloud API | +| `IONOS_TOKEN` | Specify the token used to login, if a token is being used instead of username and password | +| `IONOS_API_URL` | Specify the API URL. It will overwrite the API endpoint default value `api.ionos.com`. Note: the host URL does not contain the `/cloudapi/v6` path, so it should _not_ be included in the `IONOS_API_URL` environment variable | +| `IONOS_LOG_LEVEL` | Specify the Log Level used to log messages. Possible values: Off, Debug, Trace | +| `IONOS_PINNED_CERT` | Specify the SHA-256 public fingerprint here, enables certificate pinning | +| `IONOS_CONTRACT_NUMBER` | Specify the contract number on which you wish to provision. Only valid for reseller accounts, for other types of accounts the header will be ignored | ⚠️ **_Note: To overwrite the api endpoint - `api.ionos.com`, the environment variable `$IONOS_API_URL` can be set, and used with `NewConfigurationFromEnv()` function._** diff --git a/client.go b/client.go index 4b5a7e0..d237278 100644 --- a/client.go +++ b/client.go @@ -53,7 +53,7 @@ const ( RequestStatusFailed = "FAILED" RequestStatusDone = "DONE" - Version = "6.1.8" + Version = "6.1.9" ) // Constants for APIs @@ -444,6 +444,15 @@ func (c *APIClient) prepareRequest( var body *bytes.Buffer + val, isSetInEnv := os.LookupEnv(IonosContractNumber) + _, isSetInMap := headerParams["X-Contract-Number"] + if headerParams == nil { + headerParams = make(map[string]string) + } + if !isSetInMap && isSetInEnv { + headerParams["X-Contract-Number"] = val + } + // Detect postBody type and post. if postBody != nil { contentType := headerParams["Content-Type"] @@ -949,7 +958,8 @@ func (c *APIClient) WaitForRequest(ctx context.Context, path string) (*APIRespon } if resp.StatusCode != http.StatusOK { - return localVarAPIResponse, fmt.Errorf("WaitForRequest failed; received status code %d from API", resp.StatusCode) + msg := fmt.Sprintf("WaitForRequest failed; received status code %d from API", resp.StatusCode) + return localVarAPIResponse, NewGenericOpenAPIError(msg, localVarBody, nil, resp.StatusCode) } if status.Metadata != nil && status.Metadata.Status != nil { switch *status.Metadata.Status { @@ -964,9 +974,7 @@ func (c *APIClient) WaitForRequest(ctx context.Context, path string) (*APIRespon if status.Metadata.Message != nil { message = *status.Metadata.Message } - return localVarAPIResponse, errors.New( - fmt.Sprintf("Request %s failed: %s", id, message), - ) + return localVarAPIResponse, fmt.Errorf("Request %s failed: %s", id, message) } } select { @@ -1106,7 +1114,7 @@ func strlen(s string) int { return utf8.RuneCountInString(s) } -// GenericOpenAPIError Provides access to the body, error and model on returned errors. +// GenericOpenAPIError provides access to the body, error and model on returned errors. type GenericOpenAPIError struct { statusCode int body []byte diff --git a/configuration.go b/configuration.go index 6845f4a..075fba8 100644 --- a/configuration.go +++ b/configuration.go @@ -28,6 +28,7 @@ const ( IonosApiUrlEnvVar = "IONOS_API_URL" IonosPinnedCertEnvVar = "IONOS_PINNED_CERT" IonosLogLevelEnvVar = "IONOS_LOG_LEVEL" + IonosContractNumber = "IONOS_CONTRACT_NUMBER" DefaultIonosServerUrl = "https://api.ionos.com/cloudapi/v6" DefaultIonosBasePath = "/cloudapi/v6" defaultMaxRetries = 3 @@ -130,7 +131,7 @@ func NewConfiguration(username, password, token, hostUrl string) *Configuration cfg := &Configuration{ DefaultHeader: make(map[string]string), DefaultQueryParams: url.Values{}, - UserAgent: "ionos-cloud-sdk-go/v6.1.8", + UserAgent: "ionos-cloud-sdk-go/v6.1.9", Debug: false, Username: username, Password: password,