@@ -17,13 +17,13 @@ package cmd
17
17
18
18
import (
19
19
"fmt"
20
- "log"
21
20
"os"
22
21
"unsafe"
23
22
24
23
"github.com/spf13/cobra"
25
24
26
25
homedir "github.com/mitchellh/go-homedir"
26
+ log "github.com/sirupsen/logrus"
27
27
"github.com/spf13/viper"
28
28
29
29
"github.com/mensaah/reka/config"
40
40
cfg * config.Config
41
41
providers []* provider.Provider
42
42
backend state.Backender
43
- currentState state.State
43
+ currentState * state.State
44
44
)
45
45
46
46
// rootCmd represents the base command when called without any subcommands
@@ -91,7 +91,7 @@ func initConfig() {
91
91
92
92
// If a config file is found, read it in.
93
93
if err := viper .ReadInConfig (); err == nil {
94
- fmt . Println ("Using config file:" , viper .ConfigFileUsed ())
94
+ log . Info ("Using config file:" , viper .ConfigFileUsed ())
95
95
}
96
96
97
97
// Load Config and Defaults
@@ -108,10 +108,8 @@ func initConfig() {
108
108
// Initialize Provider objects
109
109
providers = initProviders ()
110
110
backend = state .InitBackend ()
111
- currentState = make (state.State )
112
111
}
113
112
114
- // TODO Add logger to Providers during configuration
115
113
func initProviders () []* provider.Provider {
116
114
var providers []* provider.Provider
117
115
for _ , p := range config .GetProviders () {
@@ -135,25 +133,43 @@ func initProviders() []*provider.Provider {
135
133
}
136
134
137
135
// Refresh current status of resources from Providers
136
+ // TODO Reconcile state so that new resources are added to desired states and former resources removed
138
137
func refreshResources (providers []* provider.Provider ) {
138
+ // currentState is the state stored in backend
139
+ currentState = backend .GetState ()
140
+
141
+ currentState .Current = make (state.ProvidersState )
139
142
for _ , provider := range providers {
140
143
allResources := provider .GetAllResources ()
141
- currentState [provider .Name ] = allResources
142
-
143
- // stoppableResources := provider.GetStoppableResources(allResources)
144
- // fmt.Println("Stoppable Resources: ", stoppableResources)
145
- // errs := provider.StopResources(stoppableResources)
146
- // fmt.Println("Errors Stopping Resources: ", errs)
147
-
148
- // resumableResources := provider.GetResumableResources(allResources)
149
- // fmt.Println("Resumable Resources: ", resumableResources)
150
- // errs = provider.ResumeResources(resumableResources)
151
- // fmt.Println("Errors Resuming Resources: ", errs)
152
-
153
- destroyableResources := provider .GetDestroyableResources (allResources )
154
- fmt .Println ("Destroyable Resources: " , destroyableResources )
155
- // errs = provider.DestroyResources(destroyableResources)
156
- // fmt.Println("Errors Destroying Resources: ", errs)
144
+ currentState .Current [provider .Name ] = allResources
145
+ }
146
+
147
+ // Add new resources to desired state if they don't already exists
148
+ for k , v := range currentState .Current {
149
+ if m , ok := currentState .Desired [k ]; ok || currentState .Desired [k ] == nil {
150
+ log .Error (m )
151
+ // TODO Return difference between two Resources object
152
+ continue
153
+ }
154
+ currentState .Desired [k ] = v
157
155
}
158
- backend .WriteState (& currentState )
156
+
157
+ backend .WriteState (currentState )
158
+ }
159
+
160
+ func reapResources () {
161
+ // stoppableResources := provider.GetStoppableResources(allResources)
162
+ // fmt.Println("Stoppable Resources: ", stoppableResources)
163
+ // errs := provider.StopResources(stoppableResources)
164
+ // fmt.Println("Errors Stopping Resources: ", errs)
165
+
166
+ // resumableResources := provider.GetResumableResources(allResources)
167
+ // fmt.Println("Resumable Resources: ", resumableResources)
168
+ // errs = provider.ResumeResources(resumableResources)
169
+ // fmt.Println("Errors Resuming Resources: ", errs)
170
+
171
+ // destroyableResources := provider.GetDestroyableResources(allResources)
172
+ // fmt.Println("Destroyable Resources: ", destroyableResources)
173
+ // errs = provider.DestroyResources(destroyableResources)
174
+ // fmt.Println("Errors Destroying Resources: ", errs)
159
175
}
0 commit comments