diff --git a/internal/resource/resource.go b/internal/resource/resource.go index f0042b6..2600513 100644 --- a/internal/resource/resource.go +++ b/internal/resource/resource.go @@ -60,31 +60,82 @@ func NewResource(uri string) Resource { func StorageMachine(sub machine.Subscriber) machine.Stater { // start_destroy -> absent -> start_create -> present -> start_destroy - stater := machine.New("absent") - stater.AddStates("absent", "start_create", "present", "start_delete", "start_read", "start_update") - stater.AddTransition("create", "absent", "start_create") + stater := machine.New("unknown") + stater.AddStates("unkonwn", "absent", "start_create", "present", "start_delete", "start_read", "start_update") + stater.AddTransition("create", machine.States("unknown", "absent"), "start_create") if e := stater.AddSubscription("create", sub); e != nil { return nil } - stater.AddTransition("created", "start_create", "present") - stater.AddTransition("read", "*", "start_read") + stater.AddTransition("created", machine.States("start_create"), "present") + if e := stater.AddSubscription("created", sub); e != nil { + return nil + } + stater.AddTransition("exists", machine.States("unknown", "absent"), "present") + if e := stater.AddSubscription("exists", sub); e != nil { + return nil + } + stater.AddTransition("notexists", machine.States("*"), "absent") + if e := stater.AddSubscription("notexists", sub); e != nil { + return nil + } + stater.AddTransition("read", machine.States("*"), "start_read") if e := stater.AddSubscription("read", sub); e != nil { return nil } - stater.AddTransition("state_read", "start_read", "present") - stater.AddTransition("update", "*", "start_update") + stater.AddTransition("state_read", machine.States("start_read"), "present") + stater.AddTransition("update", machine.States("*"), "start_update") if e := stater.AddSubscription("update", sub); e != nil { return nil } - stater.AddTransition("updated", "start_update", "present") - stater.AddTransition("delete", "*", "start_delete") + stater.AddTransition("updated", machine.States("start_update"), "present") + stater.AddTransition("delete", machine.States("*"), "start_delete") if e := stater.AddSubscription("delete", sub); e != nil { return nil } - stater.AddTransition("deleted", "start_delete", "absent") + stater.AddTransition("deleted", machine.States("start_delete"), "absent") + if e := stater.AddSubscription("deleted", sub); e != nil { + return nil + } return stater } -func ProcessMachine() machine.Stater { - return nil +func ProcessMachine(sub machine.Subscriber) machine.Stater { +// "enum": [ "created", "restarting", "running", "paused", "exited", "dead" ] + stater := machine.New("unknown") + stater.AddStates("unkonwn", "absent", "start_create", "present", "created", "restarting", "running", "paused", "exited", "dead", "start_delete", "start_read", "start_update") + stater.AddTransition("create", machine.States("unknown", "absent"), "start_create") + if e := stater.AddSubscription("create", sub); e != nil { + return nil + } + stater.AddTransition("created", machine.States("start_create"), "present") + if e := stater.AddSubscription("created", sub); e != nil { + return nil + } + stater.AddTransition("exists", machine.States("unknown", "absent"), "present") + if e := stater.AddSubscription("exists", sub); e != nil { + return nil + } + stater.AddTransition("notexists", machine.States("*"), "absent") + if e := stater.AddSubscription("notexists", sub); e != nil { + return nil + } + stater.AddTransition("read", machine.States("*"), "start_read") + if e := stater.AddSubscription("read", sub); e != nil { + return nil + } + stater.AddTransition("state_read", machine.States("start_read"), "present") + stater.AddTransition("update", machine.States("*"), "start_update") + if e := stater.AddSubscription("update", sub); e != nil { + return nil + } + stater.AddTransition("updated", machine.States("start_update"), "present") + stater.AddTransition("delete", machine.States("*"), "start_delete") + if e := stater.AddSubscription("delete", sub); e != nil { + return nil + } + stater.AddTransition("deleted", machine.States("start_delete"), "absent") + if e := stater.AddSubscription("deleted", sub); e != nil { + return nil + } + return stater }