go.ligato.io/vpp-agent/v3@v3.5.0/cmd/agentctl/client/infra.go (about)

     1  //  Copyright (c) 2019 Cisco and/or its affiliates.
     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 client
    16  
    17  import (
    18  	"context"
    19  	"encoding/json"
    20  	"fmt"
    21  
    22  	"go.ligato.io/cn-infra/v2/health/probe"
    23  
    24  	"go.ligato.io/vpp-agent/v3/cmd/agentctl/api/types"
    25  )
    26  
    27  // AgentVersion returns information about Agent.
    28  func (c *Client) AgentVersion(ctx context.Context) (*types.Version, error) {
    29  	resp, err := c.get(ctx, "/info/version", nil, nil)
    30  	defer ensureReaderClosed(resp)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	var v types.Version
    35  	v.APIVersion = resp.header.Get("API-Version")
    36  
    37  	err = json.NewDecoder(resp.body).Decode(&v)
    38  	return &v, err
    39  }
    40  
    41  // LoggerList returns list of all registered loggers in Agent.
    42  func (c *Client) LoggerList(ctx context.Context) ([]types.Logger, error) {
    43  	resp, err := c.get(ctx, "/log/list", nil, nil)
    44  	if err != nil {
    45  		return nil, fmt.Errorf("HTTP request failed: %v", err)
    46  	}
    47  
    48  	var loggers []types.Logger
    49  	if err := json.NewDecoder(resp.body).Decode(&loggers); err != nil {
    50  		return nil, fmt.Errorf("decoding reply failed: %v", err)
    51  	}
    52  
    53  	return loggers, nil
    54  }
    55  
    56  func (c *Client) LoggerSet(ctx context.Context, logger, level string) error {
    57  	urlPath := "/log/" + logger + "/" + level
    58  
    59  	resp, err := c.put(ctx, urlPath, nil, nil, nil)
    60  	if err != nil {
    61  		return fmt.Errorf("HTTP request failed: %v", err)
    62  	}
    63  
    64  	type Response struct {
    65  		Logger string `json:"logger,omitempty"`
    66  		Level  string `json:"level,omitempty"`
    67  		Error  string `json:"Error,omitempty"`
    68  	}
    69  
    70  	var loggerSetResponse Response
    71  	if err := json.NewDecoder(resp.body).Decode(&loggerSetResponse); err != nil {
    72  		return fmt.Errorf("decoding reply failed: %v", err)
    73  	}
    74  	if loggerSetResponse.Error != "" {
    75  		return fmt.Errorf("SERVER: %s", loggerSetResponse.Error)
    76  	}
    77  
    78  	return nil
    79  }
    80  
    81  func (c *Client) Status(ctx context.Context) (*probe.ExposedStatus, error) {
    82  	resp, err := c.get(ctx, "/readiness", nil, nil)
    83  	if err != nil {
    84  		return nil, fmt.Errorf("HTTP request failed: %v", err)
    85  	}
    86  
    87  	var status probe.ExposedStatus
    88  	if err := json.NewDecoder(resp.body).Decode(&status); err != nil {
    89  		return nil, fmt.Errorf("decoding reply failed: %v", err)
    90  	}
    91  
    92  	return &status, nil
    93  }
    94  
    95  func (c *Client) GetMetricData(ctx context.Context, metricName string) (map[string]interface{}, error) {
    96  	resp, err := c.get(ctx, "/metrics/"+metricName, nil, nil)
    97  	if err != nil {
    98  		return nil, fmt.Errorf("HTTP request failed: %v", err)
    99  	}
   100  
   101  	var metricData = make(map[string]interface{})
   102  	if err := json.NewDecoder(resp.body).Decode(&metricData); err != nil {
   103  		return nil, fmt.Errorf("decoding reply failed: %v", err)
   104  	}
   105  
   106  	return metricData, nil
   107  }