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

     1  package native
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/CyCoreSystems/ari"
     7  )
     8  
     9  // Endpoint provides the ARI Endpoint accessors for the native client
    10  type Endpoint struct {
    11  	client *Client
    12  }
    13  
    14  // Get gets a lazy handle for the endpoint entity
    15  func (e *Endpoint) Get(key *ari.Key) *ari.EndpointHandle {
    16  	return ari.NewEndpointHandle(e.client.stamp(key), e)
    17  }
    18  
    19  // List lists the current endpoints and returns a list of handles
    20  func (e *Endpoint) List(filter *ari.Key) (ex []*ari.Key, err error) {
    21  	endpoints := []struct {
    22  		Tech     string `json:"technology"`
    23  		Resource string `json:"resource"`
    24  	}{}
    25  
    26  	if filter == nil {
    27  		filter = ari.NodeKey(e.client.ApplicationName(), e.client.node)
    28  	}
    29  	err = e.client.get("/endpoints", &endpoints)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	for _, i := range endpoints {
    35  		k := e.client.stamp(ari.NewEndpointKey(i.Tech, i.Resource))
    36  		if filter.Match(k) {
    37  			ex = append(ex, k)
    38  		}
    39  	}
    40  
    41  	return
    42  }
    43  
    44  // ListByTech lists the current endpoints with the given technology and
    45  // returns a list of handles.
    46  func (e *Endpoint) ListByTech(tech string, filter *ari.Key) (ex []*ari.Key, err error) {
    47  	endpoints := []struct {
    48  		Tech     string `json:"technology"`
    49  		Resource string `json:"resource"`
    50  	}{}
    51  
    52  	if filter == nil {
    53  		filter = ari.NodeKey(e.client.ApplicationName(), e.client.node)
    54  	}
    55  	err = e.client.get("/endpoints/"+tech, &endpoints)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	for _, i := range endpoints {
    61  		k := e.client.stamp(ari.NewEndpointKey(i.Tech, i.Resource))
    62  		if filter.Match(k) {
    63  			ex = append(ex, k)
    64  		}
    65  	}
    66  
    67  	return
    68  }
    69  
    70  // Data retrieves the current state of the endpoint
    71  func (e *Endpoint) Data(key *ari.Key) (*ari.EndpointData, error) {
    72  	if key == nil || key.ID == "" {
    73  		return nil, errors.New("endpoint key not supplied")
    74  	}
    75  	if key.Kind != ari.EndpointKey {
    76  		return nil, errors.New("wrong key type")
    77  	}
    78  
    79  	var data = new(ari.EndpointData)
    80  	if err := e.client.get("/endpoints/"+key.ID, data); err != nil {
    81  		return nil, dataGetError(err, "endpoint", "%s", key.ID)
    82  	}
    83  
    84  	data.Key = e.client.stamp(key)
    85  	return data, nil
    86  }