Files
decort-golang-sdk/config/config.go

102 lines
2.6 KiB
Go
Raw Normal View History

2022-10-03 16:56:47 +03:00
package config
2023-03-24 17:09:30 +03:00
import (
"encoding/json"
"os"
2023-06-01 16:50:10 +03:00
"time"
2023-03-24 17:09:30 +03:00
"gopkg.in/yaml.v3"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
2022-12-22 17:56:47 +03:00
// Configuration for creating request to platform
2022-10-03 16:56:47 +03:00
type Config struct {
2022-12-22 17:56:47 +03:00
// JWT platform token
// Required: false
// Example: "qwqwdfwv68979we0q9bfv7e9sbvd89798qrwv97ff"
2023-03-24 17:09:30 +03:00
Token string `json:"token" yaml:"token"`
2022-12-22 17:56:47 +03:00
// Application (client) identifier for authorization
// in the cloud platform controller in oauth2 mode.
// Required: true
// Example: "ewqfrvea7s890avw804389qwguf234h0otfi3w4eiu"
2023-03-24 17:09:30 +03:00
AppID string `json:"appId" yaml:"appId" validate:"required"`
2022-12-22 17:56:47 +03:00
// Application (client) secret code for authorization
// in the cloud platform controller in oauth2 mode.
// Example: "frvet09rvesfis0c9erv9fsov0vsdfi09ovds0f"
2023-03-24 17:09:30 +03:00
AppSecret string `json:"appSecret" yaml:"appSecret" validate:"required"`
2022-12-22 17:56:47 +03:00
// Platform authentication service address
// Required: true
// Example: "https://sso.digitalenergy.online"
2023-03-24 17:09:30 +03:00
SSOURL string `json:"ssoUrl" yaml:"ssoUrl" validate:"url"`
2022-12-22 17:56:47 +03:00
// The address of the platform on which the actions are planned
// Required: true
// Example: "https://mr4.digitalenergy.online"
2023-03-24 17:09:30 +03:00
DecortURL string `json:"decortUrl" yaml:"decortUrl" validate:"url"`
2022-12-22 17:56:47 +03:00
// Amount platform request attempts
// Default value: 5
// Required: false
2023-03-24 17:09:30 +03:00
Retries uint64 `json:"retries" yaml:"retries"`
2022-12-22 17:56:47 +03:00
2023-09-24 12:11:31 +03:00
// Skip verify
2022-12-22 17:56:47 +03:00
// Required: false
2023-03-24 17:09:30 +03:00
SSLSkipVerify bool `json:"sslSkipVerify" yaml:"sslSkipVerify"`
2023-06-01 16:50:10 +03:00
// HTTP client timeout, unlimited if left empty
// Required: false
Timeout Duration `json:"timeout" yaml:"timeout"`
}
// SetTimeout is used to set HTTP client timeout.
func (c *Config) SetTimeout(dur time.Duration) {
c.Timeout = Duration(dur)
2023-03-24 17:09:30 +03:00
}
// ParseConfigJSON parses Config from specified JSON-formatted file.
func ParseConfigJSON(path string) (Config, error) {
file, err := os.ReadFile(path)
if err != nil {
return Config{}, err
}
var config Config
err = json.Unmarshal(file, &config)
if err != nil {
return Config{}, err
}
err = validators.ValidateConfig(config)
if err != nil {
2023-10-25 17:37:18 +03:00
return Config{}, validators.ValidationErrors(validators.GetErrors(err))
2023-03-24 17:09:30 +03:00
}
return config, nil
}
// ParseConfigYAML parses Config from specified YAML-formatted file.
func ParseConfigYAML(path string) (Config, error) {
file, err := os.ReadFile(path)
if err != nil {
return Config{}, err
}
var config Config
err = yaml.Unmarshal(file, &config)
if err != nil {
return Config{}, err
}
err = validators.ValidateConfig(config)
if err != nil {
2023-10-25 17:37:18 +03:00
return Config{}, validators.ValidationErrors(validators.GetErrors(err))
2023-03-24 17:09:30 +03:00
}
return config, nil
2022-10-03 16:56:47 +03:00
}