7 "github.com/hashicorp/terraform/terraform"
10 // Map is a map of resources that are supported, and provides helpers for
11 // more easily implementing a ResourceProvider.
13 Mapping map[string]Resource
16 func (m *Map) Validate(
17 t string, c *terraform.ResourceConfig) ([]string, []error) {
20 return nil, []error{fmt.Errorf("Unknown resource type: %s", t)}
23 // If there is no validator set, then it is valid
24 if r.ConfigValidator == nil {
28 return r.ConfigValidator.Validate(c)
31 // Apply performs a create or update depending on the diff, and calls
32 // the proper function on the matching Resource.
34 info *terraform.InstanceInfo,
35 s *terraform.InstanceState,
36 d *terraform.InstanceDiff,
37 meta interface{}) (*terraform.InstanceState, error) {
38 r, ok := m.Mapping[info.Type]
40 return nil, fmt.Errorf("Unknown resource type: %s", info.Type)
43 if d.Destroy || d.RequiresNew() {
45 // Destroy the resource if it is created
46 err := r.Destroy(s, meta)
54 // If we're only destroying, and not creating, then return now.
55 // Otherwise, we continue so that we can create a new resource.
61 var result *terraform.InstanceState
64 result, err = r.Create(s, d, meta)
68 "Resource type '%s' doesn't support update",
72 result, err = r.Update(s, d, meta)
75 if result.Attributes == nil {
76 result.Attributes = make(map[string]string)
79 result.Attributes["id"] = result.ID
85 // Diff performs a diff on the proper resource type.
87 info *terraform.InstanceInfo,
88 s *terraform.InstanceState,
89 c *terraform.ResourceConfig,
90 meta interface{}) (*terraform.InstanceDiff, error) {
91 r, ok := m.Mapping[info.Type]
93 return nil, fmt.Errorf("Unknown resource type: %s", info.Type)
96 return r.Diff(s, c, meta)
99 // Refresh performs a Refresh on the proper resource type.
101 // Refresh on the Resource won't be called if the state represents a
102 // non-created resource (ID is blank).
104 // An error is returned if the resource isn't registered.
105 func (m *Map) Refresh(
106 info *terraform.InstanceInfo,
107 s *terraform.InstanceState,
108 meta interface{}) (*terraform.InstanceState, error) {
109 // If the resource isn't created, don't refresh.
114 r, ok := m.Mapping[info.Type]
116 return nil, fmt.Errorf("Unknown resource type: %s", info.Type)
119 return r.Refresh(s, meta)
122 // Resources returns all the resources that are supported by this
123 // resource map and can be used to satisfy the Resources method of
124 // a ResourceProvider.
125 func (m *Map) Resources() []terraform.ResourceType {
126 ks := make([]string, 0, len(m.Mapping))
127 for k, _ := range m.Mapping {
132 rs := make([]terraform.ResourceType, 0, len(m.Mapping))
133 for _, k := range ks {
134 rs = append(rs, terraform.ResourceType{