github.com/CyCoreSystems/ari@v4.8.4+incompatible/config.go (about)

     1  package ari
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  )
     8  
     9  // Config represents a transport to the asterisk
    10  // config ARI resource.
    11  type Config interface {
    12  
    13  	// Get gets the reference to a config object
    14  	Get(key *Key) *ConfigHandle
    15  
    16  	// Data gets the data for the config object
    17  	Data(key *Key) (*ConfigData, error)
    18  
    19  	// Update creates or updates the given tuples
    20  	Update(key *Key, tuples []ConfigTuple) error
    21  
    22  	// Delete deletes the dynamic configuration object.
    23  	Delete(key *Key) error
    24  }
    25  
    26  // ConfigData contains the data for a given configuration object
    27  type ConfigData struct {
    28  	// Key is the cluster-unique identifier for this configuration
    29  	Key *Key `json:"key"`
    30  
    31  	Class string
    32  	Type  string
    33  	Name  string
    34  
    35  	Fields []ConfigTuple
    36  }
    37  
    38  // ID returns the ID of the ConfigData structure
    39  func (cd *ConfigData) ID() string {
    40  	return fmt.Sprintf("%s/%s/%s", cd.Class, cd.Type, cd.Name)
    41  }
    42  
    43  //ConfigTupleList wrap a list for asterisk ari require.
    44  type ConfigTupleList struct {
    45  	Fields []ConfigTuple `json:"fields"`
    46  }
    47  
    48  // ConfigTuple is the key-value pair that defines a configuration entry
    49  type ConfigTuple struct {
    50  	Attribute string `json:"attribute"`
    51  	Value     string `json:"value"`
    52  }
    53  
    54  // A ConfigHandle is a reference to a Config object
    55  // on the asterisk service
    56  type ConfigHandle struct {
    57  	key *Key
    58  
    59  	c Config
    60  }
    61  
    62  // NewConfigHandle builds a new config handle
    63  func NewConfigHandle(key *Key, c Config) *ConfigHandle {
    64  	return &ConfigHandle{
    65  		key: key,
    66  		c:   c,
    67  	}
    68  }
    69  
    70  // ID returns the unique identifier for the config object
    71  func (h *ConfigHandle) ID() string {
    72  	return h.key.ID
    73  }
    74  
    75  // Data gets the current data for the config handle
    76  func (h *ConfigHandle) Data() (*ConfigData, error) {
    77  	return h.c.Data(h.key)
    78  }
    79  
    80  // Update creates or updates the given config tuples
    81  func (h *ConfigHandle) Update(tuples []ConfigTuple) error {
    82  	return h.c.Update(h.key, tuples)
    83  }
    84  
    85  // Delete deletes the dynamic configuration object
    86  func (h *ConfigHandle) Delete() error {
    87  	return h.c.Delete(h.key)
    88  }
    89  
    90  // ParseConfigID parses the provided Config ID into its Class, Type, and ID components
    91  func ParseConfigID(input string) (class, kind, id string, err error) {
    92  	pieces := strings.Split(input, "/")
    93  	if len(pieces) < 3 {
    94  		err = errors.New("invalid input ID")
    95  		return
    96  	}
    97  
    98  	return pieces[0], pieces[1], pieces[2], nil
    99  }