github.com/CyCoreSystems/ari@v4.8.4+incompatible/client/native/logging.go (about)

     1  package native
     2  
     3  import (
     4  	"github.com/CyCoreSystems/ari"
     5  	"github.com/pkg/errors"
     6  )
     7  
     8  // Logging provides the ARI Logging accessors for a native client
     9  type Logging struct {
    10  	client *Client
    11  }
    12  
    13  // Create creates a logging level
    14  func (l *Logging) Create(key *ari.Key, levels string) (*ari.LogHandle, error) {
    15  	req := struct {
    16  		Levels string `json:"configuration"`
    17  	}{
    18  		Levels: levels,
    19  	}
    20  
    21  	err := l.client.post("/asterisk/logging/"+key.ID, nil, &req)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	return l.Get(key), nil
    27  }
    28  
    29  // Get returns a logging channel handle
    30  func (l *Logging) Get(key *ari.Key) *ari.LogHandle {
    31  	return ari.NewLogHandle(l.client.stamp(key), l)
    32  }
    33  
    34  func (l *Logging) getLoggingChannels() ([]*ari.LogData, error) {
    35  	var ld []*ari.LogData
    36  	err := l.client.get("/asterisk/logging", &ld)
    37  	return ld, err
    38  }
    39  
    40  // Data returns the data of a logging channel
    41  func (l *Logging) Data(key *ari.Key) (*ari.LogData, error) {
    42  	if key == nil || key.ID == "" {
    43  		return nil, errors.New("logging key not supplied")
    44  	}
    45  
    46  	logChannels, err := l.getLoggingChannels()
    47  	if err != nil {
    48  		return nil, errors.Wrap(err, "failed to get list of logging channels")
    49  	}
    50  
    51  	for _, i := range logChannels {
    52  		if i.Name == key.ID {
    53  			i.Key = l.client.stamp(key)
    54  			return i, nil
    55  		}
    56  	}
    57  	return nil, errors.New("not found")
    58  }
    59  
    60  // List lists the logging entities
    61  func (l *Logging) List(filter *ari.Key) ([]*ari.Key, error) {
    62  	ld, err := l.getLoggingChannels()
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	if filter == nil {
    67  		filter = ari.NodeKey(l.client.ApplicationName(), l.client.node)
    68  	}
    69  
    70  	var ret []*ari.Key
    71  	for _, i := range ld {
    72  		k := ari.NewKey(ari.LoggingKey, i.Name, ari.WithApp(l.client.ApplicationName()), ari.WithNode(l.client.node))
    73  		if filter.Match(k) {
    74  			ret = append(ret, k)
    75  		}
    76  	}
    77  	return ret, nil
    78  }
    79  
    80  // Rotate rotates the given log
    81  func (l *Logging) Rotate(key *ari.Key) (err error) {
    82  	name := key.ID
    83  	if name == "" {
    84  		err = errors.New("Not allowed to rotate unnamed channels")
    85  		return
    86  	}
    87  	err = l.client.put("/asterisk/logging/"+name+"/rotate", nil, nil)
    88  	return
    89  }
    90  
    91  // Delete deletes the named log
    92  func (l *Logging) Delete(key *ari.Key) (err error) {
    93  	name := key.ID
    94  	if name == "" {
    95  		err = errors.New("Not allowed to delete unnamed channels")
    96  		return
    97  	}
    98  	err = l.client.del("/asterisk/logging/"+name, nil, "")
    99  	return
   100  }