github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/annotations/client.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package annotations 5 6 import ( 7 "github.com/juju/errors" 8 9 "github.com/juju/juju/api/base" 10 "github.com/juju/juju/apiserver/params" 11 ) 12 13 // Client allows access to the annotations API end point. 14 type Client struct { 15 base.ClientFacade 16 facade base.FacadeCaller 17 } 18 19 // NewClient creates a new client for accessing the annotations API. 20 func NewClient(st base.APICallCloser) *Client { 21 frontend, backend := base.NewClientFacade(st, "Annotations") 22 return &Client{ClientFacade: frontend, facade: backend} 23 } 24 25 // Get returns annotations that have been set on the given entities. 26 func (c *Client) Get(tags []string) ([]params.AnnotationsGetResult, error) { 27 annotations := params.AnnotationsGetResults{} 28 if err := c.facade.FacadeCall("Get", entitiesFromTags(tags), &annotations); err != nil { 29 return annotations.Results, errors.Trace(err) 30 } 31 return annotations.Results, nil 32 } 33 34 // Set sets entity annotation pairs. 35 func (c *Client) Set(annotations map[string]map[string]string) ([]params.ErrorResult, error) { 36 args := params.AnnotationsSet{entitiesAnnotations(annotations)} 37 results := new(params.ErrorResults) 38 if err := c.facade.FacadeCall("Set", args, results); err != nil { 39 return nil, errors.Trace(err) 40 } 41 return results.Results, nil 42 } 43 44 func entitiesFromTags(tags []string) params.Entities { 45 entities := []params.Entity{} 46 for _, tag := range tags { 47 entities = append(entities, params.Entity{tag}) 48 } 49 return params.Entities{entities} 50 } 51 52 func entitiesAnnotations(annotations map[string]map[string]string) []params.EntityAnnotations { 53 all := []params.EntityAnnotations{} 54 for tag, pairs := range annotations { 55 one := params.EntityAnnotations{ 56 EntityTag: tag, 57 Annotations: pairs, 58 } 59 all = append(all, one) 60 } 61 return all 62 }