2017-06-17 04:04:55 +00:00
|
|
|
package api
|
|
|
|
|
2018-08-14 19:29:22 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
2018-07-24 22:49:55 +00:00
|
|
|
|
2017-06-17 04:04:55 +00:00
|
|
|
func (c *Sys) CORSStatus() (*CORSResponse, error) {
|
|
|
|
r := c.c.NewRequest("GET", "/v1/sys/config/cors")
|
2018-07-24 22:49:55 +00:00
|
|
|
|
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
defer cancelFunc()
|
|
|
|
resp, err := c.c.RawRequestWithContext(ctx, r)
|
2017-06-17 04:04:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-08-14 19:29:22 +00:00
|
|
|
secret, err := ParseSecret(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if secret == nil || secret.Data == nil {
|
|
|
|
return nil, errors.New("data from server response is empty")
|
|
|
|
}
|
|
|
|
|
2017-06-17 04:04:55 +00:00
|
|
|
var result CORSResponse
|
2018-08-14 19:29:22 +00:00
|
|
|
err = mapstructure.Decode(secret.Data, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-17 04:04:55 +00:00
|
|
|
return &result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) ConfigureCORS(req *CORSRequest) (*CORSResponse, error) {
|
|
|
|
r := c.c.NewRequest("PUT", "/v1/sys/config/cors")
|
|
|
|
if err := r.SetJSONBody(req); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-24 22:49:55 +00:00
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
defer cancelFunc()
|
|
|
|
resp, err := c.c.RawRequestWithContext(ctx, r)
|
2017-06-17 04:04:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-08-14 19:29:22 +00:00
|
|
|
secret, err := ParseSecret(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if secret == nil || secret.Data == nil {
|
|
|
|
return nil, errors.New("data from server response is empty")
|
|
|
|
}
|
|
|
|
|
2017-06-17 04:04:55 +00:00
|
|
|
var result CORSResponse
|
2018-08-14 19:29:22 +00:00
|
|
|
err = mapstructure.Decode(secret.Data, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-17 04:04:55 +00:00
|
|
|
return &result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) DisableCORS() (*CORSResponse, error) {
|
|
|
|
r := c.c.NewRequest("DELETE", "/v1/sys/config/cors")
|
|
|
|
|
2018-07-24 22:49:55 +00:00
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
defer cancelFunc()
|
|
|
|
resp, err := c.c.RawRequestWithContext(ctx, r)
|
2017-06-17 04:04:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-08-14 19:29:22 +00:00
|
|
|
secret, err := ParseSecret(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if secret == nil || secret.Data == nil {
|
|
|
|
return nil, errors.New("data from server response is empty")
|
|
|
|
}
|
|
|
|
|
2017-06-17 04:04:55 +00:00
|
|
|
var result CORSResponse
|
2018-08-14 19:29:22 +00:00
|
|
|
err = mapstructure.Decode(secret.Data, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-17 04:04:55 +00:00
|
|
|
|
2018-08-14 19:29:22 +00:00
|
|
|
return &result, err
|
2017-06-17 04:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CORSRequest struct {
|
2018-08-14 19:29:22 +00:00
|
|
|
AllowedOrigins string `json:"allowed_origins" mapstructure:"allowed_origins"`
|
|
|
|
Enabled bool `json:"enabled" mapstructure:"enabled"`
|
2017-06-17 04:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CORSResponse struct {
|
2018-08-14 19:29:22 +00:00
|
|
|
AllowedOrigins string `json:"allowed_origins" mapstructure:"allowed_origins"`
|
|
|
|
Enabled bool `json:"enabled" mapstructure:"enabled"`
|
2017-06-17 04:04:55 +00:00
|
|
|
}
|