OLD | NEW |
(Empty) | |
| 1 // Copyright 2011, 2012, 2013 Canonical Ltd. |
| 2 // Licensed under the AGPLv3, see LICENCE file for details. |
| 3 |
| 4 package charm |
| 5 |
| 6 import ( |
| 7 "fmt" |
| 8 "io" |
| 9 "io/ioutil" |
| 10 "regexp" |
| 11 |
| 12 "github.com/binary132/gojsonschema" |
| 13 "launchpad.net/goyaml" |
| 14 ) |
| 15 |
| 16 var nameRule = regexp.MustCompile("^[a-z](?:[a-z-]*[a-z])?$") |
| 17 |
| 18 // Actions defines the available actions for the charm. |
| 19 type Actions struct { |
| 20 ActionSpecs map[string]ActionSpec `yaml:"actions"` |
| 21 } |
| 22 |
| 23 // ActionSpec is a definition of the parameters and traits of an Action. |
| 24 type ActionSpec struct { |
| 25 Description string |
| 26 Params map[string]interface{} |
| 27 } |
| 28 |
| 29 // ReadActions builds an Actions spec from a charm's actions.yaml. |
| 30 func ReadActionsYaml(r io.Reader) (*Actions, error) { |
| 31 data, err := ioutil.ReadAll(r) |
| 32 if err != nil { |
| 33 return nil, err |
| 34 } |
| 35 var unmarshaledActions Actions |
| 36 if err := goyaml.Unmarshal(data, &unmarshaledActions); err != nil { |
| 37 return nil, err |
| 38 } |
| 39 |
| 40 for name, actionSpec := range unmarshaledActions.ActionSpecs { |
| 41 if valid := nameRule.MatchString(name); !valid { |
| 42 return nil, fmt.Errorf("bad action name %s", name) |
| 43 } |
| 44 _, err := gojsonschema.NewJsonSchemaDocument(actionSpec.Params) |
| 45 if err != nil { |
| 46 return nil, fmt.Errorf("invalid params schema for action
%q: %v", err) |
| 47 } |
| 48 for paramName, _ := range unmarshaledActions.ActionSpecs[name].P
arams { |
| 49 if valid := nameRule.MatchString(paramName); !valid { |
| 50 return nil, fmt.Errorf("bad param name %s", para
mName) |
| 51 } |
| 52 } |
| 53 } |
| 54 return &unmarshaledActions, nil |
| 55 } |
OLD | NEW |