github.com/timstclair/heapster@v0.20.0-alpha1/common/influxdb/influxdb.go (about)

     1  // Copyright 2015 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package influxdb
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  	"strconv"
    21  	"time"
    22  
    23  	"k8s.io/heapster/version"
    24  
    25  	influxdb "github.com/influxdb/influxdb/client"
    26  )
    27  
    28  type InfluxdbClient interface {
    29  	Write(influxdb.BatchPoints) (*influxdb.Response, error)
    30  	Query(influxdb.Query) (*influxdb.Response, error)
    31  	Ping() (time.Duration, string, error)
    32  }
    33  
    34  type InfluxdbConfig struct {
    35  	User     string
    36  	Password string
    37  	Secure   bool
    38  	Host     string
    39  	DbName   string
    40  }
    41  
    42  func NewClient(c InfluxdbConfig) (InfluxdbClient, error) {
    43  	url := &url.URL{
    44  		Scheme: "http",
    45  		Host:   c.Host,
    46  	}
    47  	if c.Secure {
    48  		url.Scheme = "https"
    49  	}
    50  
    51  	iConfig := &influxdb.Config{
    52  		URL:       *url,
    53  		Username:  c.User,
    54  		Password:  c.Password,
    55  		UserAgent: fmt.Sprintf("%v/%v", "heapster", version.HeapsterVersion),
    56  	}
    57  	client, err := influxdb.NewClient(*iConfig)
    58  
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	if _, _, err := client.Ping(); err != nil {
    63  		return nil, fmt.Errorf("failed to ping InfluxDB server at %q - %v", c.Host, err)
    64  	}
    65  	return client, nil
    66  }
    67  
    68  func BuildConfig(uri *url.URL) (*InfluxdbConfig, error) {
    69  	config := InfluxdbConfig{
    70  		User:     "root",
    71  		Password: "root",
    72  		Host:     "localhost:8086",
    73  		DbName:   "k8s",
    74  		Secure:   false,
    75  	}
    76  
    77  	if len(uri.Host) > 0 {
    78  		config.Host = uri.Host
    79  	}
    80  	opts := uri.Query()
    81  	if len(opts["user"]) >= 1 {
    82  		config.User = opts["user"][0]
    83  	}
    84  	// TODO: use more secure way to pass the password.
    85  	if len(opts["pw"]) >= 1 {
    86  		config.Password = opts["pw"][0]
    87  	}
    88  	if len(opts["db"]) >= 1 {
    89  		config.DbName = opts["db"][0]
    90  	}
    91  	if len(opts["secure"]) >= 1 {
    92  		val, err := strconv.ParseBool(opts["secure"][0])
    93  		if err != nil {
    94  			return nil, fmt.Errorf("failed to parse `secure` flag - %v", err)
    95  		}
    96  		config.Secure = val
    97  	}
    98  
    99  	return &config, nil
   100  }