chronograf/flux/client.go

120 lines
2.5 KiB
Go
Raw Normal View History

Flux/connections page (#4026) * Load services on manages sources page Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add Flux dropdown on manage sources page Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add flux connection dropdown with create, select, and update Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Prevent creation of Flux connection with duplicate name Fixed bug where refreshing page allowed for Flux or connections with duplicate names to be created Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add ability to delete a FluxService from the fluxservice dropdown Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Change FluxPage to use the active flux connection set by FluxConnectionPage Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Ensure that only flux services are loaded into redux services * Polish styles for Flux Connection Page Co-authored-by: Alex Paxton <thealexpaxton@gmail.com> Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add pingflux as a part of adding and updating a flux service Co-authored-by: Chris Goller <goller@gmail.com> Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add Swagger routes and definitions for Service endpoints Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Replace flux overlay with redirect to flux connections pages * Add test for FluxConnectionPage Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Replace hard-coded string with enum * Use active from metadata instead of service object in ServiceDropdown Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add generic for getDeep on service metadata property * Capitalize enum constant values in FluxFormMode * Show error notification when user reaches edit route for nonexistant flux Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add tests for FluxDropdown * Update types for async functions
2018-07-27 01:16:02 +00:00
package flux
import (
"context"
"errors"
Flux/connections page (#4026) * Load services on manages sources page Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add Flux dropdown on manage sources page Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add flux connection dropdown with create, select, and update Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Prevent creation of Flux connection with duplicate name Fixed bug where refreshing page allowed for Flux or connections with duplicate names to be created Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add ability to delete a FluxService from the fluxservice dropdown Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Change FluxPage to use the active flux connection set by FluxConnectionPage Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Ensure that only flux services are loaded into redux services * Polish styles for Flux Connection Page Co-authored-by: Alex Paxton <thealexpaxton@gmail.com> Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add pingflux as a part of adding and updating a flux service Co-authored-by: Chris Goller <goller@gmail.com> Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add Swagger routes and definitions for Service endpoints Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Replace flux overlay with redirect to flux connections pages * Add test for FluxConnectionPage Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Replace hard-coded string with enum * Use active from metadata instead of service object in ServiceDropdown Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add generic for getDeep on service metadata property * Capitalize enum constant values in FluxFormMode * Show error notification when user reaches edit route for nonexistant flux Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add tests for FluxDropdown * Update types for async functions
2018-07-27 01:16:02 +00:00
"io/ioutil"
"net/http"
"net/url"
"strings"
Flux/connections page (#4026) * Load services on manages sources page Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add Flux dropdown on manage sources page Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add flux connection dropdown with create, select, and update Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Prevent creation of Flux connection with duplicate name Fixed bug where refreshing page allowed for Flux or connections with duplicate names to be created Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add ability to delete a FluxService from the fluxservice dropdown Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Change FluxPage to use the active flux connection set by FluxConnectionPage Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Ensure that only flux services are loaded into redux services * Polish styles for Flux Connection Page Co-authored-by: Alex Paxton <thealexpaxton@gmail.com> Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add pingflux as a part of adding and updating a flux service Co-authored-by: Chris Goller <goller@gmail.com> Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add Swagger routes and definitions for Service endpoints Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Replace flux overlay with redirect to flux connections pages * Add test for FluxConnectionPage Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Replace hard-coded string with enum * Use active from metadata instead of service object in ServiceDropdown Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add generic for getDeep on service metadata property * Capitalize enum constant values in FluxFormMode * Show error notification when user reaches edit route for nonexistant flux Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add tests for FluxDropdown * Update types for async functions
2018-07-27 01:16:02 +00:00
"time"
"github.com/influxdata/chronograf"
"github.com/influxdata/chronograf/util"
Flux/connections page (#4026) * Load services on manages sources page Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add Flux dropdown on manage sources page Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add flux connection dropdown with create, select, and update Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Prevent creation of Flux connection with duplicate name Fixed bug where refreshing page allowed for Flux or connections with duplicate names to be created Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add ability to delete a FluxService from the fluxservice dropdown Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Change FluxPage to use the active flux connection set by FluxConnectionPage Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Ensure that only flux services are loaded into redux services * Polish styles for Flux Connection Page Co-authored-by: Alex Paxton <thealexpaxton@gmail.com> Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add pingflux as a part of adding and updating a flux service Co-authored-by: Chris Goller <goller@gmail.com> Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add Swagger routes and definitions for Service endpoints Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Replace flux overlay with redirect to flux connections pages * Add test for FluxConnectionPage Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Replace hard-coded string with enum * Use active from metadata instead of service object in ServiceDropdown Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add generic for getDeep on service metadata property * Capitalize enum constant values in FluxFormMode * Show error notification when user reaches edit route for nonexistant flux Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add tests for FluxDropdown * Update types for async functions
2018-07-27 01:16:02 +00:00
)
// Shared transports for all clients to prevent leaking connections.
var (
skipVerifyTransport = util.CreateTransport(true)
defaultTransport = util.CreateTransport(false)
Flux/connections page (#4026) * Load services on manages sources page Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add Flux dropdown on manage sources page Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add flux connection dropdown with create, select, and update Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Prevent creation of Flux connection with duplicate name Fixed bug where refreshing page allowed for Flux or connections with duplicate names to be created Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add ability to delete a FluxService from the fluxservice dropdown Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Change FluxPage to use the active flux connection set by FluxConnectionPage Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Ensure that only flux services are loaded into redux services * Polish styles for Flux Connection Page Co-authored-by: Alex Paxton <thealexpaxton@gmail.com> Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add pingflux as a part of adding and updating a flux service Co-authored-by: Chris Goller <goller@gmail.com> Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add Swagger routes and definitions for Service endpoints Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Replace flux overlay with redirect to flux connections pages * Add test for FluxConnectionPage Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Replace hard-coded string with enum * Use active from metadata instead of service object in ServiceDropdown Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add generic for getDeep on service metadata property * Capitalize enum constant values in FluxFormMode * Show error notification when user reaches edit route for nonexistant flux Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add tests for FluxDropdown * Update types for async functions
2018-07-27 01:16:02 +00:00
)
// Client is how we interact with Flux.
type Client struct {
URL *url.URL
InsecureSkipVerify bool
Timeout time.Duration
}
// Ping checks the connection of a Flux.
func (c *Client) Ping(ctx context.Context) error {
t := 2 * time.Second
if c.Timeout > 0 {
t = c.Timeout
}
ctx, cancel := context.WithTimeout(ctx, t)
defer cancel()
err := c.pingTimeout(ctx)
return err
}
func (c *Client) pingTimeout(ctx context.Context) error {
resps := make(chan (error))
go func() {
resps <- c.ping(c.URL)
}()
select {
case resp := <-resps:
return resp
case <-ctx.Done():
return chronograf.ErrUpstreamTimeout
}
}
// FluxEnabled returns true if the server has flux querying enabled.
func (c *Client) FluxEnabled() (bool, error) {
url := c.URL
url.Path = "/api/v2/query"
req, err := http.NewRequest("POST", url.String(), nil)
if err != nil {
return false, err
}
hc := &http.Client{
Timeout: c.Timeout,
}
if c.InsecureSkipVerify {
hc.Transport = skipVerifyTransport
} else {
hc.Transport = defaultTransport
}
resp, err := hc.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
contentType := resp.Header.Get("Content-Type")
// 1.x: When flux is enabled, the response has 'Content-Type' set to 'application/json' and a body
// of `{"error":"mime: no media type"}`. Otherwise it is 'text/plain; charset=utf-8' with
// `Flux query service disabled.` in the body.
// 2.x: Flux is always enabled, the 401 response with 'application/json; charset=utf-8' content type and body
// {"code":"unauthorized","message":"unauthorized access"} is received
return strings.HasPrefix(contentType, "application/json"), nil
}
Flux/connections page (#4026) * Load services on manages sources page Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add Flux dropdown on manage sources page Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add flux connection dropdown with create, select, and update Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Prevent creation of Flux connection with duplicate name Fixed bug where refreshing page allowed for Flux or connections with duplicate names to be created Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add ability to delete a FluxService from the fluxservice dropdown Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Change FluxPage to use the active flux connection set by FluxConnectionPage Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Ensure that only flux services are loaded into redux services * Polish styles for Flux Connection Page Co-authored-by: Alex Paxton <thealexpaxton@gmail.com> Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add pingflux as a part of adding and updating a flux service Co-authored-by: Chris Goller <goller@gmail.com> Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add Swagger routes and definitions for Service endpoints Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Replace flux overlay with redirect to flux connections pages * Add test for FluxConnectionPage Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Replace hard-coded string with enum * Use active from metadata instead of service object in ServiceDropdown Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add generic for getDeep on service metadata property * Capitalize enum constant values in FluxFormMode * Show error notification when user reaches edit route for nonexistant flux Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add tests for FluxDropdown * Update types for async functions
2018-07-27 01:16:02 +00:00
func (c *Client) ping(u *url.URL) error {
u.Path = "ping"
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return err
}
hc := &http.Client{}
if c.InsecureSkipVerify {
hc.Transport = skipVerifyTransport
} else {
hc.Transport = defaultTransport
}
resp, err := hc.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return errors.New(string(body))
Flux/connections page (#4026) * Load services on manages sources page Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add Flux dropdown on manage sources page Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add flux connection dropdown with create, select, and update Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Prevent creation of Flux connection with duplicate name Fixed bug where refreshing page allowed for Flux or connections with duplicate names to be created Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add ability to delete a FluxService from the fluxservice dropdown Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Change FluxPage to use the active flux connection set by FluxConnectionPage Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Ensure that only flux services are loaded into redux services * Polish styles for Flux Connection Page Co-authored-by: Alex Paxton <thealexpaxton@gmail.com> Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add pingflux as a part of adding and updating a flux service Co-authored-by: Chris Goller <goller@gmail.com> Co-authored-by: Alirie Gray <alirie.gray@gmail.com> * Add Swagger routes and definitions for Service endpoints Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Replace flux overlay with redirect to flux connections pages * Add test for FluxConnectionPage Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Replace hard-coded string with enum * Use active from metadata instead of service object in ServiceDropdown Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add generic for getDeep on service metadata property * Capitalize enum constant values in FluxFormMode * Show error notification when user reaches edit route for nonexistant flux Co-authored-by: Iris Scholten <ischolten.is@gmail.com> * Add tests for FluxDropdown * Update types for async functions
2018-07-27 01:16:02 +00:00
}
return nil
}