github.com/sarguru/terraform@v0.6.17-0.20160525232901-8fcdfd7e3dc9/terraform/state_filter.go (about) 1 package terraform 2 3 import ( 4 "fmt" 5 "sort" 6 ) 7 8 // StateFilter is responsible for filtering and searching a state. 9 // 10 // This is a separate struct from State rather than a method on State 11 // because StateFilter might create sidecar data structures to optimize 12 // filtering on the state. 13 // 14 // If you change the State, the filter created is invalid and either 15 // Reset should be called or a new one should be allocated. StateFilter 16 // will not watch State for changes and do this for you. If you filter after 17 // changing the State without calling Reset, the behavior is not defined. 18 type StateFilter struct { 19 State *State 20 } 21 22 // Filter takes the addresses specified by fs and finds all the matches. 23 // The values of fs are resource addressing syntax that can be parsed by 24 // ParseResourceAddress. 25 func (f *StateFilter) Filter(fs ...string) ([]*StateFilterResult, error) { 26 // Parse all the addresses 27 as := make([]*ResourceAddress, len(fs)) 28 for i, v := range fs { 29 a, err := ParseResourceAddress(v) 30 if err != nil { 31 return nil, fmt.Errorf("Error parsing address '%s': %s", v, err) 32 } 33 34 as[i] = a 35 } 36 37 // If we werent given any filters, then we list all 38 if len(fs) == 0 { 39 as = append(as, &ResourceAddress{Index: -1}) 40 } 41 42 // Filter each of the address. We keep track of this in a map to 43 // strip duplicates. 44 resultSet := make(map[string]*StateFilterResult) 45 for _, a := range as { 46 for _, r := range f.filterSingle(a) { 47 resultSet[r.String()] = r 48 } 49 } 50 51 // Make the result list 52 results := make([]*StateFilterResult, 0, len(resultSet)) 53 for _, v := range resultSet { 54 results = append(results, v) 55 } 56 57 // Sort them and return 58 sort.Sort(StateFilterResultSlice(results)) 59 return results, nil 60 } 61 62 func (f *StateFilter) filterSingle(a *ResourceAddress) []*StateFilterResult { 63 // The slice to keep track of results 64 var results []*StateFilterResult 65 66 // Go through modules first. 67 modules := make([]*ModuleState, 0, len(f.State.Modules)) 68 for _, m := range f.State.Modules { 69 if f.relevant(a, m) { 70 modules = append(modules, m) 71 72 // Only add the module to the results if we haven't specified a type. 73 // We also ignore the root module. 74 if a.Type == "" && len(m.Path) > 1 { 75 results = append(results, &StateFilterResult{ 76 Path: m.Path[1:], 77 Address: (&ResourceAddress{Path: m.Path[1:]}).String(), 78 Value: m, 79 }) 80 } 81 } 82 } 83 84 // With the modules set, go through all the resources within 85 // the modules to find relevant resources. 86 for _, m := range modules { 87 for n, r := range m.Resources { 88 if f.relevant(a, r) { 89 // The name in the state contains valuable information. Parse. 90 key, err := ParseResourceStateKey(n) 91 if err != nil { 92 // If we get an error parsing, then just ignore it 93 // out of the state. 94 continue 95 } 96 97 if a.Index >= 0 && key.Index != a.Index { 98 // Index doesn't match 99 continue 100 } 101 102 if a.Name != "" && a.Name != key.Name { 103 continue 104 } 105 106 // Build the address for this resource 107 addr := &ResourceAddress{ 108 Path: m.Path[1:], 109 Name: key.Name, 110 Type: key.Type, 111 Index: key.Index, 112 } 113 114 // Add the resource level result 115 resourceResult := &StateFilterResult{ 116 Path: addr.Path, 117 Address: addr.String(), 118 Value: r, 119 } 120 if !a.InstanceTypeSet { 121 results = append(results, resourceResult) 122 } 123 124 // Add the instances 125 if r.Primary != nil { 126 addr.InstanceType = TypePrimary 127 addr.InstanceTypeSet = false 128 results = append(results, &StateFilterResult{ 129 Path: addr.Path, 130 Address: addr.String(), 131 Parent: resourceResult, 132 Value: r.Primary, 133 }) 134 } 135 136 for _, instance := range r.Tainted { 137 if f.relevant(a, instance) { 138 addr.InstanceType = TypeTainted 139 addr.InstanceTypeSet = true 140 results = append(results, &StateFilterResult{ 141 Path: addr.Path, 142 Address: addr.String(), 143 Parent: resourceResult, 144 Value: instance, 145 }) 146 } 147 } 148 149 for _, instance := range r.Deposed { 150 if f.relevant(a, instance) { 151 addr.InstanceType = TypeDeposed 152 addr.InstanceTypeSet = true 153 results = append(results, &StateFilterResult{ 154 Path: addr.Path, 155 Address: addr.String(), 156 Parent: resourceResult, 157 Value: instance, 158 }) 159 } 160 } 161 } 162 } 163 } 164 165 return results 166 } 167 168 // relevant checks for relevance of this address against the given value. 169 func (f *StateFilter) relevant(addr *ResourceAddress, raw interface{}) bool { 170 switch v := raw.(type) { 171 case *ModuleState: 172 path := v.Path[1:] 173 174 if len(addr.Path) > len(path) { 175 // Longer path in address means there is no way we match. 176 return false 177 } 178 179 // Check for a prefix match 180 for i, p := range addr.Path { 181 if path[i] != p { 182 // Any mismatches don't match. 183 return false 184 } 185 } 186 187 return true 188 case *ResourceState: 189 if addr.Type == "" { 190 // If we have no resource type, then we're interested in all! 191 return true 192 } 193 194 // If the type doesn't match we fail immediately 195 if v.Type != addr.Type { 196 return false 197 } 198 199 return true 200 default: 201 // If we don't know about it, let's just say no 202 return false 203 } 204 } 205 206 // StateFilterResult is a single result from a filter operation. Filter 207 // can match multiple things within a state (module, resource, instance, etc.) 208 // and this unifies that. 209 type StateFilterResult struct { 210 // Module path of the result 211 Path []string 212 213 // Address is the address that can be used to reference this exact result. 214 Address string 215 216 // Parent, if non-nil, is a parent of this result. For instances, the 217 // parent would be a resource. For resources, the parent would be 218 // a module. For modules, this is currently nil. 219 Parent *StateFilterResult 220 221 // Value is the actual value. This must be type switched on. It can be 222 // any data structures that `State` can hold: `ModuleState`, 223 // `ResourceState`, `InstanceState`. 224 Value interface{} 225 } 226 227 func (r *StateFilterResult) String() string { 228 return fmt.Sprintf("%T: %s", r.Value, r.Address) 229 } 230 231 func (r *StateFilterResult) sortedType() int { 232 switch r.Value.(type) { 233 case *ModuleState: 234 return 0 235 case *ResourceState: 236 return 1 237 case *InstanceState: 238 return 2 239 default: 240 return 50 241 } 242 } 243 244 // StateFilterResultSlice is a slice of results that implements 245 // sort.Interface. The sorting goal is what is most appealing to 246 // human output. 247 type StateFilterResultSlice []*StateFilterResult 248 249 func (s StateFilterResultSlice) Len() int { return len(s) } 250 func (s StateFilterResultSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 251 func (s StateFilterResultSlice) Less(i, j int) bool { 252 a, b := s[i], s[j] 253 254 // If the addresses are different it is just lexographic sorting 255 if a.Address != b.Address { 256 return a.Address < b.Address 257 } 258 259 // Addresses are the same, which means it matters on the type 260 return a.sortedType() < b.sortedType() 261 }